Skip to content

Commit c6445fa

Browse files
author
Konstantin Zverev
committed
Tutorials comments and cleanup
1 parent 4b5c737 commit c6445fa

File tree

4 files changed

+132
-131
lines changed

4 files changed

+132
-131
lines changed

Tutorials/CornellBox/main.cpp

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ namespace {
4242
std::unique_ptr<ShaderManager> g_shader_manager;
4343
}
4444

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-
5545
void InitData()
5646
{
5747
std::string basepath = "../../Resources/CornellBox/";
@@ -61,7 +51,6 @@ void InitData()
6151
{
6252
throw std::runtime_error(res);
6353
}
64-
6554
}
6655

6756
float3 ConvertFromBarycentric(const float* vec, const int* ind, int prim_id, const float4& uvwt)
@@ -113,24 +102,20 @@ void InitGl()
113102
// fill data
114103
glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vdata), quad_vdata, GL_STATIC_DRAW);
115104
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quad_idata), quad_idata, GL_STATIC_DRAW);
116-
117105
glBindBuffer(GL_ARRAY_BUFFER, 0);
118106
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
119107

120-
108+
// texture
121109
glGenTextures(1, &g_texture);
122110
glBindTexture(GL_TEXTURE_2D, g_texture);
123111
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
124112
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
125-
126113
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, g_window_width, g_window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
127-
128114
glBindTexture(GL_TEXTURE_2D, 0);
129115
}
130116

131117
void DrawScene()
132118
{
133-
134119
glDisable(GL_DEPTH_TEST);
135120
glViewport(0, 0, g_window_width, g_window_height);
136121

@@ -139,9 +124,9 @@ void DrawScene()
139124
glBindBuffer(GL_ARRAY_BUFFER, g_vertex_buffer);
140125
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_index_buffer);
141126

127+
// shader data
142128
GLuint program = g_shader_manager->GetProgram("simple");
143129
glUseProgram(program);
144-
145130
GLuint texloc = glGetUniformLocation(program, "g_Texture");
146131
assert(texloc >= 0);
147132

@@ -152,13 +137,12 @@ void DrawScene()
152137

153138
GLuint position_attr = glGetAttribLocation(program, "inPosition");
154139
GLuint texcoord_attr = glGetAttribLocation(program, "inTexcoord");
155-
156140
glVertexAttribPointer(position_attr, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
157141
glVertexAttribPointer(texcoord_attr, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float) * 3));
158-
159142
glEnableVertexAttribArray(position_attr);
160143
glEnableVertexAttribArray(texcoord_attr);
161144

145+
// draw rectanle
162146
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr);
163147

164148
glDisableVertexAttribArray(texcoord_attr);
@@ -168,8 +152,6 @@ void DrawScene()
168152
glUseProgram(0);
169153

170154
glFinish();
171-
172-
173155
glutSwapBuffers();
174156
}
175157

@@ -188,15 +170,17 @@ int main(int argc, char* argv[])
188170
return -1;
189171
}
190172
#endif
191-
173+
// Prepare rectangle for drawing texture
174+
// rendered using intersection results
192175
InitGl();
176+
177+
// Load CornellBox model
193178
InitData();
194179

180+
// Choose device
195181
int nativeidx = -1;
196-
197182
// Always use OpenCL
198183
IntersectionApi::SetPlatform(DeviceInfo::kOpenCL);
199-
200184
for (auto idx = 0U; idx < IntersectionApi::GetDeviceCount(); ++idx)
201185
{
202186
DeviceInfo devinfo;
@@ -207,12 +191,10 @@ int main(int argc, char* argv[])
207191
nativeidx = idx;
208192
}
209193
}
210-
211194
assert(nativeidx != -1);
212195
IntersectionApi* api = IntersectionApi::Create(nativeidx);
213196

214-
//Shapes
215-
float3 light = {-0.01f, 1.9f, 0.1f};
197+
// Adding meshes to tracing scene
216198
for (int id = 0; id < g_objshapes.size(); ++id)
217199
{
218200
shape_t& objshape = g_objshapes[id];
@@ -226,12 +208,13 @@ int main(int argc, char* argv[])
226208
api->AttachShape(shape);
227209
shape->SetId(id);
228210
}
211+
// Ñommit scene changes
212+
api->Commit();
229213

