-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLadeObj.h
196 lines (179 loc) · 5.24 KB
/
LadeObj.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
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
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// soll unabhängig von linmath.h sein, daher Redefinition von vec2/3
struct t_vec3 {
t_vec3(char *buf) // generiere vec3 aus string
{
char *next;
strtok_r(buf, " ", &next);
x = atof(strtok_r(next, " ", &next));
y = atof(strtok_r(next, " ", &next));
z = atof(strtok_r(next, " ", &next));
}
t_vec3() { x = y = z = 0; }
float x, y, z;
};
struct t_vec2 {
t_vec2(char *buf) // generiere vec2 aus string
{
char *next;
strtok_r(buf, " ", &next);
u = atof(strtok_r(next, " ", &next));
v = atof(strtok_r(next, " ", &next));
}
float u, v;
};
struct t_triangleIndex {
t_triangleIndex(char *vert1, char *vert2, char *vert3) {
char *next;
ver[0] = atoi(strtok_r(vert1, "/", &next));
tex[0] = atoi(strtok_r(next, "/", &next));
norm[0] = atoi(strtok_r(next, "/", &next));
ver[1] = atoi(strtok_r(vert2, "/", &next));
tex[1] = atoi(strtok_r(next, "/", &next));
norm[1] = atoi(strtok_r(next, "/", &next));
ver[2] = atoi(strtok_r(vert3, "/", &next));
tex[2] = atoi(strtok_r(next, "/", &next));
norm[2] = atoi(strtok_r(next, "/", &next));
}
int ver[3];
int tex[3];
int norm[3];
};
struct myVertexType {
float x, y, z;
float xn, yn, zn;
float u, v;
myVertexType(t_vec3 vert, t_vec3 norm) {
x = vert.x;
y = vert.y;
z = vert.z;
xn = norm.x;
yn = norm.y;
zn = norm.z;
u = v = 0;
}
myVertexType(t_vec3 vert, t_vec3 norm, t_vec2 uv) {
x = vert.x;
y = vert.y;
z = vert.z;
xn = norm.x;
yn = norm.y;
zn = norm.z;
u = uv.u;
v = uv.v;
}
};
// load BMP from file "filename", pointer to "number" returns the number of
// vertices, returns VertexArray
myVertexType *loadModel(const char *filename, int *number) {
FILE *f = fopen(filename, "r");
// printf("file --> %ld\n", sizeof(f));
t_vec3 *vertexCoordinates = NULL;
t_vec3 *vertexNormals = NULL;
t_vec2 *vertexTextureUV;
t_triangleIndex *triangles;
myVertexType *vertices;
int numVertices = 0;
int numNormals = 0;
int numTexture = 0;
int numTable = 0;
int numTriangles = 0;
if (f == NULL) {
printf("Objekt Datei %s nicht gefunden!", filename);
exit(0);
} else
printf("Objekt Datei %s gefunden!\n", filename);
char buf[255];
// printf("buf --> %d\n", buf[0]);
while (fgets(buf, 255, f)) {
if (buf[0] == 'v')
switch (buf[1]) {
case (' '):
numVertices++;
break;
case ('t'):
numTexture++;
break;
case ('n'):
numNormals++;
break;
default:
break;
}
if (buf[0] == 'f')
numTable++;
// printf("%s", buf);
}
// printf("buf --> %d\n", buf[0]);
fseek(f, 0, SEEK_SET);
printf("Gefunden wurden %d Vertexe, %d Normalen, %d Texturekoordinaten und "
"eine Tabelle mit %d Dreiecken\n",
numVertices, numNormals, numTexture, numTable);
vertexCoordinates = (t_vec3 *)malloc(numVertices * sizeof(t_vec3));
vertexNormals = (t_vec3 *)malloc(numNormals * sizeof(t_vec3));
vertexTextureUV = (t_vec2 *)malloc(numTexture * sizeof(t_vec2));
triangles = (t_triangleIndex *)malloc(numTable * sizeof(t_triangleIndex));
vertices = (myVertexType *)malloc(
numTable * 3 *
sizeof(myVertexType)); //*3 because of three vertices per triangle
char *next;
char *cont;
float x;
float y;
float z;
numVertices = 0;
numNormals = 0;
numTexture = 0;
numTable = 0;
while (fgets(buf, 255, f)) {
if (buf[0] == 'v')
switch (buf[1]) {
case (' '):
vertexCoordinates[numVertices] = t_vec3(buf);
numVertices++;
break;
case ('t'):
vertexTextureUV[numTexture] = t_vec2(buf);
numTexture++;
break;
case ('n'):
vertexNormals[numNormals] = t_vec3(buf);
numNormals++;
break;
default:
break;
}
if (buf[0] == 'f') // one face -- one triangle
{
char *next;
char *vert1;
char *vert2;
char *vert3;
strtok_r(buf, " ", &next); // f away
vert1 = strtok_r(next, " ", &next);
vert2 = strtok_r(next, " ", &next);
vert3 = strtok_r(next, " ", &next);
triangles[numTable++] = t_triangleIndex(vert1, vert2, vert3);
}
}
// generate vertices
for (int i = 0; i < numTable; i++) {
vertices[i * 3] = myVertexType(vertexCoordinates[triangles[i].ver[0] - 1],
vertexNormals[triangles[i].norm[0] - 1],
vertexTextureUV[triangles[i].tex[0] - 1]);
vertices[i * 3 + 1] =
myVertexType(vertexCoordinates[triangles[i].ver[1] - 1],
vertexNormals[triangles[i].norm[1] - 1],
vertexTextureUV[triangles[i].tex[1] - 1]);
vertices[i * 3 + 2] =
myVertexType(vertexCoordinates[triangles[i].ver[2] - 1],
vertexNormals[triangles[i].norm[2] - 1],
vertexTextureUV[triangles[i].tex[2] - 1]);
//-1 because of index start at 1 in obj-file
}
*number = numTable * 3;
return vertices;
}