-
Notifications
You must be signed in to change notification settings - Fork 0
/
objloader.cpp
172 lines (150 loc) · 5.35 KB
/
objloader.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*****************************************************************************************
*Loads obj file - limited to vertices, faces, normals, texture maps
*loads to object structure as defined in .h file
******************************************************************************************/
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <io.h>
#include <stdlib.h>
#include "GL/glut.h"
#include "objloader.h"
using namespace std;
void object_type::render()
{
glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)
for (int j=0;j<polygons_qty;j++)
{
//----------------- FIRST VERTEX -----------------
//Normal coordinates of the first vertex
glNormal3f( normcoord[ polygon[j].n[0] - 1 ].i,
normcoord[ polygon[j].n[0] - 1 ].j,
normcoord[ polygon[j].n[0] - 1 ].k);
// Texture coordinates of the first vertex
glTexCoord2f( mapcoord[ polygon[j].t[0] - 1 ].u,
mapcoord[ polygon[j].t[0] - 1 ].v);
// Coordinates of the first vertex
glVertex3f( vertex[ polygon[j].v[0] - 1].x,
vertex[ polygon[j].v[0] - 1].y,
vertex[ polygon[j].v[0] - 1].z);
//----------------- SECOND VERTEX -----------------
//Normal coordinates of the first vertex
glNormal3f( normcoord[ polygon[j].n[1] - 1 ].i,
normcoord[ polygon[j].n[1] - 1 ].j,
normcoord[ polygon[j].n[1] - 1 ].k);
// Texture coordinates of the first vertex
glTexCoord2f( mapcoord[ polygon[j].t[1] - 1 ].u,
mapcoord[ polygon[j].t[1] - 1 ].v);
// Coordinates of the first vertex
glVertex3f( vertex[ polygon[j].v[1] - 1].x,
vertex[ polygon[j].v[1] - 1].y,
vertex[ polygon[j].v[1] - 1].z);
//----------------- THIRD VERTEX -----------------
//Normal coordinates of the first vertex
glNormal3f( normcoord[ polygon[j].n[2] - 1 ].i,
normcoord[ polygon[j].n[2] - 1 ].j,
normcoord[ polygon[j].n[2] - 1 ].k);
// Texture coordinates of the first vertex
glTexCoord2f( mapcoord[ polygon[j].t[2] - 1 ].u,
mapcoord[ polygon[j].t[2] - 1 ].v);
// Coordinates of the first vertex
glVertex3f( vertex[ polygon[j].v[2] - 1].x,
vertex[ polygon[j].v[2] - 1].y,
vertex[ polygon[j].v[2] - 1].z);
}
glEnd();
}
/* vertex_type vertex[MAX_VERTICES];
mapcoord_type mapcoord[MAX_VERTICES];
normcoord_type normcoord[MAX_NORMALS];
polygon_type polygon[MAX_POLYGONS];
int id_texture;
};*/
int object_type::objdatadisplay()
{
int i;
printf("VERTICES: %d\n",vertices_qty);
for (i =0;i<vertices_qty;i++)
{
printf("%f %f %f\n",vertex[i].x,vertex[i].y,vertex[i].z);
}
printf("NORMALS: %d\n",normcoord_qty);
for (i =0;i<normcoord_qty;i++)
{
printf("%f %f %f\n",normcoord[i].i,normcoord[i].j,normcoord[i].k);
}
printf("MAP COORDS: %d\n",mapcoord_qty);
for (i =0;i<mapcoord_qty;i++)
{
printf("%f %f\n",mapcoord[i].u,mapcoord[i].v);
}
printf("POLYGONS: %d\n",polygons_qty);
for (i=0;i<polygons_qty;i++) //for each vertex of polygon (triangle)
{
for (int j = 0;j<3;j++)
{
printf("%d::%d/%d/%d\n",i,polygon[i].v[j],polygon[i].t[j],polygon[i].n[j]);
}
}
return 1;
}
int object_type::objloader(char *p_filename)
{
int ivertex=0; //Index variable
int inormal =0;
int ipolygon=0;
int imap=0;
char string[256];
FILE *l_file; //File pointer
char l_char; //Char variable
// int vertex;
unsigned short l_face_flags; //Flag that stores some face information
if ((l_file=fopen (p_filename, "rt"))== NULL) return 0; //Open the file
while (!feof(l_file)) //Loop to scan the whole file
{
fscanf(l_file,"%c",&l_char);
if(l_char=='\n')//read char if'/n' -skip to next and read
fscanf(l_file,"%c",&l_char);
switch (l_char) //parse
{
default: fgets(string,256,l_file);
break;
case 'v': //a vertex or a normal or a text co-ord
fscanf(l_file,"%c",&l_char);
switch (l_char)
{
case ' ': //a vertex -expect and so read 3 floats next
fscanf(l_file,"%f %f %f",&vertex[ivertex].x, &vertex[ivertex].y,&vertex[ivertex].z);
ivertex++;
break;
case 'n': //a normal -expect and so read 3 floats next
fscanf(l_file,"%f %f %f",&normcoord[inormal].i, &normcoord[inormal].j,&normcoord[inormal].k);
inormal++;
break;
case 't': //a texture map coord-expect and so read 2 floats next
fscanf(l_file,"%f %f",&mapcoord[imap].u, &mapcoord[imap].v);
imap++;
break;
} //end switch
break;
case 'f': //a face read next assume format is -> f 1/1/1 2/2/2 3/3/3
for (int i=0;i<3;i++) //for each vertex of polygon (triangle)
{
fscanf(l_file,"%c",&l_char); //read space char - ignore this
fscanf(l_file,"%d",&polygon[ipolygon].v[i]); //read vertex.
fscanf(l_file,"%c",&l_char); //read space char - ignore this
fscanf(l_file,"%d",&polygon[ipolygon].t[i]); //read text coord.
fscanf(l_file,"%c",&l_char); //read space char - ignore this
fscanf(l_file,"%d",&polygon[ipolygon].n[i]); //read normal.
}
ipolygon++;
break;
} //end switch
}
fclose (l_file); // Closes the file stream
vertices_qty = ivertex;
polygons_qty = ipolygon;
mapcoord_qty = imap;
normcoord_qty = inormal;
return 1; //if successful
}