-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
executable file
·169 lines (151 loc) · 7.23 KB
/
Mesh.cpp
File metadata and controls
executable file
·169 lines (151 loc) · 7.23 KB
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
#include <fstream>
#include <algorithm>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
// Needed on MsWindows
#include <windows.h>
#endif // Win32 platform
#include <GLUT/glut.h>
#include "Mesh.h"
using namespace std;
//parses obj file
//obj file - list of vertices that represent triangles
//do not create mesh instance in c constructor so need initialize function in scene
Mesh::Mesh(const char *filename)
{
fstream file(filename);
if(!file.is_open())
{
return;
}
char buffer[256];
while(!file.eof())
{
file.getline(buffer,256);
rows.push_back(new string(buffer));
}
submeshFaces.push_back(std::vector<Face*>());
std::vector<Face*>* faces = &submeshFaces.at(submeshFaces.size()-1);
for(int i = 0; i < rows.size(); i++)
{
if(rows[i]->empty() || (*rows[i])[0] == '#')
continue;
else if((*rows[i])[0] == 'v' && (*rows[i])[1] == ' ')
{
float tmpx,tmpy,tmpz;
sscanf(rows[i]->c_str(), "v %f %f %f" ,&tmpx,&tmpy,&tmpz);
positions.push_back(new float3(tmpx,tmpy,tmpz));
}
else if((*rows[i])[0] == 'v' && (*rows[i])[1] == 'n')
{
float tmpx,tmpy,tmpz;
sscanf(rows[i]->c_str(), "vn %f %f %f" ,&tmpx,&tmpy,&tmpz);
normals.push_back(new float3(tmpx,tmpy,tmpz));
}
else if((*rows[i])[0] == 'v' && (*rows[i])[1] == 't')
{
float tmpx,tmpy;
sscanf(rows[i]->c_str(), "vt %f %f" ,&tmpx,&tmpy);
texcoords.push_back(new float2(tmpx,tmpy));
}
else if((*rows[i])[0] == 'f')
{
if(count(rows[i]->begin(),rows[i]->end(), ' ') == 3)
{
Face* f = new Face();
f->isQuad = false;
sscanf(rows[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d",
&f->positionIndices[0], &f->texcoordIndices[0], &f->normalIndices[0],
&f->positionIndices[1], &f->texcoordIndices[1], &f->normalIndices[1],
&f->positionIndices[2], &f->texcoordIndices[2], &f->normalIndices[2]);
faces->push_back(f);
}
else
{
Face* f = new Face();
f->isQuad = true;
sscanf(rows[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d",
&f->positionIndices[0], &f->texcoordIndices[0], &f->normalIndices[0],
&f->positionIndices[1], &f->texcoordIndices[1], &f->normalIndices[1],
&f->positionIndices[2], &f->texcoordIndices[2], &f->normalIndices[2],
&f->positionIndices[3], &f->texcoordIndices[3], &f->normalIndices[3]);
faces->push_back(f);
}
}
else if((*rows[i])[0] == 'g')
{
if(faces->size() > 0)
{
submeshFaces.push_back(std::vector<Face*>());
faces = &submeshFaces.at(submeshFaces.size()-1);
}
}
}
modelid = glGenLists(submeshFaces.size());
for(int iSubmesh=0; iSubmesh<submeshFaces.size(); iSubmesh++)
{
std::vector<Face*>& faces = submeshFaces.at(iSubmesh);
glNewList(modelid + iSubmesh,GL_COMPILE);
glBegin(GL_TRIANGLES);
for(int i=0;i<faces.size();i++)
{
if(faces[i]->isQuad)
{
glNormal3f(normals[faces[i]->normalIndices[0]-1]->x,normals[faces[i]->normalIndices[0]-1]->y,normals[faces[i]->normalIndices[0]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[0]-1]->x,1-texcoords[faces[i]->texcoordIndices[0]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[0]-1]->x,positions[faces[i]->positionIndices[0]-1]->y,positions[faces[i]->positionIndices[0]-1]->z);
glNormal3f(normals[faces[i]->normalIndices[1]-1]->x,normals[faces[i]->normalIndices[1]-1]->y,normals[faces[i]->normalIndices[1]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[1]-1]->x,1-texcoords[faces[i]->texcoordIndices[1]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[1]-1]->x,positions[faces[i]->positionIndices[1]-1]->y,positions[faces[i]->positionIndices[1]-1]->z);
glNormal3f(normals[faces[i]->normalIndices[2]-1]->x,normals[faces[i]->normalIndices[2]-1]->y,normals[faces[i]->normalIndices[2]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[2]-1]->x,1-texcoords[faces[i]->texcoordIndices[2]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[2]-1]->x,positions[faces[i]->positionIndices[2]-1]->y,positions[faces[i]->positionIndices[2]-1]->z);
glNormal3f(normals[faces[i]->normalIndices[1]-1]->x,normals[faces[i]->normalIndices[1]-1]->y,normals[faces[i]->normalIndices[1]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[1]-1]->x,1-texcoords[faces[i]->texcoordIndices[1]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[1]-1]->x,positions[faces[i]->positionIndices[1]-1]->y,positions[faces[i]->positionIndices[1]-1]->z);
glNormal3f(normals[faces[i]->normalIndices[2]-1]->x,normals[faces[i]->normalIndices[2]-1]->y,normals[faces[i]->normalIndices[2]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[2]-1]->x,1-texcoords[faces[i]->texcoordIndices[2]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[2]-1]->x,positions[faces[i]->positionIndices[2]-1]->y,positions[faces[i]->positionIndices[2]-1]->z);
glNormal3f(normals[faces[i]->normalIndices[3]-1]->x,normals[faces[i]->normalIndices[3]-1]->y,normals[faces[i]->normalIndices[3]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[3]-1]->x,1-texcoords[faces[i]->texcoordIndices[3]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[3]-1]->x,positions[faces[i]->positionIndices[3]-1]->y,positions[faces[i]->positionIndices[3]-1]->z);
}
else
{
glNormal3f(normals[faces[i]->normalIndices[0]-1]->x,normals[faces[i]->normalIndices[0]-1]->y,normals[faces[i]->normalIndices[0]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[0]-1]->x,1-texcoords[faces[i]->texcoordIndices[0]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[0]-1]->x,positions[faces[i]->positionIndices[0]-1]->y,positions[faces[i]->positionIndices[0]-1]->z);
glNormal3f(normals[faces[i]->normalIndices[1]-1]->x,normals[faces[i]->normalIndices[1]-1]->y,normals[faces[i]->normalIndices[1]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[1]-1]->x,1-texcoords[faces[i]->texcoordIndices[1]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[1]-1]->x,positions[faces[i]->positionIndices[1]-1]->y,positions[faces[i]->positionIndices[1]-1]->z);
glNormal3f(normals[faces[i]->normalIndices[2]-1]->x,normals[faces[i]->normalIndices[2]-1]->y,normals[faces[i]->normalIndices[2]-1]->z);
glTexCoord2f(texcoords[faces[i]->texcoordIndices[2]-1]->x,1-texcoords[faces[i]->texcoordIndices[2]-1]->y);
glVertex3f(positions[faces[i]->positionIndices[2]-1]->x,positions[faces[i]->positionIndices[2]-1]->y,positions[faces[i]->positionIndices[2]-1]->z);
}
}
glEnd();
glEndList();
}
}
void Mesh::draw()
{
for(int iSubmesh=0; iSubmesh<submeshFaces.size(); iSubmesh++)
glCallList(modelid + iSubmesh);
}
void Mesh::drawSubmesh(unsigned int iSubmesh)
{
glCallList(modelid + iSubmesh);
}
Mesh::~Mesh()
{
for(unsigned int i = 0; i < rows.size(); i++)
delete rows[i];
for(unsigned int i = 0; i < positions.size(); i++)
delete positions[i];
for(unsigned int i = 0; i < submeshFaces.size(); i++)
for(unsigned int j = 0; j < submeshFaces.at(i).size(); j++)
delete submeshFaces.at(i).at(j);
for(unsigned int i = 0; i < normals.size(); i++)
delete normals[i];
for(unsigned int i = 0; i < texcoords.size(); i++)
delete texcoords[i];
}