UNITY
MAYA
OpenGL
#include <iostream>
#include <fstream>
#include <glut.h>
#include "ModelData.h"
#include "ModelParser.h"
using namespace std;
vector<Vertex3D> Vertices;
vector<Normal3D> Normals;
vector<Face> Faces;
void LoadOBJ(const char *OBJFile, vector<Vertex3D> &vertices,vector<Normal3D> &normals, vector<Face> &faces)
{
ifstream ifs(OBJFile);
while(!ifs.eof())
{
char data [100];
ifs >> data; //Extract 1 word from the file
if (strcmp(data, "v") == 0)
{
float x,y,z; //Vertex data
//extract x,y,z components
ifs >> x;
ifs >> y;
ifs >> z;
Vertex3D vert; //Create Vertex3D Object
vert.x = x;
vert.y = y;
vert.z = z;
//add the vertex into the vector
vertices.push_back(vert);
}
else if (strcmp(data, "vn") == 0)
{
float x,y,z; //Vertex Normal
//Extract vertex normal
ifs >> x;
ifs >> y;
ifs >> z;
Normal3D norm; //create Normal3D object
norm.x = x;
norm.y = y;
norm.z = z;
//add this normal into the vector
normals.push_back(norm);
}
else if (strcmp(data, "f") == 0)
{
char faceData[100]; //Face Data
Face myFace;
while (true)
{
ifs >> faceData; //Extract next data
if (ContainSlash(faceData,100)) // check if data is valid
{
char Vertexindx[15];
char Normalindx[15];
ExtractVertexIdx(faceData, Vertexindx);
ExtractNormalIdx(faceData, Normalindx);
int vIndex = atoi(Vertexindx);
int nIndex = atoi(Normalindx);
myFace.VertexIndex.push_back(vIndex);
myFace.NormalIndex.push_back(nIndex);
}
else
{
faces.push_back(myFace); //INVALID face data
ifs.unget();
break;
}
}
}
}
}
void RenderModel(vector<Vertex3D> &vertices,vector<Normal3D> &normals,vector<Face> &faces)
{
glColor3f(0.5,0.5,0.5);
glLineWidth(2);
for(int i = 0; i < faces.size(); i++)
{
glBegin(GL_POLYGON);
//glBindTexture (GL_TEXTURE_2D, 1);
for (int j = 0; j < faces[i].NormalIndex.size(); j++)
{
int Normalindx = faces[i].NormalIndex[j]-1; //get the normal
float x,y,z;
x = normals[Normalindx].x;
y = normals[Normalindx].y;
z = normals[Normalindx].z;
glNormal3f(x,y,z);
//get the vertex
int Vertexindx = faces[i].VertexIndex[j] -1;
x = vertices[Vertexindx].x;
y = vertices[Vertexindx].y;
z = vertices[Vertexindx].z;
glVertex3f(x, y, z);
}
glEnd();
}
}
void GameScene()
{
//openGL Codes Here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(3.0,2.0,1.0,0.0);
glPushMatrix();
RenderModel(Vertices,Normals,Faces);
glPopMatrix();
glutSwapBuffers();
}
void Camera(int w, int h)
{
float ratio = 1.0 * w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(20,ratio,1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//intialize camera
gluLookAt(0.0,8.0,80.0, //Camera position
0.0,0.0,0.0, //where the camera is looking
0.0,1.0,0.0); // camera orientation
}
void main( int argC,char **ArgV)
{
char ModelName[100];
cout << "Enter model name: ";
cin >> ModelName;
LoadOBJ(ModelName,Vertices,Normals,Faces);
glutInit(&argC, ArgV);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 500);
glutCreateWindow("Model Loader");
glutDisplayFunc(GameScene);
glutIdleFunc(GameScene);
glutReshapeFunc(Camera);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glutMainLoop();
}
No comments:
Post a Comment