optix::GeometryInstance CornellSmall::createParallelogram(
    unsigned int meshId,
    optix::Context & context,
    const optix::float3& anchor,
    const optix::float3& offset1,
    const optix::float3& offset2,
    Material & material)
{
    optix::Geometry parallelogram = context->createGeometry();
    parallelogram->setPrimitiveCount( 1u );
    parallelogram->setIntersectionProgram( m_pgram_intersection );
    parallelogram->setBoundingBoxProgram( m_pgram_bounding_box );

    optix::float3 normal = optix::normalize( optix::cross( offset1, offset2 ) );
    float d = optix::dot( normal, anchor );
    optix::float4 plane = optix::make_float4( normal, d );

    optix::float3 v1 = offset1 / optix::dot( offset1, offset1 );
    optix::float3 v2 = offset2 / optix::dot( offset2, offset2 );

    parallelogram["meshId"]->setUint(meshId);
    parallelogram["plane"]->setFloat( plane );
    parallelogram["anchor"]->setFloat( anchor );
    parallelogram["v1"]->setFloat( v1 );
    parallelogram["v2"]->setFloat( v2 );

    optix::Material matl = material.getOptixMaterial(context);

    optix::GeometryInstance gi = context->createGeometryInstance( parallelogram, &matl, &matl+1 );
    material.registerGeometryInstanceValues(gi);
    return gi;
}
bool ParallelogramGeometry::OptixCreateDevice(optix::Context& Context)
{
	if (!_Initialized)
	{
		if (!ContentLoader::LoadOptixProgramFromFile("CUDA\\ParallelogramGeometry.cu.ptx", "intersect", Context, &_ProgIntersection )) return false;
		if (!ContentLoader::LoadOptixProgramFromFile("CUDA\\ParallelogramGeometry.cu.ptx", "bounds", Context, &_ProgBoundingBox )) return false;		
		_Initialized = true;
	} // TODO: else increment ref counter of the programs!
	_Geometry = Context->createGeometry();
	_Geometry->setPrimitiveCount( 1u );
	_Geometry->setIntersectionProgram( _ProgIntersection );
	_Geometry->setBoundingBoxProgram( _ProgBoundingBox );

	optix::float3 normal = normalize( cross( _Offset1, _Offset2 ) );
	float d       = dot( normal, _Anchor );
	optix::float4 plane  = make_float4( normal, d );

	optix::float3 v1 = _Offset1 / dot( _Offset1, _Offset1 );
	optix::float3 v2 = _Offset2 / dot( _Offset2, _Offset2 );

	_Geometry["plane"]->setFloat( plane );
	_Geometry["anchor"]->setFloat( _Anchor );
	_Geometry["v1"]->setFloat( v1 );
	_Geometry["v2"]->setFloat( v2 );
	return true;
}
bool SphereGeometry::OptixCreateDevice(optix::Context& Context)
{
	if (!_Initialized)
	{
		if (!ContentLoader::LoadOptixProgramFromFile("CUDA\\SphereGeometry.cu.ptx", "bounds", Context, &_ProgBoundingBox)) return false;
		if (!ContentLoader::LoadOptixProgramFromFile("CUDA\\SphereGeometry.cu.ptx", "intersect", Context, &_ProgIntersection)) return false;		
		_Initialized = true;
	} // TODO: else increment ref counter of the programs!
	_Geometry = Context->createGeometry();
	_Geometry->setPrimitiveCount( 1u );
	_Geometry->setIntersectionProgram( _ProgIntersection );
	_Geometry->setBoundingBoxProgram( _ProgBoundingBox );	
	
	optix::float4 sphere = optix::make_float4( _Center, _Radius);
	_Geometry["sphere"]->setFloat( sphere );		
	return true;
}
optix::Geometry PlaneObject::getOptiXGeometry(optix::Context context) {
	optix::Geometry parallelogram = context->createGeometry();
	parallelogram->setPrimitiveCount( 1u );
	parallelogram->setBoundingBoxProgram( context->createProgramFromPTXFile("ptx/PlaneObject.ptx", "bounds" ) );
	parallelogram->setIntersectionProgram( context->createProgramFromPTXFile("ptx/PlaneObject.ptx", "intersect" ) );
	parallelogram["plane"]->setFloat(    normal->x,       normal->y,       normal->z, normal->dotProduct(position));
	// Move the anchor point
	Vector3D anchor = *position;
	anchor = anchor + (*up)*(-height/2);
	anchor = anchor + (*right)*(-width/2);
	parallelogram["anchor"]->setFloat( anchor.x,     anchor.y,     anchor.z );
	Vector3D v1 = *up*width;
	Vector3D v2 = *right*height;
	v1 = v1 * (1/(v1.dotProduct(&v1)));
	v2 = v2 * (1/(v2.dotProduct(&v2)));
	parallelogram["v1"]->setFloat(v1.x, v1.y, v1.z );
	parallelogram["v2"]->setFloat(v2.x, v2.y, v2.z );
	return parallelogram;
}
示例#5
0
optix::Geometry AABInstance::getOptixGeometry( optix::Context & context)
{
    if(m_hasLoadedOptixPrograms == false)
    {
        m_programBoundingBox = context->createProgramFromPTXFile("AAB.cu.ptx", "boundingBox");
        m_programIntersection = context->createProgramFromPTXFile("AAB.cu.ptx", "intersect");
        m_hasLoadedOptixPrograms = true;
    }
    
    optix::Geometry geometry = context->createGeometry();
    geometry->setPrimitiveCount(1u);
    geometry->setBoundingBoxProgram(m_programBoundingBox);
    geometry->setIntersectionProgram(m_programIntersection);

    geometry["cuboidMin"]->setFloat( this->m_aab.min );
    geometry["cuboidMax"]->setFloat( this->m_aab.max );

    return geometry;
}
optix::Geometry SphereInstance::getOptixGeometry( optix::Context & context)
{
    if(m_hasLoadedOptixPrograms == false)
    {
        m_programBoundingBox = context->createProgramFromPTXFile("Sphere.cu.ptx", "boundingBox");
        m_programIntersection = context->createProgramFromPTXFile("Sphere.cu.ptx", "intersect");
        m_hasLoadedOptixPrograms = true;
    }

    optix::Geometry sphere = context->createGeometry();
    sphere->setPrimitiveCount(1);
    sphere->setIntersectionProgram( m_programIntersection );
    sphere->setBoundingBoxProgram( m_programBoundingBox );

    sphere["radius"]->setFloat( this->m_sphere.radius );
    sphere["center"]->setFloat( this->m_sphere.center );

    return sphere;
}
示例#7
0
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);
	}

}