-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilding.cpp
220 lines (186 loc) · 5.33 KB
/
Building.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* EECE 478 - Assignment 3
*
* Navid Fattahi
* SID: 39937099
* April 3, 2013
*
* Development Environment:
* XCode 4.4.1 - MAC OSX 10.7.5
*
* Util.h
*
* With help from:
* http://netpbm.sourceforge.net/doc/ppm.html
* http://people.sc.fsu.edu/~jburkardt/cpp_src/ppmb_io/ppmb_io.html
*/
#include "Building.h"
#include "Util.h"
/* Parse Building model files */
void Building::parseBuilding(const char* path)
{
// used to iterate through items on each line
int paramCount = 0;
bool verline = false;
bool triline = false;
bool norline = false;
bool texline = false;
string pathstr(path);
string bName;
// Extract building name from path (i.e. string before '/' character)
int counter = 0;
while( counter < pathstr.size())
{
char ch = pathstr[counter];
if (ch == '/')
{
bName = pathstr.substr(0, counter);
root += bName;
root += '/';
}
counter++;
}
FILE * file = fopen(path, "r");
if( file == NULL )
{
printf("Model file: %s cannot be found!\n", path);
}/*
else
{
printf("Model File: %s\n", path);
}*/
char textline[256];
while (fscanf(file, "%s", textline) != EOF)
{
if (verline)
{
float num = atof(textline);
vertices.push_back(num);
paramCount++;
if (paramCount == 3) // each vertex has 3 parameters
{
verline = false;
paramCount = 0;
}
}
else if (triline)
{
float num = atof(textline);
triangles.push_back(num);
paramCount++;
if (paramCount == 11) // each triangle has 11 parameters
{
triline = false;
paramCount = 0;
}
}
else if (norline)
{
float num = atof(textline);
normals.push_back(num);
paramCount++;
if (paramCount == 3) // each normal has 3 parameters
{
norline = false;
paramCount = 0;
}
}
else if (texline)
{
string s(textline);
string path = root;
path += s;
textures.push_back(path);
texline = false;
paramCount = 0;
}
// if it's a single character
else if (textline[1] == '\0')
{
// it must mean that it's either a vertex or triangle
if (textline[0] == 'v')
verline = true;
else if (textline[0] == 't')
triline = true;
else if (textline[0] == 'n')
norline = true;
else if (textline[0] == 'i')
texline = true;
}
}
loadPPM();
}
void Building::loadPPM()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
putTexture();
}
void Building::putTexture()
{
for (unsigned int i = 0; i < textures.size(); i++)
{
int width, height;
unsigned char* imgppm;
imgppm = Util::readppm(textures[i], width, height);
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width, height,0,GL_RGB,GL_UNSIGNED_BYTE,imgppm);
texIDs.push_back(texID);
delete[] imgppm;
}
}
void Building::setTrans(float x, float y, float z)
{
trans.x = x;trans.y = y;trans.z = z;
}
void Building::setRotat(float x, float y, float z)
{
rotat.x = x;rotat.y = y;rotat.z = z;
}
void Building::setScale(float x, float y, float z)
{
scale.x = x;scale.y = y;scale.z = z;
}
int Building::getTextureSize()
{
int retval = texIDs.size() + texCount;
return retval;
}
void Building::setTexCount(int val)
{
texCount = val;
}
void Building::renderBuilding()
{
glPushMatrix();
// Scale, Translate, Rotate (note: inverse)
glTranslatef(trans.x, trans.y, trans.z);
glRotatef(rotat.x, 1, 0, 0);glRotatef(rotat.y, 0, 1, 0);glRotatef(rotat.z, 0, 0, 1);
glScalef(scale.x, scale.y, scale.z);
for (int i = 0; i < triangles.size(); i+=11)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, triangles[i+4] +texCount + 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_TRIANGLES);
int normal = triangles[i+3] * 3;
glNormal3f(normals[normal], normals[normal+1], normals[normal+2]);
int v1 = triangles[i] * 3;
int v2 = triangles[i+1] * 3;
int v3 = triangles[i+2] * 3;
glTexCoord2f(triangles[i+5], triangles[i+6]);
glVertex3f(vertices[v1], vertices[v1 + 1], vertices[v1 + 2]);
glTexCoord2f(triangles[i+7], triangles[i+8]);
glVertex3f(vertices[v2], vertices[v2 + 1], vertices[v2 + 2]);
glTexCoord2f(triangles[i+9], triangles[i+10]);
glVertex3f(vertices[v3], vertices[v3 + 1], vertices[v3 + 2]);
glEnd();
glDisable(GL_TEXTURE_2D);
}
glPopMatrix();
}