Created
May 18, 2010 13:42
-
-
Save narumij/405011 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* BeginEnd.h | |
* GLBeginEnd | |
* | |
* Created by narumij on 10/05/18. | |
* Copyright 2010 narumij. All rights reserved. | |
* | |
*/ | |
// http://d.hatena.ne.jp/narumij/20100518/1274190881 | |
#include <OpenGLES/ES1/gl.h> | |
#include <vector> | |
#ifndef BEGIN_END_H_GUARD | |
#define BEGIN_END_H_GUARD | |
class BeginEndDraw | |
{ | |
struct MeshVertexAttribute { | |
GLenum semantic; | |
GLint size; | |
GLenum type; | |
GLboolean normalized; | |
GLsizei stride; | |
uint32_t pointer_offset; | |
#pragma mark - | |
MeshVertexAttribute() : semantic(), size(), type(), normalized(), stride(), pointer_offset() {} | |
MeshVertexAttribute( GLenum semantic_, | |
GLint size_, | |
GLenum type_, | |
GLboolean normalized_, | |
GLsizei stride_, | |
uint32_t pointer_offset_) | |
: semantic( semantic_ ), size(size_), type(type_), normalized(normalized_), stride(stride_), pointer_offset(pointer_offset_) {} | |
void Setup( void *data_ptr ) | |
{ | |
glEnableClientState(semantic); | |
switch (semantic) { | |
case GL_VERTEX_ARRAY: | |
glVertexPointer( size, type, stride, pointer_offset + (GLubyte *)data_ptr ); | |
break; | |
case GL_NORMAL_ARRAY: | |
glNormalPointer( type, stride, pointer_offset + (GLubyte *)data_ptr ); | |
break; | |
case GL_TEXTURE_COORD_ARRAY: | |
glTexCoordPointer( size, type, stride, pointer_offset + (GLubyte *)data_ptr ); | |
break; | |
case GL_COLOR_ARRAY: | |
glColorPointer( size, type, stride, pointer_offset + (GLubyte *)data_ptr ); | |
break; | |
default: | |
break; | |
} | |
} | |
void Teardown() | |
{ | |
glEnableClientState(semantic); | |
} | |
}; | |
std::vector<float> data; | |
std::vector<MeshVertexAttribute> vertex_attributes; | |
GLenum primitive_mode; | |
union State { | |
float array[15]; | |
struct { | |
float position[4]; | |
float normal[3]; | |
float color[4]; | |
float texcoord[4]; | |
}; | |
} state; | |
public: | |
BeginEndDraw() | |
: data(), vertex_attributes(), primitive_mode(),state() | |
{ | |
vertex_attributes.push_back( MeshVertexAttribute(GL_VERTEX_ARRAY, 4, GL_FLOAT, false, sizeof(State), 0 ) ); | |
vertex_attributes.push_back( MeshVertexAttribute(GL_NORMAL_ARRAY, 3, GL_FLOAT, false, sizeof(State), (GLubyte*)state.normal - (GLubyte*)state.position ) ); | |
vertex_attributes.push_back( MeshVertexAttribute(GL_COLOR_ARRAY, 4, GL_FLOAT, false, sizeof(State), (GLubyte*)state.color - (GLubyte*)state.position ) ); | |
vertex_attributes.push_back( MeshVertexAttribute(GL_TEXTURE_COORD_ARRAY, 4, GL_FLOAT, false, sizeof(State), (GLubyte*)state.texcoord - (GLubyte*)state.position ) ); | |
state.normal[2] = 1.0f; | |
state.color[0] = state.color[1] = state.color[2] = state.color[3] = 1.0f; | |
} | |
void Begin( GLenum primitive_mode_ ) | |
{ | |
primitive_mode = primitive_mode_; | |
data.clear(); | |
} | |
void End() | |
{ | |
for( std::vector<MeshVertexAttribute>::iterator it = vertex_attributes.begin(); | |
it != vertex_attributes.end(); ++it ) | |
{ | |
it->Setup( &data[0] ); | |
} | |
glDrawArrays( primitive_mode, 0, Size() ); | |
for( std::vector<MeshVertexAttribute>::iterator it = vertex_attributes.begin(); | |
it != vertex_attributes.end(); ++it ) | |
{ | |
it->Teardown(); | |
} | |
} | |
void Vertex2f( GLfloat x, GLfloat y ) | |
{ | |
state.position[0] = x; | |
state.position[1] = y; | |
state.position[2] = 0.0f; | |
state.position[3] = 1.0f; | |
Issue(); | |
} | |
void Vertex3f( GLfloat x, GLfloat y, GLfloat z ) | |
{ | |
state.position[0] = x; | |
state.position[1] = y; | |
state.position[2] = z; | |
state.position[3] = 1.0f; | |
Issue(); | |
} | |
void Normal3f( GLfloat nx, GLfloat ny, GLfloat nz ) | |
{ | |
state.normal[0] = nx; | |
state.normal[1] = ny; | |
state.normal[2] = nz; | |
} | |
void Texcoord2f( GLfloat s, GLfloat t ) | |
{ | |
state.texcoord[0] = s; | |
state.texcoord[1] = t; | |
state.texcoord[2] = 0.0f; | |
state.texcoord[3] = 0.0f; | |
} | |
void Texcoord3f( GLfloat s, GLfloat t, GLfloat p ) | |
{ | |
state.texcoord[0] = s; | |
state.texcoord[1] = t; | |
state.texcoord[2] = p; | |
state.texcoord[3] = 0.0f; | |
} | |
void Color4f( GLfloat r, GLfloat g, GLfloat b, GLfloat a ) | |
{ | |
state.color[0] = r; | |
state.color[1] = g; | |
state.color[2] = b; | |
state.color[3] = a; | |
} | |
private: | |
inline size_t Size() | |
{ | |
return data.size()*sizeof(float)/sizeof(State); | |
} | |
inline size_t Stride() | |
{ | |
return sizeof(State); | |
} | |
inline void Issue() | |
{ | |
copy( &state.array[0], &state.array[0] + 15, back_inserter(data) ); | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment