Changeset a0eb547 in opengl-game
- Timestamp:
- Mar 8, 2019, 7:47:15 PM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 49db5fc
- Parents:
- dd9771c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
rdd9771c ra0eb547 53 53 }; 54 54 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 55 enum AttribType { 56 ATTRIB_UNIFORM, 57 ATTRIB_OBJECT_VARYING, 58 ATTRIB_POINT_VARYING, 59 }; 60 57 61 struct SceneObject { 58 62 unsigned int id; … … 105 109 struct BufferInfo { 106 110 unsigned int vbo_base; 107 unsigned int vbo_capacity;108 111 unsigned int ubo_base; 109 112 unsigned int ubo_offset; … … 111 114 }; 112 115 116 struct AttribInfo { 117 AttribType attribType; 118 GLuint index; 119 GLint size; 120 GLenum type; 121 GLuint buffer; 122 size_t fieldOffset; 123 }; 124 113 125 struct ShaderModelGroup { 114 126 GLuint shaderProgram; 115 127 GLuint vao; 128 map<string, AttribInfo> attribs; 116 129 unsigned int numPoints; 130 unsigned int vboCapacity; 117 131 }; 118 132 … … 158 172 void addModelToGroup(ShaderModelGroup& modelGroup, SceneObject& model); 159 173 174 void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, GLenum type, size_t fieldOffset); 175 void initModelGroupAttribs(ShaderModelGroup& modelGroup); 176 177 size_t GLsizeof(GLenum); 178 GLvoid* getVectorAttribPtr(SceneObject& obj, size_t attribOffset); 179 GLvoid* getScalarAttribPtr(SceneObject& obj, size_t attribOffset); 180 160 181 void calculateObjectBoundingBox(SceneObject* obj); 161 182 … … 207 228 208 229 void translateLaser(Laser* laser, const vec3& translation, GLuint ubo); 209 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp);230 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, ShaderModelGroup& laserSmg, GLuint asteroid_sp); 210 231 bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection); 211 232 … … 441 462 * of any given object. 442 463 * 443 * There will be two shader programs for now, one for draing colored objects, and another for444 * drawing textured ones. The points, normals, and model mat ubo indices will be passed to both445 * shaders, while the colors vbo will only be passed to the colors shader, and the texcoords vbo446 * only to the texture shader.447 *448 464 * Right now, the currently selected object is drawn using one color (specified in the selected 449 465 * colors vbo) regardless of whether it is normally rendering using colors or a texture. The selected … … 454 470 */ 455 471 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(); // temporary462 463 modelGroups[TYPE_ASTEROID] = createModelGroup(464 loadShaderProgram("./asteroid.vert", "./asteroid.frag"));465 shaderBufferInfo[modelGroups[TYPE_ASTEROID].shaderProgram] = BufferInfo(); // temporary466 467 modelGroups[TYPE_LASER] = createModelGroup(468 loadShaderProgram("./laser.vert", "./laser.frag"));469 shaderBufferInfo[modelGroups[TYPE_LASER].shaderProgram] = BufferInfo(); // temporary470 471 modelGroups[TYPE_EXPLOSION] = createModelGroup(472 loadShaderProgram("./explosion.vert", "./explosion.frag"));473 shaderBufferInfo[modelGroups[TYPE_EXPLOSION].shaderProgram] = BufferInfo(); // temporary474 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 ship480 SceneObject* ship = createShip();481 objects.push_back(ship);482 483 vector<SceneObject>::iterator obj_it;484 485 472 GLuint points_vbo, colors_vbo, texcoords_vbo, normals_vbo, ubo, model_mat_idx_vbo; 486 473 … … 492 479 &ubo, 493 480 &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; 494 585 495 586 populateBuffers(objects, … … 501 592 ubo, 502 593 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 colors515 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);557 594 558 595 float cam_speed = 1.0f; … … 642 679 GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_ASTEROID].shaderProgram, "models"); 643 680 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 644 684 GLuint laser_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "view"); 645 685 GLuint laser_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "proj"); … … 872 912 873 913 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); 875 915 } 876 916 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); 878 918 } 879 919 } … … 946 986 947 987 view_mat = R * T; 948 949 //printVector("cam pos", cam_pos);950 988 951 989 glUseProgram(modelGroups[TYPE_SHIP].shaderProgram); … … 1074 1112 1075 1113 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; 1097 1135 } 1098 1136 1099 1137 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; 1130 1168 } 1131 1169 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; 1147 1185 } 1148 1186 … … 1336 1374 // Check if the buffers aren't large enough to fit the new object and, if so, call 1337 1375 // 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) || 1339 1377 bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) { 1340 1378 … … 1917 1955 } 1918 1956 1957 void 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 1975 void 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 */ 2004 size_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 */ 2021 GLvoid* getVectorAttribPtr(SceneObject& obj, size_t attribOffset) { 2022 return (GLvoid*)(&(*(vector<GLfloat>*)((size_t)&obj + attribOffset))[0]); 2023 } 2024 2025 GLvoid* getScalarAttribPtr(SceneObject& obj, size_t attribOffset) { 2026 return (GLvoid*)((size_t)&obj + attribOffset); 2027 } 2028 1919 2029 void initializeBuffers( 1920 2030 GLuint* points_vbo, … … 2080 2190 shaderBufferInfo[shaderCountIt->first].ubo_offset = 0; 2081 2191 2082 shaderBufferInfo[shaderCountIt->first].vbo_capacity = shaderCounts[shaderCountIt->first] * 2;2083 2192 shaderBufferInfo[shaderCountIt->first].ubo_capacity = shaderUboCounts[shaderCountIt->first] * 2; 2084 2193 … … 2087 2196 } 2088 2197 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 */ 2089 2201 map<ObjectType, ShaderModelGroup>::iterator modelGroupIt; 2202 ShaderModelGroup* smg; 2090 2203 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 */ 2092 2220 } 2093 2221 … … 2142 2270 2143 2271 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 } 2161 2291 } 2162 2292 … … 2219 2349 } 2220 2350 2221 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp) {2351 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, ShaderModelGroup& laserSmg, GLuint asteroid_sp) { 2222 2352 // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created, 2223 2353 // and then re-used here … … 2270 2400 length = glm::length(closestIntersection - start); 2271 2401 2272 // TODO: Find a more generic way of updating the laserhp than in updateLaserTarget2402 // TODO: Find a more generic way of updating the asteroid hp than in updateLaserTarget 2273 2403 2274 2404 glUseProgram(asteroid_sp); … … 2289 2419 laser->points[53] = -length + width / 2; 2290 2420 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)); 2293 2425 } 2294 2426
Note:
See TracChangeset
for help on using the changeset viewer.