1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
struct Triangle {
int v1;
int v2;
int v3;
};
struct Vertex {
GLfloat x;
GLfloat y;
GLfloat z;
};
void DrawOBJ(char *fName)
{
Vertex v[50000];
Triangle t[50000];
int vertexCount = 0;
int triangleCount = 0;
char line[100];
FILE *fp = fopen(fName,"r");
if (fp != NULL)
{
while (fgets(line, 99, fp))
{
if (line[0] == 'v')
{
sscanf(line, "%*c %f %f %f", &v[vertexCount].x, &v[vertexCount].y, &v[vertexCount].z);
vertexCount++;
}
else if (line[0] == 'f')
{
sscanf(line, "%*c %d %d %d", &t[triangleCount].v1, &t[triangleCount].v2, &t[triangleCount].v3);
triangleCount++;
}
}
}
fclose(fp);
car=glGenLists(1);
glPointSize(2.0);
glNewList(car, GL_COMPILE);
{
glPushMatrix();
glBegin(GL_TRIANGLES);
for (int i=0; i<triangleCount; i++)
{
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(v[t[i].v1-1].x, v[t[i].v1-1].y, v[t[i].v1-1].z);
glVertex3f(v[t[i].v2-1].x, v[t[i].v2-1].y, v[t[i].v2-1].z);
glVertex3f(v[t[i].v3-1].x, v[t[i].v3-1].y, v[t[i].v3-1].z);
}
glEnd();
}
glPopMatrix();
glEndList();
}
|