void Scene::optixInit(optix::Context context) { // Group containing each object transform Optix.group = context->createGroup(); Optix.group->setAcceleration(context->createAcceleration(OPTIX_SCENE_BUILDER, OPTIX_SCENE_TRAVERSER)); // TODO: Look into different traversers/builders for (uint i = 0; i < objects.size(); i++) { objects[i]->optixInit(context); Optix.group->addChild(objects[i]->Optix.transform); } // Sky texture sampler #if OPTIX_USE_OPENGL_TEXTURE Optix.sky = context->createTextureSamplerFromGLImage(sky->texture, RT_TARGET_GL_TEXTURE_2D); #else optix::Buffer buf = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_FLOAT4, sky->width, sky->height); memcpy(buf->map(), sky->pixels, sky->width * sky->height * sizeof(Color)); buf->unmap(); Optix.sky = context->createTextureSampler(); Optix.sky->setArraySize(1); Optix.sky->setMipLevelCount(1); Optix.sky->setBuffer(0, 0, buf); #endif Optix.sky->setWrapMode(0, RT_WRAP_REPEAT); Optix.sky->setWrapMode(1, RT_WRAP_REPEAT); Optix.sky->setIndexingMode(RT_TEXTURE_INDEX_NORMALIZED_COORDINATES); Optix.sky->setReadMode(RT_TEXTURE_READ_NORMALIZED_FLOAT); Optix.sky->setMaxAnisotropy(1.f); Optix.sky->setFilteringModes(RT_FILTER_LINEAR, RT_FILTER_LINEAR, RT_FILTER_NONE); }
optix::Buffer Texture::createBufferFromImage(optix::Context & context, const Image & image ) { optix::Buffer buffer = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_UNSIGNED_BYTE4, image.getWidth(), image.getHeight()); optix::char4* buffer_Host = (optix::char4*)buffer->map(); memcpy(buffer_Host, image.constData(), image.getWidth()*image.getHeight()*4*sizeof(unsigned char)); buffer->unmap(); return buffer; }
optix::Material Texture::getOptixMaterial(optix::Context & context) { if(!m_optixMaterialIsCreated) { m_optixMaterial = context->createMaterial(); optix::Program radianceProgram = context->createProgramFromPTXFile( "Texture.cu.ptx", "closestHitRadiance"); optix::Program photonProgram = context->createProgramFromPTXFile( "Texture.cu.ptx", "closestHitPhoton"); m_optixMaterial->setClosestHitProgram(RayType::RADIANCE, radianceProgram); m_optixMaterial->setClosestHitProgram(RayType::RADIANCE_IN_PARTICIPATING_MEDIUM, radianceProgram); m_optixMaterial->setClosestHitProgram(RayType::PHOTON, photonProgram); m_optixMaterial->setClosestHitProgram(RayType::PHOTON_IN_PARTICIPATING_MEDIUM, photonProgram); m_optixMaterial->setClosestHitProgram(RayType::LIGHT_VCM, context->createProgramFromPTXFile( "Texture.cu.ptx", "vcmClosestHitLight")); m_optixMaterial->setClosestHitProgram(RayType::CAMERA_VCM, context->createProgramFromPTXFile( "Texture.cu.ptx", "vcmClosestHitCamera")); m_optixMaterial->validate(); this->registerMaterialWithShadowProgram(context, m_optixMaterial); m_optixMaterialIsCreated = true; } // Diffuse buffer optix::Buffer buffer = createBufferFromImage(context, *m_diffuseImage); m_diffuseSampler = createTextureSamplerFromBuffer(context, buffer); optix::Buffer normalsBuffer; if(m_normalMapImage != NULL) { normalsBuffer = createBufferFromImage(context, *m_normalMapImage); } else { normalsBuffer = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_UNSIGNED_BYTE4, 0, 0); } m_normalMapSampler = createTextureSamplerFromBuffer(context, normalsBuffer); return m_optixMaterial; }
void TriangleMesh::optixInit(optix::Context context) { try { static optix::Program intersectProgram = context->createProgramFromPTXFile("ptx/triangle_mesh_program.cu.ptx", "intersect"); static optix::Program boundsProgram = context->createProgramFromPTXFile("ptx/triangle_mesh_program.cu.ptx", "bounds"); #if OPTIX_USE_GEOMETRY_VBO // Bind vertex VBO Optix.posBuffer = context->createBufferFromGLBO(RT_BUFFER_INPUT, vboPos); Optix.posBuffer->setFormat(RT_FORMAT_FLOAT3); Optix.posBuffer->setSize(posData.size()); // Bind normal VBO Optix.normalBuffer = context->createBufferFromGLBO(RT_BUFFER_INPUT, vboNormal); Optix.normalBuffer->setFormat(RT_FORMAT_FLOAT3); Optix.normalBuffer->setSize(normalData.size()); // Bind texture VBO Optix.texCoordBuffer = context->createBufferFromGLBO(RT_BUFFER_INPUT, vboTexCoord); Optix.texCoordBuffer->setFormat(RT_FORMAT_FLOAT2); Optix.texCoordBuffer->setSize(texCoordData.size()); // Bind index IBO Optix.indexBuffer = context->createBufferFromGLBO(RT_BUFFER_INPUT, ibo); Optix.indexBuffer->setFormat(RT_FORMAT_UNSIGNED_INT); Optix.indexBuffer->setSize(indexData.size()); #else // Copy position buffer Optix.posBuffer = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_FLOAT3, posData.size()); memcpy(Optix.posBuffer->map(), &posData[0], posData.size() * sizeof(Vec3)); Optix.posBuffer->unmap(); // Copy normal buffer Optix.normalBuffer = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_FLOAT3, normalData.size()); memcpy(Optix.normalBuffer->map(), &normalData[0], normalData.size() * sizeof(Vec3)); Optix.normalBuffer->unmap(); // Copy texture buffer Optix.texCoordBuffer = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_FLOAT2, texCoordData.size()); memcpy(Optix.texCoordBuffer->map(), &texCoordData[0], texCoordData.size() * sizeof(Vec2)); Optix.texCoordBuffer->unmap(); // Copy index buffer Optix.indexBuffer = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_UNSIGNED_INT3, indexData.size()); memcpy(Optix.indexBuffer->map(), &indexData[0], indexData.size() * sizeof(TrianglePrimitive)); Optix.indexBuffer->unmap(); #endif // Make geometry Optix.geometry = context->createGeometry(); Optix.geometry->setIntersectionProgram(intersectProgram); Optix.geometry->setBoundingBoxProgram(boundsProgram); Optix.geometry->setPrimitiveCount(indexData.size()); Optix.geometry["posData"]->setBuffer(Optix.posBuffer); Optix.geometry["normalData"]->setBuffer(Optix.normalBuffer); Optix.geometry["texCoordData"]->setBuffer(Optix.texCoordBuffer); Optix.geometry["indexData"]->setBuffer(Optix.indexBuffer); // Make instance if (!material->Optix.material) { #if OPTIX_USE_OPENGL_TEXTURE material->Optix.sampler = context->createTextureSamplerFromGLImage(material->image->texture, RT_TARGET_GL_TEXTURE_2D); #else optix::Buffer buf = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_FLOAT4, material->image->width, material->image->height); memcpy(buf->map(), material->image->pixels, material->image->width * material->image->height * sizeof(Color)); buf->unmap(); material->Optix.sampler = context->createTextureSampler(); material->Optix.sampler->setArraySize(1); material->Optix.sampler->setMipLevelCount(1); material->Optix.sampler->setBuffer(0, 0, buf); #endif material->Optix.sampler->setWrapMode(0, RT_WRAP_REPEAT); material->Optix.sampler->setWrapMode(1, RT_WRAP_REPEAT); material->Optix.sampler->setIndexingMode(RT_TEXTURE_INDEX_NORMALIZED_COORDINATES); material->Optix.sampler->setReadMode(RT_TEXTURE_READ_NORMALIZED_FLOAT); material->Optix.sampler->setMaxAnisotropy(1.f); RTfiltermode filter = (material->image->filter == GL_NEAREST) ? RT_FILTER_NEAREST : RT_FILTER_LINEAR; material->Optix.sampler->setFilteringModes(filter, filter, RT_FILTER_NONE); material->Optix.material = context->createMaterial(); material->Optix.material->setClosestHitProgram(0, materialClosestHitProgram); material->Optix.material->setAnyHitProgram(1, materialAnyHitProgram); material->Optix.material["sampler"]->setTextureSampler(material->Optix.sampler); material->Optix.material["ambient"]->setFloat(material->ambient.r(), material->ambient.g(), material->ambient.b(), material->ambient.a()); material->Optix.material["specular"]->setFloat(material->specular.r(), material->specular.g(), material->specular.b(), material->specular.a()); material->Optix.material["diffuse"]->setFloat(material->diffuse.r(), material->diffuse.g(), material->diffuse.b(), material->diffuse.a()); material->Optix.material["shineExponent"]->setFloat(material->shineExponent); material->Optix.material["reflectIntensity"]->setFloat(material->reflectIntensity); material->Optix.material["refractIndex"]->setFloat(material->refractIndex); } Optix.geometryInstance = context->createGeometryInstance(); Optix.geometryInstance->setGeometry(Optix.geometry); Optix.geometryInstance->addMaterial(material->Optix.material); } catch (optix::Exception e) { printException(e); } }