-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
243 lines (186 loc) · 7.79 KB
/
main.cpp
File metadata and controls
243 lines (186 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <filesystem>
#include <string>
#include <fstream>
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "include/stb_image.h"
#include "include/shaders.h"
#include "include/camera.h"
#include "include/clas.h"
#include "include/physics.h"
#include "include/writer.h"
using namespace std;
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
camera cam(glm::vec3(4.0f, 1.0f, 0.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
// Callback function to handle window resizing
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
// Callback function to handle mouse movement
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn){
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
cam.ProcessMouseMove(xoffset, yoffset);
}
// Function to process input from the keyboard
void processInput(GLFWwindow *window){
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cam.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cam.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cam.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cam.ProcessKeyboard(RIGHT, deltaTime);
}
int main(int argc, char** argv)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
{
return -1;
}
glEnable(GL_CULL_FACE); // Enable face culling
glCullFace(GL_BACK); // Cull back faces (default)
glFrontFace(GL_CCW); // Set counter-clockwise winding order as front-facing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Scene", nullptr, nullptr);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glewExperimental = GL_TRUE;
// Initialize GLEW
if (glewInit() != GLEW_OK)
{
std::cerr << "ERROR: GLEW Initialization Failed\n";
return -1;
}
// Initialize world
world myWorld = world();
// Get models from command line arguments
for (int i = 1; i < argc; i++) {
myWorld.add_tree(argv[i]);
}
// Imposes a light source in the scene
lightSource light = lightSource(20.0f, 3.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f, 0.5f, 1.0f);
// Set up the shader
Shader meuShader("../shaders/myshader.vs", "../shaders/myshader.fs");
// Set up the scene
// slighly sideways cube that falls on the sphere
myWorld.trees[1]->obj->m->mod->rotate(45.0f, 0.0f, 0.0f);
myWorld.trees[1]->obj->m->mod->translate(0.0f, 5.0f, 2.0f);
myWorld.trees[1]->set_positions();
myWorld.trees[1]->obj->isVertStatic = std::vector<bool>(myWorld.trees[1]->obj->position.size(), false); // Initialize all vertices as static
myWorld.trees[1]->obj->isStatic = false;
// Big bunny for seing phong illumination
myWorld.trees[3]->obj->m->mod->translate(0.0f, 200.0f, 800.0f);
myWorld.trees[3]->set_positions();
// Cube that is being hung by one vertex
myWorld.trees[4]->obj->m->mod->translate(0.0f, 4.0f, 10.0f);
myWorld.trees[4]->set_positions();
myWorld.trees[4]->obj->isVertStatic = std::vector<bool>(myWorld.trees[4]->obj->position.size(), false); // Initialize all vertices as static
myWorld.trees[4]->obj->isStatic = false;
myWorld.trees[4]->obj->isVertStatic[0] = true; // Make the first vertex static
myWorld.trees[4]->obj->springRestitution = 1.0f; // Set spring restitution for the object
// Cube that falls on the floor
myWorld.trees[5]->obj->m->mod->translate(0.0f, 4.0f, -10.0f);
myWorld.trees[5]->set_positions();
myWorld.trees[5]->obj->isVertStatic = std::vector<bool>(myWorld.trees[5]->obj->position.size(), false); // Initialize all vertices as static
myWorld.trees[5]->obj->isStatic = false;
// Starts the world simulation
myWorld.start_world();
// Set up the viewport
glViewport(0, 0, 800, 600);
int width, height;
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); // Fragments closer to the camera overwrite farther ones
// Enable wireframe mode for debugging
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Uncomment to enable wireframe
// If you want to save frames, set this to true and select a scene index
bool saveFrames = false;
int sceneIndex = 1;
long long frameCount = 0;
std::string path = "../scenes/scene" + std::to_string(sceneIndex);
std::filesystem::create_directory(path);
std::string pseudoObjPath = path + "/frame";
std::string mtlPath = "../scenes/scene" + std::to_string(sceneIndex) + "/scene.mtl";
writer saver(pseudoObjPath + std::to_string(frameCount) + ".obj", mtlPath);
// Save the MTL file only once at the beginning
if (saveFrames){
saver.writeMTL(myWorld);
}
std::cout << "Starting simulation..." << std::endl;
while (!glfwWindowShouldClose(window))
{
// If we want to save frames, write the OBJ file every frame
if (saveFrames && frameCount < 10){
saver.setPaths(pseudoObjPath + std::to_string(frameCount) + ".obj", mtlPath);
saver.writeOBJ(myWorld);
}
frameCount++;
// Makes the time pass
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Process input
processInput(window);
glfwGetFramebufferSize(window, &width, &height);
/* Render here */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//starts the shader
meuShader.use();
// Passes the camera and light parameters to the shader
glm::mat4 view = cam.getViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (float)width / (float)height, 0.1f, 3000.0f);
glm::mat4 model = glm::mat4(1.0f);
meuShader.setMat4("view", view);
meuShader.setMat4("projection", projection);
meuShader.setMat4("model", model);
meuShader.setVec3("viewPos", cam.Position);
meuShader.setVec3("light.position", light.position[0], light.position[1], light.position[2]);
meuShader.setVec3("light.ambient", light.ambient[0], light.ambient[1], light.ambient[2]);
meuShader.setVec3("light.diffuse", light.diffuse[0], light.diffuse[1], light.diffuse[2]);
meuShader.setVec3("light.specular", light.specular[0], light.specular[1], light.specular[2]);
// Draw the world
myWorld.draw_world(meuShader);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
//free resources
return 0;
}