Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. int main()
  2. {
  3. #ifdef TESTING
  4. testing();
  5. exit(0);
  6. #endif
  7.  
  8. setupAndInitializeWindow(768, 480, "Final Project");
  9.  
  10. TriangleTriangleCollision collisionDetector;
  11.  
  12. Asset cube1("cube.obj", "vertexShader.txt", "fragmentShader.txt");
  13.  
  14. cube1.position = glm::vec3(0.0, 2.0, 0.0);
  15. cube1.velocity = glm::vec3(0.0, -0.004, 0.0);
  16.  
  17. MVP = projection * view * model;
  18.  
  19. do{
  20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  21.  
  22. moveAsset(cube1);
  23.  
  24. renderAsset(cube1);
  25.  
  26. glfwSwapBuffers(window);
  27. glfwPollEvents();
  28.  
  29. } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
  30. glfwWindowShouldClose(window) == 0);
  31.  
  32. glfwTerminate();
  33.  
  34. return 0;
  35. }
  36.  
  37. void moveAsset(Asset &asset)
  38. {
  39. double currentTime = glfwGetTime();
  40.  
  41. asset.position.x += (asset.velocity.x * (currentTime - asset.lastTime));
  42. asset.position.y += (asset.velocity.y * (currentTime - asset.lastTime));
  43. asset.position.z += (asset.velocity.z * (currentTime - asset.lastTime));
  44.  
  45. for (glm::vec3 &vertex : asset.vertices)
  46. {
  47. glm::vec4 transformedVector = glm::translate(glm::mat4(1.0f), asset.position) * glm::vec4(vertex.x, vertex.y, vertex.z, 1);
  48. vertex = glm::vec3(transformedVector.x, transformedVector.y, transformedVector.z);
  49. }
  50.  
  51. asset.lastTime = glfwGetTime();
  52. }
  53.  
  54. void renderAsset(Asset asset)
  55. {
  56. glUseProgram(asset.programID);
  57.  
  58. GLuint MatrixID = glGetUniformLocation(asset.programID, "MVP");
  59. glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  60.  
  61. glEnableVertexAttribArray(0);
  62.  
  63. glBindBuffer(GL_ARRAY_BUFFER, asset.vertexbuffer);
  64. glBufferData(GL_ARRAY_BUFFER, asset.vertices.size() * sizeof(glm::vec3), &asset.vertices[0], GL_STATIC_DRAW);
  65. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  66. glDrawArrays(GL_TRIANGLES, 0, asset.vertices.size());
  67.  
  68. glDisableVertexAttribArray(0);
  69. }
  70.  
  71. struct Asset
  72. {
  73. Asset() { }
  74. Asset(std::string assetOBJFile, std::string vertexShader, std::string fragmentShader)
  75. {
  76. glGenVertexArrays(1, &vertexArrayID);
  77. glBindVertexArray(vertexArrayID);
  78.  
  79. programID = LoadShaders(vertexShader.c_str(), fragmentShader.c_str());
  80.  
  81. // Read our .obj file
  82. std::vector<glm::vec2> uvs;
  83. std::vector<glm::vec3> normals;
  84. loadOBJ(assetOBJFile.c_str(), vertices, uvs, normals);
  85.  
  86. // Load it into a VBO
  87. glGenBuffers(1, &vertexbuffer);
  88. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  89. glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
  90.  
  91. //velocity = glm::vec3(0.0, 1.0, 1.0);
  92. velocity = glm::vec3(0.0, 0.0, 0.0);
  93. position = glm::vec3(0.0, 0.0, 0.0);
  94.  
  95. lastTime = glfwGetTime();
  96. }
  97.  
  98. GLuint vertexArrayID;
  99. GLuint programID;
  100.  
  101. GLuint vertexbuffer;
  102.  
  103. std::vector<glm::vec3> faces;
  104. std::vector<glm::vec3> vertices;
  105.  
  106. glm::vec3 velocity;
  107.  
  108. double lastTime;
  109. glm::vec3 position;
  110. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement