Ejemplo n.º 1
0
Scene& GlassModelExample::createScene(Scene& scene, const double aspectRatio)
{
	scene = createMaterials(scene);

	std::cout << "Creating scene..." << std::endl;

	Background* background = new GradientBackground(Color(1, 1, 1), Color(0.0, 0.0, 0.0));
	scene.setBackground(background);

	Transform Tcam =  Transform::Translate(0,-3,2) * Transform::RotateXdeg(80);
	Camera* camera = new PinholeCamera(aspectRatio);
	SimpleCameraMount* mount = new SimpleCameraMount(Tcam, camera);

	scene.setCameraMount(mount);

	scene.addElement(new Object(Transform::Translate(0,0,-1), GeoPtr(new Plane()), "white"));

	TriangleMesh *mesh = LoadASC("nudobult.asc", 0.02);
	if (mesh)
	{
		GeoPtr pMesh(mesh);
		scene.addElement(new Object(Transform::Translate(0,4,0.5) *Transform::RotateZdeg(15), pMesh, "glass"));
	}

 	SkyLight* skyLight = new SkyLight(0.5, Color(0.800, 0.930, 1));
 	skyLight->setSampling(4,16,0.05);
 	scene.addElement(skyLight);

	SphereAreaLight *areaLight = NULL;
	areaLight = new SphereAreaLight(Transform::Translate(-6,-2,7),1.0, Color(0.8,0.7,0.6), 1, true, SphereAreaLight::ATT_NONE);
	areaLight->setSampling(4,16);
	scene.addElement(areaLight);

	return scene;
}
Ejemplo n.º 2
0
Scene& LensCameraExample::createScene(Scene& scene, const double aspectRatio)
{
	scene = createMaterials(scene);

	std::cout << "Creating scene..." << std::endl;

	Background* background = new GradientBackground(Color(1, 1, 1), Color(0.0, 0.0, 0.0));
	scene.setBackground(background);

	Transform Tcam =  Transform::Translate(0,-3,3.5) * Transform::RotateXdeg(80);
	Camera* camera = new LensCamera(aspectRatio, 8, 0.2);
	SimpleCameraMount* mount = new SimpleCameraMount(Tcam, camera);

	scene.setCameraMount(mount);

	scene.addElement(new Object(Transform::Translate(0,0,-1), GeoPtr(new Plane()), "white"));

	for (int i=-4; i<8; i++)
	{
		scene.addElement(new Object(Transform::Translate(2, i*8, 1), GeoPtr(new Sphere(2)), "white"));
		scene.addElement(new Object(Transform::Translate(-2, i*8+4, 1), GeoPtr(new Sphere(2)), "black"));
	}

 	SkyLight* skyLight = new SkyLight(0.3, Color(0.800, 0.930, 1));
 	skyLight->setSampling(1,1,0.05);
 	scene.addElement(skyLight);

	SphereAreaLight *areaLight = NULL;
	areaLight = new SphereAreaLight(Transform::Translate(-6,-2,7),1.0, Color(0.8,0.7,0.6), 1, true, SphereAreaLight::ATT_NONE);
	areaLight->setSampling(1,1);
	scene.addElement(areaLight);

	return scene;
}
Ejemplo n.º 3
0
GridLineFactory::GridLineFactory(void)
{
	mCellCount = mInitialCellCount;
	mLineList = NULL;
	mLineID = 0;
	createMaterials();
}
Ejemplo n.º 4
0
GridLineFactory::GridLineFactory(Ogre::SceneManager* sceneManager)
{
	mCellCount = mInitialCellCount;
	mLineList = NULL;
	mLineID = 0;
	createMaterials();
	
	setSceneManager(sceneManager);
}
Ejemplo n.º 5
0
bool ofxGLM::load(string sFile, float nScale, bool bNormalize) {
	char* filename = (char*)ofToDataPath(sFile, true).c_str();
	model = glmReadOBJ(filename);
	if(!model) {
		ofLog(OF_LOG_ERROR, "Could not load model: %s", sFile.c_str());
		return false;
	}
	if(bNormalize) {
		normalize();
	}
	if(nScale != -1.0) {
		scale(nScale);
	}
	
	createMaterials();
	createGroups();
	return true;
}
Ejemplo n.º 6
0
/*
 *  Create geometry
 */
