-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
350 lines (289 loc) · 8.29 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
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
/*
* This file is part of CLraytracer.
* CLraytracer is a raytracing based renderer that take advantage of GPU
* parallelism through OpenCL
* Copyright (C) 2012 Yuri Iozzelli <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <GL/glew.h>
#include <GL/glfw.h>
#include <GL/gl.h>
#include <oglplus/all.hpp>
#include <iostream>
#include "renderer.h"
#include "matrix.h"
#include <functional>
#include <algorithm>
#define WIDTH 1024
#define HEIGHT 1024
int windowCloseCallback( )
{
static int i = 0;
return i ++;
}
class GLRenderer;
GLRenderer& GLRendererSingleton( ); // TODO: GCC bug?
class GLRenderer
{
public:
oglplus::Context gl;
oglplus::VertexArray rect;
oglplus::Buffer verts;
oglplus::Program prog;
oglplus::Texture texture;
Scene *scene;
Renderer *renderer;
int imgW;
int imgH;
cl_float3 movement;
cl_float3 rotation;
public:
static GLRenderer& singleton( )
{
static GLRenderer r;
return r;
}
private:
void keyCallback( int key, int action )
{
#define DEFINE_CONTROL_KEY( keyA, keyB, var ) \
if( key == keyA ) { \
if( action == GLFW_PRESS ) { \
var += 1; \
} \
if( action == GLFW_RELEASE ) { \
var -= 1; \
} \
} \
if( key == keyB ) { \
if( action == GLFW_PRESS ) { \
var += -1; \
} \
if( action == GLFW_RELEASE ) { \
var += +1; \
} \
}
DEFINE_CONTROL_KEY( GLFW_KEY_UP, GLFW_KEY_DOWN, movement.s[2] )
DEFINE_CONTROL_KEY( GLFW_KEY_RIGHT, GLFW_KEY_LEFT, movement.s[0] )
DEFINE_CONTROL_KEY( GLFW_KEY_KP_ADD, GLFW_KEY_KP_SUBTRACT, movement.s[1] )
DEFINE_CONTROL_KEY( GLFW_KEY_KP_6, GLFW_KEY_KP_4, rotation.s[0] )
DEFINE_CONTROL_KEY( GLFW_KEY_KP_2, GLFW_KEY_KP_8, rotation.s[1] )
DEFINE_CONTROL_KEY( GLFW_KEY_KP_7, GLFW_KEY_KP_9, rotation.s[2] )
}
GLRenderer( )
{
glfwSetWindowTitle("CLraytracer (0.0 FPS)");
glfwSetKeyCallback( [](int key, int action){ ::GLRendererSingleton().keyCallback(key,action); } );
//glfwSetKeyCallback( std::bind( &GLRenderer::keyCallback, &GLRenderer::singleton(), std::placeholders::_1, std::placeholders::_2 ) );
// glfwEnable( GLFW_KEY_REPEAT );
imgW = WIDTH;
imgH = HEIGHT;
// disabling vertical sync
glfwSwapInterval(0);
// initializing openGL
{
using namespace oglplus;
// vertex shader
{
oglplus::VertexShader vs;
vs.Source(" \
attribute vec3 Position; \
void main() \
{ \
gl_Position = vec4(Position,1); \
gl_TexCoord[0] = ( gl_Position + vec4(1,1,0,0) ) * 0.5; \
} \
");
vs.Compile();
prog.AttachShader( vs );
}
// fragment shader
{
oglplus::FragmentShader fs;
fs.Source(" \
uniform sampler2D fava; \
void main() \
{ \
/*gl_FragColor = vec4(1,0,0,1.0);*/ \
gl_FragColor = texture2D( fava, gl_TexCoord[0].st ); \
} \
");
fs.Compile();
prog.AttachShader(fs);
}
// link and use it
prog.Link();
prog.Use();
//oglplus::VertexArray rect;
//oglplus::Buffer verts;
GLfloat rect_verts[12] = {
-1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
// bind the VBO for the rect vertices
verts.Bind( Buffer::Target::Array );
// upload the data
Buffer::Data(
Buffer::Target::Array,
12,
rect_verts
);
// setup the vertex attribs array for the vertices
VertexAttribArray vert_attr( prog, "Position" );
vert_attr.Setup( 3, DataType::Float );
vert_attr.Enable();
gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.ClearDepth(1.0f);
std::vector<cl_float3> vec(imgH*imgW);
setTexture(&vec[0]);
}
}
~GLRenderer( )
{
}
public:
void setTexture( cl_float3 *data )
{
using namespace oglplus;
texture.Bind( Texture::Target::_2D );
texture.MagFilter( Texture::Target::_2D, TextureMagFilter::Linear );
texture.MinFilter( Texture::Target::_2D, TextureMinFilter::Linear );
texture.Image2D( Texture::Target::_2D, 0, PixelDataInternalFormat::RGBA, imgW, imgH, 0, PixelDataFormat::RGBA, PixelDataType::Float, data );
}
void draw( )
{
oglplus::Exposed<oglplus::Texture> texExposed(texture) ;
GLuint tex = texExposed.Name();
using namespace std;
static cl_float r = 0;
r += 0.01;
//scene->camera = Matrix4x4::identity().translate( {{0,sin(r)*50,10}} ).rotate( {{0,sin(r),0}} ).m;
scene->camera = Matrix4x4( scene->camera ).translate( movement ).rotate( rotation*0.01 ).m;
/*std::vector<cl_float3> ris = */renderer->compute( *scene, imgW, imgH,tex );
// setTexture( &ris[0] );
using namespace oglplus;
gl.Clear().ColorBuffer().DepthBuffer();
gl.DrawArrays( PrimitiveType::TriangleStrip, 0, 4 );
}
void mainLoop( )
{
double time,t0,fps;
int frames = 0;
char title[100];
t0 = glfwGetTime();
while( glfwGetWindowParam(GLFW_OPENED) ) {
//get the current time
time = glfwGetTime();
// Calcul and display the FPS
if((time-t0) > 1.0 || frames == 0)
{
fps = (double)frames / (time-t0);
sprintf(title, "CLraytracer (%.1f FPS)", fps);
glfwSetWindowTitle(title);
t0 = time;
frames = 0;
}
frames ++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
glfwSwapBuffers();
glfwPollEvents();
}
}
};
GLRenderer& GLRendererSingleton( ) // TODO: GCC bug?
{
return GLRenderer::singleton();
}
int main(int argc, char *argv[])
{;
if( ! glfwInit() ) {
throw( "glfwInit failed" );
}
struct on_terminate { ~on_terminate(){ glfwTerminate(); } } _l1;
if( ! glfwOpenWindow( WIDTH,HEIGHT, 0,0,0,0, 0, 0, GLFW_WINDOW ) ) {
throw( "glfwOpenWindow failed" );
}
struct on_close { ~on_close(){ glfwCloseWindow(); } } _l2;
if( glewInit() != GLEW_OK ) {
throw( "gl4x4ewInit failed" );
}
try
{
GLRenderer& GLr = GLRenderer::singleton();
Renderer r;
Scene s;
s.camera = Matrix4x4::identity().m;
#define X(i) sin(i*2*M_PI/3)*10*2/sqrt(3)
#define Y(i) cos(i*2*M_PI/3)*10*2/sqrt(3)
Light l1 = {{0,30,-20},{2,2,2}};
s.lights.push_back(l1);
Light l2 = {{30,-50,0},{1,1,1}};
s.lights.push_back(l2);
Light l3 = {{-30,50,-10},{3,3,3}};
s.lights.push_back(l3);
std::vector<Plane> planes;
Plane p1 = {{PLANE_TYPE,0}, {0,0,60},{0,0,1}};
planes.push_back(p1);
std::vector<Sphere> spheres;
Sphere sp1 = {{SPHERE_TYPE,0}, {X(0),Y(0),60},10};
Material m1 = {{.6,.6,.6},{0.5,0.5,0},100};
spheres.push_back(sp1);
s.materials.push_back(m1);
Sphere sp2 = {{SPHERE_TYPE,1}, {X(1),Y(1),60},10};
Material m2 = {{.1,.1,.1},{0,0.5,0.5},30};
spheres.push_back(sp2);
s.materials.push_back(m2);
Sphere sp3 = {{SPHERE_TYPE,2}, {X(2),Y(2),60},10};
Material m3 = {{.1,.1,.1},{0.5,0,0.5},60};
spheres.push_back(sp3);
s.materials.push_back(m3);
std::copy((char*)(&spheres[0]),(char*)(&spheres[0]+spheres.size()*sizeof(Sphere)),std::back_inserter(s.objects));
std::copy((char*)(&planes[0]),(char*)(&planes[0]+planes.size()*sizeof(Plane)),std::back_inserter(s.objects));
s.nObj=spheres.size()+planes.size();
#undef X
#undef Y
GLr.scene = &s;
GLr.renderer = &r;
GLr.mainLoop();
}
catch( oglplus::CompileError &err )
{
std::cerr <<
"Error (in " << err.GLSymbol() << ", " <<
err.ClassName() << ": '" <<
err.ObjectDescription() << "'): " <<
err.what() <<
" [" << err.File() << ":" << err.Line() << "] ";
std::cerr << std::endl;
std::cerr << err.Log() << std::endl;
err.Cleanup();
}
catch( oglplus::Error &err )
{
std::cerr <<
"Error (in " << err.GLSymbol() << ", " <<
err.ClassName() << ": '" <<
err.ObjectDescription() << "'): " <<
err.what() <<
" [" << err.File() << ":" << err.Line() << "] ";
std::cerr << std::endl;
err.Cleanup();
}
return EXIT_SUCCESS;
}