Changeset 1f81ecc in opengl-game
- Timestamp:
- Apr 19, 2020, 3:06:51 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 3950236
- Parents:
- 237cbec
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
r237cbec r1f81ecc 676 676 translate(mat4(1.0f), vec3(0.0f, 0.0f, zOffset)); 677 677 texturedSquare.modified = true; 678 } else if (e.key.keycode == SDL_SCANCODE_Z && leftLaserIdx == -1) { 679 // TODO: When I start actually removing objects from the object vectors, 680 // I will need to update the indices since they might become incorrect 681 // or invalid as objects get moved around 682 683 vec3 offset(shipObjects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 684 685 addLaser( 686 vec3(-0.21f, -1.19f, 1.76f) + offset, 687 vec3(-0.21f, -1.19f, -3.0f) + offset, 688 vec3(1.0f, 0.0f, 0.0f), 0.03f); 689 690 leftLaserIdx = laserObjects.size() - 1; 691 } else if (e.key.keycode == SDL_SCANCODE_X && rightLaserIdx == -1) { 692 vec3 offset(shipObjects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 693 694 addLaser( 695 vec3(0.21f, -1.19f, 1.76f) + offset, 696 vec3(0.21f, -1.19f, -3.0f) + offset, 697 vec3(0.0f, 1.0f, 0.0f), 0.03f); 698 699 rightLaserIdx = laserObjects.size() - 1; 678 700 } else { 679 701 cout << "Key event detected" << endl; … … 681 703 break; 682 704 case UI_EVENT_KEYUP: 705 if (e.key.keycode == SDL_SCANCODE_Z && leftLaserIdx != -1) { 706 laserObjects[leftLaserIdx].ssbo.deleted = true; 707 laserObjects[leftLaserIdx].modified = true; 708 leftLaserIdx = -1; 709 } else if (e.key.keycode == SDL_SCANCODE_X && rightLaserIdx != -1) { 710 laserObjects[rightLaserIdx].ssbo.deleted = true; 711 laserObjects[rightLaserIdx].modified = true; 712 rightLaserIdx = -1; 713 } 683 714 break; 684 715 case UI_EVENT_MOUSEBUTTONDOWN: … … 708 739 * shipObjects[0].model_transform; 709 740 ship.modified = true; 741 742 if (leftLaserIdx != -1) { 743 translateLaser(leftLaserIdx, vec3(distance, 0.0f, 0.0f)); 744 } 745 if (rightLaserIdx != -1) { 746 translateLaser(rightLaserIdx, vec3(distance, 0.0f, 0.0f)); 747 } 710 748 } else if (gui->keyPressed(SDL_SCANCODE_RIGHT)) { 711 749 float distance = this->shipSpeed * this->elapsedTime; … … 714 752 * shipObjects[0].model_transform; 715 753 ship.modified = true; 754 755 if (leftLaserIdx != -1) { 756 translateLaser(leftLaserIdx, vec3(distance, 0.0f, 0.0f)); 757 } 758 if (rightLaserIdx != -1) { 759 translateLaser(rightLaserIdx, vec3(distance, 0.0f, 0.0f)); 760 } 716 761 } 717 762 … … 1496 1541 } 1497 1542 1543 void VulkanGame::addLaser( vec3 start, vec3 end, vec3 color, float width) { 1544 vec3 ray = end - start; 1545 float length = glm::length(ray); 1546 1547 SceneObject<LaserVertex, SSBO_Laser>& laser = addObject( 1548 laserObjects, laserPipeline, 1549 addObjectIndex<LaserVertex>(laserObjects.size(), { 1550 {{ width / 2, 0.0f, -width / 2 }, {1.0f, 0.5f }}, 1551 {{-width / 2, 0.0f, -width / 2 }, {0.0f, 0.5f }}, 1552 {{-width / 2, 0.0f, 0.0f }, {0.0f, 0.0f }}, 1553 {{ width / 2, 0.0f, 0.0f }, {1.0f, 0.0f }}, 1554 {{ width / 2, 0.0f, -length + width / 2}, {1.0f, 0.51f}}, 1555 {{-width / 2, 0.0f, -length + width / 2}, {0.0f, 0.51f}}, 1556 {{ width / 2, 0.0f, -length, }, {1.0f, 1.0f }}, 1557 {{-width / 2, 0.0f, -length }, {0.0f, 1.0f }} 1558 }), { 1559 0, 1, 2, 0, 2, 3, 1560 4, 5, 1, 4, 1, 0, 1561 6, 7, 5, 6, 5, 4 1562 }, { 1563 mat4(1.0f), 1564 color, 1565 false 1566 }, true); 1567 1568 float xAxisRotation = asin(ray.y / length); 1569 float yAxisRotation = atan2(-ray.x, -ray.z); 1570 1571 vec3 normal(rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * 1572 rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * 1573 vec4(0.0f, 1.0f, 0.0f, 1.0f)); 1574 1575 // To project point P onto line AB: 1576 // projection = A + dot(AP,AB) / dot(AB,AB) * AB 1577 vec3 projOnLaser = start + glm::dot(this->cam_pos - start, ray) / (length * length) * ray; 1578 vec3 laserToCam = this->cam_pos - projOnLaser; 1579 1580 float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam)); 1581 1582 laser.model_base = 1583 rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f)); 1584 1585 laser.model_transform = 1586 translate(mat4(1.0f), start) * 1587 rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * 1588 rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)); 1589 1590 laser.modified = true; 1591 } 1592 1593 void VulkanGame::translateLaser(size_t index, const vec3& translation) { 1594 SceneObject<LaserVertex, SSBO_Laser>& laser = this->laserObjects[index]; 1595 1596 // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created, 1597 // and then re-used here 1598 1599 vec3 start = vec3(laser.model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1600 vec3 end = vec3(laser.model_transform * vec4(0.0f, 0.0f, laser.vertices[6].pos.z, 1.0f)); 1601 1602 vec3 ray = end - start; 1603 float length = glm::length(ray); 1604 1605 float xAxisRotation = asin(ray.y / length); 1606 float yAxisRotation = atan2(-ray.x, -ray.z); 1607 1608 vec3 normal(rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * 1609 rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * 1610 vec4(0.0f, 1.0f, 0.0f, 1.0f)); 1611 1612 // To project point P onto line AB: 1613 // projection = A + dot(AP,AB) / dot(AB,AB) * AB 1614 vec3 projOnLaser = start + glm::dot(cam_pos - start, ray) / (length*length) * ray; 1615 vec3 laserToCam = cam_pos - projOnLaser; 1616 1617 float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam)); 1618 1619 laser.model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f)); 1620 laser.model_transform = translate(mat4(1.0f), translation) * laser.model_transform; 1621 1622 laser.modified = true; 1623 } 1624 1498 1625 void VulkanGame::createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 1499 1626 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList) { -
vulkan-game.hpp
r237cbec r1f81ecc 233 233 float spawnRate_asteroid = 0.5; 234 234 float lastSpawn_asteroid; 235 236 unsigned int leftLaserIdx = -1; 237 238 unsigned int rightLaserIdx = -1; 235 239 236 240 bool initWindow(int width, int height, unsigned char guiFlags); … … 265 269 void createSyncObjects(); 266 270 271 void addLaser(vec3 start, vec3 end, vec3 color, float width); 272 void translateLaser(size_t index, const vec3& translation); 273 267 274 // TODO: Since addObject() returns a reference to the new object now, 268 275 // stop using objects.back() to access the object that was just created … … 277 284 void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects, 278 285 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index); 286 287 template<class VertexType, class SSBOType> 288 void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 289 SceneObject<VertexType, SSBOType>& obj, size_t index); 279 290 280 291 template<class VertexType> … … 327 338 328 339 SceneObject<VertexType, SSBOType>& obj = objects.back(); 329 centerObject(obj); 340 341 if (!is_same_v<VertexType, LaserVertex>) { 342 centerObject(obj); 343 } 330 344 331 345 bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo, … … 366 380 } 367 381 382 template<class VertexType, class SSBOType> 383 void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 384 SceneObject<VertexType, SSBOType>& obj, size_t index) { 385 pipeline.updateObjectVertices(index, obj.vertices, this->commandPool, this->graphicsQueue); 386 } 387 368 388 template<class VertexType> 369 389 vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
Note:
See TracChangeset
for help on using the changeset viewer.