Changeset 3effd81 in opengl-game for new-game.cpp
- Timestamp:
- Jul 25, 2018, 2:44:09 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 42e642d
- Parents:
- fd6f465
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
rfd6f465 r3effd81 33 33 using namespace std; 34 34 using namespace glm; 35 36 /* LASERS TODO:37 * -Allow lasers that face any direction38 * -Make the lasers rotate to always face the camera39 * -Use textures to draw lasers40 * -The textures should be grayscale and have transparency41 * -The laser shader should take an input color to blend with the texture to give the lasers color42 * -DONE43 * -The lasers should be SceneObjects and drawn like all other objects44 * -DONE45 * -Make lasers shoot from the ends of the ship's wings when the user presses a button and disappear after a second or so46 *47 * NOTE: Asteroid movement currently depends on framerate, fix this in a generic/reusable way48 */49 50 /* LASER POSITIONING ALGORITHM (to be implemented)51 * -Determine the length of the laser based on the start and end points52 * -Create a laser (long thin rectangle) of that length and the appropriate width53 * along the z axis, facing up, and with its start point at the origin (to make sure rotations happen around that point)54 * -Determine the line from the camera that intersects the line the laser should lie on at a 90 degree angle55 * -The angle between that line and the laser's normal should be the angle needed to rotate the laser so it faces the camera56 * -Now, create the transformation matrix for the laser to correctly position it57 * -First, rotate along the z axis by the angle calculated above58 * -Then, determine the correct angles and rotate around the y and x axes to make the laser point in the right direction59 * -Finally, translate the laser to its correct position by getting the difference between the camera position60 * and the laser start point61 */62 35 63 36 enum State { … … 229 202 230 203 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 204 205 /* 206 * TODO: Make lasers shoot from the ends of the ship's wings when the user presses a button and disappear after a second or so 207 * TODO: Asteroid movement currently depends on framerate, fix this in a generic/reusable way 208 */ 231 209 232 210 int main(int argc, char* argv[]) { … … 408 386 shaderBufferInfo[texture_sp] = BufferInfo(); 409 387 shaderBufferInfo[laser_sp] = BufferInfo(); 388 389 cam_pos = vec3(0.0f, 0.0f, 2.0f); 390 float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f; 391 float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f; 410 392 411 393 SceneObject obj; … … 958 940 // glm::perspective can create the projection matrix 959 941 960 cam_pos = vec3(0.0f, 0.0f, 2.0f);961 //cam_pos = vec3(-2.1f, -1.5f, -1.5f); // Good position for checking ship faces962 float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;963 float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;964 965 942 mat4 T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z)); 966 943 mat4 yaw_mat = rotate(mat4(1.0f), -cam_yaw, vec3(0.0f, 1.0f, 0.0f)); … … 969 946 view_mat = R*T; 970 947 948 // TODO: Create a function to construct the projection matrix 949 // (Maybe I should just use glm::perspective, after making sure it matches what I have now) 971 950 float fov = 67.0f * ONE_DEG_IN_RAD; 972 951 float aspect = (float)width / (float)height; … … 1275 1254 vec4 ray_world = inverse(view_mat) * ray_eye; 1276 1255 1277 vec4 cam_pos_temp = vec4(cam_pos, 1.0f);1278 1279 1256 vec4 click_point; 1280 1257 vec3 closest_point = vec3(0.0f, 0.0f, -FAR_CLIP); // Any valid point will be closer than the far clipping plane, so initial value to that … … 1290 1267 vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]), 1291 1268 }, 1292 &*it, ray_world, cam_pos_temp, click_point1269 &*it, ray_world, vec4(cam_pos, 1.0f), click_point 1293 1270 )) { 1294 1271 click_point = view_mat * click_point; … … 1582 1559 } 1583 1560 1561 /* LASER RENDERING/POSITIONING ALGORITHM 1562 * -Draw a thin rectangle for the laser beam, using the specified width and endpoints 1563 * -Texture the beam with a grayscale partially transparent image 1564 * -In the shader, blend the image with a color to support lasers of different colors 1565 * 1566 * The flat part of the textured rectangle needs to always face the camera, so the laser's width is constant 1567 * This is done as follows: 1568 * -Determine the length of the laser based on the start and end points 1569 * -Draw a rectangle along the z-axis and rotated upwards along the y-axis, with the correct final length and width 1570 * -Rotate the beam around the z-axis by the correct angle, sot that in its final position, the flat part faces the camera 1571 * -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 */ 1584 1573 void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) { 1585 1574 SceneObject obj = SceneObject(); … … 1589 1578 obj.deleted = false; 1590 1579 1591 float length = (end-start).length(); 1580 vec3 ray = end - start; 1581 float length = glm::length(ray); 1592 1582 1593 1583 obj.points = { … … 1634 1624 1635 1625 obj.num_points = obj.points.size() / 3; 1636 obj.model_base = translate(mat4(1.0f), start); 1626 1627 float xAxisRotation = asin(ray.y / length); 1628 float yAxisRotation = atan2(-ray.x, -ray.z); 1629 1630 vec3 normal(rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * 1631 rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * 1632 vec4(0.0f, 1.0f, 0.0f, 1.0f)); 1633 1634 // To project point P onto line AB: 1635 // projection = A + dot(AP,AB) / dot(AB,AB) * AB 1636 vec3 projOnLaser = start + glm::dot(cam_pos-start, ray) / (length*length) * ray; 1637 vec3 laserToCam = cam_pos - projOnLaser; 1638 1639 float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam)); 1640 1641 obj.model_base = mat4(1.0f); 1642 obj.model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f)) * obj.model_base; 1643 obj.model_base = rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * obj.model_base; 1644 obj.model_base = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj.model_base; 1645 obj.model_base = translate(mat4(1.0f), start) * obj.model_base; 1637 1646 obj.model_transform = mat4(1.0f); 1638 1647 … … 1737 1746 shaderBufferInfo[shaderIt->first].ubo_base = lastShaderUboCount * 2; 1738 1747 1748 /* 1739 1749 cout << "shader: " << shaderIt->first << endl; 1740 1750 cout << "point counts: " << shaderCounts[shaderIt->first] << endl; … … 1742 1752 cout << "vbo_base: " << shaderBufferInfo[shaderIt->first].vbo_base << endl; 1743 1753 cout << "ubo_base: " << shaderBufferInfo[shaderIt->first].ubo_base << endl; 1754 */ 1744 1755 1745 1756 shaderBufferInfo[shaderIt->first].vbo_offset = 0;
Note:
See TracChangeset
for help on using the changeset viewer.