-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
206 lines (157 loc) · 4.86 KB
/
main.cpp
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
//INclude standard headers
#pragma comment(lib,"legacy_stdio_definitions.lib")
// Include standard headers
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <ctime>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <glfw/glfw3.h>
GLFWwindow* window;
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include<vector>
using namespace std;
#include "ArgParser.h"
#include "Mesh.h"
#include "Radiosity.h"
#include "UserControls.h"
int main(int argc, char *argv[])
{
ArgParser argParser(argc, argv);
// Initialise GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
// Open a window and create its OpenGL context
window = glfwCreateWindow(argParser.windowWidth, argParser.windowHeight, "Radiosity", NULL, NULL);
if (window == NULL)
{
fprintf(stderr, "Failed to open GLFW window.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glfwSetCursorPos(window, argParser.windowWidth / 2, argParser.windowHeight / 2);
// Dark blue background
glClearColor(argParser.bgcolor.r, argParser.bgcolor.g, argParser.bgcolor.b, 0.0f);
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
// Cull triangles which normal is not towards the camera
glEnable(GL_CULL_FACE);
UserControls userControls(
argParser.cameraPosition,
argParser.horizontalAngle,
argParser.verticalAngle,
argParser.interpolate
);
Mesh* mesh = new Mesh();
Radiosity* radiosity = new Radiosity();
mesh->Load(argParser.sceneName);
std::cout << sizeof(ObjectModel) << std::endl;
glShadeModel(GL_SMOOTH);
// Create and compile our GLSL program from the mesh's shaders
GLuint meshShaderProgramID = mesh->LoadDefaultShaders();
mesh->cacheVerticesFacesAndColors();
mesh->PrepareToDraw();
printf("Loading faces...\n");
radiosity->loadSceneFacesFromMesh(mesh);
//if we have number of subdivisions
if (argParser.numSubdivisions > 0)
{
printf("Subdivision\n");
for (int i = 0; i<argParser.numSubdivisions; i++)
{
printf("LOD: %d\n", i);
mesh->Subdivide();
//mesh->cacheVerticesFacesAndColors();
//mesh->PrepareToDraw();
radiosity->loadSceneFacesFromMesh(mesh);
radiosity->PrepareUnshotRadiosityValues();
}
}
//now we draw
do
{
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
userControls.computeMatrices(argParser.initialFoV, argParser.nearClippingPlane, argParser.farClippingPlane, argParser.moveSpeed, argParser.mouseSpeed);
glm::mat4 ProjectionMatrix = userControls.getProjectionMatrix();
glm::mat4 ViewMatrix = userControls.getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
mesh->SetMVP(MVP);
//if we have set number of iterations, then do them, output the image and exit immediatelly
if (argParser.numIterations > 0)
{
printf("Calculating radiosity solution for scene. This could take a while...\n");
for (int i = 0; i< argParser.numIterations; i++)
{
printf("Radiosity iteration: %d\n", i);
radiosity->calculateRadiosityValues();
radiosity->setMeshFaceColors();
}
printf("Caching vertex positions and colors...\n");
if (argParser.interpolate)
mesh->cacheVerticesFacesAndColors_Radiosity_II();
else
mesh->cacheVerticesFacesAndColors();
mesh->PrepareToDraw();
//draw the mesh
mesh->Draw();
// Swap buffers
glfwSwapBuffers(window);
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR)
{
printf("OpenGL error: %d\n", err);
}
printf("Preparing to save file\n");
time_t now = time(0);
string bmpName(to_string(now).append(".bmp"));
int windowWidth;
int windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
mesh->OutputToBitmap(bmpName, windowWidth, windowHeight);
printf("Screenshot saved: %s\n", bmpName);
//close the window
glfwSetWindowShouldClose(window, GL_TRUE);
}
userControls.handleKeyboard(mesh, radiosity);
//draw the mesh
mesh->Draw();
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR)
{
printf("OpenGL error: %d\n", err);
}
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
// Cleanup mesh VBO
mesh->Cleanup();
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}