Changeset fabed35 in opengl-game
- Timestamp:
- Jul 27, 2018, 4:02:09 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 612d1f6
- Parents:
- 8316333
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r8316333 rfabed35 177 177 178 178 int key_state[NUM_KEYS]; 179 bool key_ pressed[NUM_KEYS];179 bool key_down[NUM_KEYS]; 180 180 181 181 int width = 640; … … 203 203 204 204 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 205 206 // TODO: Come up with a better way to keep references to specific objects, 207 // which remain valid (or are recreated) when the object vector or VBOs are resized. 208 // I could store SceneObject*, but I have to keep track of when the vector is about to 209 // resize and re-init the pointers when that happens since they'll be invalidated 210 // I would prefer the above apporach, but I should write some helper functions to make it less error-prone 211 int leftLaserIdx = -1; 212 int rightLaserIdx = -1; 205 213 206 214 /* … … 777 785 objects.push_back(obj); 778 786 779 // I could also store SceneObject*, but I have to keep track of when the vector is about to780 // resize and re-init the pointers when that happens since they'll be invalidated781 // I would prefer the above apporach, but I should write some helper functions to make it less error-prone782 unsigned int leftLaserIdx = -1;783 unsigned int rightLaserIdx = -1;784 785 obj = createLaser(vec3(0.21f, -1.19f, 1.76f), vec3(0.21f, -1.19f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp);786 objects.push_back(obj);787 leftLaserIdx = objects.size() - 1;788 789 obj = createLaser(vec3(-0.21f, -1.19f, 1.76f), vec3(-0.21f, -1.19f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp);790 objects.push_back(obj);791 rightLaserIdx = objects.size() - 1;792 793 787 vector<SceneObject>::iterator obj_it; 794 788 … … 1036 1030 transformObject(objects[1], translate(mat4(1.0f), vec3(0.3f, 0.0f, 0.0f)), ubo); 1037 1031 } 1038 if (key_ pressed[GLFW_KEY_RIGHT]) {1032 if (key_down[GLFW_KEY_RIGHT]) { 1039 1033 transformObject(objects[2], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo); 1040 1034 } 1041 if (key_ pressed[GLFW_KEY_LEFT]) {1035 if (key_down[GLFW_KEY_LEFT]) { 1042 1036 transformObject(objects[2], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo); 1043 1037 } 1044 1038 */ 1045 1039 1046 if (key_ pressed[GLFW_KEY_RIGHT]) {1040 if (key_down[GLFW_KEY_RIGHT]) { 1047 1041 transformObject(objects[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo); 1048 transformObject(objects[leftLaserIdx], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo); 1049 transformObject(objects[rightLaserIdx], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo); 1042 1043 // TODO: Update the rotation of the lasers as they moves so they point towards the camera 1044 if (leftLaserIdx != -1) { 1045 transformObject(objects[leftLaserIdx], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo); 1046 } 1047 if (rightLaserIdx != -1) { 1048 transformObject(objects[rightLaserIdx], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo); 1049 } 1050 1050 } 1051 if (key_ pressed[GLFW_KEY_LEFT]) {1051 if (key_down[GLFW_KEY_LEFT]) { 1052 1052 transformObject(objects[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo); 1053 transformObject(objects[leftLaserIdx], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo); 1054 transformObject(objects[rightLaserIdx], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo); 1053 1054 // TODO: Update the rotation of the lasers as they moves so they point towards the camera 1055 if (leftLaserIdx != -1) { 1056 transformObject(objects[leftLaserIdx], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo); 1057 } 1058 if (rightLaserIdx != -1) { 1059 transformObject(objects[rightLaserIdx], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo); 1060 } 1055 1061 } 1056 if (key_pressed[GLFW_KEY_Z]) { 1062 1063 if (key_state[GLFW_KEY_Z] == GLFW_PRESS) { 1057 1064 vec3 offset(objects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1065 leftLaserIdx = objects.size(); 1058 1066 1059 1067 SceneObject obj = createLaser(vec3(-0.21f, -1.19f, 1.76f)+offset, vec3(-0.21f, -1.19f, -3.0f)+offset, … … 1067 1075 ubo, 1068 1076 model_mat_idx_vbo); 1077 } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) { 1078 removeObjectFromScene(objects[leftLaserIdx], ubo); 1069 1079 } 1070 if (key_pressed[GLFW_KEY_X]) { 1080 1081 if (key_state[GLFW_KEY_X] == GLFW_PRESS) { 1071 1082 vec3 offset(objects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1083 rightLaserIdx = objects.size(); 1072 1084 1073 1085 SceneObject obj = createLaser(vec3(0.21f, -1.19f, 1.76f) + offset, vec3(0.21f, -1.19f, -3.0f) + offset, … … 1081 1093 ubo, 1082 1094 model_mat_idx_vbo); 1095 } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) { 1096 removeObjectFromScene(objects[rightLaserIdx], ubo); 1083 1097 } 1084 1098 … … 1096 1110 } 1097 1111 1112 // TODO: Probablu remove this since it was only added to test that object removal owrks 1098 1113 if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) { 1099 1114 removeObjectFromScene(objects[0], ubo); … … 1106 1121 1107 1122 float dist = cam_speed * elapsed_seconds; 1108 if (key_ pressed[GLFW_KEY_A]) {1123 if (key_down[GLFW_KEY_A]) { 1109 1124 vec3 dir = vec3(inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f)); 1110 1125 cam_pos += dir * dist; … … 1112 1127 cam_moved = true; 1113 1128 } 1114 if (key_ pressed[GLFW_KEY_D]) {1129 if (key_down[GLFW_KEY_D]) { 1115 1130 vec3 dir = vec3(inverse(R) * vec4(1.0f, 0.0f, 0.0f, 1.0f)); 1116 1131 cam_pos += dir * dist; … … 1118 1133 cam_moved = true; 1119 1134 } 1120 if (key_ pressed[GLFW_KEY_W]) {1135 if (key_down[GLFW_KEY_W]) { 1121 1136 vec3 dir = vec3(inverse(R) * vec4(0.0f, 0.0f, -1.0f, 1.0f)); 1122 1137 cam_pos += dir * dist; … … 1124 1139 cam_moved = true; 1125 1140 } 1126 if (key_ pressed[GLFW_KEY_S]) {1141 if (key_down[GLFW_KEY_S]) { 1127 1142 vec3 dir = vec3(inverse(R) * vec4(0.0f, 0.0f, 1.0f, 1.0f)); 1128 1143 cam_pos += dir * dist; … … 1131 1146 } 1132 1147 /* 1133 if (key_ pressed[GLFW_KEY_LEFT]) {1148 if (key_down[GLFW_KEY_LEFT]) { 1134 1149 cam_yaw += cam_yaw_speed * elapsed_seconds; 1135 1150 cam_moved = true; 1136 1151 } 1137 if (key_ pressed[GLFW_KEY_RIGHT]) {1152 if (key_down[GLFW_KEY_RIGHT]) { 1138 1153 cam_yaw -= cam_yaw_speed * elapsed_seconds; 1139 1154 cam_moved = true; 1140 1155 } 1141 if (key_ pressed[GLFW_KEY_UP]) {1156 if (key_down[GLFW_KEY_UP]) { 1142 1157 cam_pitch += cam_pitch_speed * elapsed_seconds; 1143 1158 cam_moved = true; 1144 1159 } 1145 if (key_ pressed[GLFW_KEY_DOWN]) {1160 if (key_down[GLFW_KEY_DOWN]) { 1146 1161 cam_pitch -= cam_pitch_speed * elapsed_seconds; 1147 1162 cam_moved = true; … … 1262 1277 1263 1278 // should be true for GLFW_PRESS and GLFW_REPEAT 1264 key_ pressed[key] = (action != GLFW_RELEASE);1279 key_down[key] = (action != GLFW_RELEASE); 1265 1280 } 1266 1281 … … 1446 1461 if (bufferInfo->vbo_capacity < (bufferInfo->ubo_offset + obj.num_points) || 1447 1462 bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) { 1463 1464 if (leftLaserIdx != -1 && objects[leftLaserIdx].deleted) { 1465 leftLaserIdx = -1; 1466 } 1467 if (rightLaserIdx != -1 && objects[rightLaserIdx].deleted) { 1468 rightLaserIdx = -1; 1469 } 1470 1448 1471 populateBuffers(objects, shaderBufferInfo, 1449 1472 points_vbo, … … 1454 1477 ubo, 1455 1478 model_mat_idx_vbo); 1456 } 1457 else { 1479 } else { 1458 1480 copyObjectDataToBuffers(objects.back(), shaderBufferInfo, 1459 1481 points_vbo, … … 1676 1698 for (it = objects.begin(); it != objects.end();) { 1677 1699 if (it->deleted) { 1700 // terrible way of keeping track of the laser scene objects 1701 // TODO: Find a more robust way of doing this, one that would work well 1702 // for any objects in the scene 1703 if (leftLaserIdx > it - objects.begin()) { 1704 leftLaserIdx--; 1705 } 1706 if (rightLaserIdx > it - objects.begin()) { 1707 rightLaserIdx--; 1708 } 1709 1678 1710 it = objects.erase(it); 1679 1711 } else { … … 1814 1846 1815 1847 void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo) { 1848 if (obj.deleted) return; 1849 1816 1850 obj.model_transform = transform * obj.model_transform; 1817 1851 obj.model_mat = obj.model_transform * obj.model_base;
Note:
See TracChangeset
for help on using the changeset viewer.