bool srs_ui_but::CButProjection::createGeometry(const ros::NodeHandle & nh)
{
//	std::cerr << "CButProjection::createGeometry S" << std::endl;

    // Get camera.
    rviz::RenderPanel * panel = vis_manager_->getRenderPanel();
    if( panel == 0 )
        {
        ROS_DEBUG( "No render panel... ");
        return false;
        }

    Ogre::Camera * camera = panel->getCamera();

    // Create rtt texture
    //m_textureWithRtt = new CRosRttTexture( 512, 512, camera );

    /*
    Ogre::String lNameOfTheMesh = "MeshCube";
    {
        // Here, I create a 3D element, by using the interface of ManualObject.
        // ManualObject is very close to the opengl old simple way to specify geometry.
        // There are other interfaces (Hardwarebuffers), you can check the ogremanual fo them and wiki.
        // For each vertex I will provide positions and attributes (normal, vertex color, texture coordinates...).
        // Then for each primitive (given its type : triangle, line, line strip etc...),
        // I give the corresponding group of vertex index.
        Ogre::ManualObject* lManualObject = NULL;
        {
            // The manualObject creation requires a name.
            Ogre::String lManualObjectName = "CubeWithAxes";
            lManualObject = scene_manager_->createManualObject(lManualObjectName);

            // Always tell if you want to update the 3D (vertex/index) later or not.
            bool lDoIWantToUpdateItLater = false;
            lManualObject->setDynamic(lDoIWantToUpdateItLater);

            // Here I create a cube in a first part with triangles, and then axes (in red/green/blue).

            // BaseWhiteNoLighting is the name of a material that already exist inside Ogre.
            // Ogre::RenderOperation::OT_TRIANGLE_LIST is a kind of primitive.
            float lSize = 5.0f;
            lManualObject->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST);
            {
                float cp = 1.0f * lSize ;
                float cm = -1.0f * lSize;

                lManualObject->position(cm, cp, cm);// a vertex
                lManualObject->colour(Ogre::ColourValue(0.0f,1.0f,0.0f,1.0f));
                lManualObject->textureCoord(0.0, 1.0);
                lManualObject->position(cp, cp, cm);// a vertex
                lManualObject->colour(Ogre::ColourValue(1.0f,1.0f,0.0f,1.0f));
                lManualObject->textureCoord(1.0, 1.0);
                lManualObject->position(cp, cm, cm);// a vertex
                lManualObject->colour(Ogre::ColourValue(1.0f,0.0f,0.0f,1.0f));
                lManualObject->textureCoord(1.0, 0.0);
                lManualObject->position(cm, cm, cm);// a vertex
                lManualObject->colour(Ogre::ColourValue(0.0f,0.0f,0.0f,1.0f));
                lManualObject->textureCoord(0.0, 0.0);

                lManualObject->position(cm, cp, cp);// a vertex
                lManualObject->colour(Ogre::ColourValue(0.0f,1.0f,1.0f,1.0f));
                lManualObject->textureCoord(0.0, 1.0);
                lManualObject->position(cp, cp, cp);// a vertex
                lManualObject->colour(Ogre::ColourValue(1.0f,1.0f,1.0f,1.0f));
                lManualObject->textureCoord(1.0, 1.0);
                lManualObject->position(cp, cm, cp);// a vertex
                lManualObject->colour(Ogre::ColourValue(1.0f,0.0f,1.0f,1.0f));
                lManualObject->textureCoord(1.0, 0.0);
                lManualObject->position(cm, cm, cp);// a vertex
                lManualObject->colour(Ogre::ColourValue(0.0f,0.0f,1.0f,1.0f));
                lManualObject->textureCoord(0.0, 0.0);

                // face behind / front
                lManualObject->triangle(0,1,2);
                lManualObject->triangle(2,3,0);
                lManualObject->triangle(4,6,5);
                lManualObject->triangle(6,4,7);

                // face top / down
                lManualObject->triangle(0,4,5);
                lManualObject->triangle(5,1,0);
                lManualObject->triangle(2,6,7);
                lManualObject->triangle(7,3,2);

                // face left / right
                lManualObject->triangle(0,7,4);
                lManualObject->triangle(7,0,3);
                lManualObject->triangle(1,5,6);
                lManualObject->triangle(6,2,1);
            }
            lManualObject->end();
            // Here I have finished my ManualObject construction.
            // It is possible to add more begin()-end() thing, in order to use
            // different rendering operation types, or different materials in 1 ManualObject.
        }

        lManualObject->convertToMesh(lNameOfTheMesh, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

        // Now I can create several entities using that mesh
        int lNumberOfEntities = 1;
        for(int iter = 0; iter < lNumberOfEntities; ++iter)
            {
            Ogre::Entity* lEntity = scene_manager_->createEntity(lNameOfTheMesh);
            // Now I attach it to a scenenode, so that it becomes present in the scene.
            Ogre::SceneNode* lNode = scene_manager_->getRootSceneNode()->createChildSceneNode();
            lNode->attachObject(lEntity);
            // I move the SceneNode so that it is visible to the camera.
            float lPositionOffset = float(1+ iter * 5.0) - (float(lNumberOfEntities));
            lNode->translate(lPositionOffset, lPositionOffset, -10.0f);
            }
    }

    // Create entity on screen

    Ogre::Entity* lAdditionalEntity = scene_manager_->createEntity( lNameOfTheMesh );
    // lAdditionalEntity->setMaterialName( m_textureWithRtt->getMaterialName() );
    Ogre::SceneNode* lVisibleOnlyByFirstCam = scene_manager_->getRootSceneNode()->createChildSceneNode();
    lVisibleOnlyByFirstCam->attachObject( lAdditionalEntity );
    lVisibleOnlyByFirstCam->setPosition( 1.5,-0.5,-5);
    //*/

    // Create and initialize materials
    createMaterials( camera );

 //   std::cerr << "CButProjection::createGeometry E" << std::endl;

    return true;
}
void OgreCollada::MeshWriter::finish() {
  createMaterials();

  // close manualobject and convert to mesh
  m_mesh = m_manobj->convertToMesh(m_vsRootNodes[0]->getName() + "_mesh");
}
Ejemplo n.º 8
0
void Model::createObject(const char* Filename, bool FitSize) {
	std::ifstream fileStream(Filename);
	if (!fileStream) {
		std::cout << "Die Datei \"" << Filename << "\" kann nicht geoeffnet werden." << std::endl;
		exit(-1);
	}

	std::string line = "";
	int fileLength = 1;
	int numVertices = 0;
	int numTexCoord = 0;
	int numFaces = 0;

	//Anzahl von v f vt
	while (std::getline(fileStream, line)) {

		if (!line.empty() && line[0] != '\0' && line[0] != '#') {
			char firstChar = line[0];
			char secondChar = line[1];

			if (firstChar == 'f') {
				numFaces++;
			}
			else if (firstChar == 'v') {
				if (secondChar == 't') {
					numTexCoord++;
				}
				else {
					numVertices++;
				}

			}
		}
		fileLength++;
	}
    
	fileStream.clear();
	fileStream.seekg(0, fileStream.beg);

	struct Face {
		//Position Index, Textur Index
		unsigned int pidx[3], tidx[3];
	};

	std::vector<Face> f;
    f.reserve(numFaces);

	std::vector<Vector> v;
    v.reserve(numVertices);
    
	struct Texcoord {
		float s, t;
	};

	std::vector<Texcoord> vt;
    vt.reserve(numTexCoord);

	std::string lastMtl="";

	// Ganze Zahl, die das nächste zu lesende Zeichen darstellt, oder -1, wenn keine zu lesenden Zeichen vorhanden sind.
	while (fileStream.peek() != -1) {

		std::getline(fileStream, line);
        
		const char *charPointer = line.c_str();

		// Ueberspringe Endzeichen, Kommentare oder leere Zeilen
		if (line[0] == '\0' || line[0] == '#' || line.empty()) {
			continue;
		}

		// Textur
		if (charPointer[0] == 'f') {
			charPointer += 2;

			Face firstFace;
			Face secondFace;
			unsigned int posidx[4], texidx[4];

			int converted = sscanf(charPointer, "%d/%d %d/%d %d/%d %d/%d", &posidx[0], &texidx[0], &posidx[1], &texidx[1], &posidx[2], &texidx[2], &posidx[3], &texidx[3]);

			firstFace.pidx[0] = posidx[0];
			firstFace.pidx[1] = posidx[1];
			firstFace.pidx[2] = posidx[2];
			firstFace.tidx[0] = texidx[0];
			firstFace.tidx[1] = texidx[1];
			firstFace.tidx[2] = texidx[2];
			f.push_back(firstFace);

			if (converted == 8) {
				secondFace.pidx[0] = posidx[0];
				secondFace.pidx[1] = posidx[2];
				secondFace.pidx[2] = posidx[3];
				secondFace.tidx[0] = texidx[0];
				secondFace.tidx[1] = texidx[2];
				secondFace.tidx[2] = texidx[3];
				f.push_back(secondFace);
			}
		}
		else if (charPointer[0] == 'v') {
			// Texturkoordinaten
			if (charPointer[1] == 't') {
				charPointer += 3;

				Texcoord tex;

				sscanf(charPointer, "%f %f", &tex.s, &tex.t);

				vt.push_back(tex);
				
			// Eckpunkt
			} else {
				charPointer += 2;

				Vector vertex;

				sscanf(charPointer, "%f %f %f", &vertex.X, &vertex.Y, &vertex.Z);

				v.push_back(vertex);
			}
		}
        else if(strncmp(charPointer,"usemtl",6) == 0){
            charPointer += 7;
            std::string mtlName(charPointer);

			m_mtlMap_iter = m_mtlMap.find(lastMtl);
			if (mtlName.compare(lastMtl) != 0) {
				if (m_mtlMap_iter != m_mtlMap.end()) {
					//f.size() - 1 : last index of material before
					m_mtlMap_iter->second.push_back(f.size() - 1);
				}
			}
			else {
				m_mtlMap_iter->second.pop_back();
			}

			m_mtlMap_iter = m_mtlMap.find(mtlName);

			if (m_mtlMap_iter == m_mtlMap.end()) {
				std::vector<unsigned int> faceIndex;
				faceIndex.push_back(f.size());
				m_mtlMap.emplace(mtlName, faceIndex);
			}
			else {
				m_mtlMap_iter->second.push_back(f.size());
			}
			lastMtl = mtlName;
        }
		else if (strncmp(charPointer, "mtllib", 6) == 0) {
			charPointer += 7;
			
			char mtlFilename[256];
			replaceFilename(Filename, charPointer, mtlFilename);
			createMaterials(mtlFilename);
		}
	}
    
    //Close out last material
	m_mtlMap_iter = m_mtlMap.find(lastMtl);
	if(m_mtlMap_iter != m_mtlMap.end()) {
		//f.size() - 1 : last index of material before
		m_mtlMap_iter->second.push_back(f.size() - 1);
	}

	// BoundingBox 
	m_Box.Max.X = m_Box.Min.X = v[0].X;
	m_Box.Max.Y = m_Box.Min.Y = v[0].Y;
	m_Box.Max.Z = m_Box.Min.Z = v[0].Z;

	for (unsigned int i = 0; i < v.size(); i++) {
		Vector vertex = v[i];

		// Box kleiner
		if (m_Box.Min.X < vertex.X) {
			m_Box.Min.X = vertex.X;
		}
		if (m_Box.Min.Y < vertex.Y) {
			m_Box.Min.Y = vertex.Y;
		}
		if (m_Box.Min.Z < vertex.Z) {
			m_Box.Min.Z = vertex.Z;
		}
		// Box größer
		if (m_Box.Max.X > vertex.X) {
			m_Box.Max.X = vertex.X;
		}
		if (m_Box.Max.Y > vertex.Y) {
			m_Box.Max.Y = vertex.Y;
		}
		if (m_Box.Max.Z > vertex.Z) {
			m_Box.Max.Z = vertex.Z;
		}
	}

	// Objekt skalieren
	if (FitSize) {
		float scale = 7 / ((m_Box.Min - m_Box.Max).length()*2);
		for (unsigned int i = 0; i < v.size(); i++) {
			v[i] = v[i] * scale;
		}
	}

	// Eckpunkte und Materialien zusammenstellen
	unsigned int faceCount = (unsigned int)f.size();
	m_pVertices = new Vertex[faceCount * 3];
	m_VertexCount = faceCount * 3;

	//Write vertices to indexbuffer
	for (unsigned int i = 0; i < faceCount; i++) {
		unsigned int PosIdx0 = f[i].pidx[0] - 1;
		unsigned int PosIdx1 = f[i].pidx[1] - 1;
		unsigned int PosIdx2 = f[i].pidx[2] - 1;

		unsigned int TexIdx0 = f[i].tidx[0] - 1;
		unsigned int TexIdx1 = f[i].tidx[1] - 1;
		unsigned int TexIdx2 = f[i].tidx[2] - 1;

		Vector a = m_pVertices[i * 3].Position = v[PosIdx0];
		Vector b = m_pVertices[i * 3 + 1].Position = v[PosIdx1];
		Vector c = m_pVertices[i * 3 + 2].Position = v[PosIdx2];

		m_pVertices[i * 3].TexcoordS = vt[TexIdx0].s;
		m_pVertices[i * 3 + 1].TexcoordS = vt[TexIdx1].s;
		m_pVertices[i * 3 + 2].TexcoordS = vt[TexIdx2].s;

		m_pVertices[i * 3].TexcoordT = vt[TexIdx0].t;
		m_pVertices[i * 3 + 1].TexcoordT = vt[TexIdx1].t;
		m_pVertices[i * 3 + 2].TexcoordT = vt[TexIdx2].t;
		//break at i == 12272 xy same normal = (0,0,0)  "f 23745/23745 23746/23746 23747/23747 "
		Vector normal = (b - a).cross(c - a);
        
        if(normal.length() == 0){
            std::cout << "fehlerfall" <<std::endl;
            normal = Vector(1,0,0);
        } else{
            normal.normalize(); 
        }
		m_pVertices[i * 3].Normal =
			m_pVertices[i * 3 + 1].Normal =
			m_pVertices[i * 3 + 2].Normal = normal;
	}

	//print mtl with index
	/*for (auto const &itMap : m_mtlMap) {
		std::cout << " name: " << itMap.first <<std::endl;
		std::cout << "{";
		for (int i = 0; i < itMap.second.size(); i++) {
			std::cout << itMap.second[i] << " ";
		}
		std::cout << "}" << std::endl;
	}
	printf("Count: %d", m_VertexCount);

	printf("Vertices:\n");
	for (unsigned int i = 0; i<m_VertexCount; i++) {
		printf("%2i: ", i);
		printf("p(%2.1f, %2.1f, %2.1f) ", m_pVertices[i].Position.X, m_pVertices[i].Position.Y, m_pVertices[i].Position.Z);
		printf("n(%2.1f, %2.1f, %2.1f) ", m_pVertices[i].Normal.X, m_pVertices[i].Normal.Y, m_pVertices[i].Normal.Z);
		printf("t(%2.1f, %2.1f)\n", m_pVertices[i].TexcoordS, m_pVertices[i].TexcoordT);
	}*/
    fileStream.close();
	
}