Changeset fabed35 in opengl-game


Ignore:
Timestamp:
Jul 27, 2018, 4:02:09 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
612d1f6
Parents:
8316333
Message:

Make the left and right lasers fire while the Z and X keys are held down, respectively. Keep the laser indices up-to-date as scene objects get removed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r8316333 rfabed35  
    177177
    178178int key_state[NUM_KEYS];
    179 bool key_pressed[NUM_KEYS];
     179bool key_down[NUM_KEYS];
    180180
    181181int width = 640;
     
    203203
    204204ImVec4 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
     211int leftLaserIdx = -1;
     212int rightLaserIdx = -1;
    205213
    206214/*
     
    777785   objects.push_back(obj);
    778786
    779    // I could also store SceneObject*, but I have to keep track of when the vector is about to
    780    // resize and re-init the pointers when that happens since they'll be invalidated
    781    // I would prefer the above apporach, but I should write some helper functions to make it less error-prone
    782    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 
    793787   vector<SceneObject>::iterator obj_it;
    794788
     
    10361030            transformObject(objects[1], translate(mat4(1.0f), vec3(0.3f, 0.0f, 0.0f)), ubo);
    10371031         }
    1038          if (key_pressed[GLFW_KEY_RIGHT]) {
     1032         if (key_down[GLFW_KEY_RIGHT]) {
    10391033            transformObject(objects[2], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);
    10401034         }
    1041          if (key_pressed[GLFW_KEY_LEFT]) {
     1035         if (key_down[GLFW_KEY_LEFT]) {
    10421036            transformObject(objects[2], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);
    10431037         }
    10441038         */
    10451039
    1046          if (key_pressed[GLFW_KEY_RIGHT]) {
     1040         if (key_down[GLFW_KEY_RIGHT]) {
    10471041            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            }
    10501050         }
    1051          if (key_pressed[GLFW_KEY_LEFT]) {
     1051         if (key_down[GLFW_KEY_LEFT]) {
    10521052            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            }
    10551061         }
    1056          if (key_pressed[GLFW_KEY_Z]) {
     1062
     1063         if (key_state[GLFW_KEY_Z] == GLFW_PRESS) {
    10571064            vec3 offset(objects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));
     1065            leftLaserIdx = objects.size();
    10581066
    10591067            SceneObject obj = createLaser(vec3(-0.21f, -1.19f, 1.76f)+offset, vec3(-0.21f, -1.19f, -3.0f)+offset,
     
    10671075               ubo,
    10681076               model_mat_idx_vbo);
     1077         } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) {
     1078            removeObjectFromScene(objects[leftLaserIdx], ubo);
    10691079         }
    1070          if (key_pressed[GLFW_KEY_X]) {
     1080
     1081         if (key_state[GLFW_KEY_X] == GLFW_PRESS) {
    10711082            vec3 offset(objects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));
     1083            rightLaserIdx = objects.size();
    10721084
    10731085            SceneObject obj = createLaser(vec3(0.21f, -1.19f, 1.76f) + offset, vec3(0.21f, -1.19f, -3.0f) + offset,
     
    10811093               ubo,
    10821094               model_mat_idx_vbo);
     1095         } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) {
     1096            removeObjectFromScene(objects[rightLaserIdx], ubo);
    10831097         }
    10841098
     
    10961110         }
    10971111
     1112         // TODO: Probablu remove this since it was only added to test that object removal owrks
    10981113         if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
    10991114            removeObjectFromScene(objects[0], ubo);
     
    11061121
    11071122      float dist = cam_speed * elapsed_seconds;
    1108       if (key_pressed[GLFW_KEY_A]) {
     1123      if (key_down[GLFW_KEY_A]) {
    11091124         vec3 dir = vec3(inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f));
    11101125         cam_pos += dir * dist;
     
    11121127         cam_moved = true;
    11131128      }
    1114       if (key_pressed[GLFW_KEY_D]) {
     1129      if (key_down[GLFW_KEY_D]) {
    11151130         vec3 dir = vec3(inverse(R) * vec4(1.0f, 0.0f, 0.0f, 1.0f));
    11161131         cam_pos += dir * dist;
     
    11181133         cam_moved = true;
    11191134      }
    1120       if (key_pressed[GLFW_KEY_W]) {
     1135      if (key_down[GLFW_KEY_W]) {
    11211136         vec3 dir = vec3(inverse(R) * vec4(0.0f, 0.0f, -1.0f, 1.0f));
    11221137         cam_pos += dir * dist;
     
    11241139         cam_moved = true;
    11251140      }
    1126       if (key_pressed[GLFW_KEY_S]) {
     1141      if (key_down[GLFW_KEY_S]) {
    11271142         vec3 dir = vec3(inverse(R) * vec4(0.0f, 0.0f, 1.0f, 1.0f));
    11281143         cam_pos += dir * dist;
     
    11311146      }
    11321147      /*
    1133       if (key_pressed[GLFW_KEY_LEFT]) {
     1148      if (key_down[GLFW_KEY_LEFT]) {
    11341149      cam_yaw += cam_yaw_speed * elapsed_seconds;
    11351150      cam_moved = true;
    11361151      }
    1137       if (key_pressed[GLFW_KEY_RIGHT]) {
     1152      if (key_down[GLFW_KEY_RIGHT]) {
    11381153      cam_yaw -= cam_yaw_speed * elapsed_seconds;
    11391154      cam_moved = true;
    11401155      }
    1141       if (key_pressed[GLFW_KEY_UP]) {
     1156      if (key_down[GLFW_KEY_UP]) {
    11421157      cam_pitch += cam_pitch_speed * elapsed_seconds;
    11431158      cam_moved = true;
    11441159      }
    1145       if (key_pressed[GLFW_KEY_DOWN]) {
     1160      if (key_down[GLFW_KEY_DOWN]) {
    11461161      cam_pitch -= cam_pitch_speed * elapsed_seconds;
    11471162      cam_moved = true;
     
    12621277
    12631278   // should be true for GLFW_PRESS and GLFW_REPEAT
    1264    key_pressed[key] = (action != GLFW_RELEASE);
     1279   key_down[key] = (action != GLFW_RELEASE);
    12651280}
    12661281
     
    14461461   if (bufferInfo->vbo_capacity < (bufferInfo->ubo_offset + obj.num_points) ||
    14471462      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
    14481471      populateBuffers(objects, shaderBufferInfo,
    14491472         points_vbo,
     
    14541477         ubo,
    14551478         model_mat_idx_vbo);
    1456    }
    1457    else {
     1479   } else {
    14581480      copyObjectDataToBuffers(objects.back(), shaderBufferInfo,
    14591481         points_vbo,
     
    16761698   for (it = objects.begin(); it != objects.end();) {
    16771699      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
    16781710         it = objects.erase(it);
    16791711      } else {
     
    18141846
    18151847void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo) {
     1848   if (obj.deleted) return;
     1849
    18161850   obj.model_transform = transform * obj.model_transform;
    18171851   obj.model_mat = obj.model_transform * obj.model_base;
Note: See TracChangeset for help on using the changeset viewer.