예제 #1
0
void TestGame::init()
{
	m_root.setEngine(m_engine);

	IndexedModel model = IndexedModel();
    std::vector<Vertex> vertices;
    std::vector<unsigned int> indices;

//	vertices.push_back(Vertex(Vector3f(-1, -1, -1), Vector2f(1, 0)));
//	vertices.push_back(Vertex(Vector3f(-1,  1, -1), Vector2f(0, 0)));
//	vertices.push_back(Vertex(Vector3f( 1,  1, -1), Vector2f(0, 1)));
//	vertices.push_back(Vertex(Vector3f( 1, -1, -1), Vector2f(1, 1)));
//        
//	vertices.push_back(Vertex(Vector3f(-1, -1, 1), Vector2f(1, 0)));
//	vertices.push_back(Vertex(Vector3f(-1,  1, 1), Vector2f(0, 0)));
//	vertices.push_back(Vertex(Vector3f( 1,  1, 1), Vector2f(0, 1)));
//	vertices.push_back(Vertex(Vector3f( 1, -1, 1), Vector2f(1, 1)));
//        
//	vertices.push_back(Vertex(Vector3f(-1, -1, -1), Vector2f(0, 1)));
//	vertices.push_back(Vertex(Vector3f(-1, -1,  1), Vector2f(1, 1)));
//	vertices.push_back(Vertex(Vector3f( 1, -1,  1), Vector2f(1, 0)));
//	vertices.push_back(Vertex(Vector3f( 1, -1, -1), Vector2f(0, 0)));
//        
//	vertices.push_back(Vertex(Vector3f(-1, 1, -1), Vector2f(0, 1)));
//	vertices.push_back(Vertex(Vector3f(-1, 1,  1), Vector2f(1, 1)));
//	vertices.push_back(Vertex(Vector3f( 1, 1,  1), Vector2f(1, 0)));
//	vertices.push_back(Vertex(Vector3f( 1, 1, -1), Vector2f(0, 0)));
//        
//	vertices.push_back(Vertex(Vector3f(-1, -1, -1), Vector2f(1, 1)));
//	vertices.push_back(Vertex(Vector3f(-1, -1,  1), Vector2f(1, 0)));
//	vertices.push_back(Vertex(Vector3f(-1,  1,  1), Vector2f(0, 0)));
//	vertices.push_back(Vertex(Vector3f(-1,  1, -1), Vector2f(0, 1)));
//        
//	vertices.push_back(Vertex(Vector3f(1, -1, -1), Vector2f(1, 1)));
//	vertices.push_back(Vertex(Vector3f(1, -1,  1), Vector2f(1, 0)));
//	vertices.push_back(Vertex(Vector3f(1,  1,  1), Vector2f(0, 0)));
//	vertices.push_back(Vertex(Vector3f(1,  1, -1), Vector2f(0, 1)));
//    
//    indices.push_back(0);
//    indices.push_back(1);
//    indices.push_back(2);
//    indices.push_back(0);
//    indices.push_back(2);
//    indices.push_back(3);
//    
//    indices.push_back(6);
//    indices.push_back(5);
//    indices.push_back(4);
//    indices.push_back(7);
//    indices.push_back(6);
//    indices.push_back(4);
//    
//    indices.push_back(10);
//    indices.push_back(9);
//    indices.push_back(8);
//    indices.push_back(11);
//    indices.push_back(10);
//    indices.push_back(8);
//    
//    indices.push_back(12);
//    indices.push_back(13);
//    indices.push_back(14);
//    indices.push_back(12);
//    indices.push_back(14);
//    indices.push_back(15);
//    
//    indices.push_back(16);
//    indices.push_back(17);
//    indices.push_back(18);
//    indices.push_back(16);
//    indices.push_back(18);
//    indices.push_back(19);
//    
//    indices.push_back(22);
//    indices.push_back(21);
//    indices.push_back(20);
//    indices.push_back(23);
//    indices.push_back(22);
//    indices.push_back(20);
    
    vertices.push_back(Vertex(Vector3f(-1.0f, -1.0f, 0.5773f), Vector2f(0.0f, 0.0f)));
    vertices.push_back(Vertex(Vector3f(0.0f, -1.0f, -1.15475f), Vector2f(0.5f, 0.0f)));
    vertices.push_back(Vertex(Vector3f(1.0f, -1.0f, 0.5773f), Vector2f(1.0f, 0.0f)));
    vertices.push_back(Vertex(Vector3f(0.0f, 1.0f, 0.0f), Vector2f(0.5f, 1.0f)));
    
    indices.push_back(0);
    indices.push_back(3);
    indices.push_back(1);
    indices.push_back(1);
    indices.push_back(3);
    indices.push_back(2);
    indices.push_back(2);
    indices.push_back(3);
    indices.push_back(0);
    indices.push_back(1);
    indices.push_back(2);
    indices.push_back(0);
    
    model.addVertices(vertices, indices, true);
    
    Material material = Material(m_texture, Vector3f(1.0f, 1.0f, 1.0f), 2.0f, 32.0f);

	Mesh mesh0 = Mesh(model, material);

	// IndexedModel model1 = IndexedModel();
	// model1.addVertex(Vertex(Vector3f(-5, -3, -5), Vector2f(0, 1)));
	// model1.addVertex(Vertex(Vector3f(-5, -3,  5), Vector2f(1, 1)));
	// model1.addVertex(Vertex(Vector3f( 5, -3,  5), Vector2f(1, 0)));
	// model1.addVertex(Vertex(Vector3f( 5, -3, -5), Vector2f(0, 0)));
	// model1.addFace(Vector3i(2, 1, 0));
	// model1.addFace(Vector3i(3, 2, 0));
	// Mesh mesh1 = Mesh(model1);

	// m_camera = new Camera(Vector3f(0.0f, 0.0f, -3.0f), Vector3f(0.0f, 0.0f, 1.0f), Vector3f(0.0f, 1.0f, 0.0f), 70.0f, m_engine->getWindow()->getAspectRatio(), 0.1f, 1000.0f);

	m_object0 = (new Entity())->addComponent(new MeshRenderer(mesh0));
	add(m_object0);
    m_object0->getTransform().setRotation(Quaternion(Vector3f(1.0f, 0.0f, 1.0f).normalized(), MATH_PI));

	// m_object1 = (new Entity())->addComponent(new MeshRenderer(mesh1));
	// add(m_object1);

	add((new Entity())->addComponent(new CameraComponent(70.0f, m_engine->getWindow()->getAspectRatio(), 0.1f, 1000.0f))
		->addComponent(new FreeMove(0.2f))
		->addComponent(new FreeLook(m_engine->getWindow()->getCenter(), 0.2f)));
    
    m_engine->getRenderingEngine()->setAmbientLight(Vector3f(0.1f, 0.1f, 0.1f));
    m_engine->getRenderingEngine()->setDirectionalLight(DirectionalLight(Light(Vector3f(1.0f, 1.0f, 1.0f), 0.8f), Vector3f(0.0f, 1.0f, 0.0f).normalized()));
}
예제 #2
0
Mesh::Mesh(const std::string& fileName) :
	m_fileName(fileName),
	m_meshData(0)
{
	std::map<std::string, MeshData*>::const_iterator it = s_resourceMap.find(fileName);
	if(it != s_resourceMap.end())
	{
		m_meshData = it->second;
		m_meshData->AddReference();
	}
	else
	{
		Assimp::Importer importer;
		
		const aiScene* scene = importer.ReadFile(("./res/models/" + fileName).c_str(), 
		                                         aiProcess_Triangulate |
		                                         aiProcess_GenSmoothNormals | 
		                                         aiProcess_FlipUVs |
		                                         aiProcess_CalcTangentSpace);
		
		if(!scene)
		{
			std::cout << "Mesh load failed!: " << fileName << std::endl;
			assert(0 == 0);
		}
		
		const aiMesh* model = scene->mMeshes[0];
		
		std::vector<Vector3f> positions;
		std::vector<Vector2f> texCoords;
		std::vector<Vector3f> normals;
		std::vector<Vector3f> tangents;
		std::vector<unsigned int> indices;

		const aiVector3D aiZeroVector(0.0f, 0.0f, 0.0f);
		for(unsigned int i = 0; i < model->mNumVertices; i++) 
		{
			const aiVector3D pos = model->mVertices[i];
			const aiVector3D normal = model->mNormals[i];
			const aiVector3D texCoord = model->HasTextureCoords(0) ? model->mTextureCoords[0][i] : aiZeroVector;
			const aiVector3D tangent = model->mTangents[i];

			positions.push_back(Vector3f(pos.x, pos.y, pos.z));
			texCoords.push_back(Vector2f(texCoord.x, texCoord.y));
			normals.push_back(Vector3f(normal.x, normal.y, normal.z));
			tangents.push_back(Vector3f(tangent.x, tangent.y, tangent.z));
		}

		for(unsigned int i = 0; i < model->mNumFaces; i++)
		{
			const aiFace& face = model->mFaces[i];
			assert(face.mNumIndices == 3);
			indices.push_back(face.mIndices[0]);
			indices.push_back(face.mIndices[1]);
			indices.push_back(face.mIndices[2]);
		}
		
		m_meshData = new MeshData(IndexedModel(indices, positions, texCoords, normals, tangents));
		s_resourceMap.insert(std::pair<std::string, MeshData*>(fileName, m_meshData));
	}
}