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