230214
const int k_raypack_size = g_window_height * g_window_width;
231-
// Rays
215+
216+
// Prepare rays. One for each texture pixel.
232217
std::vector<ray> rays(k_raypack_size);
233-
234-
// Prepare the ray
235218
float4 camera_pos = { 0.f, 1.f, 3.f, 1000.f };
236219
for (int i = 0; i < g_window_height; ++i)
237220
for (int j = 0; j < g_window_width; ++j)
@@ -241,30 +224,38 @@ int main(int argc, char* argv[])
241224
float x = -1.f + xstep * (float)j;
242225
float y = ystep * (float)i;
243226
float z = 1.f;
227+
// Perspective view
244228
rays[i * g_window_width + j].o = camera_pos;
245229
rays[i * g_window_width + j].d = float3(x - camera_pos.x, y - camera_pos.y, z - camera_pos.z);
246230
}
231+
Buffer* ray_buffer = api->CreateBuffer(rays.size() * sizeof(ray), rays.data());
247232

248-
// Intersection and hit data
233+
// Intersection data
249234
std::vector<Intersection> isect(k_raypack_size);
250-
251-
Buffer* ray_buffer = api->CreateBuffer(rays.size() * sizeof(ray), rays.data());
252235
Buffer* isect_buffer = api->CreateBuffer(isect.size() * sizeof(Intersection), nullptr);
253236

254-
api->Commit();
237+
// Intersection
255238
api->QueryIntersection(ray_buffer, k_raypack_size, isect_buffer, nullptr, nullptr);
239+
240+
// Get results
256241
Event* e = nullptr;
257242
Intersection* tmp = nullptr;
258243
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.
259245
e->Wait();
260246
api->DeleteEvent(e);
261247
e = nullptr;
262248

249+
// Copy results
263250
for (int i = 0; i < k_raypack_size; ++i)
264251
{
265252
isect[i] = tmp[i];
266253
}
267254

255+
// Point light position
256+
float3 light = { -0.01f, 1.9f, 0.1f };
257+
258+
// Draw
268259
std::vector<unsigned char> tex_data(k_raypack_size * 4);
269260
for (int i = 0; i < k_raypack_size ; ++i)
270261
{
@@ -281,14 +272,16 @@ int main(int argc, char* argv[])
281272
mat.diffuse[1],
282273
mat.diffuse[2] };
283274

275+
// Calculate position and normal of the intersection point
284276
float3 pos = ConvertFromBarycentric(mesh.positions.data(), mesh.indices.data(), prim_id, isect[i].uvwt);
285277
float3 norm = ConvertFromBarycentric(mesh.normals.data(), mesh.indices.data(), prim_id, isect[i].uvwt);
286278
norm.normalize();
279+
280+
// Calculate lighting
287281
float3 col = { 0.f, 0.f, 0.f };
288282
float3 light_dir = light - pos;
289283
light_dir.normalize();
290284
float dot_prod = dot(norm, light_dir);
291-
292285
if (dot_prod > 0)
293286
col += dot_prod * diff_col;
294287

@@ -298,12 +291,17 @@ int main(int argc, char* argv[])
298291
tex_data[i * 4 + 3] = 255;
299292
}
300293
}
294+
295+
// Update texture data
301296
glBindTexture(GL_TEXTURE_2D, g_texture);
302297
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_window_width, g_window_height, GL_RGBA, GL_UNSIGNED_BYTE, tex_data.data());
303298
glBindTexture(GL_TEXTURE_2D, NULL);
304299

300+
// Start the main loop
305301
glutDisplayFunc(DrawScene);
306-
glutMainLoop(); //Start the main loop
302+
glutMainLoop();
303+
304+
// Cleanup
307305
IntersectionApi::Delete(api);
308306

309307
return 0;

0 commit comments

Comments
 (0)