Skip to content

Commit 2695970

Browse files
committed
Demo - add Material Per Face
1 parent f01802b commit 2695970

File tree

9 files changed

+269
-17
lines changed

9 files changed

+269
-17
lines changed

Resources/Textures/flower.jpg

170 KB
Loading

tutorials/29_ocio/compare.jpg

1.14 KB
Loading

tutorials/29_ocio/main.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@
4242
// simple garbage collector for RPR objects.
4343
RPRGarbageCollector g_gc;
4444

45-
// vertices data
46-
struct vertexP3N3T2
47-
{
48-
rpr_float pos[3];
49-
rpr_float norm[3];
50-
rpr_float tex[2];
51-
};
52-
5345

5446
// Convert Color Space :
5547
// From Linear sRGB to ACEScg
@@ -80,7 +72,7 @@ RadeonProRender::float4 sRGB_to_ACEScg(const RadeonProRender::float4& srgb)
8072

8173

8274
// create a simple quad shape
83-
rpr_shape CreateQuad(rpr_context context, rpr_scene scene, vertexP3N3T2* meshVertices, unsigned int meshVertices_nbOfElement )
75+
rpr_shape CreateQuad(rpr_context context, rpr_scene scene, vertex* meshVertices, unsigned int meshVertices_nbOfElement )
8476
{
8577
rpr_int indices[] = { 3,2,1,0, };
8678
rpr_int num_face_vertices[] = { 4, };
@@ -90,9 +82,9 @@ rpr_shape CreateQuad(rpr_context context, rpr_scene scene, vertexP3N3T2* meshVer
9082
rpr_shape mesh = nullptr;
9183

9284
CHECK( rprContextCreateMesh(context,
93-
(rpr_float const*)&meshVertices[0], meshVertices_nbOfElement , sizeof(vertexP3N3T2),
94-
(rpr_float const*)((char*)&meshVertices[0] + sizeof(rpr_float)*3), meshVertices_nbOfElement, sizeof(vertexP3N3T2),
95-
(rpr_float const*)((char*)&meshVertices[0] + sizeof(rpr_float)*6), meshVertices_nbOfElement, sizeof(vertexP3N3T2),
85+
(rpr_float const*)&meshVertices[0], meshVertices_nbOfElement , sizeof(vertex),
86+
(rpr_float const*)((char*)&meshVertices[0] + sizeof(rpr_float)*3), meshVertices_nbOfElement, sizeof(vertex),
87+
(rpr_float const*)((char*)&meshVertices[0] + sizeof(rpr_float)*6), meshVertices_nbOfElement, sizeof(vertex),
9688
(rpr_int const*)indices, sizeof(rpr_int),
9789
(rpr_int const*)indices, sizeof(rpr_int),
9890
(rpr_int const*)indices, sizeof(rpr_int),
@@ -106,7 +98,7 @@ rpr_shape CreateQuad(rpr_context context, rpr_scene scene, vertexP3N3T2* meshVer
10698
// Create a Quad shape on the YZ plane
10799
rpr_shape CreateQuad_YZ(rpr_context context, rpr_scene scene, float ax, float ay, float bx, float by, float X)
108100
{
109-
vertexP3N3T2 meshVertices[] =
101+
vertex meshVertices[] =
110102
{
111103
{ X, ax, by, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
112104
{ X, bx, by, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
@@ -123,7 +115,7 @@ rpr_shape CreateQuad_YZ(rpr_context context, rpr_scene scene, float ax, float ay
123115
// Create a Quad shape on the XZ plane
124116
rpr_shape CreateQuad_XZ(rpr_context context, rpr_scene scene, float ax, float ay, float bx, float by, float Y, float normal)
125117
{
126-
vertexP3N3T2 meshVertices[] =
118+
vertex meshVertices[] =
127119
{
128120
{ ax, Y, by, 0.0f, normal, 0.0f, 0.0f, 0.0f },
129121
{ bx, Y, by, 0.0f, normal, 0.0f, 1.0f, 0.0f },
@@ -140,7 +132,7 @@ rpr_shape CreateQuad_XZ(rpr_context context, rpr_scene scene, float ax, float ay
140132
// Create a Quad shape on the XY plane
141133
rpr_shape CreateQuad_XY(rpr_context context, rpr_scene scene, float ax, float ay, float bx, float by, float Z)
142134
{
143-
vertexP3N3T2 meshVertices[] =
135+
vertex meshVertices[] =
144136
{
145137
{ ax, by, Z, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
146138
{ bx, by, Z, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f },
@@ -415,6 +407,14 @@ int main()
415407
// This means the texture is using the sRGB color space with a gamma correction.
416408
CHECK( rprImageSetOcioColorspace(img , "Utility - sRGB - Texture") );
417409
}
410+
else
411+
{
412+
// art.jpg image is stored in the non-linear sRGB space.
413+
// if we are not using the OCIO workflow, then we need to set the gamma for non-linear images ( like most of .png, .jpg... representing color )
414+
// This gamma value is used to do a basic IDT operation, transforming the image from non-linear "Texture Colorspace" to a linear "Render Colorspace".
415+
// In our case: from "non-linear sRGB" to "linear sRGB"
416+
CHECK( rprImageSetGamma(img, 2.2) );
417+
}
418418

419419
CHECK( rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_IMAGE_TEXTURE, &imgSampler));
420420
g_gc.GCAdd(imgSampler);
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*****************************************************************************\
2+
*
3+
* Module Name Per Face Material Demo
4+
* Project Radeon ProRender rendering tutorial
5+
*
6+
* Description Radeon ProRender SDK tutorials
7+
*
8+
* Copyright(C) 2011-2022 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+
// Demo of the rprShapeSetMaterialFaces API. This allows to set materials for specific faces of the shape.
20+
//
21+
22+
// garbage collector of this demo
23+
RPRGarbageCollector g_gc;
24+
25+
int main()
26+
{
27+
// for Debugging you can enable Radeon ProRender API trace
28+
// set this before any RPR API calls
29+
// rprContextSetParameterByKey1u(0,RPR_CONTEXT_TRACING_ENABLED,1);
30+
31+
// the RPR context object.
32+
rpr_context context = nullptr;
33+
34+
// Register the RPR DLL
35+
rpr_int tahoePluginID = rprRegisterPlugin(RPR_PLUGIN_FILE_NAME);
36+
CHECK_NE(tahoePluginID , -1);
37+
rpr_int plugins[] = { tahoePluginID };
38+
size_t pluginCount = sizeof(plugins) / sizeof(plugins[0]);
39+
40+
// Create context
41+
CHECK( rprCreateContext(RPR_API_VERSION, plugins, pluginCount, g_ContextCreationFlags, NULL, NULL, &context) );
42+
43+
// Set the active plugin.
44+
CHECK( rprContextSetActivePlugin(context, plugins[0]) );
45+
46+
std::cout << "RPR Context creation succeeded." << std::endl;
47+
48+
// Create the scene.
49+
rpr_scene scene = nullptr;
50+
CHECK( rprContextCreateScene(context, &scene) );
51+
CHECK( rprContextSetScene(context, scene) );
52+
53+
// Create the camera
54+
rpr_camera camera = nullptr;
55+
CHECK( rprContextCreateCamera(context, &camera) );
56+
CHECK( rprCameraLookAt(camera, 0, 10, 12, 0, -2, 0, 0, 1, 0) );
57+
CHECK( rprSceneSetCamera(scene, camera) );
58+
59+
// Create an environment light
60+
CHECK( CreateNatureEnvLight(context, scene, g_gc, 1.0f) );
61+
62+
// create the material system
63+
rpr_material_system matsys = nullptr;
64+
CHECK( rprContextCreateMaterialSystem(context, 0, &matsys) );
65+
66+
67+
68+
// create the shape plane
69+
rpr_shape plane = NULL;
70+
{
71+
72+
const int gridSize = 4; // create a 3x3 grid (4*4 vertices)
73+
vertex plane_data[gridSize*gridSize];
74+
75+
// grid dimensions
76+
const float minGrid = -5.0f;
77+
const float maxGrid = +5.0f;
78+
79+
// create grid
80+
for(int y=0; y<gridSize; y++)
81+
{
82+
for(int x=0; x<gridSize; x++)
83+
{
84+
plane_data[y*gridSize+x].pos[0] = minGrid + (maxGrid-minGrid) / ((float)gridSize-1.0f) * ((float)x);
85+
plane_data[y*gridSize+x].pos[1] = 0.0f;
86+
plane_data[y*gridSize+x].pos[2] = minGrid + (maxGrid-minGrid) / ((float)gridSize-1.0f) * ((float)y);
87+
88+
plane_data[y*gridSize+x].norm[0] = 0.0f;
89+
plane_data[y*gridSize+x].norm[1] = 1.0f;
90+
plane_data[y*gridSize+x].norm[2] = 0.0f;
91+
92+
plane_data[y*gridSize+x].tex[0] = (plane_data[y*gridSize+x].pos[0] - minGrid) / (maxGrid - minGrid);
93+
plane_data[y*gridSize+x].tex[1] = 1.0f - (plane_data[y*gridSize+x].pos[2] - minGrid) / (maxGrid - minGrid);
94+
}
95+
}
96+
97+
// create indices of the grid
98+
rpr_int indices[(gridSize-1)*(gridSize-1)*4];
99+
for(int y=0; y<gridSize-1; y++)
100+
{
101+
for(int x=0; x<gridSize-1; x++)
102+
{
103+
indices[ (x+y*(gridSize-1))*4 + 0 ] = y*gridSize+x ;
104+
indices[ (x+y*(gridSize-1))*4 + 1 ] = y*gridSize+(x+1) ;
105+
indices[ (x+y*(gridSize-1))*4 + 3 ] = (y+1)*gridSize+x ;
106+
indices[ (x+y*(gridSize-1))*4 + 2 ] = (y+1)*gridSize+(x+1) ;
107+
}
108+
}
109+
110+
// create face list
111+
rpr_int num_face_vertices[(gridSize-1)*(gridSize-1)];
112+
for(int y=0; y<gridSize-1; y++)
113+
{
114+
for(int x=0; x<gridSize-1; x++)
115+
{
116+
num_face_vertices[ (x+y*(gridSize-1)) ] = 4; // use quad face: 4 vertices per face.
117+
}
118+
}
119+
120+
CHECK( rprContextCreateMesh(context,
121+
(rpr_float const*)&plane_data[0], gridSize*gridSize, sizeof(vertex),
122+
(rpr_float const*)((char*)&plane_data[0] + sizeof(rpr_float)*3), gridSize*gridSize, sizeof(vertex),
123+
(rpr_float const*)((char*)&plane_data[0] + sizeof(rpr_float)*6), gridSize*gridSize, sizeof(vertex),
124+
(rpr_int const*)indices, sizeof(rpr_int),
125+
(rpr_int const*)indices, sizeof(rpr_int),
126+
(rpr_int const*)indices, sizeof(rpr_int),
127+
num_face_vertices, (gridSize-1)*(gridSize-1), &plane) );
128+
g_gc.GCAdd(plane);
129+
130+
// attach the plane to the scene.
131+
CHECK( rprSceneAttachShape(scene, plane));
132+
133+
}
134+
135+
136+
// create the base material for the plane shape
137+
{
138+
rpr_material_node diffuseMaterial = nullptr;
139+
rpr_image image2 = nullptr;
140+
rpr_material_node materialImage2 = nullptr;
141+
142+
const std::string pathImageFileA = "../../Resources/Textures/flower.jpg";
143+
rpr_status status = rprContextCreateImageFromFile(context, pathImageFileA.c_str(), &image2);
144+
g_gc.GCAdd(image2);
145+
if (status == RPR_ERROR_IO_ERROR)
146+
{
147+
std::cout << "Error : " << pathImageFileA << " not found.\n";
148+
return status;
149+
}
150+
CHECK(status);
151+
152+
// This texture is using the classic 2.2 gamma compression.
153+
CHECK( rprImageSetGamma(image2, 2.2f) );
154+
155+
CHECK(rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_IMAGE_TEXTURE, &materialImage2));
156+
g_gc.GCAdd(materialImage2);
157+
CHECK(rprMaterialNodeSetInputImageDataByKey(materialImage2, RPR_MATERIAL_INPUT_DATA, image2)); // Set image data
158+
159+
CHECK(rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_DIFFUSE, &diffuseMaterial));
160+
g_gc.GCAdd(diffuseMaterial);
161+
CHECK(rprMaterialNodeSetInputNByKey(diffuseMaterial, RPR_MATERIAL_INPUT_COLOR, materialImage2)); // set image sampler as the color input of diffuse material
162+
163+
164+
// assign the material on the shape
165+
CHECK(rprShapeSetMaterial(plane, diffuseMaterial));
166+
167+
}
168+
169+
// create a simple Red diffuse material
170+
rpr_material_node diffuseRed = NULL;
171+
CHECK( rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_DIFFUSE,&diffuseRed) );
172+
g_gc.GCAdd(diffuseRed);
173+
CHECK( rprMaterialNodeSetInputFByKey(diffuseRed, RPR_MATERIAL_INPUT_COLOR, 1, 0.1, 0.05f, 1) );
174+
175+
// create a simple Blue diffuse material
176+
rpr_material_node diffuseBlue = NULL;
177+
CHECK( rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_DIFFUSE,&diffuseBlue) );
178+
g_gc.GCAdd(diffuseBlue);
179+
CHECK( rprMaterialNodeSetInputFByKey(diffuseBlue, RPR_MATERIAL_INPUT_COLOR, 0.05f, 0.1f, 1, 1) );
180+
181+
// assign the Red material to faces '0' and '2'
182+
rpr_int indicesList1[] = { 0, 2 };
183+
CHECK( rprShapeSetMaterialFaces(plane, diffuseRed, indicesList1, sizeof(indicesList1) / sizeof(indicesList1[0]) ) );
184+
185+
// assign the Blue material to faces '5', '6' and '7'
186+
rpr_int indicesList2[] = { 5, 6, 7 };
187+
CHECK( rprShapeSetMaterialFaces(plane, diffuseBlue, indicesList2, sizeof(indicesList2) / sizeof(indicesList2[0]) ) );
188+
189+
// Create framebuffer
190+
rpr_framebuffer_desc desc = { 800 , 600 };
191+
rpr_framebuffer_format fmt = {4, RPR_COMPONENT_TYPE_FLOAT32};
192+
rpr_framebuffer frame_buffer = nullptr;
193+
rpr_framebuffer frame_buffer_resolved = nullptr;
194+
CHECK( rprContextCreateFrameBuffer(context, fmt, &desc, &frame_buffer) );
195+
CHECK( rprContextCreateFrameBuffer(context, fmt, &desc, &frame_buffer_resolved) );
196+
CHECK( rprContextSetAOV(context, RPR_AOV_COLOR, frame_buffer) );
197+
198+
// set rendering gamma
199+
CHECK( rprContextSetParameterByKey1f(context, RPR_CONTEXT_DISPLAY_GAMMA , 2.2f ) );
200+
201+
// Render the scene
202+
CHECK(rprContextSetParameterByKey1u(context,RPR_CONTEXT_ITERATIONS, 200));
203+
CHECK( rprContextRender(context) );
204+
CHECK(rprContextResolveFrameBuffer(context,frame_buffer,frame_buffer_resolved,false));
205+
CHECK(rprFrameBufferSaveToFile(frame_buffer_resolved,"34.png"));
206+
207+
208+
// Release the stuff we created
209+
CHECK(rprObjectDelete(camera)); camera=nullptr;
210+
CHECK(rprObjectDelete(frame_buffer)); frame_buffer=nullptr;
211+
CHECK(rprObjectDelete(frame_buffer_resolved)); frame_buffer_resolved=nullptr;
212+
g_gc.GCClean();
213+
CHECK(rprObjectDelete(scene)); scene=nullptr;
214+
CHECK(rprObjectDelete(matsys)); matsys=nullptr;
215+
CheckNoLeak(context);
216+
CHECK(rprObjectDelete(context)); context=nullptr;
217+
return 0;
218+
}
219+
220+
221+
222+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
project "34_material_per_face"
2+
kind "ConsoleApp"
3+
location "../build"
4+
files { "../34_material_per_face/**.h", "../34_material_per_face/**.cpp"}
5+
files { "../common/common.cpp","../common/common.h"}
6+
7+
-- remove filters for Visual Studio
8+
vpaths { [""] = { "../34_material_per_face/**.h", "../34_material_per_face/**.cpp","../common/common.cpp","../common/common.h"} }
9+
10+
11+
includedirs{ "../../RadeonProRender/inc" }
12+
13+
buildoptions "-std=c++14"
14+
15+
configuration {"x64"}
16+
links {"RadeonProRender64"}
17+
18+
if os.istarget("linux") then
19+
links {"pthread"}
20+
end
21+
22+
configuration {"x64", "Debug"}
23+
targetdir "../Bin"
24+
configuration {"x64", "Release"}
25+
targetdir "../Bin"
26+
configuration {}
27+
27.5 KB
Loading

tutorials/common/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ class MatballScene
162162

163163

164164
//
165-
// Garbage Collector functions :
165+
// Simple Garbage Collector for Radeon ProRender objects.
166+
// Add RPR objects with GCAdd. Then call GCClean to clear each object
166167
//
167168
class RPRGarbageCollector
168169
{

tutorials/premake5.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ solution "Tutorials"
9797
include "31_framebuffer_access"
9898
include "32_gl_interop"
9999
include "33_aov"
100+
include "34_material_per_face"
100101
include "50_curve"
101102
include "51_volume"
102103
include "60_mesh_export"

tutorials/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ List of tutorials in this SDK
2424
| [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. It also contains an example of DeepEXR framebuffer export. |
2525
| [OpenGL interop](32_gl_interop) | ![](32_gl_interop/screenshot.png) | Demo covering an RPR rendering inside an OpenGL app. |
2626
| [AOVs](33_aov) | ![](33_aov/screenshot.png) | This demo covers AOV (Arbitrary Output Variables), providing way to render different material component - mostly used for scene debugging. |
27+
| [Material Per Face](34_material_per_face) | ![](34_material_per_face/screenshot.png) | Demo of the rprShapeSetMaterialFaces API. This allows to set materials for specific faces of the shape. |
2728
| [Curves](50_curve) | ![](50_curve/screenshot.png) | Demo covering Curves rendering. Curves are often used for hair rendering. |
2829
| [Volume](51_volume) | ![](51_volume/screenshot.png) | This demo demonstrates Volumes with RPR |
2930
| [RPR Scene Export](60_mesh_export) | ![](60_mesh_export/screenshot.png) | Shows how to export an RPR scene as RPRS files ( native RPR file format ) or GLTF ( Khronos Group ). |
3031
| [RPR Scene Import](61_mesh_import) | ![](61_mesh_import/screenshot.png) | Shows how to import an RPR scene as RPRS files ( native RPR file format ) or GLTF ( Khronos Group ). It's advised to execute the demo "60_mesh_export" first in order to create the files used in this "61_mesh_import" Demo. |
3132
| [Hybrid](63_hybrid) | ![](63_hybrid/screenshot.png) | This is a demo for the Hybrid.DLL plugin. |
32-
| [Mesh OBJ Demo](64_mesh_obj_demo) | ![](64_mesh_obj_demo/screenshot.png) | Project that import and rendering any OBJ file with Radeon ProRender. |
33+
| [Mesh OBJ Demo](64_mesh_obj_demo) | ![](64_mesh_obj_demo/screenshot.png) | Project that imports and renders any OBJ file with Radeon ProRender. |
3334

3435

0 commit comments

Comments
 (0)