-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmain.cpp
More file actions
438 lines (324 loc) · 9.93 KB
/
main.cpp
File metadata and controls
438 lines (324 loc) · 9.93 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* @file main.cpp
* @brief Main file for CUDA rasterizer. Handles CUDA-GL interop for display.
* @authors Skeleton code: Yining Karl Li, Kai Ninomiya
* @date 2012-2015
* @copyright University of Pennsylvania
*/
#include "main.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtx/transform.hpp"
#include "GLFW/glfw3.h"
//-------------------------------
//-------------MAIN--------------
//-------------------------------
int main(int argc, char **argv) {
if (argc != 2) {
cout << "Usage: [obj file]" << endl;
return 0;
}
obj *mesh = new obj();
{
objLoader loader(argv[1], mesh);
mesh->buildBufPoss();
}
frame = 0;
seconds = time(NULL);
fpstracker = 0;
// Launch CUDA/GL
if (init(mesh)) {
// GLFW main loop
mainLoop();
}
delete mesh;
return 0;
}
void mainLoop() {
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
//MY
setupCamera();
runCuda();
time_t seconds2 = time (NULL);
if (seconds2 - seconds >= 1) {
fps = fpstracker / (seconds2 - seconds);
fpstracker = 0;
seconds = seconds2;
}
string title = "CIS565 Rasterizer | " + utilityCore::convertIntToString((int)fps) + " FPS";
glfwSetWindowTitle(window, title.c_str());
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
glBindTexture(GL_TEXTURE_2D, displayImage);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glClear(GL_COLOR_BUFFER_BIT);
// VAO, shader program, and texture already bound
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();
}
//-------------------------------
//---------RUNTIME STUFF---------
//-------------------------------
//translate x,y, angle, scale for keyboard operations
float scale = 0.15f;
//float scale = 0.05f;
float x_trans = 0.0f, y_trans = 0.0f, z_trans = -10.0f;
float x_angle = 0.0f, y_angle = 0.0f;
glm::mat4 M_model;
glm::mat4 M_view;
glm::mat4 M_perspective;
glm::mat4 inv_trans_M_view; //for normal transformation
//lights
void runCuda() {
// Map OpenGL buffer object for writing from CUDA on a single GPU
// No data is moved (Win & Linux). When mapped to CUDA, OpenGL should not use this buffer
dptr = NULL;
//int xpos, ypos;
//glfwGetMousePos(&xpos, &ypos);
cudaGLMapBufferObject((void **)&dptr, pbo);
vertexShader(M_perspective * M_view * M_model, M_view*M_model, inv_trans_M_view);
rasterize(dptr);
cudaGLUnmapBufferObject(pbo);
frame++;
fpstracker++;
}
//MY
void setupCamera()
{
//model-view
//M_model = glm::mat4(1.0f);
//M_model = glm::translate(M_model, glm::vec3(0, 0, z_trans));
//x_angle, y_angle : radium
M_model = glm::translate(glm::vec3(x_trans, y_trans, z_trans))
* glm::rotate(x_angle, glm::vec3(1.0f, 0.0f, 0.0f))
* glm::rotate(y_angle, glm::vec3(0.0f, 1.0f, 0.0f));
M_view = glm::mat4(1.0f);
//projection
//left, right, bottom, top, near, far
M_perspective = glm::frustum<float>(-scale * ((float)width) / ((float)height),
scale * ((float)width / (float)height),
-scale, scale, 1.0, 1000.0);
inv_trans_M_view = glm::transpose(glm::inverse(M_view * M_model));
}
//-------------------------------
//----------SETUP STUFF----------
//-------------------------------
bool init(obj *mesh) {
glfwSetErrorCallback(errorCallback);
if (!glfwInit()) {
return false;
}
width = 800;
height = 800;
window = glfwCreateWindow(width, height, "CIS 565 Pathtracer", NULL, NULL);
if (!window) {
glfwTerminate();
return false;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyCallback);
//MY Mouse Control
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetCursorPosCallback(window, mouseMotionCallback);
glfwSetScrollCallback(window,mouseWheelCallback);
/////
// Set up GL context
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
return false;
}
// Initialize other stuff
initVAO();
initTextures();
initCuda();
initPBO();
float cbo[] = {
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0
};
rasterizeSetBuffers(mesh->getBufIdxsize(), mesh->getBufIdx(),
mesh->getBufPossize() / 3,
mesh->getFaces()->size(),
mesh->getBufPos(), mesh->getBufNor(), mesh->getBufCol()
, (mesh->getTextureCoords())->size() > 0, mesh->getBufTex());
initTextureData(mesh->diffuse_tex.size() > 0, mesh->diffuse_width, mesh->diffuse_height, mesh->diffuse_tex.data(),
mesh->specular_tex.size() > 0, mesh->specular_width, mesh->specular_height, mesh->specular_tex.data(),
mesh->ambient_color,mesh->diffuse_color,mesh->specular_color,mesh->specular_exponent);
GLuint passthroughProgram;
passthroughProgram = initShader();
glUseProgram(passthroughProgram);
glActiveTexture(GL_TEXTURE0);
return true;
}
void initPBO() {
// set up vertex data parameter
int num_texels = width * height;
int num_values = num_texels * 4;
int size_tex_data = sizeof(GLubyte) * num_values;
// Generate a buffer ID called a PBO (Pixel Buffer Object)
glGenBuffers(1, &pbo);
// Make this the current UNPACK buffer (OpenGL is state-based)
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
// Allocate data for the buffer. 4-channel 8-bit image
glBufferData(GL_PIXEL_UNPACK_BUFFER, size_tex_data, NULL, GL_DYNAMIC_COPY);
cudaGLRegisterBufferObject(pbo);
}
void initCuda() {
// Use device with highest Gflops/s
cudaGLSetGLDevice(0);
rasterizeInit(width, height);
// Clean up on program exit
atexit(cleanupCuda);
}
void initTextures() {
glGenTextures(1, &displayImage);
glBindTexture(GL_TEXTURE_2D, displayImage);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA,
GL_UNSIGNED_BYTE, NULL);
}
void initVAO(void) {
GLfloat vertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
1.0f, 1.0f,
-1.0f, 1.0f,
};
GLfloat texcoords[] = {
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
GLushort indices[] = { 0, 1, 3, 3, 1, 2 };
GLuint vertexBufferObjID[3];
glGenBuffers(3, vertexBufferObjID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)positionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(positionLocation);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)texcoordsLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(texcoordsLocation);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertexBufferObjID[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
GLuint initShader() {
const char *attribLocations[] = { "Position", "Tex" };
GLuint program = glslUtility::createDefaultProgram(attribLocations, 2);
GLint location;
glUseProgram(program);
if ((location = glGetUniformLocation(program, "u_image")) != -1) {
glUniform1i(location, 0);
}
return program;
}
//-------------------------------
//---------CLEANUP STUFF---------
//-------------------------------
void cleanupCuda() {
if (pbo) {
deletePBO(&pbo);
}
if (displayImage) {
deleteTexture(&displayImage);
}
}
void deletePBO(GLuint *pbo) {
if (pbo) {
// unregister this buffer object with CUDA
cudaGLUnregisterBufferObject(*pbo);
glBindBuffer(GL_ARRAY_BUFFER, *pbo);
glDeleteBuffers(1, pbo);
*pbo = (GLuint)NULL;
}
}
void deleteTexture(GLuint *tex) {
glDeleteTextures(1, tex);
*tex = (GLuint)NULL;
}
void shut_down(int return_code) {
rasterizeFree();
cudaDeviceReset();
#ifdef __APPLE__
glfwTerminate();
#endif
exit(return_code);
}
//------------------------------
//-------GLFW CALLBACKS---------
//------------------------------
void errorCallback(int error, const char *description) {
fputs(description, stderr);
}
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
else if (key == GLFW_KEY_W && action == GLFW_PRESS)
{
changeGeomMode();
}
else if (key == GLFW_KEY_S && action == GLFW_PRESS)
{
changeShaderMode( ) ;
}
else if (key == GLFW_KEY_B && action == GLFW_PRESS)
{
changeBackFaceCulling();
}
}
enum ControlState {NONE=0,ROTATE,TRANSLATE};
ControlState mouseState = NONE;
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
if (action == GLFW_PRESS)
{
if (button == GLFW_MOUSE_BUTTON_LEFT)
{
mouseState = ROTATE;
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
mouseState = TRANSLATE;
}
}
else if (action == GLFW_RELEASE)
{
mouseState = NONE;
}
//printf("%d\n", mouseState);
}
double lastx = (double)width / 2;
double lasty = (double)height / 2;
void mouseMotionCallback(GLFWwindow* window, double xpos, double ypos)
{
const double s_r = 0.01;
const double s_t = 0.01;
double diffx = xpos - lastx;
double diffy = ypos - lasty;
lastx = xpos;
lasty = ypos;
if (mouseState == ROTATE)
{
//rotate
x_angle += (float)s_r * diffy;
y_angle += (float)s_r * diffx;
}
else if (mouseState == TRANSLATE)
{
//translate
x_trans += (float)(s_t * diffx);
y_trans += (float)(-s_t * diffy);
}
}
void mouseWheelCallback(GLFWwindow* window, double xoffset, double yoffset)
{
const double s_s = 0.01;
scale += (float)(-s_s * yoffset);
}