Skip to content

Commit cfd125a

Browse files
committed
Tutorials - add 2-sided material demo
1 parent b86232b commit cfd125a

File tree

5 files changed

+223
-0
lines changed

5 files changed

+223
-0
lines changed

tutorials/23_twosided/main.cpp

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*****************************************************************************\
2+
*
3+
* Module Name Two Sided Material Demo
4+
* Project Radeon ProRender rendering tutorial
5+
*
6+
* Description Radeon ProRender SDK tutorials
7+
*
8+
* Copyright(C) 2011-2021 Advanced Micro Devices, Inc. All rights reserved.
9+
*
10+
\*****************************************************************************/
11+
12+
#include "RadeonProRender.h"
13+
#include "Math/mathutils.h"
14+
#include "../common/common.h"
15+
#include <cassert>
16+
#include <iostream>
17+
18+
//
19+
// This demo illustrates how to define different materials on the front and back face of mesh faces.
20+
//
21+
22+
int main()
23+
{
24+
// for Debugging you can enable Radeon ProRender API trace
25+
// set this before any RPR API calls
26+
// rprContextSetParameterByKey1u(0,RPR_CONTEXT_TRACING_ENABLED,1);
27+
28+
// the RPR context object.
29+
rpr_context context = nullptr;
30+
31+
// Register the RPR DLL
32+
rpr_int tahoePluginID = rprRegisterPlugin(RPR_PLUGIN_FILE_NAME);
33+
CHECK_NE(tahoePluginID , -1);
34+
rpr_int plugins[] = { tahoePluginID };
35+
size_t pluginCount = sizeof(plugins) / sizeof(plugins[0]);
36+
37+
// Create context using a single GPU
38+
CHECK( rprCreateContext(RPR_API_VERSION, plugins, pluginCount, g_ContextCreationFlags, NULL, NULL, &context) );
39+
40+
// Set the active plugin.
41+
CHECK( rprContextSetActivePlugin(context, plugins[0]) );
42+
43+
std::cout << "RPR Context creation succeeded." << std::endl;
44+
45+
// create the material system
46+
rpr_material_system matsys = nullptr;
47+
CHECK( rprContextCreateMaterialSystem(context, 0, &matsys) );
48+
49+
// Create a scene.
50+
rpr_scene scene = nullptr;
51+
CHECK( rprContextCreateScene(context, &scene) ); // create the scene
52+
CHECK( rprContextSetScene(context, scene) ); // set this scene as the "active" scene used for rendering.
53+
54+
55+
// Create the camera
56+
rpr_camera camera = nullptr;
57+
{
58+
// create the camera
59+
CHECK( rprContextCreateCamera(context, &camera) );
60+
61+
// camera position in world space:
62+
CHECK( rprCameraLookAt(camera, 5, 5, 7, 0, 0, 0, 0, 1, 0) );
63+
64+
// attach the camera to the scene.
65+
CHECK( rprSceneSetCamera(scene, camera) );
66+
}
67+
68+
// Create a simple point light
69+
rpr_light light=nullptr;
70+
{
71+
CHECK(rprContextCreatePointLight(context, &light));
72+
73+
// Set transform for the light
74+
RadeonProRender::matrix lightm = RadeonProRender::translation(RadeonProRender::float3(8, 20, 8));
75+
CHECK(rprLightSetTransform(light, RPR_TRUE, &lightm.m00));
76+
77+
// Set light power
78+
CHECK(rprPointLightSetRadiantPower3f(light, 1800, 1800, 1800));
79+
80+
// Attach the light to the scene
81+
CHECK(rprSceneAttachLight(scene, light));
82+
}
83+
84+
85+
// add a new shape representing the floor of the scene.
86+
rpr_shape plane = nullptr;
87+
{
88+
CHECK(rprContextCreateMesh(context,
89+
(rpr_float const*)&plane_data[0], 4, sizeof(vertex),
90+
(rpr_float const*)((char*)&plane_data[0] + sizeof(rpr_float) * 3), 4, sizeof(vertex),
91+
(rpr_float const*)((char*)&plane_data[0] + sizeof(rpr_float) * 6), 4, sizeof(vertex),
92+
(rpr_int const*)indices, sizeof(rpr_int),
93+
(rpr_int const*)indices, sizeof(rpr_int),
94+
(rpr_int const*)indices, sizeof(rpr_int),
95+
num_face_vertices, 2, &plane));
96+
97+
// Add plane into the scene
98+
CHECK(rprSceneAttachShape(scene, plane));
99+
}
100+
101+
// set a material on the floor
102+
rpr_material_node diffuseMat=nullptr;
103+
{
104+
CHECK(rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_DIFFUSE, &diffuseMat));
105+
CHECK(rprMaterialNodeSetInputFByKey(diffuseMat, RPR_MATERIAL_INPUT_COLOR, 1.0f, 1.0f, 1.0f, 0.0f));
106+
CHECK(rprShapeSetMaterial(plane, diffuseMat));
107+
}
108+
109+
// create "face A" shape facing the camera
110+
rpr_shape faceA = nullptr;
111+
{
112+
CHECK(rprContextCreateInstance(context,plane,&faceA));
113+
114+
// Create a transform:
115+
RadeonProRender::matrix m =
116+
RadeonProRender::translation(RadeonProRender::float3(0, 1.2f, 1.0f)) *
117+
RadeonProRender::rotation_x(MY_PI/2.0f) *
118+
RadeonProRender::scale(RadeonProRender::float3(0.0667f, 0.0667f, 0.0667f));
119+
120+
// Set the transform
121+
CHECK(rprShapeSetTransform(faceA, RPR_TRUE, &m.m00));
122+
123+
CHECK(rprSceneAttachShape(scene, faceA));
124+
}
125+
126+
// create "face B" shape back-facing the camera
127+
rpr_shape faceB = nullptr;
128+
{
129+
CHECK(rprContextCreateInstance(context,plane,&faceB));
130+
131+
// Create a transform:
132+
RadeonProRender::matrix m =
133+
RadeonProRender::translation(RadeonProRender::float3(1.0f, 1.2f, 0)) *
134+
RadeonProRender::rotation_z(MY_PI/2.0f) *
135+
RadeonProRender::scale(RadeonProRender::float3(0.0667f, 0.0667f, 0.0667f));
136+
137+
// Set the transform
138+
CHECK(rprShapeSetTransform(faceB, RPR_TRUE, &m.m00));
139+
140+
CHECK(rprSceneAttachShape(scene, faceB));
141+
}
142+
143+
// material used for front face
144+
rpr_material_node materialA = NULL;
145+
CHECK( rprMaterialSystemCreateNode(matsys,RPR_MATERIAL_NODE_DIFFUSE,&materialA));
146+
CHECK( rprMaterialNodeSetInputFByKey(materialA, RPR_MATERIAL_INPUT_COLOR, 1.0f, 0.2f, 0.1f, 0.0f));
147+
148+
// material used for back face
149+
rpr_material_node materialB = NULL;
150+
CHECK( rprMaterialSystemCreateNode(matsys,RPR_MATERIAL_NODE_DIFFUSE,&materialB));
151+
CHECK( rprMaterialNodeSetInputFByKey(materialB, RPR_MATERIAL_INPUT_COLOR, 0.3f, 0.8f, 1.0f, 0.0f));
152+
153+
// two sided material taking the front and back materials
154+
rpr_material_node material2sides = NULL;
155+
CHECK( rprMaterialSystemCreateNode(matsys,RPR_MATERIAL_NODE_TWOSIDED,&material2sides));
156+
CHECK( rprMaterialNodeSetInputNByKey(material2sides, RPR_MATERIAL_INPUT_FRONTFACE, materialA));
157+
CHECK( rprMaterialNodeSetInputNByKey(material2sides, RPR_MATERIAL_INPUT_BACKFACE, materialB));
158+
159+
// apply the 2-sided material on both shapes.
160+
CHECK( rprShapeSetMaterial(faceA, material2sides));
161+
CHECK( rprShapeSetMaterial(faceB, material2sides));
162+
163+
// Create framebuffer to store rendering result
164+
rpr_framebuffer_desc desc = { 800 , 600 }; // resolution in pixels
165+
rpr_framebuffer_format fmt = {4, RPR_COMPONENT_TYPE_FLOAT32}; // format: 4 component 32-bit float value each
166+
rpr_framebuffer frame_buffer = nullptr;
167+
rpr_framebuffer frame_buffer_resolved = nullptr;
168+
CHECK( rprContextCreateFrameBuffer(context, fmt, &desc, &frame_buffer) );
169+
CHECK( rprContextCreateFrameBuffer(context, fmt, &desc, &frame_buffer_resolved) );
170+
CHECK( rprContextSetAOV(context, RPR_AOV_COLOR, frame_buffer) ); // attach 'frame_buffer' to the Color AOV ( this is the main AOV for final rendering )
171+
172+
// Execute the rendering.
173+
CHECK(rprContextSetParameterByKey1u(context,RPR_CONTEXT_ITERATIONS, 100));
174+
CHECK( rprContextRender(context) );
175+
CHECK(rprContextResolveFrameBuffer(context,frame_buffer,frame_buffer_resolved,true));
176+
CHECK(rprFrameBufferSaveToFile(frame_buffer_resolved,"23_00.png"));
177+
std::cout << "rendering finished." << std::endl;
178+
179+
// Release the stuff we created
180+
CHECK(rprObjectDelete(diffuseMat)); diffuseMat=nullptr;
181+
CHECK(rprObjectDelete(frame_buffer)); frame_buffer=nullptr;
182+
CHECK(rprObjectDelete(frame_buffer_resolved)); frame_buffer_resolved=nullptr;
183+
CHECK(rprObjectDelete(materialA)); materialA=nullptr;
184+
CHECK(rprObjectDelete(materialB)); materialB=nullptr;
185+
CHECK(rprObjectDelete(material2sides)); material2sides=nullptr;
186+
CHECK(rprObjectDelete(faceA)); faceA=nullptr;
187+
CHECK(rprObjectDelete(faceB)); faceB=nullptr;
188+
CHECK(rprObjectDelete(plane)); plane=nullptr;
189+
CHECK(rprObjectDelete(light)); light=nullptr;
190+
CHECK(rprObjectDelete(camera)); camera=nullptr;
191+
CHECK(rprObjectDelete(scene)); scene=nullptr;
192+
CHECK(rprObjectDelete(matsys)); matsys=nullptr;
193+
CheckNoLeak(context);
194+
CHECK(rprObjectDelete(context)); context=nullptr;
195+
return 0;
196+
197+
}
198+
199+

