Changeset 8316333 in opengl-game for new-game.cpp
- Timestamp:
- Jul 27, 2018, 1:24:03 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- fabed35
- Parents:
- 4d0820f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r4d0820f r8316333 98 98 void print4DVector(string label, vec4 v); 99 99 100 void addObjectToSceneDuringInit(SceneObject& obj); 101 void addObjectToScene(SceneObject& obj, map<GLuint, BufferInfo>& shaderBufferInfo, 100 void initObject(SceneObject& obj); 101 void addObjectToScene(SceneObject& obj, 102 map<GLuint, BufferInfo>& shaderBufferInfo, 102 103 GLuint points_vbo, 103 104 GLuint colors_vbo, … … 111 112 void calculateObjectBoundingBox(SceneObject& obj); 112 113 113 void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp);114 SceneObject createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp); 114 115 115 116 void initializeBuffers( … … 394 395 mat4 T_model, R_model; 395 396 396 // TODO: Confirm there's nothing I need from the commented out models and delete them397 // (Check to make sure the textured square is drawn correctly)398 399 /*400 // triangle401 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 // square436 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.0f454 };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 471 397 // player ship 472 398 obj = SceneObject(); … … 842 768 843 769 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)); 845 771 R_model = mat4(1.0f); 846 772 obj.model_base = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); … … 848 774 obj.translate_mat = T_model; 849 775 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; 854 792 855 793 vector<SceneObject>::iterator obj_it; … … 1108 1046 if (key_pressed[GLFW_KEY_RIGHT]) { 1109 1047 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); 1110 1050 } 1111 1051 if (key_pressed[GLFW_KEY_LEFT]) { 1112 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); 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); 1113 1083 } 1114 1084 … … 1424 1394 } 1425 1395 1426 void addObjectToSceneDuringInit(SceneObject& obj) {1396 void initObject(SceneObject& obj) { 1427 1397 // Each objects must have at least 3 points, so the size of 1428 1398 // the points array must be a positive multiple of 9 … … 1452 1422 } 1453 1423 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 1431 void 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) { 1458 1440 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);1471 1441 1472 1442 BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program]; … … 1484 1454 ubo, 1485 1455 model_mat_idx_vbo); 1486 } else { 1456 } 1457 else { 1487 1458 copyObjectDataToBuffers(objects.back(), shaderBufferInfo, 1488 1459 points_vbo, … … 1571 1542 * -Rotate the beam along the x-axis and then along the y-axis and then translate it to put it into its final position 1572 1543 */ 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 1546 SceneObject createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) { 1574 1547 SceneObject obj = SceneObject(); 1575 obj.id = objects.size(); // currently unused1576 1548 obj.type = TYPE_LASER; 1577 1549 obj.shader_program = laser_sp; 1578 obj.deleted = false;1579 1550 1580 1551 vec3 ray = end - start; … … 1623 1594 }; 1624 1595 1625 obj.num_points = obj.points.size() / 3;1626 1627 1596 float xAxisRotation = asin(ray.y / length); 1628 1597 float yAxisRotation = atan2(-ray.x, -ray.z); … … 1644 1613 obj.model_base = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj.model_base; 1645 1614 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; 1649 1619 } 1650 1620 … … 2100 2070 obj.translate_mat = T; 2101 2071 2072 initObject(obj); 2102 2073 addObjectToScene(obj, shaderBufferInfo, 2103 2074 points_vbo,
Note:
See TracChangeset
for help on using the changeset viewer.