Created
November 3, 2013 11:21
-
-
Save a60814billy/7289167 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <cstdio> | |
#include <GL/freeglut.h> | |
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
#include <list> | |
#include <vector> | |
using namespace std; | |
typedef struct _vertex | |
{ | |
GLfloat data[3]; | |
} vertex; | |
typedef struct _face | |
{ | |
int vertex1; | |
int vertex2; | |
int vertex3; | |
} face; | |
vector<vertex> vertexs; | |
vector<face> faces; | |
void init(){ | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); | |
glutInitWindowSize(800,600); | |
glutCreateWindow("Homework"); | |
} | |
float angle = 10.0f; | |
void ChangeSize(int w, int h){ | |
glViewport(0,0,w,h); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(10.0f , 1 , 0.0f , 500.0f); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
gluLookAt(0,0,50, | |
0,0,0, | |
0,1,0); | |
} | |
void RenderScene(){ | |
glClear(GL_COLOR_BUFFER_BIT); | |
//glPointSize(10.0f); | |
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); | |
//glLineWidth(5.0f); | |
glBegin(GL_TRIANGLES); | |
glPointSize(3.0f); | |
glColor3f(0.0,0.0,0.0); | |
int i = 0; | |
for(vector<face>::iterator lfit = faces.begin() ; lfit != faces.end() ; lfit++){ | |
glVertex3fv(vertexs[(lfit->vertex1)-1].data); | |
glVertex3fv(vertexs[(lfit->vertex2)-1].data); | |
glVertex3fv(vertexs[(lfit->vertex3)-1].data); | |
i++; | |
} | |
cout << i << endl; | |
glEnd(); | |
glutSwapBuffers(); | |
} | |
void readFile(){ | |
fstream fin; | |
string i; | |
string j; | |
//fin.open("C:/octahedron.obj" , ios::in); | |
fin.open("C:/gourd.obj" , ios::in); | |
//fin.open("C:/teapot.obj" , ios::in); | |
//fin.open("C:/teddy.obj" , ios::in); | |
while(getline(fin,i)){ | |
if(i == "")continue; | |
istringstream is(i); | |
is >> j; | |
if(j == "#"){ | |
}else if(j == "g"){ | |
}else if(j == "v"){ | |
vertex newVertex; | |
is >> newVertex.data[2] >> newVertex.data[1] >> newVertex.data[0]; | |
vertexs.push_back(newVertex); | |
}else if(j == "f"){ | |
face newFace; | |
is >> newFace.vertex1 >> newFace.vertex2 >> newFace.vertex3 ; | |
faces.push_back(newFace); | |
} | |
} | |
fin.close(); | |
} | |
void SpecialKeys(int key, int x, int y){ | |
switch(key){ | |
case GLUT_KEY_UP: | |
glRotatef(1.0,1,0,0); | |
break; | |
case GLUT_KEY_DOWN: | |
glRotatef(-1.0,1,0,0); | |
break; | |
case GLUT_KEY_LEFT: | |
glRotatef(1.0,0,0,1); | |
break; | |
case GLUT_KEY_RIGHT: | |
glRotatef(-1.0,1,0,1); | |
break; | |
} | |
glutPostRedisplay(); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
readFile(); | |
glutInit(&argc, argv); | |
init(); | |
glClearColor(1.0,1.0,1.0,1.0); | |
glutReshapeFunc(ChangeSize); | |
glutDisplayFunc(RenderScene); | |
glutSpecialFunc(SpecialKeys); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment