Exemple #1
0
void Mesh::ReadVertexData(FILE *fp, int num, bool have_normals, bool have_tex)
{
	double n[3];
	double v[3];
	double t[2];
	char buf[1024];
	for(int i = 0; i < num; i++)
	{
		fgets(buf, 1024, fp);
		if(have_normals && have_tex)
		{
			sscanf(buf, "%lf %lf %lf %lf %lf %lf %lf %lf", v, v+1, v+2, n, n+1, n+2, t, t+1);
			AddVertex(v);
			AddNormal(n);
			AddTexcoord(t);
		}
		else if(have_normals)
		{
			sscanf(buf, "%lf %lf %lf %lf %lf %lf", v, v+1, v+2, n, n+1, n+2);
			AddVertex(v);
			AddNormal(n);
		}
		else if(have_tex)
		{
			sscanf(buf, "%lf %lf %lf %lf %lf", v, v+1, v+2, t, t+1);
			AddVertex(v);
			AddTexcoord(t);
		}
		else
		{
			sscanf(buf, "%lf %lf %lf", v, v+1, v+2);
			AddVertex(v);
		}
	}
}
Exemple #2
0
/*
====================
BuildNormal
====================
*/
void G3DMExport::BuildNormal( MESH& mesh )
{
	// clear the normals currently in there
	for(std::vector<VTNIS>::iterator it = mesh.vertexes.begin(); it != mesh.vertexes.end(); it++)
	{ it->normal = Point3::Origin; }

	// build the normals
	for(std::vector<TRIANGLE>::iterator it = mesh.triangles.begin(); it != mesh.triangles.end(); it++)	
	{
		TRIANGLE& tri = *it;
		Point3 v1 = mesh.vertexes[tri.index1[1]].pos - mesh.vertexes[tri.index1[0]].pos;
		Point3 v2 = mesh.vertexes[tri.index1[2]].pos - mesh.vertexes[tri.index1[0]].pos;
		Point3 normal = v1^v2;
		normal = Normalize(normal);

		v1 = mesh.vertexes[tri.index1[1]].pos - mesh.vertexes[tri.index1[0]].pos;
		v2 = mesh.vertexes[tri.index1[2]].pos - mesh.vertexes[tri.index1[0]].pos;
		AddNormal( mesh, normal * NormalAngle(v1, v2), tri.index0[0], tri.smoothing, tri.index1[0] );

		v1 = mesh.vertexes[tri.index1[2]].pos - mesh.vertexes[tri.index1[1]].pos;
		v2 = mesh.vertexes[tri.index1[0]].pos - mesh.vertexes[tri.index1[1]].pos;
		AddNormal( mesh, normal * NormalAngle(v1, v2), tri.index0[1], tri.smoothing, tri.index1[1] );

		v1 = mesh.vertexes[tri.index1[0]].pos - mesh.vertexes[tri.index1[2]].pos;
		v2 = mesh.vertexes[tri.index1[1]].pos - mesh.vertexes[tri.index1[2]].pos;
		AddNormal( mesh, normal * NormalAngle(v1, v2), tri.index0[2], tri.smoothing, tri.index1[2] );
	}

	// normalize all of the vertexes
	for(std::vector<VTNIS>::iterator it = mesh.vertexes.begin(); it != mesh.vertexes.end(); it++)
	{ it->normal = Normalize(it->normal); }
}
Exemple #3
0
Mesh::Mesh(Program &program)
	: Drawable(program)
	, vbuffer(-1)
	, nbuffer(-1)
	, tbuffer(-1)
	, fbuffer(-1)
	, texture(NULL)
{
	AddVertex(-1.0, -1.0, -1.0);
	AddVertex(-1.0, 1.0, -1.0);
	AddVertex(1.0, 1.0, -1.0);
	AddVertex(1.0, -1.0, -1.0);

	AddTexcoord(0, 1);
	AddTexcoord(0, 0);
	AddTexcoord(1, 0);
	AddTexcoord(1, 1);

	AddColour(1.0, 0.0, 0.0);
	AddColour(0.0, 1.0, 0.0);
	AddColour(0.0, 0.0, 1.0);
	AddColour(1.0, 1.0, 1.0);

	AddNormal(0.0, 0.0, 1.0);
	AddNormal(0.0, 0.0, 1.0);
	AddNormal(0.0, 0.0, 1.0);
	AddNormal(0.0, 0.0, 1.0);

	AddFace(0, 1, 2);
	AddFace(2, 3, 0);
	InitBuffers();
}
Exemple #4
0
int Mesh::NewNormal(int a, int b, Scalar c)
{
	Vector &A = GetNormal(a);
	Vector &B = GetNormal(b);

	return AddNormal(A*c + B*(1-c));
}
Exemple #5
0
void Mesh::CalcNormals()
{
	std::vector<Face>::iterator vitrf;
	std::vector<VertexID>::iterator vitri;
	Vector3 va;
	Vector3 vc;
	Vector3 vNormal;

	// for each face
	vitrf = vecFaces.begin();
	while (vitrf != vecFaces.end()) {
		vc = vecPts[vitrf->vecVert.at(0).vertIndex]-vecPts[vitrf->vecVert.at(1).vertIndex];
		va = vecPts[vitrf->vecVert.at(2).vertIndex]-vecPts[vitrf->vecVert.at(1).vertIndex];
		vNormal = va.cross(vc);
		vNormal.normalize();

		// for each index in the face
		vitri = vitrf->vecVert.begin();
		int ni = AddNormal(vNormal);
		while (vitri != vitrf->vecVert.end()) {
			(*vitri).normIndex = ni;
			++vitri;
		}
		++vitrf;
	}
}
Exemple #6
0
//*********************
//  ASCII
//******************
void ModelLoader::ReadASCIIObj(const tstring& assetName)
{
    ifstream stream;
    stream.open(assetName.c_str());
    if (stream.fail())
    {
        wcout << "File open fail: '" << assetName << "'\n";
        return;
    }

    string line;
    
    vector<vector<int>> faceData;
    for (int i = 0; i < 3; ++i)
    {
        faceData.push_back(vector<int>(3));
    }
    while (stream.eof() == false)
    {
        getline(stream, line);
        if (line.find("vn", 0) == 0)
        {
            Vector3 v;
            sscanf_s(line.c_str(), "vn %f %f %f", &v.X, &v.Y, &v.Z);
            AddNormal(v);
        }
        else if (line.find("vt", 0) == 0)
        {
            Vector2 v;
            sscanf_s(line.c_str(), "vt %f %f ", &v.X, &v.Y);
            AddTexCoord(v);
        }
        else if (line.find("f", 0) == 0)
        {
            sscanf_s(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", 
                                          &faceData[0][0], &faceData[0][1], &faceData[0][2],
                                          &faceData[1][0], &faceData[1][1], &faceData[1][2],
                                          &faceData[2][0], &faceData[2][1], &faceData[2][2]);
            AddTri(faceData);
        }
        else if (line.find("g", 0) == 0)
        {
            wchar_t s[40];
            sscanf_s(line.c_str(), "g %s40", s, _countof(s));
            AddMesh(s);
        }
        else if (line.find("v", 0) == 0) //v is 0'd char
        {
            Vector3 v;
            sscanf_s(line.c_str(), "v %f %f %f", &v.X, &v.Y, &v.Z);
            AddVertex(v);
        }
    }
    FlushMesh(); //apply last mesh

    stream.close();
}
Exemple #7
0
bool triebWerk::COBJParser::LoadOBJ(const char * a_pPath)
{

	std::cout << a_pPath << std::endl;
	m_Vertices = new CMesh::SVertex[m_MAX_VERTICES];
	m_VertexPoint = new DirectX::XMFLOAT3[m_MAX_VERTICES];
	m_UV = new DirectX::XMFLOAT2[m_MAX_VERTICES];
	m_Normal = new DirectX::XMFLOAT3[m_MAX_VERTICES];

	//Let the FileReader preload the data
	bool success = ReadData(a_pPath);
	
	if (!success)
		return false;

	//Reserve memory for the string
	char* line;

	do
	{
		//Get Line to read
		line = GetLine();

		if (line[0] == '#' || line[0] == '\r')
			continue;

		if (BeginLineWith(line, "v "))
		{
			AddVertexPoint(line);
		}
		else if(BeginLineWith(line, "vt "))
		{
			AddUV(line);
		}
		else if(BeginLineWith(line, "vn "))
		{
			AddNormal(line);
		}
		else if(BeginLineWith(line, "f "))
		{
			ReadFaces(line);
		}

	} while (ReachedEndOfFile() != true);

	if (m_VertexCount > 0 )
	{
		m_pVertices = m_Vertices;
		//m_pIndices = &m_Indices[0];
	}
	else
	{
		return false;
	}

	m_IndexCount = m_VertexCount;

	delete[] m_UV;
	delete[] m_Normal;
	delete[] m_VertexPoint;

	return true;
}
Exemple #8
0
// to read in a filed mesh
// Currently ignores material info.
int Mesh::readFile(std::string fileName)
{
	int nVertis = 0;
	int nFaces = 0;
	int nFaceVerts = 0;
	int nVNormals = 0;
	int nFNormals = 0;
	int nFNormVerts = 0;
	int nTexCoors = 0;
	int nMaterials = 0;
	int nMaterialFacesAdded = 0;
	std::string line;
	std::string token;
	bool bInMesh = false;
	bool bInVertices = false;
	bool bInFaces = false;
	bool bFirstLine = false;
	bool bSecondLine = false;
	bool bInVNormals = false;
	bool bInFNormals = false;
	bool bInMaterials = false;
	bool bInMaterialList = false;
	bool bInTexCoors = false;

	size_t pos;
	Vector3 p3;
	int index[4]; // support triangles or quads only
	float vert[4]; // also used for RGBA

	std::ifstream myfile (fileName.c_str());
	if (myfile.is_open())
	{
		while ( myfile.good() )
		{
			getline (myfile,line);
			//std::cout << line << std::endl;

			if ((pos = line.find("//")) != std::string::npos) {
					line = line.substr(0, pos);
			}
			if ((pos = line.find("#")) != std::string::npos) {
					line = line.substr(0, pos);
			}
			
			if (line.length() == 0) {
				continue;
			}
			else if ((pos = line.find("Mesh ")) != std::string::npos) {
				bInMesh = true;
				bInVertices = true;
				bFirstLine = true;
			}
			else if (bInVertices && bFirstLine) {
				bFirstLine = false;
				pos = line.find(";");
				if (pos != std::string::npos) {
					line.erase(pos);
					nVertis = atoi(line.c_str());
				}
				else {
					myfile.close();
					return(1);
				}
			}
			else if (bInVertices) {
				int i = 0;
				while (i < 3) {
					if ((pos = line.find(";")) != std::string::npos) {
						token = line.substr(0, pos);
						line = line.substr(pos+1);
						vert[i] = atof(token.c_str());
						i++;
					}
					else {
						myfile.close();
						return(1);
					}
				}
				p3 = vert;
				AddVertex(p3);
				if (nVertis == numVerts) {
					bInVertices = false;
					bInFaces = true;
					bFirstLine = true;
				}
			}


			else if (bInFaces && bFirstLine) {
				bFirstLine = false;
				pos = line.find(";");
				if (pos != std::string::npos) {
					line.erase(pos);
					nFaces = atoi(line.c_str());
				}
				else {
					myfile.close();
					return(1);
				}
			}
			else if (bInFaces) {
				int i = 0;
				if ((pos = line.find(";")) != std::string::npos) {
					token = line.substr(0, pos);
					line = line.substr(pos+1);
					nFaceVerts = atoi(token.c_str());
				}
				else {
					myfile.close();
					return(1);
				}
				while (i < nFaceVerts) {
					if ((pos = line.find(",")) != std::string::npos) {
						token = line.substr(0, pos);
						line = line.substr(pos+1);
						index[i] = atoi(token.c_str());
						i++;
					}
					else if ((pos = line.find(";")) != std::string::npos) {
						token = line.substr(0, pos);
						line = line.substr(pos+1);
						index[i] = atoi(token.c_str());
						i++;
					}
					else {
						myfile.close();
						return(1);
					}
				}
				AddFace(nFaceVerts, index);
				if (nFaces == numFaces) {
					bInFaces = false;
				}
			}




			else if ((pos = line.find("MeshNormals")) != std::string::npos) {
				bInVNormals = true;
				bFirstLine = true;
			}
			else if (bInVNormals && bFirstLine) {
				bFirstLine = false;
				pos = line.find(";");
				if (pos != std::string::npos) {
					line.erase(pos);
					nVNormals = atoi(line.c_str());
				}
				else {
					myfile.close();
					return(1);
				}
			}
			else if (bInVNormals) {
				int i = 0;
				while (i < 3) {
					if ((pos = line.find(";")) != std::string::npos) {
						token = line.substr(0, pos);
						line = line.substr(pos+1);
						vert[i] = atof(token.c_str());
						i++;
					}
					else {
						myfile.close();
						return(1);
					}
				}
				p3 = vert;
				AddNormal(p3);
				if (nVNormals == numNormals) {
					bInVNormals = false;
					bInFNormals = true;
					bFirstLine = true;
				}
			}

			else if (bInFNormals && bFirstLine) {
				bFirstLine = false;
				pos = line.find(";");
				if (pos != std::string::npos) {
					line.erase(pos);
					//nFNormals = atoi(line.c_str());
				}
				else {
					myfile.close();
					return(1);
				}
			}
			else if (bInFNormals) {
				int i = 0;
				if ((pos = line.find(";")) != std::string::npos) {
					token = line.substr(0, pos);
					line = line.substr(pos+1);
					nFNormVerts = atoi(token.c_str());
				}
				else {
					myfile.close();
					return(1);
				}
				while (i < nFNormVerts) {
					if ((pos = line.find(",")) != std::string::npos) {
						token = line.substr(0, pos);
						line = line.substr(pos+1);
						index[i] = atoi(token.c_str());
						i++;
					}
					else if ((pos = line.find(";")) != std::string::npos) {
						token = line.substr(0, pos);
						line = line.substr(pos+1);
						index[i] = atoi(token.c_str());
						i++;
					}
					else {
						myfile.close();
						return(1);
					}
				}
				AddNormal(nFNormals, index);
				nFNormals++;
				if (nFNormals == numFaces) {
					bInFNormals = false;
				}
			}

			else if ((pos = line.find("MaterialList ")) != std::string::npos) {
				bInMaterialList = true;
				bFirstLine = true;
			}
			else if (bInMaterialList && bFirstLine) {
				bFirstLine = false;
				bSecondLine = true;

				int numMat = atoi(line.c_str());
				if (numMat == 0) {
					bInMaterialList = false;
					bSecondLine = false;
				}
			}
			else if (bInMaterialList && bSecondLine) {
				bFirstLine = false;
				bSecondLine = false;
			}
			else if (bInMaterialList) {
				vecFaces[nMaterialFacesAdded].materialIndex = atoi(line.c_str());
				nMaterialFacesAdded++;
				if (nMaterialFacesAdded == nFaces) {
					bInMaterialList = false;
				}
			}


			else if ((pos = line.find("Material ")) != std::string::npos) {
				bInMaterials = true;
				bFirstLine = true;
			}
			else if (bInMaterials && bFirstLine) {
				bInMaterials = false;
				bFirstLine = false;
				int i = 0;

				while (i < 4) {
					if ((pos = line.find(";")) != std::string::npos) {
						token = line.substr(0, pos);
						line = line.substr(pos+1);
						vert[i] = atof(token.c_str());
						i++;
					}
					else {
						myfile.close();
						return(1);
					}
				}
				AddMaterial(vert);
			}



			else if ((pos = line.find("MeshTextureCoords")) != std::string::npos) {
				bInTexCoors = true;
				bFirstLine = true;
			}
			else if (bInTexCoors && bFirstLine) {
				bFirstLine = false;
				pos = line.find(";");
				if (pos != std::string::npos) {
					line.erase(pos);
					nTexCoors = atoi(line.c_str());
				}
				else {
					myfile.close();
					return(1);
				}
			}
			else if (bInTexCoors) {
				int i = 0;
				while (i < 2) {
					if ((pos = line.find(";")) != std::string::npos) {
						token = line.substr(0, pos);
						line = line.substr(pos+1);
						vert[i] = atof(token.c_str());
						i++;
					}
					else {
						myfile.close();
						return(1);
					}
				}
				p3 = vert;
				AddTexCoor(p3);
				if (nTexCoors == numTexCoors) {
					bInTexCoors = false;
					bFirstLine = true;
				}
			}




		}
		myfile.close();
	}
	else {
		std::cout << "Unable to open file" << std::endl; 
	}

	return(0);
}
Exemple #9
0
void Mesh::AddNormal(double *data)
{
	AddNormal(data[0], data[1], data[2]);
}