@@ -42,16 +42,6 @@ namespace {
42
42
std::unique_ptr<ShaderManager> g_shader_manager;
43
43
}
44
44
45
- void CheckGlErr ()
46
- {
47
- auto err = glGetError ();
48
- while (err != GL_NO_ERROR)
49
- {
50
- std::cout << " err " << err << std::endl;
51
- err = glGetError ();
52
- }
53
- }
54
-
55
45
void InitData ()
56
46
{
57
47
std::string basepath = " ../../Resources/CornellBox/" ;
@@ -61,7 +51,6 @@ void InitData()
61
51
{
62
52
throw std::runtime_error (res);
63
53
}
64
-
65
54
}
66
55
67
56
float3 ConvertFromBarycentric (const float * vec, const int * ind, int prim_id, const float4& uvwt)
@@ -113,24 +102,20 @@ void InitGl()
113
102
// fill data
114
103
glBufferData (GL_ARRAY_BUFFER, sizeof (quad_vdata), quad_vdata, GL_STATIC_DRAW);
115
104
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (quad_idata), quad_idata, GL_STATIC_DRAW);
116
-
117
105
glBindBuffer (GL_ARRAY_BUFFER, 0 );
118
106
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0 );
119
107
120
-
108
+ // texture
121
109
glGenTextures (1 , &g_texture);
122
110
glBindTexture (GL_TEXTURE_2D, g_texture);
123
111
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
124
112
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
125
-
126
113
glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA, g_window_width, g_window_height, 0 , GL_RGBA, GL_UNSIGNED_BYTE, nullptr );
127
-
128
114
glBindTexture (GL_TEXTURE_2D, 0 );
129
115
}
130
116
131
117
void DrawScene ()
132
118
{
133
-
134
119
glDisable (GL_DEPTH_TEST);
135
120
glViewport (0 , 0 , g_window_width, g_window_height);
136
121
@@ -139,9 +124,9 @@ void DrawScene()
139
124
glBindBuffer (GL_ARRAY_BUFFER, g_vertex_buffer);
140
125
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, g_index_buffer);
141
126
127
+ // shader data
142
128
GLuint program = g_shader_manager->GetProgram (" simple" );
143
129
glUseProgram (program);
144
-
145
130
GLuint texloc = glGetUniformLocation (program, " g_Texture" );
146
131
assert (texloc >= 0 );
147
132
@@ -152,13 +137,12 @@ void DrawScene()
152
137
153
138
GLuint position_attr = glGetAttribLocation (program, " inPosition" );
154
139
GLuint texcoord_attr = glGetAttribLocation (program, " inTexcoord" );
155
-
156
140
glVertexAttribPointer (position_attr, 3 , GL_FLOAT, GL_FALSE, sizeof (float ) * 5 , 0 );
157
141
glVertexAttribPointer (texcoord_attr, 2 , GL_FLOAT, GL_FALSE, sizeof (float ) * 5 , (void *)(sizeof (float ) * 3 ));
158
-
159
142
glEnableVertexAttribArray (position_attr);
160
143
glEnableVertexAttribArray (texcoord_attr);
161
144
145
+ // draw rectanle
162
146
glDrawElements (GL_TRIANGLES, 6 , GL_UNSIGNED_SHORT, nullptr );
163
147
164
148
glDisableVertexAttribArray (texcoord_attr);
@@ -168,8 +152,6 @@ void DrawScene()
168
152
glUseProgram (0 );
169
153
170
154
glFinish ();
171
-
172
-
173
155
glutSwapBuffers ();
174
156
}
175
157
@@ -188,15 +170,17 @@ int main(int argc, char* argv[])
188
170
return -1 ;
189
171
}
190
172
#endif
191
-
173
+ // Prepare rectangle for drawing texture
174
+ // rendered using intersection results
192
175
InitGl ();
176
+
177
+ // Load CornellBox model
193
178
InitData ();
194
179
180
+ // Choose device
195
181
int nativeidx = -1 ;
196
-
197
182
// Always use OpenCL
198
183
IntersectionApi::SetPlatform (DeviceInfo::kOpenCL );
199
-
200
184
for (auto idx = 0U ; idx < IntersectionApi::GetDeviceCount (); ++idx)
201
185
{
202
186
DeviceInfo devinfo;
@@ -207,12 +191,10 @@ int main(int argc, char* argv[])
207
191
nativeidx = idx;
208
192
}
209
193
}
210
-
211
194
assert (nativeidx != -1 );
212
195
IntersectionApi* api = IntersectionApi::Create (nativeidx);
213
196
214
- // Shapes
215
- float3 light = {-0 .01f , 1 .9f , 0 .1f };
197
+ // Adding meshes to tracing scene
216
198
for (int id = 0 ; id < g_objshapes.size (); ++id)
217
199
{
218
200
shape_t & objshape = g_objshapes[id];
@@ -226,12 +208,13 @@ int main(int argc, char* argv[])
226
208
api->AttachShape (shape);
227
209
shape->SetId (id);
228
210
}
211
+ // Ñommit scene changes
212
+ api->Commit ();
229
213
230
214
const int k_raypack_size = g_window_height * g_window_width;
231
- // Rays
215
+
216
+ // Prepare rays. One for each texture pixel.
232
217
std::vector<ray> rays (k_raypack_size);
233
-
234
- // Prepare the ray
235
218
float4 camera_pos = { 0 .f , 1 .f , 3 .f , 1000 .f };
236
219
for (int i = 0 ; i < g_window_height; ++i)
237
220
for (int j = 0 ; j < g_window_width; ++j)
@@ -241,30 +224,38 @@ int main(int argc, char* argv[])
241
224
float x = -1 .f + xstep * (float )j;
242
225
float y = ystep * (float )i;
243
226
float z = 1 .f ;
227
+ // Perspective view
244
228
rays[i * g_window_width + j].o = camera_pos;
245
229
rays[i * g_window_width + j].d = float3 (x - camera_pos.x , y - camera_pos.y , z - camera_pos.z );
246
230
}
231
+ Buffer* ray_buffer = api->CreateBuffer (rays.size () * sizeof (ray), rays.data ());
247
232
248
- // Intersection and hit data
233
+ // Intersection data
249
234
std::vector<Intersection> isect (k_raypack_size);
250
-
251
- Buffer* ray_buffer = api->CreateBuffer (rays.size () * sizeof (ray), rays.data ());
252
235
Buffer* isect_buffer = api->CreateBuffer (isect.size () * sizeof (Intersection), nullptr );
253
236
254
- api-> Commit ();
237
+ // Intersection
255
238
api->QueryIntersection (ray_buffer, k_raypack_size, isect_buffer, nullptr , nullptr );
239
+
240
+ // Get results
256
241
Event* e = nullptr ;
257
242
Intersection* tmp = nullptr ;
258
243
api->MapBuffer (isect_buffer, kMapRead , 0 , isect.size () * sizeof (Intersection), (void **)&tmp, &e);
244
+ // RadeonRays calls are asynchronous, so need to wait for calculation to complete.
259
245
e->Wait ();
260
246
api->DeleteEvent (e);
261
247
e = nullptr ;
262
248
249
+ // Copy results
263
250
for (int i = 0 ; i < k_raypack_size; ++i)
264
251
{
265
252
isect[i] = tmp[i];
266
253
}
267
254
255
+ // Point light position
256
+ float3 light = { -0 .01f , 1 .9f , 0 .1f };
257
+
258
+ // Draw
268
259
std::vector<unsigned char > tex_data (k_raypack_size * 4 );
269
260
for (int i = 0 ; i < k_raypack_size ; ++i)
270
261
{
@@ -281,14 +272,16 @@ int main(int argc, char* argv[])
281
272
mat.diffuse [1 ],
282
273
mat.diffuse [2 ] };
283
274
275
+ // Calculate position and normal of the intersection point
284
276
float3 pos = ConvertFromBarycentric (mesh.positions .data (), mesh.indices .data (), prim_id, isect[i].uvwt );
285
277
float3 norm = ConvertFromBarycentric (mesh.normals .data (), mesh.indices .data (), prim_id, isect[i].uvwt );
286
278
norm.normalize ();
279
+
280
+ // Calculate lighting
287
281
float3 col = { 0 .f , 0 .f , 0 .f };
288
282
float3 light_dir = light - pos;
289
283
light_dir.normalize ();
290
284
float dot_prod = dot (norm, light_dir);
291
-
292
285
if (dot_prod > 0 )
293
286
col += dot_prod * diff_col;
294
287
@@ -298,12 +291,17 @@ int main(int argc, char* argv[])
298
291
tex_data[i * 4 + 3 ] = 255 ;
299
292
}
300
293
}
294
+
295
+ // Update texture data
301
296
glBindTexture (GL_TEXTURE_2D, g_texture);
302
297
glTexSubImage2D (GL_TEXTURE_2D, 0 , 0 , 0 , g_window_width, g_window_height, GL_RGBA, GL_UNSIGNED_BYTE, tex_data.data ());
303
298
glBindTexture (GL_TEXTURE_2D, NULL );
304
299
300
+ // Start the main loop
305
301
glutDisplayFunc (DrawScene);
306
- glutMainLoop (); // Start the main loop
302
+ glutMainLoop ();
303
+
304
+ // Cleanup
307
305
IntersectionApi::Delete (api);
308
306
309
307
return 0 ;
0 commit comments