Reduce duplicate OpenGL plumbing codepaths

- Apple //e "framebuffer" OpenGL model/texture is now a complete GLModel object
    - Readability FTW! ... use args struct in quadCreation function to allow for named args
This commit is contained in:
Aaron Culliney 2016-01-02 13:10:02 -08:00
parent 7ca679350d
commit 8b3f288018
9 changed files with 150 additions and 286 deletions

View File

@ -59,10 +59,21 @@ static void _alertToModel(char *message, unsigned int messageCols, unsigned int
const unsigned int fbWidth = (messageCols * FONT80_WIDTH_PIXELS);
const unsigned int fbHeight = (messageRows * FONT_HEIGHT_PIXELS);
messageModel = mdlCreateQuad(-0.3, -0.3, 0.7, 0.7, MODEL_DEPTH, fbWidth, fbHeight, (GLCustom){
messageModel = mdlCreateQuad((GLModelParams_s){
.skew_x = -0.3,
.skew_y = -0.3,
.z = MODEL_DEPTH,
.obj_w = 0.7,
.obj_h = 0.7,
.positionUsageHint = GL_STATIC_DRAW, // positions don't change
.tex_w = fbWidth,
.tex_h = fbHeight,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // but texture (message pixels) does
}, (GLCustom){
.create = &_create_alert,
.setup = NULL,
.destroy = &glhud_destroyDefault,
});
});
if (!messageModel) {
LOG("OOPS cannot create animation message HUD model!");
break;

View File

@ -209,11 +209,23 @@ static void gltouchjoy_setup(void) {
joyglobals.isShuttingDown = false;
axes.model = mdlCreateQuad(-1.05, -1.0, AXIS_OBJ_W, AXIS_OBJ_H, MODEL_DEPTH, AXIS_FB_WIDTH, AXIS_FB_HEIGHT, (GLCustom){
// axis object
axes.model = mdlCreateQuad((GLModelParams_s){
.skew_x = -1.05,
.skew_y = -1.0,
.z = MODEL_DEPTH,
.obj_w = AXIS_OBJ_W,
.obj_h = AXIS_OBJ_H,
.positionUsageHint = GL_DYNAMIC_DRAW, // positions can change
.tex_w = AXIS_FB_WIDTH,
.tex_h = AXIS_FB_HEIGHT,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // so can texture
}, (GLCustom){
.create = &_create_touchjoy_hud,
.setup = &_setup_axis_object,
.destroy = &glhud_destroyDefault,
});
});
if (!axes.model) {
LOG("gltouchjoy not initializing axis");
return;
@ -225,11 +237,21 @@ static void gltouchjoy_setup(void) {
// button object
buttons.model = mdlCreateQuad(1.05-BUTTON_OBJ_W, -1.0, BUTTON_OBJ_W, BUTTON_OBJ_H, MODEL_DEPTH, BUTTON_FB_WIDTH, BUTTON_FB_HEIGHT, (GLCustom){
buttons.model = mdlCreateQuad((GLModelParams_s){
.skew_x = 1.05-BUTTON_OBJ_W,
.skew_y = -1.0,
.z = MODEL_DEPTH,
.obj_w = BUTTON_OBJ_W,
.obj_h = BUTTON_OBJ_H,
.positionUsageHint = GL_DYNAMIC_DRAW, // positions can change
.tex_w = BUTTON_FB_WIDTH,
.tex_h = BUTTON_FB_HEIGHT,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // so can texture
}, (GLCustom){
.create = &_create_touchjoy_hud,
.setup = &_setup_button_object,
.destroy = &glhud_destroyDefault,
});
});
if (!buttons.model) {
LOG("gltouchjoy not initializing buttons");
return;

View File

@ -514,13 +514,21 @@ static void gltouchkbd_setup(void) {
gltouchkbd_shutdown();
GLsizei texW = KBD_FB_WIDTH * kbd.glyphMultiplier;
GLsizei texH = KBD_FB_HEIGHT * kbd.glyphMultiplier;
kbd.model = mdlCreateQuad(-1.0, -1.0, KBD_OBJ_W, KBD_OBJ_H, MODEL_DEPTH, texW, texH, (GLCustom){
kbd.model = mdlCreateQuad((GLModelParams_s){
.skew_x = -1.0,
.skew_y = -1.0,
.z = MODEL_DEPTH,
.obj_w = KBD_OBJ_W,
.obj_h = KBD_OBJ_H,
.positionUsageHint = GL_STATIC_DRAW, // positions don't change
.tex_w = KBD_FB_WIDTH * kbd.glyphMultiplier,
.tex_h = KBD_FB_HEIGHT * kbd.glyphMultiplier,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // but key texture does
}, (GLCustom){
.create = &_create_touchkbd_hud,
.setup = &_setup_touchkbd_hud,
.destroy = &_destroy_touchkbd_hud,
});
});
if (!kbd.model) {
LOG("gltouchkbd initialization problem");
return;

View File

@ -398,13 +398,21 @@ static void gltouchmenu_setup(void) {
gltouchmenu_shutdown();
GLsizei texW = MENU_FB_WIDTH * menu.glyphMultiplier;
GLsizei texH = MENU_FB_HEIGHT * menu.glyphMultiplier;
menu.model = mdlCreateQuad(-1.0, 1.0-MENU_OBJ_H, MENU_OBJ_W, MENU_OBJ_H, MODEL_DEPTH, texW, texH, (GLCustom){
menu.model = mdlCreateQuad((GLModelParams_s){
.skew_x = -1.0,
.skew_y = 1.0-MENU_OBJ_H,
.z = MODEL_DEPTH,
.obj_w = MENU_OBJ_W,
.obj_h = MENU_OBJ_H,
.positionUsageHint = GL_STATIC_DRAW, // positions don't change
.tex_w = MENU_FB_WIDTH * menu.glyphMultiplier,
.tex_h = MENU_FB_HEIGHT * menu.glyphMultiplier,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // but menu texture does
}, (GLCustom){
.create = &_create_touchmenu,
.setup = &_setup_touchmenu,
.destroy = &_destroy_touchmenu,
});
});
if (!menu.model) {
LOG("gltouchmenu initialization problem");
return;

View File

@ -34,16 +34,6 @@ bool hackAroundBrokenAdreno200 = false;
bool hackAroundBrokenAdreno205 = false;
static GLint uniformMVPIdx = UNINITIALIZED_GL;
static GLenum crtElementType = UNINITIALIZED_GL;
static GLuint crtNumElements = UNINITIALIZED_GL;
static GLuint a2TextureName = UNINITIALIZED_GL;
static GLuint defaultFBO = UNINITIALIZED_GL;
static GLuint crtVAOName = UNINITIALIZED_GL;
static GLuint posBufferName = UNINITIALIZED_GL;
static GLuint texcoordBufferName = UNINITIALIZED_GL;
static GLuint elementBufferName = UNINITIALIZED_GL;
static GLModel *crtModel = NULL;
static GLuint vertexShader = UNINITIALIZED_GL;
@ -58,207 +48,6 @@ static int glutWindow = -1;
#endif
//----------------------------------------------------------------------------
//
// OpenGL helper routines
//
static void _create_CRT_model(void) {
// NOTE: vertices in Normalized Device Coordinates
const GLfloat crt_positions[] = {
// CRT screen quad
-1.0, -1.0, 0.0, 1.0,
1.0, -1.0, 0.0, 1.0,
-1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0,
#if PERSPECTIVE
// CRT back side point
0.0, 0.0, -1.0, 1.0,
#endif
};
const GLfloat crt_texcoords[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0,
};
const GLushort indices[] = {
// CRT screen quad
0, 1, 2, 2, 1, 3
#if PERSPECTIVE
// ...
#endif
};
GLModel *crt = CALLOC(1, sizeof(GLModel));
crt->numVertices = 4;
crt->numElements = 6;
crt->positions = MALLOC(sizeof(crt_positions));
memcpy(crt->positions, &crt_positions[0], sizeof(crt_positions));
crt->positionType = GL_FLOAT;
crt->positionSize = 4; // x,y,z coordinates
crt->positionArraySize = sizeof(crt_positions);
crt->texCoords = MALLOC(sizeof(crt_texcoords));
memcpy(crt->texCoords, &crt_texcoords[0], sizeof(crt_texcoords));
crt->texcoordType = GL_FLOAT;
crt->texcoordSize = 2; // s,t coordinates
crt->texcoordArraySize = sizeof(crt_texcoords);
crt->normals = NULL;
crt->normalType = GL_NONE;
crt->normalSize = GL_NONE;
crt->normalArraySize = 0;
crt->elements = MALLOC(sizeof(indices));
memcpy(crt->elements, &indices[0], sizeof(indices));
crt->elementType = GL_UNSIGNED_SHORT;
crt->elementArraySize = sizeof(indices);
mdlDestroyModel(&crtModel);
crtModel = crt;
}
static void _create_VAO_VBOs(void) {
// Create a vertex array object (VAO) to cache model parameters
#if USE_VAO
glGenVertexArrays(1, &crtVAOName);
glBindVertexArray(crtVAOName);
#endif
// Create a vertex buffer object (VBO) to store positions and load data
glGenBuffers(1, &posBufferName);
glBindBuffer(GL_ARRAY_BUFFER, posBufferName);
glBufferData(GL_ARRAY_BUFFER, crtModel->positionArraySize, crtModel->positions, GL_STATIC_DRAW);
#if USE_VAO
// Enable the position attribute for this VAO
glEnableVertexAttribArray(POS_ATTRIB_IDX);
// Get the size of the position type so we can set the stride properly
GLsizei posTypeSize = getGLTypeSize(crtModel->positionType);
// Set up parmeters for position attribute in the VAO including,
// size, type, stride, and offset in the currenly bound VAO
// This also attaches the position VBO to the VAO
glVertexAttribPointer(POS_ATTRIB_IDX, // What attibute index will this array feed in the vertex shader (see buildProgram)
crtModel->positionSize, // How many elements are there per position?
crtModel->positionType, // What is the type of this data?
GL_FALSE, // Do we want to normalize this data (0-1 range for fixed-pont types)
crtModel->positionSize*posTypeSize, // What is the stride (i.e. bytes between positions)?
0); // What is the offset in the VBO to the position data?
#endif
if (crtModel->texCoords) {
// Create a VBO to store texcoords
glGenBuffers(1, &texcoordBufferName);
glBindBuffer(GL_ARRAY_BUFFER, texcoordBufferName);
// Allocate and load texcoord data into the VBO
glBufferData(GL_ARRAY_BUFFER, crtModel->texcoordArraySize, crtModel->texCoords, GL_STATIC_DRAW);
#if USE_VAO
// Enable the texcoord attribute for this VAO
glEnableVertexAttribArray(TEXCOORD_ATTRIB_IDX);
// Get the size of the texcoord type so we can set the stride properly
GLsizei texcoordTypeSize = getGLTypeSize(crtModel->texcoordType);
// Set up parmeters for texcoord attribute in the VAO including,
// size, type, stride, and offset in the currenly bound VAO
// This also attaches the texcoord VBO to VAO
glVertexAttribPointer(TEXCOORD_ATTRIB_IDX, // What attibute index will this array feed in the vertex shader (see buildProgram)
crtModel->texcoordSize, // How many elements are there per texture coord?
crtModel->texcoordType, // What is the type of this data in the array?
GL_TRUE, // Do we want to normalize this data (0-1 range for fixed-point types)
crtModel->texcoordSize*texcoordTypeSize, // What is the stride (i.e. bytes between texcoords)?
0); // What is the offset in the VBO to the texcoord data?
#endif
}
// Create a VBO to vertex array elements
// This also attaches the element array buffer to the VAO
glGenBuffers(1, &elementBufferName);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferName);
// Allocate and load vertex array element data into VBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, crtModel->elementArraySize, crtModel->elements, GL_STATIC_DRAW);
GL_ERRLOG("finished creating VAO/VBOs");
}
static void _destroy_VAO(GLuint vaoName) {
// Bind the VAO so we can get data from it
#if USE_VAO
glBindVertexArray(vaoName);
// For every possible attribute set in the VAO
for (GLuint index = 0; index < 16; index++) {
// Get the VBO set for that attibute
GLuint bufName = 0;
glGetVertexAttribiv(index , GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, (GLint*)&bufName);
// If there was a VBO set...
if (bufName) {
//...delete the VBO
glDeleteBuffers(1, &bufName);
}
}
// Get any element array VBO set in the VAO
{
GLuint bufName = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, (GLint*)&bufName);
// If there was a element array VBO set in the VAO
if (bufName) {
//...delete the VBO
glDeleteBuffers(1, &bufName);
}
}
// Finally, delete the VAO
glDeleteVertexArrays(1, &vaoName);
#else
glDeleteBuffers(1, &posBufferName);
posBufferName = UNINITIALIZED_GL;
glDeleteBuffers(1, &texcoordBufferName);
texcoordBufferName = UNINITIALIZED_GL;
glDeleteBuffers(1, &elementBufferName);
elementBufferName = UNINITIALIZED_GL;
#endif
GL_ERRLOG("destroying VAO/VBOs");
}
static GLuint _create_CRT_texture(void) {
GLuint texName;
// Create a texture object to apply to model
glGenTextures(1, &texName);
glActiveTexture(TEXTURE_ACTIVE_FRAMEBUFFER);
glBindTexture(GL_TEXTURE_2D, texName);
// Set up filter and wrap modes for this texture object
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Indicate that pixel rows are tightly packed (defaults to a stride of sizeof(PIXEL_TYPE) which is good for RGBA or
// FLOAT data types)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Allocate and load image data into texture
glTexImage2D(GL_TEXTURE_2D, /*level*/0, TEX_FORMAT_INTERNAL, SCANWIDTH, SCANHEIGHT, /*border*/0, TEX_FORMAT, TEX_TYPE, NULL);
GL_ERRLOG("finished creating CRT texture");
return texName;
}
static void gldriver_render(void);
@ -370,29 +159,22 @@ static void gldriver_init_common(void) {
}
// ----------------------------
// Create CRT model VAO/VBOs
// Create Cathode Ray Tube (CRT) model ... which currently is just a simple texture quad model ...
// create CRT model
_create_CRT_model();
// Build Vertex Buffer Objects (VBOs) and Vertex Array Object (VAOs) with our model data
_create_VAO_VBOs();
// Cache the number of element and primType to use later in our glDrawElements calls
crtNumElements = crtModel->numElements;
crtElementType = crtModel->elementType;
#if USE_VAO
// We're using VAOs we can destroy certain buffers since they are already
// loaded into GL and we've saved anything else we need
FREE(crtModel->elements);
FREE(crtModel->positions);
FREE(crtModel->normals);
FREE(crtModel->texCoords);
#endif
// Build a default texture object with our image data
a2TextureName = _create_CRT_texture();
mdlDestroyModel(&crtModel);
glActiveTexture(TEXTURE_ACTIVE_FRAMEBUFFER);
#warning HACK FIXME TODO ^^^^^^^ is glActiveTexture() call needed here?
crtModel = mdlCreateQuad((GLModelParams_s){
.skew_x = -1.0, // model space coords
.skew_y = -1.0,
.z = 0.0,
.obj_w = 2.0, // entire model space (-1.0 to 1.0)
.obj_h = 2.0,
.positionUsageHint = GL_STATIC_DRAW, // positions don't change
.tex_w = SCANWIDTH,
.tex_h = SCANHEIGHT,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // but texture (Apple //e framebuffer) does
}, (GLCustom){ 0 });
// ----------------------------
// Load/setup shaders
@ -404,7 +186,7 @@ static void gldriver_init_common(void) {
assert(frgSource && "Catastrophic failure if fragment shader did not compile");
// Build/use Program
mainShaderProgram = glshader_buildProgram(vtxSource, frgSource, /*withNormal:*/false, /*withTexcoord:*/true, &vertexShader, &fragShader);
mainShaderProgram = glshader_buildProgram(vtxSource, frgSource, /*withTexcoord:*/true, &vertexShader, &fragShader);
glshader_destroySource(vtxSource);
glshader_destroySource(frgSource);
@ -466,9 +248,6 @@ static void gldriver_init_common(void) {
// Check for errors to make sure all of our setup went ok
GL_ERRLOG("finished initialization");
#if !defined(__APPLE__)
//glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
#endif
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
ERRQUIT("framebuffer status: %04X", status);
@ -479,15 +258,6 @@ static void _gldriver_shutdown(void) {
LOG("Beginning GLDriver shutdown ...");
// Cleanup all OpenGL objects
if (a2TextureName != UNINITIALIZED_GL) {
glDeleteTextures(1, &a2TextureName);
a2TextureName = UNINITIALIZED_GL;
}
if (crtVAOName != UNINITIALIZED_GL) {
_destroy_VAO(crtVAOName);
crtVAOName = UNINITIALIZED_GL;
}
mdlDestroyModel(&crtModel);
@ -529,7 +299,7 @@ static void gldriver_shutdown(void) {
//
#if USE_GLUT
static void gldriver_update(int unused) {
#if DEBUG_GL
#if FPS_LOG
static uint32_t prevCount = 0;
static uint32_t idleCount = 0;
@ -596,7 +366,7 @@ static void gldriver_render(void) {
unsigned long wasDirty = video_clearDirty();
char pixels[SCANWIDTH * SCANHEIGHT * sizeof(PIXEL_TYPE)]; // HACK FIXME TODO ... are we sure this does not overflow max stack buffer size?
char *pixels = (char *)crtModel->texPixels;
if (wasDirty) {
SCOPE_TRACE_VIDEO("pixel convert");
// Update texture from indexed-color Apple //e internal framebuffer
@ -613,19 +383,19 @@ static void gldriver_render(void) {
}
glActiveTexture(TEXTURE_ACTIVE_FRAMEBUFFER);
glBindTexture(GL_TEXTURE_2D, a2TextureName);
glBindTexture(GL_TEXTURE_2D, crtModel->textureName);
glUniform1i(texSamplerLoc, TEXTURE_ID_FRAMEBUFFER);
if (wasDirty) {
SCOPE_TRACE_VIDEO("glvideo texImage2D");
_HACKAROUND_GLTEXIMAGE2D_PRE(TEXTURE_ACTIVE_FRAMEBUFFER, a2TextureName);
_HACKAROUND_GLTEXIMAGE2D_PRE(TEXTURE_ACTIVE_FRAMEBUFFER, crtModel->textureName);
glTexImage2D(GL_TEXTURE_2D, /*level*/0, TEX_FORMAT_INTERNAL, SCANWIDTH, SCANHEIGHT, /*border*/0, TEX_FORMAT, TEX_TYPE, (GLvoid *)&pixels[0]);
}
// Bind our vertex array object
#if USE_VAO
glBindVertexArray(crtVAOName);
glBindVertexArray(crtModel->vaoName);
#else
glBindBuffer(GL_ARRAY_BUFFER, posBufferName);
glBindBuffer(GL_ARRAY_BUFFER, crtModel->posBufferName);
GLsizei posTypeSize = getGLTypeSize(crtModel->positionType);
GLsizei texcoordTypeSize = getGLTypeSize(crtModel->texcoordType);
@ -642,7 +412,7 @@ static void gldriver_render(void) {
// Set up parmeters for texcoord attribute in the VAO including, size, type, stride, and offset in the currenly
// bound VAO This also attaches the texcoord VBO to VAO
glBindBuffer(GL_ARRAY_BUFFER, texcoordBufferName);
glBindBuffer(GL_ARRAY_BUFFER, crtModel->texcoordBufferName);
glVertexAttribPointer(TEXCOORD_ATTRIB_IDX, // What attibute index will this array feed in the vertex shader (see buildProgram)
crtModel->texcoordSize, // How many elements are there per texture coord?
crtModel->texcoordType, // What is the type of this data in the array?
@ -651,7 +421,7 @@ static void gldriver_render(void) {
0); // What is the offset in the VBO to the texcoord data?
glEnableVertexAttribArray(TEXCOORD_ATTRIB_IDX);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferName);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, crtModel->elementBufferName);
#endif
glUniform1f(alphaValue, 1.0);
@ -662,7 +432,7 @@ static void gldriver_render(void) {
// Draw the CRT object and others
_HACKAROUND_GLDRAW_PRE();
glDrawElements(GL_TRIANGLES, crtNumElements, crtElementType, 0);
glDrawElements(GL_TRIANGLES, crtModel->numElements, crtModel->elementType, 0);
// Render HUD nodes
glnode_renderNodes();
@ -716,7 +486,7 @@ static void gldriver_reshape(int w, int h) {
}
#if USE_GLUT
static void gldriver_init_glut(GLuint fbo) {
static void gldriver_init_glut(void) {
glutInit(&argc, argv);
glutInitDisplayMode(/*GLUT_DOUBLE|*/GLUT_RGBA);
glutInitWindowSize(windowWidth, windowHeight);
@ -750,16 +520,12 @@ static void gldriver_init_glut(GLuint fbo) {
//----------------------------------------------------------------------------
// backend renderer API
static void gldriver_init(void *fbo) {
static void gldriver_init(void *unused) {
safe_to_do_opengl_logging = true;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
defaultFBO = (GLuint)fbo;
#pragma GCC diagnostic pop
#if defined(__APPLE__) || defined(ANDROID)
gldriver_init_common();
#elif USE_GLUT
gldriver_init_glut(defaultFBO);
gldriver_init_glut();
#else
#error no working codepaths
#endif

View File

@ -312,7 +312,7 @@ GLModel *mdlLoadQuadModel(void) {
return model;
}
#endif
#endif // 0
static void _quadCreateVAOAndVBOs(GLModel *model) {
@ -325,7 +325,7 @@ static void _quadCreateVAOAndVBOs(GLModel *model) {
// Create a vertex buffer object (VBO) to store positions and load data
glGenBuffers(1, &(model->posBufferName));
glBindBuffer(GL_ARRAY_BUFFER, model->posBufferName);
glBufferData(GL_ARRAY_BUFFER, model->positionArraySize, model->positions, GL_DYNAMIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, model->positionArraySize, model->positions, model->positionUsageHint);
#if USE_VAO
// Enable the position attribute for this VAO
@ -351,7 +351,7 @@ static void _quadCreateVAOAndVBOs(GLModel *model) {
glBindBuffer(GL_ARRAY_BUFFER, model->texcoordBufferName);
// Allocate and load texcoord data into the VBO
glBufferData(GL_ARRAY_BUFFER, model->texcoordArraySize, model->texCoords, GL_DYNAMIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, model->texcoordArraySize, model->texCoords, model->texcoordUsageHint);
#if USE_VAO
// Enable the texcoord attribute for this VAO
@ -378,7 +378,16 @@ static void _quadCreateVAOAndVBOs(GLModel *model) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->elementBufferName);
// Allocate and load vertex array element data into VBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, model->elementArraySize, model->elements, GL_DYNAMIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, model->elementArraySize, model->elements, GL_STATIC_DRAW/* HACK TODO FIXME: investigate*/);
#if USE_VAO
// We're using VAOs we can destroy certain buffers since they are already
// loaded into GL and we've saved anything else we need
FREE(model->elements);
FREE(model->positions);
//FREE(model->normals);
FREE(model->texCoords);
#endif
GL_ERRLOG("quad creation of VAO/VBOs");
}
@ -409,7 +418,15 @@ static GLuint _quadCreateTexture(GLModel *model) {
return texName;
}
GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat obj_h, GLfloat z, GLsizei tex_w, GLsizei tex_h, GLCustom clazz) {
GLModel *mdlCreateQuad(GLModelParams_s parms, GLCustom clazz) {
const GLfloat skew_x = parms.skew_x;
const GLfloat skew_y = parms.skew_y;
const GLfloat obj_w = parms.obj_w;
const GLfloat obj_h = parms.obj_h;
const GLfloat z = parms.z;
const GLsizei tex_w = parms.tex_w;
const GLsizei tex_h = parms.tex_h;
/* 2...3
* .
@ -444,6 +461,8 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob
model->numVertices = 4;
model->numElements = 6;
model->primType = GL_TRIANGLES;
model->positionUsageHint = parms.positionUsageHint;
model->texcoordUsageHint = parms.texcoordUsageHint;
model->positions = MALLOC(sizeof(obj_positions));
if (!(model->positions)) {
@ -465,6 +484,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob
model->texcoordArraySize = sizeof(obj_texcoords);
}
#if 0
{
// NO NORMALS for now
model->normals = NULL;
@ -472,6 +492,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob
model->normalSize = GL_NONE;
model->normalArraySize = 0;
}
#endif
model->elements = MALLOC(sizeof(indices));
if (!(model->elements)) {
@ -543,7 +564,7 @@ void mdlDestroyModel(INOUT GLModel **model) {
FREE(m->elements);
FREE(m->positions);
FREE(m->normals);
//FREE(m->normals);
FREE(m->texCoords);
FREE(m->texPixels);
@ -558,6 +579,7 @@ void mdlDestroyModel(INOUT GLModel **model) {
// For every possible attribute set in the VAO
for (GLuint index = 0; index < 16; index++) {
#warning FIXME TODO ... why magick hardcoded 16? ...
// Get the VBO set for that attibute
GLuint bufName = 0;
glGetVertexAttribiv(index , GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, (GLint*)&bufName);
@ -603,7 +625,7 @@ void mdlDestroyModel(INOUT GLModel **model) {
}
#endif
if (m->custom) {
if (m->custom && m->custom->destroy) {
m->custom->destroy(m);
}

View File

@ -19,7 +19,9 @@
enum {
POS_ATTRIB_IDX,
TEXCOORD_ATTRIB_IDX,
#if 0
NORMAL_ATTRIB_IDX,
#endif
};
typedef struct GLModel;
@ -42,16 +44,21 @@ typedef struct GLModel {
GLenum positionType;
GLuint positionSize;
GLsizei positionArraySize;
GLenum positionUsageHint;
GLvoid *texCoords;
GLenum texcoordType;
GLuint texcoordSize;
GLsizei texcoordArraySize;
GLenum texcoordUsageHint;
#if 0
GLvoid *normals;
GLenum normalType;
GLuint normalSize;
GLsizei normalArraySize;
GLenum normalUsageHint;
#endif
GLvoid *elements;
GLenum elementType;
@ -80,11 +87,29 @@ typedef struct GLModel {
GLCustom *custom;
} GLModel;
#if 0
GLModel *mdlLoadModel(const char *filepathname);
GLModel *mdlLoadQuadModel();
#endif
GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat obj_h, GLfloat z, GLsizei tex_w, GLsizei tex_h, GLCustom clazz);
typedef struct GLModelParams_s {
// positions
GLfloat skew_x;
GLfloat skew_y;
GLfloat z;
GLfloat obj_w;
GLfloat obj_h;
GLenum positionUsageHint;
// texture
GLsizei tex_w;
GLsizei tex_h;
GLenum texcoordUsageHint;
} GLModelParams_s;
GLModel *mdlCreateQuad(GLModelParams_s parms, GLCustom clazz);
void mdlDestroyModel(INOUT GLModel **model);

View File

@ -82,7 +82,7 @@ void glshader_destroySource(demoSource *source) {
FREE(source);
}
GLuint glshader_buildProgram(demoSource *vertexSource, demoSource *fragmentSource, bool hasNormal, bool hasTexcoord, OUTPARM GLuint *vertexShader, OUTPARM GLuint *fragShader) {
GLuint glshader_buildProgram(demoSource *vertexSource, demoSource *fragmentSource, /*bool hasNormal, */bool hasTexcoord, OUTPARM GLuint *vertexShader, OUTPARM GLuint *fragShader) {
GLuint prgName = UNINITIALIZED_GL;
GLint logLength = UNINITIALIZED_GL;
GLint status = UNINITIALIZED_GL;
@ -124,9 +124,11 @@ GLuint glshader_buildProgram(demoSource *vertexSource, demoSource *fragmentSourc
// See buildVAO to see where vertex arrays are actually set
glBindAttribLocation(prgName, POS_ATTRIB_IDX, "inPosition");
#if 0
if (hasNormal) {
glBindAttribLocation(prgName, NORMAL_ATTRIB_IDX, "inNormal");
}
#endif
if (hasTexcoord) {
glBindAttribLocation(prgName, TEXCOORD_ATTRIB_IDX, "inTexcoord");

View File

@ -29,6 +29,6 @@ extern demoSource *glshader_createSource(const char *filepathname);
extern void glshader_destroySource(demoSource *source);
// Builds a GL program from shader sources
extern GLuint glshader_buildProgram(demoSource *vertexSource, demoSource *fragmentSource, bool hasNormal, bool hasTexcoord, OUTPARM GLuint *vertexShader, OUTPARM GLuint *fragShader);
extern GLuint glshader_buildProgram(demoSource *vertexSource, demoSource *fragmentSource, /*bool hasNormal, */bool hasTexcoord, OUTPARM GLuint *vertexShader, OUTPARM GLuint *fragShader);
#endif // __SOURCE_UTIL_H__