@@ -51,18 +51,58 @@ rpr_material_system g_matsys = NULL;
51
51
rpr_light g_light = NULL ;
52
52
rpr_framebuffer g_frame_buffer_2 = NULL ;
53
53
ShaderManager g_shader_manager;
54
+ rpr_scene g_scene=nullptr ;
55
+ rpr_shape g_cube=nullptr ;
56
+ rpr_camera g_camera=nullptr ;
57
+ rpr_material_node g_diffuse=nullptr ;
58
+ rpr_shape g_plane=nullptr ;
59
+ float * g_fbdata = nullptr ;
60
+ float g_camera_posX = 0.0 ;
54
61
55
62
void Update ()
56
63
{
57
64
// Send update event
58
65
glutPostRedisplay ();
59
66
}
60
67
68
+ void OnKeyboardEvent (unsigned char key, int xmouse, int ymouse)
69
+ {
70
+ bool cameraMoves = false ;
71
+ switch (key)
72
+ {
73
+ case ' w' :
74
+ case ' z' :
75
+ {
76
+ g_camera_posX += 0 .5f ;
77
+ cameraMoves = true ;
78
+ break ;
79
+ }
80
+
81
+ case ' s' :
82
+ {
83
+ g_camera_posX -= 0 .5f ;
84
+ cameraMoves = true ;
85
+ break ;
86
+ }
87
+
88
+ default :
89
+ break ;
90
+ }
91
+
92
+ if ( cameraMoves )
93
+ {
94
+ CHECK ( rprCameraLookAt (g_camera, g_camera_posX, 5 , 20 , 0 , 1 , 0 , 0 , 1 , 0 ) );
95
+
96
+ // camera moved, so we need to redraw the framebuffer.
97
+ CHECK ( rprFrameBufferClear (g_frame_buffer) );
98
+ }
99
+ }
100
+
61
101
void Display ()
62
102
{
63
103
// Render FR image into the GL texture
64
104
rprContextRender (g_context);
65
- rprContextResolveFrameBuffer (g_context, g_frame_buffer, g_frame_buffer_2, false ); // Resolve normalization postprocess
105
+ rprContextResolveFrameBuffer (g_context, g_frame_buffer, g_frame_buffer_2, true );
66
106
67
107
// Clear backbuffer
68
108
glClear (GL_COLOR_BUFFER_BIT);
@@ -84,6 +124,16 @@ void Display()
84
124
glActiveTexture (GL_TEXTURE0);
85
125
glBindTexture (GL_TEXTURE_2D, g_texture);
86
126
127
+
128
+
129
+ CHECK (rprFrameBufferGetInfo (g_frame_buffer_2, RPR_FRAMEBUFFER_DATA, WINDOW_WIDTH*WINDOW_HEIGHT*sizeof (float )*4 , g_fbdata, NULL ));
130
+
131
+ // glBindTexture(GL_TEXTURE_2D, g_texture);
132
+ glTexSubImage2D (GL_TEXTURE_2D, 0 , 0 , 0 , WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_FLOAT, static_cast <const GLvoid*>(g_fbdata));
133
+ // glBindTexture(GL_TEXTURE_2D, 0);
134
+
135
+
136
+
87
137
GLuint position_attr_id = glGetAttribLocation (program, " inPosition" );
88
138
GLuint texcoord_attr_id = glGetAttribLocation (program, " inTexcoord" );
89
139
@@ -163,6 +213,24 @@ void InitGraphics()
163
213
glBindTexture (GL_TEXTURE_2D, 0 );
164
214
}
165
215
216
+ void OnExit ()
217
+ {
218
+ // // Release the stuff we created
219
+ std::cout <<" Release memory...\n " ;
220
+
221
+ CHECK (rprObjectDelete (g_scene));g_scene=nullptr ;
222
+ CHECK (rprObjectDelete (g_cube));g_cube=nullptr ;
223
+ CHECK (rprObjectDelete (g_camera));g_camera=nullptr ;
224
+ CHECK (rprObjectDelete (g_diffuse));g_diffuse=nullptr ;
225
+ CHECK (rprObjectDelete (g_plane));g_plane=nullptr ;
226
+ CHECK (rprObjectDelete (g_matsys));g_matsys=nullptr ;
227
+ CHECK (rprObjectDelete (g_light));g_light=nullptr ;
228
+ CHECK (rprObjectDelete (g_frame_buffer));g_frame_buffer=nullptr ;
229
+ CHECK (rprObjectDelete (g_frame_buffer_2));g_frame_buffer_2=nullptr ;
230
+ CheckNoLeak (g_context);
231
+ CHECK (rprObjectDelete (g_context));g_context=nullptr ; // Always delete the RPR Context in last.
232
+ delete[] g_fbdata; g_fbdata=nullptr ;
233
+ }
166
234
167
235
int main (int argc, char ** argv)
168
236
{
@@ -210,39 +278,33 @@ int main(int argc, char** argv)
210
278
std::cout << " Context successfully created.\n " ;
211
279
212
280
// Create a scene
213
- rpr_scene scene=nullptr ;
214
- CHECK ( rprContextCreateScene (g_context, &scene) );
281
+ CHECK ( rprContextCreateScene (g_context, &g_scene) );
215
282
216
283
// Create point light
217
284
{
218
285
CHECK ( rprContextCreatePointLight (g_context, &g_light) );
219
286
RadeonProRender::matrix lightm = RadeonProRender::translation (RadeonProRender::float3 (0 , 8 , 2 ));
220
287
CHECK (rprLightSetTransform (g_light, RPR_TRUE, &lightm.m00 ));
221
- CHECK ( rprSceneAttachLight (scene , g_light) );
288
+ CHECK ( rprSceneAttachLight (g_scene , g_light) );
222
289
CHECK (rprPointLightSetRadiantPower3f (g_light, 255 , 241 , 224 ));
223
290
}
224
291
225
292
// Create camera
226
- rpr_camera camera=nullptr ;
227
293
{
228
- CHECK ( rprContextCreateCamera (g_context, &camera ) );
294
+ CHECK ( rprContextCreateCamera (g_context, &g_camera ) );
229
295
230
296
// Position camera in world space:
231
- // Camera position is (5,5,20)
232
- // Camera aimed at (0,0,0)
233
- // Camera up vector is (0,1,0)
234
- CHECK ( rprCameraLookAt (camera, 0 , 5 , 20 , 0 , 1 , 0 , 0 , 1 , 0 ) );
297
+ CHECK ( rprCameraLookAt (g_camera, g_camera_posX, 5 , 20 , 0 , 1 , 0 , 0 , 1 , 0 ) );
235
298
236
- CHECK ( rprCameraSetFocalLength (camera , 75 .f ) );
299
+ CHECK ( rprCameraSetFocalLength (g_camera , 75 .f ) );
237
300
238
301
// Set camera for the scene
239
- CHECK ( rprSceneSetCamera (scene, camera ) );
302
+ CHECK ( rprSceneSetCamera (g_scene, g_camera ) );
240
303
}
241
304
// Set scene to render for the context
242
- CHECK ( rprContextSetScene (g_context, scene ) );
305
+ CHECK ( rprContextSetScene (g_context, g_scene ) );
243
306
244
307
// Create cube mesh
245
- rpr_shape cube=nullptr ;
246
308
{
247
309
CHECK (rprContextCreateMesh (g_context,
248
310
(rpr_float const *)&cube_data[0 ], 24 , sizeof (vertex),
@@ -251,19 +313,18 @@ int main(int argc, char** argv)
251
313
(rpr_int const *)indices, sizeof (rpr_int),
252
314
(rpr_int const *)indices, sizeof (rpr_int),
253
315
(rpr_int const *)indices, sizeof (rpr_int),
254
- num_face_vertices, 12 , &cube ));
316
+ num_face_vertices, 12 , &g_cube ));
255
317
256
318
// Add cube into the scene
257
- CHECK (rprSceneAttachShape (scene, cube ));
319
+ CHECK (rprSceneAttachShape (g_scene, g_cube ));
258
320
259
321
// Create a transform: -2 unit along X axis and 1 unit up Y axis
260
322
RadeonProRender::matrix m = RadeonProRender::translation (RadeonProRender::float3 (-2 , 1 , 0 ));
261
323
262
324
// Set the transform
263
- CHECK (rprShapeSetTransform (cube , RPR_TRUE, &m.m00 ));
325
+ CHECK (rprShapeSetTransform (g_cube , RPR_TRUE, &m.m00 ));
264
326
}
265
327
// Create plane mesh
266
- rpr_shape plane=nullptr ;
267
328
{
268
329
CHECK (rprContextCreateMesh (g_context,
269
330
(rpr_float const *)&plane_data[0 ], 4 , sizeof (vertex),
@@ -272,81 +333,58 @@ int main(int argc, char** argv)
272
333
(rpr_int const *)indices, sizeof (rpr_int),
273
334
(rpr_int const *)indices, sizeof (rpr_int),
274
335
(rpr_int const *)indices, sizeof (rpr_int),
275
- num_face_vertices, 2 , &plane ));
336
+ num_face_vertices, 2 , &g_plane ));
276
337
277
338
// Add plane into the scene
278
- CHECK (rprSceneAttachShape (scene, plane ));
339
+ CHECK (rprSceneAttachShape (g_scene, g_plane ));
279
340
}
341
+
280
342
// Create simple diffuse shader
281
- rpr_material_node diffuse=nullptr ;
282
343
{
283
- CHECK (rprMaterialSystemCreateNode (g_matsys, RPR_MATERIAL_NODE_DIFFUSE, &diffuse ));
344
+ CHECK (rprMaterialSystemCreateNode (g_matsys, RPR_MATERIAL_NODE_DIFFUSE, &g_diffuse ));
284
345
285
346
// Set diffuse color parameter to gray
286
- CHECK (rprMaterialNodeSetInputFByKey (diffuse , RPR_MATERIAL_INPUT_COLOR, 0 .5f , 0 .5f , 0 .5f , 1 .f ));
347
+ CHECK (rprMaterialNodeSetInputFByKey (g_diffuse , RPR_MATERIAL_INPUT_COLOR, 0 .5f , 0 .5f , 0 .5f , 1 .f ));
287
348
288
349
// Set shader for cube & plane meshes
289
- CHECK (rprShapeSetMaterial (cube, diffuse ));
350
+ CHECK (rprShapeSetMaterial (g_cube, g_diffuse ));
290
351
291
- CHECK (rprShapeSetMaterial (plane, diffuse ));
352
+ CHECK (rprShapeSetMaterial (g_plane, g_diffuse ));
292
353
}
293
354
294
355
// Create framebuffer to store rendering result
295
- rpr_framebuffer_desc desc = { 800 , 600 };
356
+ rpr_framebuffer_desc desc = { WINDOW_WIDTH,WINDOW_HEIGHT };
296
357
297
358
// 4 component 32-bit float value each
298
359
rpr_framebuffer_format fmt = { 4 , RPR_COMPONENT_TYPE_FLOAT32 };
299
360
CHECK (rprContextCreateFrameBuffer (g_context, fmt, &desc, &g_frame_buffer));
300
-
301
- // Clear framebuffer to black color
302
- CHECK (rprFrameBufferClear (g_frame_buffer));
361
+ CHECK (rprContextCreateFrameBuffer (g_context, fmt, &desc, &g_frame_buffer_2));
303
362
304
363
// Set framebuffer for the context
305
364
CHECK (rprContextSetAOV (g_context, RPR_AOV_COLOR, g_frame_buffer));
306
365
307
- // Check 40_postprocess tutorial for thoses line
308
- // need to normalize before bloom
309
- rpr_post_effect normalizationEff = 0 ;
310
- CHECK (rprContextCreatePostEffect (g_context, RPR_POST_EFFECT_NORMALIZATION, &normalizationEff));
311
- CHECK (rprContextAttachPostEffect (g_context, normalizationEff));
312
-
313
366
CHECK ( rprContextSetParameterByKey1u (g_context,RPR_CONTEXT_PREVIEW, 1u ) );
314
367
315
- // ///////// GL Interop Tutorial //////////
316
-
317
- // 1/ You must init GL
318
- // 2/ You must create the rpr_context with RPR_CREATION_FLAGS_ENABLE_GL_INTEROP
319
- // 3/ Create a GL_TEXTURE_2D
320
- // 4/ Create a FrameBuffer from GLTexture2D rprContextCreateFramebufferFromGLTexture2D from RadeonProRender_GL.h
321
-
322
- // Create GL Interop framebuffer to store rendering result
323
- CHECK (rprContextCreateFramebufferFromGLTexture2D (g_context, GL_TEXTURE_2D, 0 , g_texture, &g_frame_buffer_2));
324
-
325
- // Clear framebuffer to black color
326
- CHECK (rprFrameBufferClear (g_frame_buffer_2));
327
-
328
368
// Set framebuffer for the context
329
369
CHECK (rprContextSetAOV (g_context, RPR_AOV_COLOR, g_frame_buffer));
330
370
371
+ g_fbdata = new float [WINDOW_WIDTH * WINDOW_HEIGHT * 4 ];
372
+
373
+
374
+ std::cout << " Press W or S to move the camera.\n " ;
331
375
332
376
glutDisplayFunc (Display);
333
377
glutIdleFunc (Update);
378
+ glutKeyboardFunc (OnKeyboardEvent);
334
379
glutMainLoop ();
335
380
336
- // // Release the stuff we created
337
- // CHECK(rprObjectDelete(matsys));matsys=nullptr;
338
- // CHECK(rprObjectDelete(plane));plane=nullptr;
339
- // CHECK(rprObjectDelete(cube));cube=nullptr;
340
- // CHECK(rprObjectDelete(light));light=nullptr;
341
- // CHECK(rprObjectDelete(diffuse));diffuse=nullptr;
342
- // CHECK(rprObjectDelete(scene));scene=nullptr;
343
- // CHECK(rprObjectDelete(camera));camera=nullptr;
344
- // CHECK(rprObjectDelete(frame_buffer));frame_buffer=nullptr;
345
- // CheckNoLeak(context);
346
- // CHECK(rprObjectDelete(context));context=nullptr; // Always delete the RPR Context in last.
381
+
382
+ // this should be called when the OpenGL Application closes
383
+ // ( note that it may not be called, as GLUT doesn't have "Exit" callback )
384
+ OnExit ();
385
+
386
+
347
387
return 0 ;
348
388
}
349
389
350
390
351
- // Things to try in this tutorial:
352
- // 1) You could try to test a CPU copy with rprFrameBufferGetInfo RPR_FRAMEBUFFER_DATA and then upload it with gl call
0 commit comments