tutorials/23_twosided/premake4.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
project "23_twosided"
2+
kind "ConsoleApp"
3+
location "../build"
4+
files { "../23_twosided/**.h", "../23_twosided/**.cpp"}
5+
files { "../common/common.cpp","../common/common.h"}
6+
7+
-- remove filters for Visual Studio
8+
vpaths { [""] = { "../23_twosided/**.h", "../23_twosided/**.cpp","../common/common.cpp","../common/common.h"} }
9+
10+
includedirs{ "../../RadeonProRender/inc" }
11+
12+
buildoptions "-std=c++11"
13+
14+
configuration {"x64"}
15+
links {"RadeonProRender64"}
16+
17+
configuration {"x64", "Debug"}
18+
targetdir "../Bin"
19+
configuration {"x64", "Release"}
20+
targetdir "../Bin"
21+
configuration {}
22+

tutorials/23_twosided/screenshot.png

13.6 KB
Loading

tutorials/premake5.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ solution "Tutorials"
7878
include "13_deformation_motion_blur"
7979
include "21_material"
8080
include "22_material_uber"
81+
include "23_twosided"
8182
include "30_tiled_render"
8283
include "31_framebuffer_access"
8384
include "32_gl_interop"

tutorials/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ List of tutorials in this SDK
1010
| [Deformation Motion Blur](13_deformation_motion_blur) | ![](13_deformation_motion_blur/screenshot.png) | This demo covers vertices deformation over time for a blur effect. |
1111
| [Material](21_material) | ![](21_material/screenshot.png) | This demo illustrates how to create materials in RPR. It's not covering the UBER material which is used inside the 22_material_uber demo. |
1212
| [Uber Material](22_material_uber) | ![](22_material_uber/screenshot.png) | This demo illustrates examples for the UBER ( RPR_MATERIAL_NODE_UBERV2 ) material. |
13+
| [Two Sided materials](23_twosided) | ![](23_twosided/screenshot.png) | This demo illustrates how to define different materials on the front and back face of mesh faces. |
1314
| [Tiled Render](30_tiled_render) | ![](30_tiled_render/screenshot.png) | This demo illustrates how to break down the framebuffer into smaller render regions (tiles) |
1415
| [Framebuffer access](31_framebuffer_access) | ![](31_framebuffer_access/screenshot.png) | Here we demonstrate usage of rprFrameBufferGetInfo: We can access the image data of a frame buffer. In this demo, we use the data of a first rendering and use it as an input texture for a second rendering. |
1516
| [OpenGL interop](32_gl_interop) | ![](32_gl_interop/screenshot.png) | Demo covering an RPR rendering inside an OpenGL app. |

0 commit comments

Comments
 (0)