Skip to content

Commit de2ef08

Browse files
committed
fix the OpenGL Demo for Northstar
1 parent 8772afa commit de2ef08

File tree

1 file changed

+99
-61
lines changed

1 file changed

+99
-61
lines changed

tutorials/32_gl_interop/main.cpp

Lines changed: 99 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,58 @@ rpr_material_system g_matsys = NULL;
5151
rpr_light g_light = NULL;
5252
rpr_framebuffer g_frame_buffer_2 = NULL;
5353
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;
5461

5562
void Update()
5663
{
5764
// Send update event
5865
glutPostRedisplay();
5966
}
6067

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+
61101
void Display()
62102
{
63103
// Render FR image into the GL texture
64104
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);
66106

67107
// Clear backbuffer
68108
glClear(GL_COLOR_BUFFER_BIT);
@@ -84,6 +124,16 @@ void Display()
84124
glActiveTexture(GL_TEXTURE0);
85125
glBindTexture(GL_TEXTURE_2D, g_texture);
86126

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+
87137
GLuint position_attr_id = glGetAttribLocation(program, "inPosition");
88138
GLuint texcoord_attr_id = glGetAttribLocation(program, "inTexcoord");
89139

@@ -163,6 +213,24 @@ void InitGraphics()
163213
glBindTexture(GL_TEXTURE_2D, 0);
164214
}
165215

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+
}
166234

167235
int main(int argc, char** argv)
168236
{
@@ -210,39 +278,33 @@ int main(int argc, char** argv)
210278
std::cout << "Context successfully created.\n";
211279

212280
// Create a scene
213-
rpr_scene scene=nullptr;
214-
CHECK( rprContextCreateScene(g_context, &scene) );
281+
CHECK( rprContextCreateScene(g_context, &g_scene) );
215282

216283
// Create point light
217284
{
218285
CHECK( rprContextCreatePointLight(g_context, &g_light) );
219286
RadeonProRender::matrix lightm = RadeonProRender::translation(RadeonProRender::float3(0, 8, 2));
220287
CHECK(rprLightSetTransform(g_light, RPR_TRUE, &lightm.m00));
221-
CHECK( rprSceneAttachLight(scene, g_light) );
288+
CHECK( rprSceneAttachLight(g_scene, g_light) );
222289
CHECK(rprPointLightSetRadiantPower3f(g_light, 255, 241, 224));
223290
}
224291

225292
// Create camera
226-
rpr_camera camera=nullptr;
227293
{
228-
CHECK( rprContextCreateCamera(g_context, &camera) );
294+
CHECK( rprContextCreateCamera(g_context, &g_camera) );
229295

230296
// 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) );
235298

236-
CHECK( rprCameraSetFocalLength(camera, 75.f) );
299+
CHECK( rprCameraSetFocalLength(g_camera, 75.f) );
237300

238301
// Set camera for the scene
239-
CHECK( rprSceneSetCamera(scene, camera) );
302+
CHECK( rprSceneSetCamera(g_scene, g_camera) );
240303
}
241304
// Set scene to render for the context
242-
CHECK( rprContextSetScene(g_context, scene) );
305+
CHECK( rprContextSetScene(g_context, g_scene) );
243306

244307
// Create cube mesh
245-
rpr_shape cube=nullptr;
246308
{
247309
CHECK(rprContextCreateMesh(g_context,
248310
(rpr_float const*)&cube_data[0], 24, sizeof(vertex),
@@ -251,19 +313,18 @@ int main(int argc, char** argv)
251313
(rpr_int const*)indices, sizeof(rpr_int),
252314
(rpr_int const*)indices, sizeof(rpr_int),
253315
(rpr_int const*)indices, sizeof(rpr_int),
254-
num_face_vertices, 12, &cube));
316+
num_face_vertices, 12, &g_cube));
255317

256318
// Add cube into the scene
257-
CHECK(rprSceneAttachShape(scene, cube));
319+
CHECK(rprSceneAttachShape(g_scene, g_cube));
258320

259321
// Create a transform: -2 unit along X axis and 1 unit up Y axis
260322
RadeonProRender::matrix m = RadeonProRender::translation(RadeonProRender::float3(-2, 1, 0));
261323

262324
// Set the transform
263-
CHECK(rprShapeSetTransform(cube, RPR_TRUE, &m.m00));
325+
CHECK(rprShapeSetTransform(g_cube, RPR_TRUE, &m.m00));
264326
}
265327
// Create plane mesh
266-
rpr_shape plane=nullptr;
267328
{
268329
CHECK(rprContextCreateMesh(g_context,
269330
(rpr_float const*)&plane_data[0], 4, sizeof(vertex),
@@ -272,81 +333,58 @@ int main(int argc, char** argv)
272333
(rpr_int const*)indices, sizeof(rpr_int),
273334
(rpr_int const*)indices, sizeof(rpr_int),
274335
(rpr_int const*)indices, sizeof(rpr_int),
275-
num_face_vertices, 2, &plane));
336+
num_face_vertices, 2, &g_plane));
276337

277338
// Add plane into the scene
278-
CHECK(rprSceneAttachShape(scene, plane));
339+
CHECK(rprSceneAttachShape(g_scene, g_plane));
279340
}
341+
280342
// Create simple diffuse shader
281-
rpr_material_node diffuse=nullptr;
282343
{
283-
CHECK(rprMaterialSystemCreateNode(g_matsys, RPR_MATERIAL_NODE_DIFFUSE, &diffuse));
344+
CHECK(rprMaterialSystemCreateNode(g_matsys, RPR_MATERIAL_NODE_DIFFUSE, &g_diffuse));
284345

285346
// 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));
287348

288349
// Set shader for cube & plane meshes
289-
CHECK(rprShapeSetMaterial(cube, diffuse));
350+
CHECK(rprShapeSetMaterial(g_cube, g_diffuse));
290351

291-
CHECK(rprShapeSetMaterial(plane, diffuse));
352+
CHECK(rprShapeSetMaterial(g_plane, g_diffuse));
292353
}
293354

294355
// Create framebuffer to store rendering result
295-
rpr_framebuffer_desc desc = { 800,600 };
356+
rpr_framebuffer_desc desc = { WINDOW_WIDTH,WINDOW_HEIGHT };
296357

297358
// 4 component 32-bit float value each
298359
rpr_framebuffer_format fmt = { 4, RPR_COMPONENT_TYPE_FLOAT32 };
299360
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));
303362

304363
// Set framebuffer for the context
305364
CHECK(rprContextSetAOV(g_context, RPR_AOV_COLOR, g_frame_buffer));
306365

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-
313366
CHECK( rprContextSetParameterByKey1u(g_context,RPR_CONTEXT_PREVIEW, 1u ) );
314367

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-
328368
// Set framebuffer for the context
329369
CHECK(rprContextSetAOV(g_context, RPR_AOV_COLOR, g_frame_buffer));
330370

371+
g_fbdata = new float[WINDOW_WIDTH * WINDOW_HEIGHT * 4];
372+
373+
374+
std::cout << "Press W or S to move the camera.\n";
331375

332376
glutDisplayFunc(Display);
333377
glutIdleFunc(Update);
378+
glutKeyboardFunc(OnKeyboardEvent);
334379
glutMainLoop();
335380

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+
347387
return 0;
348388
}
349389

350390

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

Comments
 (0)