REFACTOR : GLModel render pass is common for HUD models

This commit is contained in:
Aaron Culliney
2015-04-12 11:27:33 -07:00
parent 06051e44e7
commit 121e250c62
6 changed files with 70 additions and 117 deletions

View File

@@ -57,7 +57,7 @@ static void _cpuanim_show(GLModel *parent) {
const unsigned int row = (CPUTIMING_TEMPLATE_COLS+1);
char buf[8];
char buf[8] = { 0 };
double scale = (alt_speed_enabled ? cpu_altscale_factor : cpu_scale_factor);
int percentScale = scale * 100;
if (percentScale < 100.0) {
@@ -76,9 +76,8 @@ static void _cpuanim_show(GLModel *parent) {
((hudElement->tpl)+(row*1))[4] = buf[2];
}
setupDefaultGLModelHUDElement(parent);
glhud_setupDefault(parent);
isEnabled = true;
clock_gettime(CLOCK_MONOTONIC, &cputiming_begin);
}
@@ -87,9 +86,9 @@ static void cpuanim_init(void) {
mdlDestroyModel(&cpuMessageObjModel);
cpuMessageObjModel = mdlCreateQuad(-0.3, -0.3, 0.6, 0.6, MODEL_DEPTH, MESSAGE_FB_WIDTH, MESSAGE_FB_HEIGHT, GL_RGBA/*RGBA_8888*/, (GLCustom){
.create = createDefaultGLModelHUDElement,
.create = &glhud_createDefault,
.setup = &_cpuanim_show,
.destroy = destroyDefaultGLModelHUDElement,
.destroy = &glhud_destroyDefault,
});
if (!cpuMessageObjModel) {
LOG("not initializing CPU speed animations");
@@ -108,46 +107,6 @@ static void cpuanim_destroy(void) {
mdlDestroyModel(&cpuMessageObjModel);
}
static void _render_message_object(GLModel *model) {
// Bind our vertex array object
#if USE_VAO
glBindVertexArray(model->vaoName);
#else
glBindBuffer(GL_ARRAY_BUFFER, model->posBufferName);
GLsizei posTypeSize = _get_gl_type_size(model->positionType);
GLsizei texcoordTypeSize = _get_gl_type_size(model->texcoordType);
// 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)
model->positionSize, // How many elements are there per position?
model->positionType, // What is the type of this data?
GL_FALSE, // Do we want to normalize this data (0-1 range for fixed-pont types)
model->positionSize*posTypeSize, // What is the stride (i.e. bytes between positions)?
0); // What is the offset in the VBO to the position data?
glEnableVertexAttribArray(POS_ATTRIB_IDX);
// 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, model->texcoordBufferName);
glVertexAttribPointer(TEXCOORD_ATTRIB_IDX, // What attibute index will this array feed in the vertex shader (see buildProgram)
model->texcoordSize, // How many elements are there per texture coord?
model->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)
model->texcoordSize*texcoordTypeSize,// What is the stride (i.e. bytes between texcoords)?
0); // What is the offset in the VBO to the texcoord data?
glEnableVertexAttribArray(TEXCOORD_ATTRIB_IDX);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->elementBufferName);
#endif
// Draw the message object
glDrawElements(model->primType, model->numElements, model->elementType, 0);
GL_ERRLOG("CPU message render");
}
static void cpuanim_render(void) {
if (!isAvailable) {
return;
@@ -180,7 +139,7 @@ static void cpuanim_render(void) {
glTexImage2D(GL_TEXTURE_2D, /*level*/0, /*internal format*/GL_RGBA, MESSAGE_FB_WIDTH, MESSAGE_FB_HEIGHT, /*border*/0, /*format*/GL_RGBA, GL_UNSIGNED_BYTE, cpuMessageObjModel->texPixels);
}
glUniform1i(uniformTex2Use, TEXTURE_ID_MESSAGE);
_render_message_object(cpuMessageObjModel);
glhud_renderDefault(cpuMessageObjModel);
}
static void cpuanim_reshape(int w, int h) {
@@ -192,6 +151,7 @@ static void cpuanim_show(void) {
return;
}
_cpuanim_show(cpuMessageObjModel);
isEnabled = true;
}
__attribute__((constructor))

View File

@@ -10,12 +10,9 @@
*/
#include "glhudmodel.h"
#include "glvideo.h"
void *(*createDefaultGLModelHUDElement)(void) = NULL;
void (*setupDefaultGLModelHUDElement)(GLModel *parent) = NULL;
void (*destroyDefaultGLModelHUDElement)(GLModel *parent) = NULL;
static void *createDefault(void) {
void *glhud_createDefault(void) {
GLModelHUDElement *custom = (GLModelHUDElement *)calloc(sizeof(GLModelHUDElement), 1);
if (!custom) {
return NULL;
@@ -23,7 +20,7 @@ static void *createDefault(void) {
return custom;
}
static void setupDefault(GLModel *parent) {
void glhud_setupDefault(GLModel *parent) {
GLModelHUDElement *hudElement = (GLModelHUDElement *)parent->custom;
char *submenu = (char *)(hudElement->tpl);
@@ -55,7 +52,47 @@ static void setupDefault(GLModel *parent) {
parent->texDirty = true;
}
static void destroyDefault(GLModel *parent) {
void glhud_renderDefault(GLModel *parent) {
// Bind our vertex array object
#if USE_VAO
glBindVertexArray(parent->vaoName);
#else
glBindBuffer(GL_ARRAY_BUFFER, parent->posBufferName);
GLsizei posTypeSize = getGLTypeSize(parent->positionType);
GLsizei texcoordTypeSize = getGLTypeSize(parent->texcoordType);
// 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)
parent->positionSize, // How many elements are there per position?
parent->positionType, // What is the type of this data?
GL_FALSE, // Do we want to normalize this data (0-1 range for fixed-pont types)
parent->positionSize*posTypeSize, // What is the stride (i.e. bytes between positions)?
0); // What is the offset in the VBO to the position data?
glEnableVertexAttribArray(POS_ATTRIB_IDX);
// 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, parent->texcoordBufferName);
glVertexAttribPointer(TEXCOORD_ATTRIB_IDX, // What attibute index will this array feed in the vertex shader (see buildProgram)
parent->texcoordSize, // How many elements are there per texture coord?
parent->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)
parent->texcoordSize*texcoordTypeSize, // What is the stride (i.e. bytes between texcoords)?
0); // What is the offset in the VBO to the texcoord data?
glEnableVertexAttribArray(TEXCOORD_ATTRIB_IDX);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, parent->elementBufferName);
#endif
// Draw the object
glDrawElements(parent->primType, parent->numElements, parent->elementType, 0);
GL_ERRLOG("glhudparent render");
}
void glhud_destroyDefault(GLModel *parent) {
GLModelHUDElement *hudElement = (GLModelHUDElement *)parent->custom;
if (!hudElement) {
return;
@@ -72,10 +109,3 @@ static void destroyDefault(GLModel *parent) {
FREE(parent->custom);
}
__attribute__((constructor))
static void _init_glhudmodel_common(void) {
LOG("Setting up default GL HUD model stuff");
createDefaultGLModelHUDElement = &createDefault;
setupDefaultGLModelHUDElement = &setupDefault;
destroyDefaultGLModelHUDElement = &destroyDefault;
}

View File

@@ -30,12 +30,15 @@ MODEL_CLASS(GLModelHUDElement,
);
// default model creation
extern void *(*createDefaultGLModelHUDElement)(void);
void *glhud_createDefault(void);
// default model setup
extern void (*setupDefaultGLModelHUDElement)(GLModel *parent);
void glhud_setupDefault(GLModel *parent);
// render default
void glhud_renderDefault(GLModel *parent);
// default model destruction
extern void (*destroyDefaultGLModelHUDElement)(GLModel *parent);
void glhud_destroyDefault(GLModel *parent);
#endif

View File

@@ -151,11 +151,11 @@ static void _setup_axis_object(GLModel *parent) {
((hudElement->tpl)+(row*2))[4] = axes.eastChar;
((hudElement->tpl)+(row*4))[2] = axes.southChar;
setupDefaultGLModelHUDElement(parent);
glhud_setupDefault(parent);
}
static void *_create_touchjoy_hud(void) {
GLModelHUDElement *hudElement = (GLModelHUDElement *)createDefaultGLModelHUDElement();
GLModelHUDElement *hudElement = (GLModelHUDElement *)glhud_createDefault();
hudElement->blackIsTransparent = true;
return hudElement;
}
@@ -183,7 +183,7 @@ static void _setup_button_object(GLModel *parent) {
const unsigned int row = (BUTTON_TEMPLATE_COLS+1);
((hudElement->tpl)+(row*0))[0] = buttons.activeChar;
setupDefaultGLModelHUDElement(parent);
glhud_setupDefault(parent);
}
static inline void _screen_to_model(float x, float y, float *screenX, float *screenY) {
@@ -201,7 +201,7 @@ static void _model_to_screen(float screenCoords[4], GLModel *model) {
#warning NOTE: we possibly should use matrix calculations (but assuming HUD elements are identity/orthographic for now)
GLfloat *positions = (GLfloat *)(model->positions);
unsigned int stride = model->positionSize;
unsigned int len = model->positionArraySize/_get_gl_type_size(model->positionType);
unsigned int len = model->positionArraySize/getGLTypeSize(model->positionType);
for (unsigned int i=0; i < len; i += stride) {
float x = (positions[i] + 1.f) / 2.f;
if (x < x0) {
@@ -239,7 +239,7 @@ static void gltouchjoy_init(void) {
axes.model = mdlCreateQuad(-1.05, -1.0, AXIS_OBJ_W, AXIS_OBJ_H, MODEL_DEPTH, AXIS_FB_WIDTH, AXIS_FB_HEIGHT, GL_RGBA/*RGBA_8888*/, (GLCustom){
.create = &_create_touchjoy_hud,
.setup = &_setup_axis_object,
.destroy = destroyDefaultGLModelHUDElement,
.destroy = &glhud_destroyDefault,
});
if (!axes.model) {
LOG("gltouchjoy not initializing axis");
@@ -251,7 +251,7 @@ static void gltouchjoy_init(void) {
buttons.model = mdlCreateQuad(1.05-BUTTON_OBJ_W, -1.0, BUTTON_OBJ_W, BUTTON_OBJ_H, MODEL_DEPTH, BUTTON_FB_WIDTH, BUTTON_FB_HEIGHT, GL_RGBA/*RGBA_8888*/, (GLCustom){
.create = &_create_touchjoy_hud,
.setup = &_setup_button_object,
.destroy = destroyDefaultGLModelHUDElement,
.destroy = &glhud_destroyDefault,
});
if (!buttons.model) {
LOG("gltouchjoy not initializing buttons");
@@ -276,46 +276,6 @@ static void gltouchjoy_destroy(void) {
mdlDestroyModel(&buttons.model);
}
static void _render_object(GLModel *model) {
// Bind our vertex array object
#if USE_VAO
glBindVertexArray(model->vaoName);
#else
glBindBuffer(GL_ARRAY_BUFFER, model->posBufferName);
GLsizei posTypeSize = _get_gl_type_size(model->positionType);
GLsizei texcoordTypeSize = _get_gl_type_size(model->texcoordType);
// 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)
model->positionSize, // How many elements are there per position?
model->positionType, // What is the type of this data?
GL_FALSE, // Do we want to normalize this data (0-1 range for fixed-pont types)
model->positionSize*posTypeSize, // What is the stride (i.e. bytes between positions)?
0); // What is the offset in the VBO to the position data?
glEnableVertexAttribArray(POS_ATTRIB_IDX);
// 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, model->texcoordBufferName);
glVertexAttribPointer(TEXCOORD_ATTRIB_IDX, // What attibute index will this array feed in the vertex shader (see buildProgram)
model->texcoordSize, // How many elements are there per texture coord?
model->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)
model->texcoordSize*texcoordTypeSize,// What is the stride (i.e. bytes between texcoords)?
0); // What is the offset in the VBO to the texcoord data?
glEnableVertexAttribArray(TEXCOORD_ATTRIB_IDX);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->elementBufferName);
#endif
// Draw the object
glDrawElements(model->primType, model->numElements, model->elementType, 0);
GL_ERRLOG("gltouchjoy render");
}
static void gltouchjoy_render(void) {
if (!isAvailable) {
return;
@@ -362,7 +322,7 @@ static void gltouchjoy_render(void) {
glBufferData(GL_ARRAY_BUFFER, axes.model->positionArraySize, axes.model->positions, GL_DYNAMIC_DRAW);
}
glUniform1i(uniformTex2Use, TEXTURE_ID_TOUCHJOY_AXIS);
_render_object(axes.model);
glhud_renderDefault(axes.model);
}
// draw button(s)
@@ -396,7 +356,7 @@ static void gltouchjoy_render(void) {
glBufferData(GL_ARRAY_BUFFER, buttons.model->positionArraySize, buttons.model->positions, GL_DYNAMIC_DRAW);
}
glUniform1i(uniformTex2Use, TEXTURE_ID_TOUCHJOY_BUTTON);
_render_object(buttons.model);
glhud_renderDefault(buttons.model);
}
}

View File

@@ -134,7 +134,7 @@ static void _create_VAO_VBOs(void) {
glEnableVertexAttribArray(POS_ATTRIB_IDX);
// Get the size of the position type so we can set the stride properly
GLsizei posTypeSize = _get_gl_type_size(crtModel->positionType);
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
@@ -162,7 +162,7 @@ static void _create_VAO_VBOs(void) {
glEnableVertexAttribArray(NORMAL_ATTRIB_IDX);
// Get the size of the normal type so we can set the stride properly
GLsizei normalTypeSize = _get_gl_type_size(crtModel->normalType);
GLsizei normalTypeSize = getGLTypeSize(crtModel->normalType);
// Set up parmeters for position attribute in the VAO including,
// size, type, stride, and offset in the currenly bound VAO
@@ -189,7 +189,7 @@ static void _create_VAO_VBOs(void) {
glEnableVertexAttribArray(TEXCOORD_ATTRIB_IDX);
// Get the size of the texcoord type so we can set the stride properly
GLsizei texcoordTypeSize = _get_gl_type_size(crtModel->texcoordType);
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
@@ -725,8 +725,8 @@ static void gldriver_render(void) {
#else
glBindBuffer(GL_ARRAY_BUFFER, posBufferName);
GLsizei posTypeSize = _get_gl_type_size(crtModel->positionType);
GLsizei texcoordTypeSize = _get_gl_type_size(crtModel->texcoordType);
GLsizei posTypeSize = getGLTypeSize(crtModel->positionType);
GLsizei texcoordTypeSize = getGLTypeSize(crtModel->texcoordType);
// 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

View File

@@ -60,7 +60,7 @@ enum {
#endif
};
static inline GLsizei _get_gl_type_size(GLenum type) {
static inline GLsizei getGLTypeSize(GLenum type) {
switch (type) {
case GL_BYTE:
return sizeof(GLbyte);