-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.h
155 lines (142 loc) · 3.3 KB
/
Object.h
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
//
// Created by 田淙宇 on 2018/10/21.
//
#ifndef GRENDER_OBJECT_H
#define GRENDER_OBJECT_H
#include <vector>
#include <fstream>
#include <cstring>
#include <cmath>
#include "Matrix.h"
using std::vector;
using std::string;
using std::ifstream;
class Triangle{
public:
Triangle(){}
Triangle(const Vec3i& vl){
vertex[0]=vl[0];
vertex[1]=vl[1];
vertex[2]=vl[2];
}
Triangle(int *v){
vertex[0]=v[0];
vertex[1]=v[1];
vertex[2]=v[2];
}
Triangle(int a,int b,int c){
vertex[0]=a;
vertex[1]=b;
vertex[2]=c;
}
int& operator[](int index){
return vertex[index];
}
int vert(int index)const {
return vertex[index];
}
int UV(int idx)const{
return uv[idx];
}
int norm(int idx)const{
return normal[idx];
}
void setUv(int a,int b,int c){
uv[0]=a;
uv[1]=b;
uv[2]=c;
}
void setNormal(int a,int b,int c){
normal[0]=a;
normal[1]=b;
normal[2]=c;
}
private:
//存储顶点序列
int vertex[3];
//存储uv坐标
int uv[3];
//存储法线坐标
int normal[3];
};
class Object{
public:
Object(const string& filename){
ifstream in(filename,std::ios::in);
if(!in.is_open()){
std::cerr<<"read false";
exit(0);
}
while(!in.eof())
{
string flag;
in >>flag;
float x,y,z;
if(flag=="v") {
in >> x >> y >> z;
vertexList.emplace_back(Vec3f(x, y, z));
}
else if(flag=="vt") {
in >> x >> y >> z;
uvList.emplace_back(Vec3f(x, y, z));
}
else if(flag=="vn") {
in >> x >> y >> z;
normalList.emplace_back(Vec3f(x, y, z));
}
else if(flag=="f") {
int v1, v2, v3, uv1, uv2, uv3, n1, n2, n3;
in >> v1;
in.ignore();
in >> uv1;
in.ignore();
in >> n1;
in >> v2;
in.ignore();
in >> uv2;
in.ignore();
in >> n2;
in >> v3;
in.ignore();
in >> uv3;
in.ignore();
in >> n3;
Triangle temp(v1, v2, v3);
temp.setUv(uv1, uv2, uv3);
temp.setNormal(n1, n2, n3);
faceList.push_back(temp);
}
}
in.close();
}
//返回第face个面的第i个顶点
Vec3f vert(int face,int i)const{
return vertexList[faceList[face].vert(i)-1];
}
//返回uv
Vec3f uv(int face,int i)const{
return uvList[faceList[face].UV(i)];
}
//返回法线
Vec3f normal(int face,int i)const{
return normalList[faceList[face].norm(i)];
}
//返回面的数量
int length()const{
return faceList.size();
}
bool uv_avaliable()const{
return uvList.size()>0;
}
bool normal_avaliable()const{
return normalList.size()>0;
}
private:
//顶点列表
vector<Vec3f> vertexList;
//三角面列表
vector<Triangle> faceList;
vector<Vec3f> uvList;
vector<Vec3f>normalList;
};
#endif //GRENDER_OBJECT_H