Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- #ifdef TESTING
- testing();
- exit(0);
- #endif
- setupAndInitializeWindow(768, 480, "Final Project");
- TriangleTriangleCollision collisionDetector;
- Asset cube1("cube.obj", "vertexShader.txt", "fragmentShader.txt");
- cube1.position = glm::vec3(0.0, 2.0, 0.0);
- cube1.velocity = glm::vec3(0.0, -0.004, 0.0);
- MVP = projection * view * model;
- do{
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- moveAsset(cube1);
- renderAsset(cube1);
- glfwSwapBuffers(window);
- glfwPollEvents();
- } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
- glfwWindowShouldClose(window) == 0);
- glfwTerminate();
- return 0;
- }
- void moveAsset(Asset &asset)
- {
- double currentTime = glfwGetTime();
- asset.position.x += (asset.velocity.x * (currentTime - asset.lastTime));
- asset.position.y += (asset.velocity.y * (currentTime - asset.lastTime));
- asset.position.z += (asset.velocity.z * (currentTime - asset.lastTime));
- for (glm::vec3 &vertex : asset.vertices)
- {
- glm::vec4 transformedVector = glm::translate(glm::mat4(1.0f), asset.position) * glm::vec4(vertex.x, vertex.y, vertex.z, 1);
- vertex = glm::vec3(transformedVector.x, transformedVector.y, transformedVector.z);
- }
- asset.lastTime = glfwGetTime();
- }
- void renderAsset(Asset asset)
- {
- glUseProgram(asset.programID);
- GLuint MatrixID = glGetUniformLocation(asset.programID, "MVP");
- glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, asset.vertexbuffer);
- glBufferData(GL_ARRAY_BUFFER, asset.vertices.size() * sizeof(glm::vec3), &asset.vertices[0], GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
- glDrawArrays(GL_TRIANGLES, 0, asset.vertices.size());
- glDisableVertexAttribArray(0);
- }
- struct Asset
- {
- Asset() { }
- Asset(std::string assetOBJFile, std::string vertexShader, std::string fragmentShader)
- {
- glGenVertexArrays(1, &vertexArrayID);
- glBindVertexArray(vertexArrayID);
- programID = LoadShaders(vertexShader.c_str(), fragmentShader.c_str());
- // Read our .obj file
- std::vector<glm::vec2> uvs;
- std::vector<glm::vec3> normals;
- loadOBJ(assetOBJFile.c_str(), vertices, uvs, normals);
- // Load it into a VBO
- glGenBuffers(1, &vertexbuffer);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
- //velocity = glm::vec3(0.0, 1.0, 1.0);
- velocity = glm::vec3(0.0, 0.0, 0.0);
- position = glm::vec3(0.0, 0.0, 0.0);
- lastTime = glfwGetTime();
- }
- GLuint vertexArrayID;
- GLuint programID;
- GLuint vertexbuffer;
- std::vector<glm::vec3> faces;
- std::vector<glm::vec3> vertices;
- glm::vec3 velocity;
- double lastTime;
- glm::vec3 position;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement