Changeset c3c3158 in opengl-game for new-game.cpp
- Timestamp:
- Jun 22, 2018, 3:06:15 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- c94a699
- Parents:
- c9af90a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
rc9af90a rc3c3158 42 42 GLuint shader_program; 43 43 unsigned int num_points; 44 GL int vertex_vbo_offset;45 GL int ubo_offset;44 GLuint vertex_vbo_offset; 45 GLuint ubo_offset; 46 46 vector<GLfloat> points; 47 47 vector<GLfloat> colors; … … 49 49 vector<GLfloat> normals; 50 50 vector<GLfloat> selected_colors; 51 bool deleted; 52 }; 53 54 struct BufferInfo { 55 unsigned int vbo_base; 56 unsigned int vbo_offset; 57 unsigned int vbo_capacity; 58 unsigned int ubo_base; 59 unsigned int ubo_offset; 60 unsigned int ubo_capacity; 51 61 }; 52 62 … … 84 94 mat4 proj_mat; 85 95 96 // TODO: Consider using a list instead since it will make element deletion more efficient 86 97 vector<SceneObject> objects; 87 98 queue<Event> events; … … 113 124 void print4DVector(string label, vec4 v); 114 125 115 void addObjectToScene(SceneObject& obj); 126 void addObjectToSceneDuringInit(SceneObject& obj); 127 void addObjectToScene(SceneObject& obj, map<GLuint, BufferInfo>& shaderBufferInfo, 128 GLuint points_vbo, 129 GLuint colors_vbo, 130 GLuint selected_colors_vbo, 131 GLuint texcoords_vbo, 132 GLuint normals_vbo, 133 GLuint ubo, 134 GLuint model_mat_idx_vbo); 135 void removeObjectFromScene(int objectId, GLuint ubo); 136 137 void initializeBuffers( 138 GLuint* points_vbo, 139 GLuint* colors_vbo, 140 GLuint* selected_colors_vbo, 141 GLuint* texcoords_vbo, 142 GLuint* normals_vbo, 143 GLuint* ubo, 144 GLuint* model_mat_idx_vbo); 145 116 146 void populateBuffers(vector<SceneObject>& objects, 117 GLuint* points_vbo, 118 GLuint* colors_vbo, 119 GLuint* selected_colors_vbo, 120 GLuint* texcoords_vbo, 121 GLuint* normals_vbo, 122 GLuint* ubo, 123 GLuint* model_mat_idx_vbo, 124 map<GLuint, unsigned int>& shaderCounts, 125 map<GLuint, unsigned int>& curShaderBase); 147 map<GLuint, BufferInfo>& shaderBufferInfo, 148 GLuint points_vbo, 149 GLuint colors_vbo, 150 GLuint selected_colors_vbo, 151 GLuint texcoords_vbo, 152 GLuint normals_vbo, 153 GLuint ubo, 154 GLuint model_mat_idx_vbo); 155 156 void copyObjectDataToBuffers(SceneObject& obj, 157 map<GLuint, BufferInfo>& shaderBufferInfo, 158 GLuint points_vbo, 159 GLuint colors_vbo, 160 GLuint selected_colors_vbo, 161 GLuint texcoords_vbo, 162 GLuint normals_vbo, 163 GLuint ubo, 164 GLuint model_mat_idx_vbo); 126 165 127 166 void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo); … … 136 175 GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo, 137 176 SceneObject* selectedObject, 138 map<GLuint, unsigned int>& shaderCounts, 139 map<GLuint, unsigned int>& curShaderBase); 177 map<GLuint, BufferInfo>& shaderBufferInfo); 140 178 void renderSceneGui(); 141 179 142 void spawnAsteroid(vec3 pos, GLuint shader); 180 void spawnAsteroid(vec3 pos, GLuint shader, 181 map<GLuint, BufferInfo>& shaderBufferInfo, 182 GLuint points_vbo, 183 GLuint colors_vbo, 184 GLuint selected_colors_vbo, 185 GLuint texcoords_vbo, 186 GLuint normals_vbo, 187 GLuint ubo, 188 GLuint model_mat_idx_vbo); 143 189 144 190 int main(int argc, char* argv[]) { … … 285 331 */ 286 332 333 map<GLuint, BufferInfo> shaderBufferInfo; 334 287 335 GLuint color_sp = loadShaderProgram("./color.vert", "./color.frag"); 288 336 GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag"); 337 338 shaderBufferInfo[color_sp] = BufferInfo(); 339 shaderBufferInfo[texture_sp] = BufferInfo(); 289 340 290 341 SceneObject obj; … … 328 379 }; 329 380 330 T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));381 T_model = translate(mat4(), vec3(0.45f, -1.5f, 0.0f)); 331 382 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 332 383 obj.model_base = T_model*R_model; 333 384 334 addObjectToScene (obj);385 addObjectToSceneDuringInit(obj); 335 386 336 387 // square … … 370 421 }; 371 422 372 T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));423 T_model = translate(mat4(), vec3(-0.5f, -1.5f, -1.00f)); 373 424 R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f)); 374 425 obj.model_base = T_model*R_model; 375 426 376 addObjectToScene (obj);427 addObjectToSceneDuringInit(obj); 377 428 */ 378 429 … … 753 804 obj.model_base = T_model * R_model * scale(mat4(), vec3(0.1f, 0.1f, 0.1f)); 754 805 755 addObjectToScene(obj); 756 757 spawnAsteroid(vec3(0.0f, -1.2f, -21.5f), color_sp); 758 spawnAsteroid(vec3(1.0f, -1.2f, -21.5f), color_sp); 759 spawnAsteroid(vec3(-0.5f, -1.2f, -20.8f), color_sp); 806 addObjectToSceneDuringInit(obj); 760 807 761 808 vector<SceneObject>::iterator obj_it; 762 GLsizeiptr offset;763 809 764 810 GLuint points_vbo, colors_vbo, selected_colors_vbo, texcoords_vbo, 765 811 normals_vbo, ubo, model_mat_idx_vbo; 766 812 767 map<GLuint, unsigned int> shaderCounts, curShaderBase; 813 initializeBuffers( 814 &points_vbo, 815 &colors_vbo, 816 &selected_colors_vbo, 817 &texcoords_vbo, 818 &normals_vbo, 819 &ubo, 820 &model_mat_idx_vbo); 768 821 769 822 populateBuffers(objects, 770 &points_vbo, 771 &colors_vbo, 772 &selected_colors_vbo, 773 &texcoords_vbo, 774 &normals_vbo, 775 &ubo, 776 &model_mat_idx_vbo, 777 shaderCounts, 778 curShaderBase); 823 shaderBufferInfo, 824 points_vbo, 825 colors_vbo, 826 selected_colors_vbo, 827 texcoords_vbo, 828 normals_vbo, 829 ubo, 830 model_mat_idx_vbo); 831 832 spawnAsteroid(vec3(0.0f, -1.2f, -21.5f), color_sp, 833 shaderBufferInfo, 834 points_vbo, 835 colors_vbo, 836 selected_colors_vbo, 837 texcoords_vbo, 838 normals_vbo, 839 ubo, 840 model_mat_idx_vbo); 841 spawnAsteroid(vec3(1.0f, -1.2f, -21.5f), color_sp, 842 shaderBufferInfo, 843 points_vbo, 844 colors_vbo, 845 selected_colors_vbo, 846 texcoords_vbo, 847 normals_vbo, 848 ubo, 849 model_mat_idx_vbo); 850 spawnAsteroid(vec3(-0.5f, -1.2f, -20.8f), color_sp, 851 shaderBufferInfo, 852 points_vbo, 853 colors_vbo, 854 selected_colors_vbo, 855 texcoords_vbo, 856 normals_vbo, 857 ubo, 858 model_mat_idx_vbo); 859 spawnAsteroid(vec3(-0.3f, -1.2f, -20.8f), color_sp, 860 shaderBufferInfo, 861 points_vbo, 862 colors_vbo, 863 selected_colors_vbo, 864 texcoords_vbo, 865 normals_vbo, 866 ubo, 867 model_mat_idx_vbo); 868 spawnAsteroid(vec3(-0.1f, -1.2f, -20.8f), color_sp, 869 shaderBufferInfo, 870 points_vbo, 871 colors_vbo, 872 selected_colors_vbo, 873 texcoords_vbo, 874 normals_vbo, 875 ubo, 876 model_mat_idx_vbo); 779 877 780 878 GLuint vao = 0; … … 969 1067 transformObject(objects[2], translate(mat4(), vec3(0.0f, 0.0f, 0.06f)), ubo); 970 1068 transformObject(objects[3], translate(mat4(), vec3(0.0f, 0.0f, 0.04f)), ubo); 1069 transformObject(objects[4], translate(mat4(), vec3(0.0f, 0.0f, 0.06f)), ubo); 1070 transformObject(objects[5], translate(mat4(), vec3(0.0f, 0.0f, 0.04f)), ubo); 971 1071 } 1072 1073 if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) { 1074 removeObjectFromScene(0, ubo); 1075 } 1076 } 1077 1078 if (key_state[GLFW_KEY_ESCAPE] == GLFW_PRESS) { 1079 glfwSetWindowShouldClose(window, 1); 1080 } 1081 1082 float dist = cam_speed * elapsed_seconds; 1083 if (key_pressed[GLFW_KEY_A]) { 1084 vec3 dir = (inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f)).xyz(); 1085 cam_pos += dir * dist; 1086 1087 cam_moved = true; 1088 } 1089 if (key_pressed[GLFW_KEY_D]) { 1090 vec3 dir = (inverse(R) * vec4(1.0f, 0.0f, 0.0f, 1.0f)).xyz(); 1091 cam_pos += dir * dist; 1092 1093 cam_moved = true; 1094 } 1095 if (key_pressed[GLFW_KEY_W]) { 1096 vec3 dir = (inverse(R) * vec4(0.0f, 0.0f, -1.0f, 1.0f)).xyz(); 1097 cam_pos += dir * dist; 1098 1099 cam_moved = true; 1100 } 1101 if (key_pressed[GLFW_KEY_S]) { 1102 vec3 dir = (inverse(R) * vec4(0.0f, 0.0f, 1.0f, 1.0f)).xyz(); 1103 cam_pos += dir * dist; 1104 1105 cam_moved = true; 1106 } 1107 /* 1108 if (key_pressed[GLFW_KEY_LEFT]) { 1109 cam_yaw += cam_yaw_speed * elapsed_seconds; 1110 cam_moved = true; 1111 } 1112 if (key_pressed[GLFW_KEY_RIGHT]) { 1113 cam_yaw -= cam_yaw_speed * elapsed_seconds; 1114 cam_moved = true; 1115 } 1116 if (key_pressed[GLFW_KEY_UP]) { 1117 cam_pitch += cam_pitch_speed * elapsed_seconds; 1118 cam_moved = true; 1119 } 1120 if (key_pressed[GLFW_KEY_DOWN]) { 1121 cam_pitch -= cam_pitch_speed * elapsed_seconds; 1122 cam_moved = true; 1123 } 1124 */ 1125 if (cam_moved) { 1126 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z)); 1127 1128 mat4 yaw_mat = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f)); 1129 mat4 pitch_mat = rotate(mat4(), -cam_pitch, vec3(1.0f, 0.0f, 0.0f)); 1130 R = pitch_mat * yaw_mat; 1131 1132 view_mat = R * T; 1133 1134 //printVector("cam pos", cam_pos); 1135 1136 glUseProgram(color_sp); 1137 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat)); 1138 1139 glUseProgram(texture_sp); 1140 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 1141 1142 cam_moved = false; 972 1143 } 973 1144 … … 988 1159 colors_vbo, texcoords_vbo, selected_colors_vbo, 989 1160 selectedObject, 990 shader Counts, curShaderBase);1161 shaderBufferInfo); 991 1162 renderSceneGui(); 992 1163 break; … … 994 1165 995 1166 glfwSwapBuffers(window); 996 997 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {998 glfwSetWindowShouldClose(window, 1);999 }1000 1001 float dist = cam_speed * elapsed_seconds;1002 if (glfwGetKey(window, GLFW_KEY_A)) {1003 vec3 dir = (inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f)).xyz();1004 cam_pos += dir * dist;1005 1006 cam_moved = true;1007 }1008 if (glfwGetKey(window, GLFW_KEY_D)) {1009 vec3 dir = (inverse(R) * vec4(1.0f, 0.0f, 0.0f, 1.0f)).xyz();1010 cam_pos += dir * dist;1011 1012 cam_moved = true;1013 }1014 if (glfwGetKey(window, GLFW_KEY_W)) {1015 vec3 dir = (inverse(R) * vec4(0.0f, 0.0f, -1.0f, 1.0f)).xyz();1016 cam_pos += dir * dist;1017 1018 cam_moved = true;1019 }1020 if (glfwGetKey(window, GLFW_KEY_S)) {1021 vec3 dir = (inverse(R) * vec4(0.0f, 0.0f, 1.0f, 1.0f)).xyz();1022 cam_pos += dir * dist;1023 1024 cam_moved = true;1025 }1026 /*1027 if (glfwGetKey(window, GLFW_KEY_LEFT)) {1028 cam_yaw += cam_yaw_speed * elapsed_seconds;1029 cam_moved = true;1030 }1031 if (glfwGetKey(window, GLFW_KEY_RIGHT)) {1032 cam_yaw -= cam_yaw_speed * elapsed_seconds;1033 cam_moved = true;1034 }1035 if (glfwGetKey(window, GLFW_KEY_UP)) {1036 cam_pitch += cam_pitch_speed * elapsed_seconds;1037 cam_moved = true;1038 }1039 if (glfwGetKey(window, GLFW_KEY_DOWN)) {1040 cam_pitch -= cam_pitch_speed * elapsed_seconds;1041 cam_moved = true;1042 }1043 */1044 if (cam_moved) {1045 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));1046 1047 mat4 yaw_mat = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));1048 mat4 pitch_mat = rotate(mat4(), -cam_pitch, vec3(1.0f, 0.0f, 0.0f));1049 R = pitch_mat * yaw_mat;1050 1051 view_mat = R*T;1052 1053 //printVector("cam pos", cam_pos);1054 1055 glUseProgram(color_sp);1056 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));1057 1058 glUseProgram(texture_sp);1059 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));1060 1061 cam_moved = false;1062 }1063 1167 } 1064 1168 … … 1265 1369 } 1266 1370 1267 void addObjectToScene (SceneObject& obj) {1371 void addObjectToSceneDuringInit(SceneObject& obj) { 1268 1372 obj.id = objects.size(); // currently unused 1269 1373 obj.num_points = obj.points.size() / 3; 1270 1374 obj.model_transform = mat4(); 1375 obj.deleted = false; 1271 1376 1272 1377 obj.normals.reserve(obj.points.size()); … … 1289 1394 } 1290 1395 1396 void addObjectToScene(SceneObject& obj, 1397 map<GLuint, BufferInfo>& shaderBufferInfo, 1398 GLuint points_vbo, 1399 GLuint colors_vbo, 1400 GLuint selected_colors_vbo, 1401 GLuint texcoords_vbo, 1402 GLuint normals_vbo, 1403 GLuint ubo, 1404 GLuint model_mat_idx_vbo) { 1405 addObjectToSceneDuringInit(obj); 1406 1407 BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program]; 1408 1409 // Check if the buffers aren't large enough to fit the new object and, if so, print an error and quit. 1410 // This is a temporary sanity check to make sure things are working as expected 1411 if (bufferInfo->vbo_capacity < (bufferInfo->ubo_offset + obj.num_points) || 1412 bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) { 1413 populateBuffers(objects, shaderBufferInfo, 1414 points_vbo, 1415 colors_vbo, 1416 selected_colors_vbo, 1417 texcoords_vbo, 1418 normals_vbo, 1419 ubo, 1420 model_mat_idx_vbo); 1421 } else { 1422 copyObjectDataToBuffers(objects.back(), shaderBufferInfo, 1423 points_vbo, 1424 colors_vbo, 1425 selected_colors_vbo, 1426 texcoords_vbo, 1427 normals_vbo, 1428 ubo, 1429 model_mat_idx_vbo); 1430 } 1431 } 1432 1433 void removeObjectFromScene(int objectId, GLuint ubo) { 1434 SceneObject& obj = objects[objectId]; 1435 1436 if (!obj.deleted) { 1437 // Move the object outside the render bounds of the scene so it doesn't get rendered 1438 // TODO: Find a better way of hiding the object until the next time buffers are repopulated 1439 transformObject(obj, translate(mat4(), vec3(0.0f, 0.0f, FAR_CLIP * 1000.0f)), ubo); 1440 obj.deleted = true; 1441 } 1442 } 1443 1444 void initializeBuffers( 1445 GLuint* points_vbo, 1446 GLuint* colors_vbo, 1447 GLuint* selected_colors_vbo, 1448 GLuint* texcoords_vbo, 1449 GLuint* normals_vbo, 1450 GLuint* ubo, 1451 GLuint* model_mat_idx_vbo) { 1452 *points_vbo = 0; 1453 glGenBuffers(1, points_vbo); 1454 1455 *colors_vbo = 0; 1456 glGenBuffers(1, colors_vbo); 1457 1458 *selected_colors_vbo = 0; 1459 glGenBuffers(1, selected_colors_vbo); 1460 1461 *texcoords_vbo = 0; 1462 glGenBuffers(1, texcoords_vbo); 1463 1464 *normals_vbo = 0; 1465 glGenBuffers(1, normals_vbo); 1466 1467 *ubo = 0; 1468 glGenBuffers(1, ubo); 1469 1470 *model_mat_idx_vbo = 0; 1471 glGenBuffers(1, model_mat_idx_vbo); 1472 } 1473 1291 1474 void populateBuffers(vector<SceneObject>& objects, 1292 GLuint* points_vbo, 1293 GLuint* colors_vbo, 1294 GLuint* selected_colors_vbo, 1295 GLuint* texcoords_vbo, 1296 GLuint* normals_vbo, 1297 GLuint* ubo, 1298 GLuint* model_mat_idx_vbo, 1299 map<GLuint, unsigned int>& shaderCounts, 1300 map<GLuint, unsigned int>& curShaderBase) { 1475 map<GLuint, BufferInfo>& shaderBufferInfo, 1476 GLuint points_vbo, 1477 GLuint colors_vbo, 1478 GLuint selected_colors_vbo, 1479 GLuint texcoords_vbo, 1480 GLuint normals_vbo, 1481 GLuint ubo, 1482 GLuint model_mat_idx_vbo) { 1301 1483 GLsizeiptr points_buffer_size = 0; 1302 1484 GLsizeiptr textures_buffer_size = 0; … … 1304 1486 GLsizeiptr model_mat_idx_buffer_size = 0; 1305 1487 1306 map<GLuint, unsigned int> curShaderOffset; 1307 1488 map<GLuint, unsigned int> shaderCounts; 1308 1489 map<GLuint, unsigned int> shaderUboCounts; 1309 map<GLuint, unsigned int> curShaderUboBase;1310 map<GLuint, unsigned int> curShaderUboOffset;1311 1490 1312 1491 vector<SceneObject>::iterator it; 1313 1492 1314 1493 /* Find all shaders that need to be used and the number of objects and 1315 * number of points for each shader. Construct a map from shader id to count 1316 * of points being drawn using that shader (for thw model matrix ubo, we 1317 * need object counts instead). These will be used to get offsets into the 1318 * vertex buffer for each shader. 1319 */ 1320 for (it = objects.begin(); it != objects.end(); it++) { 1321 points_buffer_size += it->points.size() * sizeof(GLfloat); 1322 textures_buffer_size += it->texcoords.size() * sizeof(GLfloat); 1323 ubo_buffer_size += 16 * sizeof(GLfloat); 1324 model_mat_idx_buffer_size += it->num_points * sizeof(GLuint); 1325 1326 if (shaderCounts.count(it->shader_program) == 0) { 1327 shaderCounts[it->shader_program] = it->num_points; 1328 shaderUboCounts[it->shader_program] = 1; 1494 * number of points for each shader. Construct a map from shader id to count 1495 * of points being drawn using that shader (for thw model matrix ubo, we 1496 * need object counts instead). These will be used to get offsets into the 1497 * vertex buffer for each shader. 1498 */ 1499 for (it = objects.begin(); it != objects.end();) { 1500 if (it->deleted) { 1501 it = objects.erase(it); 1329 1502 } else { 1330 shaderCounts[it->shader_program] += it->num_points; 1331 shaderUboCounts[it->shader_program]++; 1332 } 1333 } 1503 points_buffer_size += it->points.size() * sizeof(GLfloat); 1504 textures_buffer_size += it->texcoords.size() * sizeof(GLfloat); 1505 ubo_buffer_size += 16 * sizeof(GLfloat); 1506 model_mat_idx_buffer_size += it->num_points * sizeof(GLuint); 1507 1508 if (shaderCounts.count(it->shader_program) == 0) { 1509 shaderCounts[it->shader_program] = it->num_points; 1510 shaderUboCounts[it->shader_program] = 1; 1511 } else { 1512 shaderCounts[it->shader_program] += it->num_points; 1513 shaderUboCounts[it->shader_program]++; 1514 } 1515 1516 it++; 1517 } 1518 } 1519 1520 // double the buffer sizes to leave room for new objects 1521 points_buffer_size *= 2; 1522 textures_buffer_size *= 2; 1523 ubo_buffer_size *= 2; 1524 model_mat_idx_buffer_size *= 2; 1334 1525 1335 1526 map<GLuint, unsigned int>::iterator shaderIt; … … 1338 1529 1339 1530 /* 1340 * The counts calculated above can be used to get the starting offset of 1341 * each shader in the vertex buffer. Create a map of base offsets to mark 1342 * where the data for the first object using a given shader begins. Also, 1343 * create a map of current offsets to mark where to copy data for the next 1344 * object being added. 1345 */ 1346 cout << "Shader counts:" << endl; 1531 * The counts calculated above can be used to get the starting offset of 1532 * each shader in the vertex buffer. Create a map of base offsets to mark 1533 * where the data for the first object using a given shader begins. Also, 1534 * create a map of current offsets to mark where to copy data for the next 1535 * object being added. 1536 */ 1347 1537 for (shaderIt = shaderCounts.begin(); shaderIt != shaderCounts.end(); shaderIt++) { 1348 curShaderOffset[shaderIt->first] = 0; 1349 curShaderUboOffset[shaderIt->first] = 0; 1350 1351 curShaderBase[shaderIt->first] = lastShaderCount; 1538 shaderBufferInfo[shaderIt->first].vbo_base = lastShaderCount * 2; 1539 shaderBufferInfo[shaderIt->first].ubo_base = lastShaderUboCount * 2; 1540 cout << "shader: " << shaderIt->first << endl; 1541 cout << "point counts: " << shaderCounts[shaderIt->first] << endl; 1542 cout << "object counts: " << shaderUboCounts[shaderIt->first] << endl; 1543 cout << "vbo_base: " << shaderBufferInfo[shaderIt->first].vbo_base << endl; 1544 cout << "ubo_base: " << shaderBufferInfo[shaderIt->first].ubo_base << endl; 1545 1546 shaderBufferInfo[shaderIt->first].vbo_offset = 0; 1547 shaderBufferInfo[shaderIt->first].ubo_offset = 0; 1548 1549 shaderBufferInfo[shaderIt->first].vbo_capacity = shaderCounts[shaderIt->first] * 2; 1550 shaderBufferInfo[shaderIt->first].ubo_capacity = shaderUboCounts[shaderIt->first] * 2; 1551 1352 1552 lastShaderCount += shaderCounts[shaderIt->first]; 1353 1354 curShaderUboBase[shaderIt->first] = lastShaderUboCount;1355 1553 lastShaderUboCount += shaderUboCounts[shaderIt->first]; 1356 1554 } 1357 1555 1358 // Initialize all the buffers using the counts calculated above 1359 1360 *points_vbo = 0; 1361 glGenBuffers(1, points_vbo); 1362 glBindBuffer(GL_ARRAY_BUFFER, *points_vbo); 1556 // Allocate all the buffers using the counts calculated above 1557 1558 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 1363 1559 glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW); 1364 1560 1365 *colors_vbo = 0; 1366 glGenBuffers(1, colors_vbo); 1367 glBindBuffer(GL_ARRAY_BUFFER, *colors_vbo); 1561 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 1368 1562 glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW); 1369 1563 1370 *selected_colors_vbo = 0; 1371 glGenBuffers(1, selected_colors_vbo); 1372 glBindBuffer(GL_ARRAY_BUFFER, *selected_colors_vbo); 1564 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 1373 1565 glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW); 1374 1566 1375 *texcoords_vbo = 0; 1376 glGenBuffers(1, texcoords_vbo); 1377 glBindBuffer(GL_ARRAY_BUFFER, *texcoords_vbo); 1567 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 1378 1568 glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW); 1379 1569 1380 *normals_vbo = 0; 1381 glGenBuffers(1, normals_vbo); 1382 glBindBuffer(GL_ARRAY_BUFFER, *normals_vbo); 1570 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 1383 1571 glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW); 1384 1572 1385 *ubo = 0; 1386 glGenBuffers(1, ubo); 1387 glBindBuffer(GL_UNIFORM_BUFFER, *ubo); 1573 glBindBuffer(GL_UNIFORM_BUFFER, ubo); 1388 1574 glBufferData(GL_UNIFORM_BUFFER, ubo_buffer_size, NULL, GL_DYNAMIC_DRAW); 1389 1575 1390 *model_mat_idx_vbo = 0; 1391 glGenBuffers(1, model_mat_idx_vbo); 1392 glBindBuffer(GL_ARRAY_BUFFER, *model_mat_idx_vbo); 1576 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 1393 1577 glBufferData(GL_ARRAY_BUFFER, model_mat_idx_buffer_size, NULL, GL_DYNAMIC_DRAW); 1394 1578 1395 1579 for (it = objects.begin(); it != objects.end(); it++) { 1396 it->vertex_vbo_offset = curShaderBase[it->shader_program] + curShaderOffset[it->shader_program]; 1397 it->ubo_offset = curShaderUboBase[it->shader_program] + curShaderUboOffset[it->shader_program]; 1398 1399 glBindBuffer(GL_ARRAY_BUFFER, *points_vbo); 1400 glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 3, it->points.size() * sizeof(GLfloat), &it->points[0]); 1401 1402 glBindBuffer(GL_ARRAY_BUFFER, *colors_vbo); 1403 glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 3, it->colors.size() * sizeof(GLfloat), &it->colors[0]); 1404 1405 glBindBuffer(GL_ARRAY_BUFFER, *selected_colors_vbo); 1406 glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 3, it->selected_colors.size() * sizeof(GLfloat), &it->selected_colors[0]); 1407 1408 glBindBuffer(GL_ARRAY_BUFFER, *texcoords_vbo); 1409 glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 2, it->texcoords.size() * sizeof(GLfloat), &it->texcoords[0]); 1410 1411 glBindBuffer(GL_ARRAY_BUFFER, *normals_vbo); 1412 glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 3, it->normals.size() * sizeof(GLfloat), &it->normals[0]); 1413 1414 glBindBuffer(GL_ARRAY_BUFFER, *model_mat_idx_vbo); 1415 for (int i = 0; i < it->num_points; i++) { 1416 glBufferSubData(GL_ARRAY_BUFFER, (it->vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &it->ubo_offset); 1417 } 1418 1419 curShaderOffset[it->shader_program] += it->num_points; 1420 1421 it->model_mat = it->model_base * it->model_transform; 1422 glBindBuffer(GL_UNIFORM_BUFFER, *ubo); 1423 glBufferSubData(GL_UNIFORM_BUFFER, it->ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(it->model_mat)); 1424 1425 curShaderUboOffset[it->shader_program]++; 1426 } 1580 copyObjectDataToBuffers(*it, shaderBufferInfo, 1581 points_vbo, 1582 colors_vbo, 1583 selected_colors_vbo, 1584 texcoords_vbo, 1585 normals_vbo, 1586 ubo, 1587 model_mat_idx_vbo); 1588 } 1589 } 1590 1591 void copyObjectDataToBuffers(SceneObject& obj, 1592 map<GLuint, BufferInfo>& shaderBufferInfo, 1593 GLuint points_vbo, 1594 GLuint colors_vbo, 1595 GLuint selected_colors_vbo, 1596 GLuint texcoords_vbo, 1597 GLuint normals_vbo, 1598 GLuint ubo, 1599 GLuint model_mat_idx_vbo) { 1600 BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program]; 1601 1602 obj.vertex_vbo_offset = bufferInfo->vbo_base + bufferInfo->vbo_offset; 1603 obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset; 1604 1605 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 1606 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]); 1607 1608 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 1609 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]); 1610 1611 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 1612 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]); 1613 1614 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 1615 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]); 1616 1617 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 1618 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]); 1619 1620 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 1621 for (int i = 0; i < obj.num_points; i++) { 1622 glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &obj.ubo_offset); 1623 } 1624 1625 obj.model_mat = obj.model_base * obj.model_transform; 1626 glBindBuffer(GL_UNIFORM_BUFFER, ubo); 1627 glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat)); 1628 1629 bufferInfo->vbo_offset += obj.num_points; 1630 bufferInfo->ubo_offset++; 1427 1631 } 1428 1632 … … 1441 1645 GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo, 1442 1646 SceneObject* selectedObject, 1443 map<GLuint, unsigned int>& shaderCounts, 1444 map<GLuint, unsigned int>& curShaderBase) { 1647 map<GLuint, BufferInfo>& shaderBufferInfo) { 1445 1648 1446 1649 glUseProgram(color_sp); … … 1457 1660 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0); 1458 1661 1459 glDrawArrays(GL_TRIANGLES, curShaderBase[color_sp], shaderCounts[color_sp]);1662 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[color_sp].vbo_base, shaderBufferInfo[color_sp].vbo_offset); 1460 1663 1461 1664 glUseProgram(texture_sp); 1462 1665 glBindVertexArray(vao2); 1463 1666 1464 glDrawArrays(GL_TRIANGLES, curShaderBase[texture_sp], shaderCounts[texture_sp]);1667 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[texture_sp].vbo_base, shaderBufferInfo[texture_sp].vbo_offset); 1465 1668 } 1466 1669 … … 1556 1759 } 1557 1760 1558 void spawnAsteroid(vec3 pos, GLuint shader) { 1761 void spawnAsteroid(vec3 pos, GLuint shader, 1762 map<GLuint, BufferInfo>& shaderBufferInfo, 1763 GLuint points_vbo, 1764 GLuint colors_vbo, 1765 GLuint selected_colors_vbo, 1766 GLuint texcoords_vbo, 1767 GLuint normals_vbo, 1768 GLuint ubo, 1769 GLuint model_mat_idx_vbo) { 1559 1770 SceneObject obj = SceneObject(); 1560 1771 obj.shader_program = shader; … … 1665 1876 obj.model_base = T * R * scale(mat4(), vec3(0.1f, 0.1f, 0.1f)); 1666 1877 1667 addObjectToScene(obj); 1668 } 1878 addObjectToScene(obj, shaderBufferInfo, 1879 points_vbo, 1880 colors_vbo, 1881 selected_colors_vbo, 1882 texcoords_vbo, 1883 normals_vbo, 1884 ubo, 1885 model_mat_idx_vbo); 1886 }
Note:
See TracChangeset
for help on using the changeset viewer.