/
OBJ.cpp
96 lines (84 loc) · 2.48 KB
/
OBJ.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
//////////////////////////////////////////////////////////////////////////////////////
// Copyright © Charalambos "Charis" Poullis, charalambos@poullis.org //
// http://www.poullis.org //
//////////////////////////////////////////////////////////////////////////////////////
#ifndef __OBJ_CPP__
#define __OBJ_CPP__
#include "OBJ.h"
OBJ::OBJ() {
faces.clear();
normals.clear();
texture_coords.clear();
vertices.clear();
has_tex_coords = false;
has_normals = false;
}
OBJ::~OBJ() {
for (unsigned int i=0;i<faces.size();i++) {
delete faces[i];
}
faces.clear();
normals.clear();
texture_coords.clear();
vertices.clear();
}
void OBJ::AddFace(std::vector<int> const &vertexIndices,
std::vector<int> const &normalIndices,
std::vector<int> const &textureIndices) {
Face *face = new Face;
face->vertex_indices = vertexIndices;
face->normal_indices = normalIndices;
face->texture_indices = textureIndices;
faces.push_back(face);
return;
}
void OBJ::AddNormal(double nx,double ny,double nz) {
normals.push_back(Vector3d(nx,ny,nz));
return;
}
void OBJ::AddTextureCoord(double tx, double ty) {
texture_coords.push_back(Vector2d(tx,ty));
return;
}
void OBJ::AddVertex(double vx,double vy, double vz) {
vertices.push_back(Vector3d(vx,vy,vz));
return;
}
void OBJ::Print(void) {
for (int i=0;i<vertices.size();i++) {
printf("v %lf %lf %lf\n",vertices[i](0),vertices[i](1),vertices[i](2));
}
for (int i=0;i<normals.size();i++) {
printf("vn %lf %lf %lf\n",normals[i](0),normals[i](1),normals[i](2));
}
for (int i=0;i<texture_coords.size();i++) {
printf("vt %lf %lf\n",texture_coords[i](0),texture_coords[i](1));
}
std::vector<int> vertexIndices;
std::vector<int> normalIndices;
std::vector<int> textureIndices;
printf("Faces: %d\n",faces.size());
for (int i=0;i<faces.size();i++) {
vertexIndices = faces[i]->vertex_indices;
normalIndices = faces[i]->normal_indices;
textureIndices = faces[i]->texture_indices;
printf("f ");
for (int j=0;j<vertexIndices.size();j++) {
printf("%d",vertexIndices[j]+1);
if (normalIndices.size()!=0) {
printf("/%d",normalIndices[j]);
}
if (textureIndices.size()!=0) {
printf("/%d",textureIndices[j]);
}
printf(" ");
}
printf("\n");
}
printf("Vertices: %d\n", vertices.size());
printf("Normals: %d\n", normals.size());
printf("Faces: %d\n", faces.size());
printf("Texture coordinates: %d\n", texture_coords.size());
return;
}
#endif