예제 #1
0
파일: passOptix.cpp 프로젝트: monthero/nau
void 
PassOptix::addScene (const std::string &sceneName)
{
	if (m_SceneVector.end() == std::find (m_SceneVector.begin(), m_SceneVector.end(), sceneName)) {
	
		m_SceneVector.push_back (sceneName);
	
		std::set<std::string> *materialNames = new std::set<std::string>;
		RENDERMANAGER->getScene(sceneName)->getMaterialNames(materialNames);
		
		std::set<std::string>::iterator iter;
		iter = materialNames->begin();
		for ( ; iter != materialNames->end(); ++iter) {
			
			if (m_MaterialMap.count((*iter)) == 0)
				m_MaterialMap[(*iter)] = MaterialID(DEFAULTMATERIALLIBNAME, (*iter));
		}
		delete materialNames;


		IScene *sc = RENDERMANAGER->getScene(sceneName);
		sc->compile();

/*		std::vector<SceneObject *> objs = sc->getAllObjects();
		for (unsigned int i = 0; i < objs.size(); ++i) {

			IRenderable &r = objs[i]->getRenderable();
			VertexData &v = r.getVertexData();
			// just to make sure we get this data compiled
			//r.getVertexData().compile();

			// which attrs should we send to optix?
			unsigned int id = v.getBufferID(0);
			// clear OpenGL Errors so far; otherwise Optix won't work
			int e = glGetError();

			optix::Buffer buffers[VertexData::MaxAttribs];
			try {
				for (unsigned int b = 0; b < VertexData::MaxAttribs; ++b) { 
					if (v.getBufferID(b)) {
						buffers[b] = o_Context->createBufferFromGLBO(RT_BUFFER_INPUT,v.getBufferID(b));
						buffers[b]->setFormat(RT_FORMAT_FLOAT4);
						buffers[b]->setSize(v.getDataOf(0).size());
					}
				}
			}
			catch ( optix::Exception& e ) {
				NAU_THROW("Optix Error: Adding scene %s to pass %s (creating buffers from VBOs) [%s]",
										sc->getName().c_str(), m_Name.c_str(),e.getErrorString().c_str()); 
			}

			std::vector<IMaterialGroup *> mg = r.getMaterialGroups();
			for (unsigned int g = 0; g < mg.size(); ++g) {

				if (mg[g]->getNumberOfPrimitives() > 0) {
					try {
						optix::Geometry geom = o_Context->createGeometry();
						geom->setPrimitiveCount(mg[g]->getNumberOfPrimitives());
						geom->setBoundingBoxProgram(o_BoundingBoxProgram);
						geom->setIntersectionProgram(o_GeometryIntersectionProgram);

						geom["vertex_buffer"]->setBuffer(buffers[0]);
						for (unsigned int b = 1; b < VertexData::MaxAttribs; ++b) {
							if (v.getBufferID(b))
								geom[VertexData::Syntax[b]]->setBuffer(buffers[b]);
						}
						id = mg[g]->getIndexData().getBufferID();
						optix::Buffer indices = o_Context->createBufferFromGLBO(RT_BUFFER_INPUT, id);
						indices->setFormat(RT_FORMAT_UNSIGNED_INT);
						indices->setSize(mg[g]->getIndexData().getIndexSize());

						geom["index_buffer"]->setBuffer(indices);
						o_Material = o_Context->createMaterial();
						o_Material->setClosestHitProgram(0, o_ClosestHitProgram);
						
						o_Material["diffuse"]->set4fv(MATERIALLIBMANAGER->getMaterial(this->m_MaterialMap[mg[g]->getMaterialName()])->getColor().getDiffuse());

						o_GeomInstances.push_back(o_Context->createGeometryInstance());
						o_GeomInstances[o_GeomInstances.size()-1]->setMaterialCount(1);
						o_GeomInstances[o_GeomInstances.size()-1]->setMaterial(0, o_Material);
						o_GeomInstances[o_GeomInstances.size()-1]->setGeometry(geom);
					}
					catch ( optix::Exception& e ) {
						NAU_THROW("Optix Error: Adding scene %s to pass %s (adding material groups) [%s]",
												sc->getName().c_str(), m_Name.c_str(),e.getErrorString().c_str()); 
					}
				}
			}
		}
		try {
			o_GeomGroup->setChildCount(o_GeomInstances.size());
			for (unsigned int i = 0; i < o_GeomInstances.size(); ++i) {
				o_GeomGroup->setChild(i,o_GeomInstances[i]);
			}

			o_GeomGroup->setAcceleration(o_Context->createAcceleration("Bvh","Bvh"));
		}
		catch ( optix::Exception& e ) {
			NAU_THROW("Optix Error: Adding scene %s to pass %s (adding instances to geometry group) [%s]",
												sc->getName().c_str(), m_Name.c_str(),e.getErrorString().c_str()); 
		}

		//o_Context["geometry"]->set(o_GeomGroup);






*/		

	}
}
예제 #2
0
파일: passOptix.cpp 프로젝트: monthero/nau
void
PassOptix::optixInit() {

	glGetError();

	// Can these be removed due to texture count?
	o_TexLib.addTexture(1);
	o_Context["tex0"]->set(o_TexLib.getTexture(1));

	std::set<std::string> *materialNames = new std::set<std::string>;
	
	std::vector<std::string>::iterator scenesIter;
	scenesIter = m_SceneVector.begin();
	for ( ; scenesIter != m_SceneVector.end(); ++scenesIter) {

		IScene *aScene = RENDERMANAGER->getScene (*scenesIter);
		aScene->compile();

		// Adding Materials to optix lib
		aScene->getMaterialNames(materialNames);
		std::set<std::string>::iterator matIter;
		matIter = materialNames->begin();
		for( ; matIter != materialNames->end(); ++matIter) {
		
			o_MatLib.addMaterial(m_MaterialMap[*matIter]);
		}

		materialNames->clear();


		std::vector<SceneObject *> objs = aScene->getAllObjects();
		std::vector<nau::scene::SceneObject*>::iterator objsIter;

		objsIter = objs.begin();
		for ( ; objsIter != objs.end(); ++objsIter) {
			o_GeomLib.addSceneObject(*objsIter, m_MaterialMap);
//			o_GeomLib.addSceneObject((*objsIter)->getId(), m_MaterialMap);
		}
	}
	o_GeomLib.buildGeometryGroup();
	o_Context["top_object"]->set(o_GeomLib.getGeometryGroup());

	o_MatLib.applyMissPrograms();


	std::map<std::string, std::string>::iterator iter;
	iter = o_InputBuffers.begin();
	for ( ; iter != o_InputBuffers.end(); ++iter) {
		try {
			unsigned int id = RESOURCEMANAGER->getTexture(iter->second)->getPropi(ITexture::ID);
			if (RESOURCEMANAGER->getTexture(iter->second)->getPrope(ITexture::DIMENSION) == GL_TEXTURE_2D) {
				optix::TextureSampler rtWorldSpaceTexture = o_Context->createTextureSamplerFromGLImage(id, RT_TARGET_GL_TEXTURE_2D);
				rtWorldSpaceTexture->setWrapMode(0, RT_WRAP_CLAMP_TO_EDGE);
				rtWorldSpaceTexture->setWrapMode(1, RT_WRAP_CLAMP_TO_EDGE);
				rtWorldSpaceTexture->setIndexingMode(RT_TEXTURE_INDEX_ARRAY_INDEX);
				rtWorldSpaceTexture->setReadMode(RT_TEXTURE_READ_ELEMENT_TYPE);
				rtWorldSpaceTexture->setMaxAnisotropy(1.0f);
				rtWorldSpaceTexture->setFilteringModes(RT_FILTER_NEAREST, RT_FILTER_NEAREST, RT_FILTER_NONE);
				o_Context[iter->first]->setTextureSampler(rtWorldSpaceTexture);
			}
		}
		catch(optix::Exception& e) {
			NAU_THROW("Optix Error: Input Buffer preparation in pass %s [%s]", m_Name.c_str(), e.getErrorString().c_str());
		}
	}

	std::map<std::string, databuffer>::iterator iter2;
	iter2 = o_OutputDataBuffer.begin();
	ITexture *texID;

	for ( ; iter2 != o_OutputDataBuffer.end() ; ++iter2) {

		texID = RESOURCEMANAGER->getTexture(iter2->second.texName);
//		int format = texID->getPrope(ITexture::FORMAT);
		int tex = texID->getPropi(ITexture::ID);

		unsigned int pbo;
		glGenBuffers(1, &pbo);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
		// need to allow different types
		glBufferData(GL_PIXEL_UNPACK_BUFFER, texID->getPropi(ITexture::WIDTH)*texID->getPropi(ITexture::HEIGHT)*texID->getPropi(ITexture::ELEMENT_SIZE), 0, GL_STREAM_READ);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

		optix::Buffer ob = o_Context->createBufferFromGLBO(RT_BUFFER_OUTPUT, pbo);
		ob->setSize(texID->getPropi(ITexture::WIDTH), texID->getPropi(ITexture::HEIGHT));
		// same here (types)
		ob->setFormat(getOptixFormat(texID));

		o_Context[iter2->first]->setBuffer(ob);

		o_OutputDataBuffer[iter2->first].pbo = pbo;
		//
	}

#if (TEST == 2)
	try {
		unsigned int id = RESOURCEMANAGER->getTexture("Deferred Render Targets::pos")->getId();
		optix::ITextureSampler rtWorldSpaceTexture = o_Context->createTextureSamplerFromGLImage(id, RT_TARGET_GL_TEXTURE_2D);
		rtWorldSpaceTexture->setWrapMode(0, RT_WRAP_CLAMP_TO_EDGE);
		rtWorldSpaceTexture->setWrapMode(1, RT_WRAP_CLAMP_TO_EDGE);
		rtWorldSpaceTexture->setIndexingMode(RT_TEXTURE_INDEX_ARRAY_INDEX);
		rtWorldSpaceTexture->setReadMode(RT_TEXTURE_READ_ELEMENT_TYPE);
		rtWorldSpaceTexture->setMaxAnisotropy(1.0f);
		rtWorldSpaceTexture->setFilteringModes(RT_FILTER_NEAREST, RT_FILTER_NEAREST, RT_FILTER_NONE);
		o_Context["pos_buffer"]->setTextureSampler(rtWorldSpaceTexture);
	}
	catch(optix::Exception& e) {
		NAU_THROW("Optix Error: Input Buffer preparation in pass %s [%s]", m_Name.c_str(), e.getErrorString().c_str());
	}
#endif
	o_OptixIsPrepared = true;
}