Changeset a0eb547 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Mar 8, 2019, 7:47:15 PM (6 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
49db5fc
Parents:
dd9771c
Message:

Create an AttribInfo array for each ShaderModelGroup to support generic handling of shader attributes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rdd9771c ra0eb547  
    5353};
    5454
    55 // TODO: Once all object types use ShaderModelGroup objects,, I should be able to remove the
    56 // shader_program field since it would be available via modelGroups[type].shaderProgram
     55enum AttribType {
     56   ATTRIB_UNIFORM,
     57   ATTRIB_OBJECT_VARYING,
     58   ATTRIB_POINT_VARYING,
     59};
     60
    5761struct SceneObject {
    5862   unsigned int id;
     
    105109struct BufferInfo {
    106110   unsigned int vbo_base;
    107    unsigned int vbo_capacity;
    108111   unsigned int ubo_base;
    109112   unsigned int ubo_offset;
     
    111114};
    112115
     116struct AttribInfo {
     117   AttribType attribType;
     118   GLuint index;
     119   GLint size;
     120   GLenum type;
     121   GLuint buffer;
     122   size_t fieldOffset;
     123};
     124
    113125struct ShaderModelGroup {
    114126   GLuint shaderProgram;
    115127   GLuint vao;
     128   map<string, AttribInfo> attribs;
    116129   unsigned int numPoints;
     130   unsigned int vboCapacity;
    117131};
    118132
     
    158172void addModelToGroup(ShaderModelGroup& modelGroup, SceneObject& model);
    159173
     174void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, GLenum type, size_t fieldOffset);
     175void initModelGroupAttribs(ShaderModelGroup& modelGroup);
     176
     177size_t GLsizeof(GLenum);
     178GLvoid* getVectorAttribPtr(SceneObject& obj, size_t attribOffset);
     179GLvoid* getScalarAttribPtr(SceneObject& obj, size_t attribOffset);
     180
    160181void calculateObjectBoundingBox(SceneObject* obj);
    161182
     
    207228
    208229void translateLaser(Laser* laser, const vec3& translation, GLuint ubo);
    209 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp);
     230void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, ShaderModelGroup& laserSmg, GLuint asteroid_sp);
    210231bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection);
    211232
     
    441462    * of any given object.
    442463    *
    443     * There will be two shader programs for now, one for draing colored objects, and another for
    444     * drawing textured ones. The points, normals, and model mat ubo indices will be passed to both
    445     * shaders, while the colors vbo will only be passed to the colors shader, and the texcoords vbo
    446     * only to the texture shader.
    447     *
    448464    * Right now, the currently selected object is drawn using one color (specified in the selected
    449465    * colors vbo) regardless of whether it is normally rendering using colors or a texture. The selected
     
    454470    */
    455471
    456    map<GLuint, BufferInfo> shaderBufferInfo;
    457    map<ObjectType, ShaderModelGroup> modelGroups;
    458 
    459    modelGroups[TYPE_SHIP] = createModelGroup(
    460       loadShaderProgram("./ship.vert", "./ship.frag"));
    461    shaderBufferInfo[modelGroups[TYPE_SHIP].shaderProgram] = BufferInfo(); // temporary
    462 
    463    modelGroups[TYPE_ASTEROID] = createModelGroup(
    464       loadShaderProgram("./asteroid.vert", "./asteroid.frag"));
    465    shaderBufferInfo[modelGroups[TYPE_ASTEROID].shaderProgram] = BufferInfo(); // temporary
    466 
    467    modelGroups[TYPE_LASER] = createModelGroup(
    468       loadShaderProgram("./laser.vert", "./laser.frag"));
    469    shaderBufferInfo[modelGroups[TYPE_LASER].shaderProgram] = BufferInfo(); // temporary
    470 
    471    modelGroups[TYPE_EXPLOSION] = createModelGroup(
    472       loadShaderProgram("./explosion.vert", "./explosion.frag"));
    473    shaderBufferInfo[modelGroups[TYPE_EXPLOSION].shaderProgram] = BufferInfo(); // temporary
    474 
    475    cam_pos = vec3(0.0f, 0.0f, 2.0f);
    476    float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
    477    float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
    478 
    479    // player ship
    480    SceneObject* ship = createShip();
    481    objects.push_back(ship);
    482 
    483    vector<SceneObject>::iterator obj_it;
    484 
    485472   GLuint points_vbo, colors_vbo, texcoords_vbo, normals_vbo, ubo, model_mat_idx_vbo;
    486473
     
    492479      &ubo,
    493480      &model_mat_idx_vbo);
     481
     482   map<GLuint, BufferInfo> shaderBufferInfo;
     483   map<ObjectType, ShaderModelGroup> modelGroups;
     484
     485   modelGroups[TYPE_SHIP] = createModelGroup(
     486      loadShaderProgram("./ship.vert", "./ship.frag"));
     487   shaderBufferInfo[modelGroups[TYPE_SHIP].shaderProgram] = BufferInfo(); // temporary
     488
     489   defineModelGroupAttrib(modelGroups[TYPE_SHIP], "vertex_position", ATTRIB_POINT_VARYING,
     490      3, GL_FLOAT, offsetof(SceneObject, points));
     491   defineModelGroupAttrib(modelGroups[TYPE_SHIP], "vertex_color", ATTRIB_POINT_VARYING,
     492      3, GL_FLOAT, offsetof(SceneObject, colors));
     493   defineModelGroupAttrib(modelGroups[TYPE_SHIP], "vertex_normal", ATTRIB_POINT_VARYING,
     494      3, GL_FLOAT, offsetof(SceneObject, normals));
     495   defineModelGroupAttrib(modelGroups[TYPE_SHIP], "ubo_index", ATTRIB_OBJECT_VARYING,
     496      1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset));
     497
     498   initModelGroupAttribs(modelGroups[TYPE_SHIP]);
     499
     500   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     501   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     502   modelGroups[TYPE_SHIP].attribs["vertex_position"].buffer = points_vbo;
     503
     504   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     505   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     506   modelGroups[TYPE_SHIP].attribs["vertex_color"].buffer = colors_vbo;
     507
     508   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     509   glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     510   modelGroups[TYPE_SHIP].attribs["vertex_normal"].buffer = normals_vbo;
     511
     512   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
     513   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
     514   modelGroups[TYPE_SHIP].attribs["ubo_index"].buffer = model_mat_idx_vbo;
     515
     516   modelGroups[TYPE_ASTEROID] = createModelGroup(
     517      loadShaderProgram("./asteroid.vert", "./asteroid.frag"));
     518   shaderBufferInfo[modelGroups[TYPE_ASTEROID].shaderProgram] = BufferInfo(); // temporary
     519
     520   defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "vertex_position", ATTRIB_POINT_VARYING,
     521      3, GL_FLOAT, offsetof(SceneObject, points));
     522   defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "vertex_color", ATTRIB_POINT_VARYING,
     523      3, GL_FLOAT, offsetof(SceneObject, colors));
     524   defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "vertex_normal", ATTRIB_POINT_VARYING,
     525      3, GL_FLOAT, offsetof(SceneObject, normals));
     526   defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "ubo_index", ATTRIB_OBJECT_VARYING,
     527      1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset));
     528
     529   initModelGroupAttribs(modelGroups[TYPE_ASTEROID]);
     530
     531   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     532   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     533   modelGroups[TYPE_ASTEROID].attribs["vertex_position"].buffer = points_vbo;
     534
     535   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     536   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     537   modelGroups[TYPE_ASTEROID].attribs["vertex_color"].buffer = colors_vbo;
     538
     539   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     540   glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     541   modelGroups[TYPE_ASTEROID].attribs["vertex_normal"].buffer = normals_vbo;
     542
     543   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
     544   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
     545   modelGroups[TYPE_ASTEROID].attribs["ubo_index"].buffer = model_mat_idx_vbo;
     546
     547   modelGroups[TYPE_LASER] = createModelGroup(
     548      loadShaderProgram("./laser.vert", "./laser.frag"));
     549   shaderBufferInfo[modelGroups[TYPE_LASER].shaderProgram] = BufferInfo(); // temporary
     550
     551   defineModelGroupAttrib(modelGroups[TYPE_LASER], "vertex_position", ATTRIB_POINT_VARYING,
     552      3, GL_FLOAT, offsetof(SceneObject, points));
     553   defineModelGroupAttrib(modelGroups[TYPE_LASER], "vt", ATTRIB_POINT_VARYING,
     554      2, GL_FLOAT, offsetof(SceneObject, texcoords));
     555   defineModelGroupAttrib(modelGroups[TYPE_LASER], "ubo_index", ATTRIB_OBJECT_VARYING,
     556      1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset));
     557
     558   initModelGroupAttribs(modelGroups[TYPE_LASER]);
     559
     560   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     561   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     562   modelGroups[TYPE_LASER].attribs["vertex_position"].buffer = points_vbo;
     563
     564   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
     565   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
     566   modelGroups[TYPE_LASER].attribs["vt"].buffer = texcoords_vbo;
     567
     568   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
     569   glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, NULL);
     570   modelGroups[TYPE_LASER].attribs["ubo_index"].buffer = model_mat_idx_vbo;
     571
     572   modelGroups[TYPE_EXPLOSION] = createModelGroup(
     573      loadShaderProgram("./explosion.vert", "./explosion.frag"));
     574   shaderBufferInfo[modelGroups[TYPE_EXPLOSION].shaderProgram] = BufferInfo(); // temporary
     575
     576   cam_pos = vec3(0.0f, 0.0f, 2.0f);
     577   float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
     578   float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
     579
     580   // player ship
     581   SceneObject* ship = createShip();
     582   objects.push_back(ship);
     583
     584   vector<SceneObject>::iterator obj_it;
    494585
    495586   populateBuffers(objects,
     
    501592      ubo,
    502593      model_mat_idx_vbo);
    503 
    504    glBindVertexArray(modelGroups[TYPE_SHIP].vao);
    505 
    506    glEnableVertexAttribArray(0);
    507    glEnableVertexAttribArray(1);
    508    glEnableVertexAttribArray(2);
    509    glEnableVertexAttribArray(3);
    510 
    511    glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    512    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    513 
    514    // Comment these two lines out when I want to use selected colors
    515    glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    516    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    517 
    518    glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    519    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    520 
    521    glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    522    glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
    523 
    524    glBindVertexArray(modelGroups[TYPE_ASTEROID].vao);
    525 
    526    glEnableVertexAttribArray(0);
    527    glEnableVertexAttribArray(1);
    528    glEnableVertexAttribArray(2);
    529    glEnableVertexAttribArray(3);
    530 
    531    glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    532    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    533 
    534    glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    535    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    536 
    537    glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    538    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    539 
    540    glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    541    glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
    542 
    543    glBindVertexArray(modelGroups[TYPE_LASER].vao);
    544 
    545    glEnableVertexAttribArray(0);
    546    glEnableVertexAttribArray(1);
    547    glEnableVertexAttribArray(2);
    548 
    549    glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    550    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    551 
    552    glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    553    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    554 
    555    glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    556    glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, NULL);
    557594
    558595   float cam_speed = 1.0f;
     
    642679   GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_ASTEROID].shaderProgram, "models");
    643680
     681   // may want to do initialization for basic_texture uniform here too
     682   // Right now, I think I'm getting away without getting that uniform location because I'm only
     683   // using one texture, so setting it to GL_TEXTURE0 once works
    644684   GLuint laser_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "view");
    645685   GLuint laser_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "proj");
     
    872912
    873913         if (leftLaser != NULL && !leftLaser->deleted) {
    874             updateLaserTarget(leftLaser, objects, points_vbo, modelGroups[TYPE_ASTEROID].shaderProgram);
     914            updateLaserTarget(leftLaser, objects, modelGroups[TYPE_LASER], modelGroups[TYPE_ASTEROID].shaderProgram);
    875915         }
    876916         if (rightLaser != NULL && !rightLaser->deleted) {
    877             updateLaserTarget(rightLaser, objects, points_vbo, modelGroups[TYPE_ASTEROID].shaderProgram);
     917            updateLaserTarget(rightLaser, objects, modelGroups[TYPE_LASER], modelGroups[TYPE_ASTEROID].shaderProgram);
    878918         }
    879919      }
     
    946986
    947987         view_mat = R * T;
    948 
    949          //printVector("cam pos", cam_pos);
    950988
    951989         glUseProgram(modelGroups[TYPE_SHIP].shaderProgram);
     
    10741112
    10751113   switch (source) {
    1076    case 0x8246:
    1077       strcpy(source_str, "API");
    1078       break;
    1079    case 0x8247:
    1080       strcpy(source_str, "WINDOW_SYSTEM");
    1081       break;
    1082    case 0x8248:
    1083       strcpy(source_str, "SHADER_COMPILER");
    1084       break;
    1085    case 0x8249:
    1086       strcpy(source_str, "THIRD_PARTY");
    1087       break;
    1088    case 0x824A:
    1089       strcpy(source_str, "APPLICATION");
    1090       break;
    1091    case 0x824B:
    1092       strcpy(source_str, "OTHER");
    1093       break;
    1094    default:
    1095       strcpy(source_str, "undefined");
    1096       break;
     1114      case 0x8246:
     1115         strcpy(source_str, "API");
     1116         break;
     1117      case 0x8247:
     1118         strcpy(source_str, "WINDOW_SYSTEM");
     1119         break;
     1120      case 0x8248:
     1121         strcpy(source_str, "SHADER_COMPILER");
     1122         break;
     1123      case 0x8249:
     1124         strcpy(source_str, "THIRD_PARTY");
     1125         break;
     1126      case 0x824A:
     1127         strcpy(source_str, "APPLICATION");
     1128         break;
     1129      case 0x824B:
     1130         strcpy(source_str, "OTHER");
     1131         break;
     1132      default:
     1133         strcpy(source_str, "undefined");
     1134         break;
    10971135   }
    10981136
    10991137   switch (type) {
    1100    case 0x824C:
    1101       strcpy(type_str, "ERROR");
    1102       break;
    1103    case 0x824D:
    1104       strcpy(type_str, "DEPRECATED_BEHAVIOR");
    1105       break;
    1106    case 0x824E:
    1107       strcpy(type_str, "UNDEFINED_BEHAVIOR");
    1108       break;
    1109    case 0x824F:
    1110       strcpy(type_str, "PORTABILITY");
    1111       break;
    1112    case 0x8250:
    1113       strcpy(type_str, "PERFORMANCE");
    1114       break;
    1115    case 0x8251:
    1116       strcpy(type_str, "OTHER");
    1117       break;
    1118    case 0x8268:
    1119       strcpy(type_str, "MARKER");
    1120       break;
    1121    case 0x8269:
    1122       strcpy(type_str, "PUSH_GROUP");
    1123       break;
    1124    case 0x826A:
    1125       strcpy(type_str, "POP_GROUP");
    1126       break;
    1127    default:
    1128       strcpy(type_str, "undefined");
    1129       break;
     1138      case 0x824C:
     1139         strcpy(type_str, "ERROR");
     1140         break;
     1141      case 0x824D:
     1142         strcpy(type_str, "DEPRECATED_BEHAVIOR");
     1143         break;
     1144      case 0x824E:
     1145         strcpy(type_str, "UNDEFINED_BEHAVIOR");
     1146         break;
     1147      case 0x824F:
     1148         strcpy(type_str, "PORTABILITY");
     1149         break;
     1150      case 0x8250:
     1151         strcpy(type_str, "PERFORMANCE");
     1152         break;
     1153      case 0x8251:
     1154         strcpy(type_str, "OTHER");
     1155         break;
     1156      case 0x8268:
     1157         strcpy(type_str, "MARKER");
     1158         break;
     1159      case 0x8269:
     1160         strcpy(type_str, "PUSH_GROUP");
     1161         break;
     1162      case 0x826A:
     1163         strcpy(type_str, "POP_GROUP");
     1164         break;
     1165      default:
     1166         strcpy(type_str, "undefined");
     1167         break;
    11301168   }
    11311169   switch (severity) {
    1132    case 0x9146:
    1133       strcpy(severity_str, "HIGH");
    1134       break;
    1135    case 0x9147:
    1136       strcpy(severity_str, "MEDIUM");
    1137       break;
    1138    case 0x9148:
    1139       strcpy(severity_str, "LOW");
    1140       break;
    1141    case 0x826B:
    1142       strcpy(severity_str, "NOTIFICATION");
    1143       break;
    1144    default:
    1145       strcpy(severity_str, "undefined");
    1146       break;
     1170      case 0x9146:
     1171         strcpy(severity_str, "HIGH");
     1172         break;
     1173      case 0x9147:
     1174         strcpy(severity_str, "MEDIUM");
     1175         break;
     1176      case 0x9148:
     1177         strcpy(severity_str, "LOW");
     1178         break;
     1179      case 0x826B:
     1180         strcpy(severity_str, "NOTIFICATION");
     1181         break;
     1182      default:
     1183         strcpy(severity_str, "undefined");
     1184         break;
    11471185   }
    11481186
     
    13361374   // Check if the buffers aren't large enough to fit the new object and, if so, call
    13371375   // populateBuffers() to resize and repopupulate them
    1338    if (bufferInfo->vbo_capacity < (modelGroups[obj->type].numPoints + obj->num_points) ||
     1376   if (modelGroups[obj->type].vboCapacity < (modelGroups[obj->type].numPoints + obj->num_points) ||
    13391377      bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) {
    13401378
     
    19171955}
    19181956
     1957void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType,
     1958                  GLint size, GLenum type, size_t fieldOffset) {
     1959   if (type != GL_FLOAT && type != GL_UNSIGNED_INT) {
     1960      cout << "Unknown shader program attribute type: " << type << endl;
     1961      return;
     1962   }
     1963
     1964   AttribInfo attribInfo;
     1965
     1966   attribInfo.attribType = attribType;
     1967   attribInfo.index = modelGroup.attribs.size();
     1968   attribInfo.size = size;
     1969   attribInfo.type = type;
     1970   attribInfo.fieldOffset = fieldOffset;
     1971
     1972   modelGroup.attribs[name] = attribInfo;
     1973}
     1974
     1975void initModelGroupAttribs(ShaderModelGroup& modelGroup) {
     1976   glBindVertexArray(modelGroup.vao);
     1977
     1978   map<string, AttribInfo>::iterator it;
     1979   for (it = modelGroup.attribs.begin(); it != modelGroup.attribs.end(); it++) {
     1980      glEnableVertexAttribArray(it->second.index);
     1981
     1982      glGenBuffers(1, &it->second.buffer);
     1983      glBindBuffer(GL_ARRAY_BUFFER, it->second.buffer);
     1984
     1985      switch (it->second.type) {
     1986         case GL_FLOAT: {
     1987            glVertexAttribPointer(it->second.index, it->second.size, it->second.type, GL_FALSE, 0, NULL);
     1988            break;
     1989         }
     1990         case GL_UNSIGNED_INT: {
     1991            glVertexAttribIPointer(it->second.index, it->second.size, it->second.type, 0, NULL);
     1992            break;
     1993         }
     1994      }
     1995   }
     1996}
     1997
     1998/* The purpose of this function is to replace the use of sizeof() when calling
     1999 * function like glBufferSubData and using AttribInfo to get offsets and types
     2000 * I need instead of hardcoding them. I can't save a type like GLfloat, but I cam
     2001 * save GL_FLOAT and use this function to return sizeof(GLfloat) when GL_FLOAT is
     2002 * passed.
     2003 */
     2004size_t GLsizeof(GLenum type) {
     2005   switch (type) {
     2006      case GL_FLOAT:
     2007         return sizeof(GLfloat);
     2008      case GL_UNSIGNED_INT:
     2009         return sizeof(GLuint);
     2010      default:
     2011         cout << "Uknown GL type passed to GLsizeof: " << type << endl;
     2012         return 0;
     2013   }
     2014}
     2015
     2016/* This function returns a reference to the first element of a given vector
     2017 * attribute in obj. The vector is assumed to hold GLfloats. If the same thing
     2018 * needs to be done later for vectors of other types, we could pass in a GLenum,
     2019 * and do something similar to GLsizeof
     2020 */
     2021GLvoid* getVectorAttribPtr(SceneObject& obj, size_t attribOffset) {
     2022   return (GLvoid*)(&(*(vector<GLfloat>*)((size_t)&obj + attribOffset))[0]);
     2023}
     2024
     2025GLvoid* getScalarAttribPtr(SceneObject& obj, size_t attribOffset) {
     2026   return (GLvoid*)((size_t)&obj + attribOffset);
     2027}
     2028
    19192029void initializeBuffers(
    19202030                  GLuint* points_vbo,
     
    20802190      shaderBufferInfo[shaderCountIt->first].ubo_offset = 0;
    20812191
    2082       shaderBufferInfo[shaderCountIt->first].vbo_capacity = shaderCounts[shaderCountIt->first] * 2;
    20832192      shaderBufferInfo[shaderCountIt->first].ubo_capacity = shaderUboCounts[shaderCountIt->first] * 2;
    20842193
     
    20872196   }
    20882197
     2198   /* Since we just want to start with lasers, make a loop that goes through all the laser model group attributes
     2199    * and allocates data for them. Determine how to go from GLenum (e.g. GL_FLOAT) to typedef (e.g. GLfloat)
     2200    */
    20892201   map<ObjectType, ShaderModelGroup>::iterator modelGroupIt;
     2202   ShaderModelGroup* smg;
    20902203   for (modelGroupIt = modelGroups.begin(); modelGroupIt != modelGroups.end(); modelGroupIt++) {
    2091       modelGroups[modelGroupIt->first].numPoints = 0;
     2204      smg = &modelGroups[modelGroupIt->first];
     2205
     2206      smg->numPoints = 0;
     2207      smg->vboCapacity = shaderCounts[smg->shaderProgram] * 2;
     2208      /*
     2209      if (modelGroupIt->first == TYPE_LASER) {
     2210         smg = &modelGroups[modelGroupIt->first];
     2211         smg->numPoints = shaderCounts[smg->shaderProgram];
     2212
     2213         // Here, I could add glBufferData calls to allocate space for laser buffers
     2214         if (modelGroupIt->first == TYPE_LASER) {
     2215            glBindBuffer(GL_ARRAY_BUFFER, smg->attribs["vertex_position"].buffer);
     2216            glBufferData(GL_ARRAY_BUFFER, smg->numPoints * sizeof(GLfloat) * smg->attribs["vertex_position"].size, NULL, GL_DYNAMIC_DRAW);
     2217         }
     2218      }
     2219      */
    20922220   }
    20932221
     
    21422270
    21432271   if (obj.type != TYPE_EXPLOSION) {
    2144       glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    2145       for (unsigned int i = 0; i < obj.num_points; i++) {
    2146          glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &obj.ubo_offset);
    2147       }
    2148 
    2149       glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    2150       glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]);
    2151 
    2152       glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    2153       glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]);
    2154 
    2155       if (obj.type != TYPE_LASER) {
    2156          glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    2157          glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]);
    2158 
    2159          glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    2160          glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]);
     2272      ShaderModelGroup& smg = modelGroups[obj.type];
     2273
     2274      for (map<string, AttribInfo>::iterator it = smg.attribs.begin(); it != smg.attribs.end(); it++) {
     2275         glBindBuffer(GL_ARRAY_BUFFER, it->second.buffer);
     2276
     2277         switch (it->second.attribType) {
     2278            case ATTRIB_POINT_VARYING:
     2279               glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * GLsizeof(it->second.type) * it->second.size,
     2280                  obj.num_points * GLsizeof(it->second.type) * it->second.size, getVectorAttribPtr(obj, it->second.fieldOffset));
     2281               break;
     2282            case ATTRIB_OBJECT_VARYING:
     2283               for (unsigned int i = 0; i < obj.num_points; i++) {
     2284                  glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * GLsizeof(it->second.type) * it->second.size,
     2285                     GLsizeof(it->second.type) * it->second.size, getScalarAttribPtr(obj, it->second.fieldOffset));
     2286               }
     2287               break;
     2288            case ATTRIB_UNIFORM:
     2289              break;
     2290         }
    21612291      }
    21622292
     
    22192349}
    22202350
    2221 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp) {
     2351void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, ShaderModelGroup& laserSmg, GLuint asteroid_sp) {
    22222352   // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created,
    22232353   // and then re-used here
     
    22702400      length = glm::length(closestIntersection - start);
    22712401
    2272       // TODO: Find a more generic way of updating the laser hp than in updateLaserTarget
     2402      // TODO: Find a more generic way of updating the asteroid hp than in updateLaserTarget
    22732403
    22742404      glUseProgram(asteroid_sp);
     
    22892419   laser->points[53] = -length + width / 2;
    22902420
    2291    glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    2292    glBufferSubData(GL_ARRAY_BUFFER, laser->vertex_vbo_offset * sizeof(GLfloat) * 3, laser->points.size() * sizeof(GLfloat), &laser->points[0]);
     2421   AttribInfo* attrib = &laserSmg.attribs["vertex_position"];
     2422   glBindBuffer(GL_ARRAY_BUFFER, attrib->buffer);
     2423   glBufferSubData(GL_ARRAY_BUFFER, laser->vertex_vbo_offset * GLsizeof(attrib->type) * attrib->size,
     2424      laser->num_points * GLsizeof(attrib->type) * attrib->size, getVectorAttribPtr(*laser, attrib->fieldOffset));
    22932425}
    22942426
Note: See TracChangeset for help on using the changeset viewer.