-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.cpp
109 lines (88 loc) · 2.91 KB
/
Graphics.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
//
// Created by Filip K on 24/10/16.
//
#include <cstdlib>
#include <iostream>
#include "Graphics.h"
#include "kernels/cutil_math.h"
GLuint vbo;
float3* pixels;
float3* clrPixels;
float3* h_clrPixels = new float3[window_height*window_height];
bool cancel = false;
void RenderSample(float3* pixels, float3* clr_pixels);
Graphics::Graphics(int *argc, char **argv) {
if (!InitGL(argc, argv)) return;
cudaGLSetGLDevice(0);
}
bool Graphics::InitGL(int *argc, char **argv) {
cudaMalloc(&clrPixels, window_width * window_height * sizeof(float3));
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(window_width, window_height);
glutCreateWindow("Raytracer CUDA");
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, window_width, 0.0, window_height);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glewInit();
//Timer(0);
CreateVBO();
glutMainLoop();
return true;
}
void Graphics::CreateVBO() {
//create vertex buffer object
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, *&vbo);
//initialize VBO
unsigned int size = window_width * window_height * sizeof(float3);
glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//register VBO with CUDA
cudaGLRegisterBufferObject(*&vbo);
}
bool Graphics::CheckHW(char *name, const char *gpuType, int dev) {
return false;
}
int Graphics::FindGraphicsGPU(char *name) {
return 0;
}
void display() {
cudaThreadSynchronize();
cudaGLMapBufferObject((void**)&pixels, vbo);
glClear(GL_COLOR_BUFFER_BIT);
RenderSample(pixels, clrPixels);
cudaThreadSynchronize();
cudaGLUnmapBufferObject(vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(2, GL_FLOAT, 12, 0);
glColorPointer(4, GL_UNSIGNED_BYTE, 12, (GLvoid*)8);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_POINTS, 0, window_width * window_height);
glDisableClientState(GL_VERTEX_ARRAY);
glutSwapBuffers();
if (!cancel)
glutPostRedisplay();
}
inline int toInt(float x){ return int(pow(clamp(x), 1 / 2.2) * 255 + .5); }
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
switch (key)
{
case (27):
cancel = true;
cudaMemcpy(h_clrPixels, clrPixels, window_width * window_height * sizeof(float3), cudaMemcpyDeviceToHost);
cudaFree(pixels);
FILE *f = fopen("render.ppm", "w");
fprintf(f, "P3\n%d %d\n%d\n", window_width, window_height, 255);
for (int i = 0; i < window_width * window_height; i++) {
fprintf(f, "%d %d %d ", toInt(h_clrPixels[i].x), toInt(h_clrPixels[i].y), toInt(h_clrPixels[i].z));
}
printf("Saved image to 'render.ppm'\n");
exit(0);
}
}