|
| 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 | + |
0 commit comments