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


Ignore:
Timestamp:
Jul 27, 2018, 1:24:03 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
fabed35
Parents:
4d0820f
Message:

Refactor the scene object creation functions, make the initial two laser move with the ship, and add new lasers when the user presses Z or X.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r4d0820f r8316333  
    9898void print4DVector(string label, vec4 v);
    9999
    100 void addObjectToSceneDuringInit(SceneObject& obj);
    101 void addObjectToScene(SceneObject& obj, map<GLuint, BufferInfo>& shaderBufferInfo,
     100void initObject(SceneObject& obj);
     101void addObjectToScene(SceneObject& obj,
     102                  map<GLuint, BufferInfo>& shaderBufferInfo,
    102103                  GLuint points_vbo,
    103104                  GLuint colors_vbo,
     
    111112void calculateObjectBoundingBox(SceneObject& obj);
    112113
    113 void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp);
     114SceneObject createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp);
    114115
    115116void initializeBuffers(
     
    394395   mat4 T_model, R_model;
    395396
    396    // TODO: Confirm there's nothing I need from the commented out models and delete them
    397    // (Check to make sure the textured square is drawn correctly)
    398 
    399    /*
    400    // triangle
    401    obj = SceneObject();
    402    obj.shader_program = color_sp;
    403    obj.points = {
    404        0.0f,  0.5f,  0.0f,
    405       -0.5f, -0.5f,  0.0f,
    406        0.5f, -0.5f,  0.0f,
    407        0.5f, -0.5f,  0.0f,
    408       -0.5f, -0.5f,  0.0f,
    409        0.0f,  0.5f,  0.0f,
    410    };
    411    obj.colors = {
    412       1.0f, 0.0f, 0.0f,
    413       0.0f, 0.0f, 1.0f,
    414       0.0f, 1.0f, 0.0f,
    415       0.0f, 1.0f, 0.0f,
    416       0.0f, 0.0f, 1.0f,
    417       1.0f, 0.0f, 0.0f,
    418    };
    419    obj.texcoords = { 0.0f };
    420    obj.selected_colors = {
    421       0.0f, 1.0f, 0.0f,
    422       0.0f, 1.0f, 0.0f,
    423       0.0f, 1.0f, 0.0f,
    424       0.0f, 1.0f, 0.0f,
    425       0.0f, 1.0f, 0.0f,
    426       0.0f, 1.0f, 0.0f,
    427    };
    428 
    429    T_model = translate(mat4(1.0f), vec3(0.45f, -1.5f, 0.0f));
    430    R_model = rotate(mat4(1.0f), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    431    obj.model_base = T_model*R_model;
    432 
    433    addObjectToSceneDuringInit(obj);
    434 
    435    // square
    436    obj = SceneObject();
    437    obj.shader_program = texture_sp;
    438    obj.points = {
    439        0.5f,  0.5f,  0.0f,
    440       -0.5f,  0.5f,  0.0f,
    441       -0.5f, -0.5f,  0.0f,
    442        0.5f,  0.5f,  0.0f,
    443       -0.5f, -0.5f,  0.0f,
    444        0.5f, -0.5f,  0.0f,
    445    };
    446    obj.colors = { 0.0f };
    447    obj.texcoords = {
    448       1.0f, 1.0f,
    449       0.0f, 1.0f,
    450       0.0f, 0.0f,
    451       1.0f, 1.0f,
    452       0.0f, 0.0f,
    453       1.0f, 0.0f
    454    };
    455    obj.selected_colors = {
    456       0.0f, 0.6f, 0.9f,
    457       0.0f, 0.6f, 0.9f,
    458       0.0f, 0.6f, 0.9f,
    459       0.0f, 0.6f, 0.9f,
    460       0.0f, 0.6f, 0.9f,
    461       0.0f, 0.6f, 0.9f,
    462    };
    463 
    464    T_model = translate(mat4(1.0f), vec3(-0.5f, -1.5f, -1.00f));
    465    R_model = rotate(mat4(1.0f), 0.5f, vec3(0.0f, 1.0f, 0.0f));
    466    obj.model_base = T_model*R_model;
    467 
    468    addObjectToSceneDuringInit(obj);
    469    */
    470 
    471397   // player ship
    472398   obj = SceneObject();
     
    842768
    843769   T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f));
    844    R_model = rotate(mat4(1.0f), 20.0f * (float)ONE_DEG_IN_RAD, vec3(1.0f, 0.0f, 0.0f));
     770   //R_model = rotate(mat4(1.0f), 20.0f * (float)ONE_DEG_IN_RAD, vec3(1.0f, 0.0f, 0.0f));
    845771   R_model = mat4(1.0f);
    846772   obj.model_base = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
     
    848774   obj.translate_mat = T_model;
    849775
    850    addObjectToSceneDuringInit(obj);
    851 
    852    addLaserToScene(vec3(0.34f, -2.0f, 1.6f), vec3(0.34f, -2.0f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.04f, laser_sp);
    853    addLaserToScene(vec3(-0.34f, -2.0f, 1.6f), vec3(-0.34f, -2.0f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.04f, laser_sp);
     776   initObject(obj);
     777   objects.push_back(obj);
     778
     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;
    854792
    855793   vector<SceneObject>::iterator obj_it;
     
    11081046         if (key_pressed[GLFW_KEY_RIGHT]) {
    11091047            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);
    11101050         }
    11111051         if (key_pressed[GLFW_KEY_LEFT]) {
    11121052            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);
     1055         }
     1056         if (key_pressed[GLFW_KEY_Z]) {
     1057            vec3 offset(objects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));
     1058
     1059            SceneObject obj = createLaser(vec3(-0.21f, -1.19f, 1.76f)+offset, vec3(-0.21f, -1.19f, -3.0f)+offset,
     1060               vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp);
     1061            addObjectToScene(obj, shaderBufferInfo,
     1062               points_vbo,
     1063               colors_vbo,
     1064               selected_colors_vbo,
     1065               texcoords_vbo,
     1066               normals_vbo,
     1067               ubo,
     1068               model_mat_idx_vbo);
     1069         }
     1070         if (key_pressed[GLFW_KEY_X]) {
     1071            vec3 offset(objects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));
     1072
     1073            SceneObject obj = createLaser(vec3(0.21f, -1.19f, 1.76f) + offset, vec3(0.21f, -1.19f, -3.0f) + offset,
     1074               vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp);
     1075            addObjectToScene(obj, shaderBufferInfo,
     1076               points_vbo,
     1077               colors_vbo,
     1078               selected_colors_vbo,
     1079               texcoords_vbo,
     1080               normals_vbo,
     1081               ubo,
     1082               model_mat_idx_vbo);
    11131083         }
    11141084
     
    14241394}
    14251395
    1426 void addObjectToSceneDuringInit(SceneObject& obj) {
     1396void initObject(SceneObject& obj) {
    14271397   // Each objects must have at least 3 points, so the size of
    14281398   // the points array must be a positive multiple of 9
     
    14521422   }
    14531423
    1454    calculateObjectBoundingBox(obj);
    1455 
    1456    obj.bounding_center = vec3(obj.translate_mat * vec4(obj.bounding_center, 1.0f));
    1457 
     1424   if (obj.type != TYPE_LASER) {
     1425      calculateObjectBoundingBox(obj);
     1426
     1427      obj.bounding_center = vec3(obj.translate_mat * vec4(obj.bounding_center, 1.0f));
     1428   }
     1429}
     1430
     1431void addObjectToScene(SceneObject& obj,
     1432   map<GLuint, BufferInfo>& shaderBufferInfo,
     1433   GLuint points_vbo,
     1434   GLuint colors_vbo,
     1435   GLuint selected_colors_vbo,
     1436   GLuint texcoords_vbo,
     1437   GLuint normals_vbo,
     1438   GLuint ubo,
     1439   GLuint model_mat_idx_vbo) {
    14581440   objects.push_back(obj);
    1459 }
    1460 
    1461 void addObjectToScene(SceneObject& obj,
    1462                   map<GLuint, BufferInfo>& shaderBufferInfo,
    1463                   GLuint points_vbo,
    1464                   GLuint colors_vbo,
    1465                   GLuint selected_colors_vbo,
    1466                   GLuint texcoords_vbo,
    1467                   GLuint normals_vbo,
    1468                   GLuint ubo,
    1469                   GLuint model_mat_idx_vbo) {
    1470    addObjectToSceneDuringInit(obj);
    14711441
    14721442   BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program];
     
    14841454         ubo,
    14851455         model_mat_idx_vbo);
    1486    } else {
     1456   }
     1457   else {
    14871458      copyObjectDataToBuffers(objects.back(), shaderBufferInfo,
    14881459         points_vbo,
     
    15711542* -Rotate the beam along the x-axis and then along the y-axis and then translate it to put it into its final position
    15721543*/
    1573 void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) {
     1544// TODO: Make the color parameter have an effect
     1545// TODO: Come up with a better way of passing the object back than copying it
     1546SceneObject createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) {
    15741547   SceneObject obj = SceneObject();
    1575    obj.id = objects.size(); // currently unused
    15761548   obj.type = TYPE_LASER;
    15771549   obj.shader_program = laser_sp;
    1578    obj.deleted = false;
    15791550
    15801551   vec3 ray = end - start;
     
    16231594   };
    16241595
    1625    obj.num_points = obj.points.size() / 3;
    1626 
    16271596   float xAxisRotation = asin(ray.y / length);
    16281597   float yAxisRotation = atan2(-ray.x, -ray.z);
     
    16441613   obj.model_base = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj.model_base;
    16451614   obj.model_base = translate(mat4(1.0f), start) * obj.model_base;
    1646    obj.model_transform = mat4(1.0f);
    1647 
    1648    objects.push_back(obj);
     1615
     1616   initObject(obj);
     1617
     1618   return obj;
    16491619}
    16501620
     
    21002070   obj.translate_mat = T;
    21012071
     2072   initObject(obj);
    21022073   addObjectToScene(obj, shaderBufferInfo,
    21032074                  points_vbo,
Note: See TracChangeset for help on using the changeset viewer.