-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
448 lines (366 loc) · 14.3 KB
/
util.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include "./lib.h"
int LINE_LENGTH=100;
extern ply_object bunny;
//reads a file to a char array
GLchar * simple_fileread(char * file_name, GLint * length)
{
#ifdef DEBUG
printf("loading data from file %s\n", file_name);
#endif
GLint chars = 0;
FILE *src;
src = fopen (file_name, "rt"); //open shader file
if (src == NULL){
fprintf(stderr, "Fatal error in opening file %s", file_name);
return NULL;
}
int currentsize = 128;
GLchar * shader = (char *) malloc(currentsize*sizeof(char));
char c;
while((c = fgetc(src)) != EOF)
{
shader[chars] = c;
chars++;
if (chars >= currentsize){
shader = (char *) realloc(shader, 2*currentsize*sizeof(char));
currentsize = 2*currentsize;
}
}
*length = chars;
#ifdef DEBUG
printf("done loading data from file %s\n", file_name);
#endif
return shader;
}
//reads a 24bit rgb file to a struct
img_data * simple_bmp_read(char * file_name)
{
#ifdef DEBUG
printf("loading texture from file %s\n", file_name);
#endif
int foo = 0;
img_data * pic_p;
pic_p = (img_data*)malloc(sizeof(img_data));
(*pic_p).whole = simple_fileread(file_name, &foo);
#ifdef DEBUG
printf("file is %i bytes \n", *((int*)&(*pic_p).whole[0x0002]));
#endif
(*pic_p).rgbstart = *((int*)&(*pic_p).whole[0x000A]); //start of file table
(*pic_p).width = *((int*)&(*pic_p).whole[0x0012]);
(*pic_p).height = *((int*)&(*pic_p).whole[0x0016]);
#ifdef DEBUG
printf("texture is %i by %i pixels\n", (*pic_p).width, (*pic_p).height);
#endif
#ifdef DEBUG
printf("done loading texturefrom file %s\n", file_name);
#endif
return pic_p;
}
void calc_normal(float * first, float * second, float * third, float * returnme)
{
float v1[3];
float v2[3];
float *r = returnme;
float normalization;
//compute vectors
v1[0] = first[0] - second[0];
v1[1] = first[1] - second[1];
v1[2] = first[2] - second[2];
v2[0] = first[0] - third[0];
v2[1] = first[1] - third[1];
v2[2] = first[2] - third[2];
//cross product
r[0] = v1[1]*v2[2]- v2[1]*v1[2];
r[1] = v2[0]*v1[2] - v1[0]*v2[2];
r[2] = v1[0]*v2[1]- v2[0]*v1[1];
// just calculate the normal the normalization should happen elsewhere
// the area of the triangle is important to weight the normal compared
// to other normals (?)
normalization = sqrt(r[0]*r[0] + r[1]*r[1] + r[2]*r[2]);
r[0] = r[0]/normalization;
r[1] = r[1]/normalization;
r[2] = r[2]/normalization;
}
void lazy_calc_normal(int a, int b, int c, float*ret){
//point vectors to the vertices for the triangle
float v1[3] = {bunny.vertices[(a * 3)],
bunny.vertices[(a * 3) + 1],
bunny.vertices[(a * 3) + 2]};
float v2[3] = {bunny.vertices[(b * 3)],
bunny.vertices[(b * 3) + 1],
bunny.vertices[(b * 3) + 2]};
float v3[3] = {bunny.vertices[(c * 3)],
bunny.vertices[(c * 3) + 1],
bunny.vertices[(c * 3) + 2]};
calc_normal(v1, v2, v3, ret);
}
float calc_dot_product(float * v1, float * v2)
{
return (v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]);
}
void recursive_orient(int face_index,
float * faces_normals,
int * faces_indices,
int ** vertex_in_faces,
bool * visited) {
if(visited[face_index])
return;
//mark this face as visited
visited[face_index] = true;
//current_face normal
float current_face_normal[3] = {
faces_normals[(face_index * 3)],
faces_normals[(face_index * 3) + 1],
faces_normals[(face_index * 3) + 2]
};
//vertex indices that is connected to the current_face
int vi1 = faces_indices[(face_index * 3)];
int vi2 = faces_indices[(face_index * 3) + 1];
int vi3 = faces_indices[(face_index * 3) + 2];
int i;
for(i = 0; i < 9; i++) {
//the face indexes that is connected to the current_face
int fi1 = vertex_in_faces[vi1][i];
int fi2 = vertex_in_faces[vi2][i];
int fi3 = vertex_in_faces[vi3][i];
if(fi1 != -1 && fi1 != face_index && !visited[fi1]) {
float nface_normal[3] = {
faces_normals[(fi1 * 3)],
faces_normals[(fi1 * 3) + 1],
faces_normals[(fi1 * 3) + 2]
};
if(calc_dot_product(nface_normal, current_face_normal) < -0.1) {
faces_normals[(fi1 * 3)] = -1 * faces_normals[(fi1 * 3)];
faces_normals[(fi1 * 3) + 1] = -1 * faces_normals[(fi1 * 3) + 1];
faces_normals[(fi1 * 3) + 2] = -1 * faces_normals[(fi1 * 3) + 2];
}
}
if(fi2 != -1 && fi2 != face_index && !visited[fi1]) {
float nface_normal[3] = {
faces_normals[(fi2 * 3)],
faces_normals[(fi2 * 3) + 1],
faces_normals[(fi2 * 3) + 2]
};
if(calc_dot_product(nface_normal, current_face_normal) < -0.1) {
faces_normals[(fi2 * 3)] = -1 * faces_normals[(fi2 * 3)];
faces_normals[(fi2 * 3) + 1] = -1 * faces_normals[(fi2 * 3) + 1];
faces_normals[(fi2 * 3) + 2] = -1 * faces_normals[(fi2 * 3) + 2];
}
}
if(fi3 != -1 && fi3 != face_index && !visited[fi1]) {
float nface_normal[3] = {
faces_normals[(fi3 * 3)],
faces_normals[(fi3 * 3) + 1],
faces_normals[(fi3 * 3) + 2]
};
if(calc_dot_product(nface_normal, current_face_normal) < -0.1) {
faces_normals[(fi3 * 3)] = -faces_normals[(fi3 * 3)];
faces_normals[(fi3 * 3) + 1] = -faces_normals[(fi3 * 3) + 1];
faces_normals[(fi3 * 3) + 2] = -faces_normals[(fi3 * 3) + 2];
}
}
if(fi1 != -1) {
recursive_orient(fi1,
faces_normals,
faces_indices,
vertex_in_faces,
visited);
}
if(fi2 != -1) {
recursive_orient(fi2,
faces_normals,
faces_indices,
vertex_in_faces,
visited);
}
if(fi3 != -1) {
recursive_orient(fi3,
faces_normals,
faces_indices,
vertex_in_faces,
visited);
}
}
}
/*
This function reads the ply file and returns a
ply_object that describes the file read
*/
ply_object read_ply_from_file(const char *file_name)
{
//BEGIN PLY FILE HANDLING
FILE *ply;
char line[LINE_LENGTH]; //line to be read
bunny.amount_of_vertices = 0;
bunny.amount_of_faces = 0;
ply = fopen (file_name, "rt"); //open ply file
//read header
while (fgets(line, LINE_LENGTH, ply) != NULL)
{
/* convert the string to a long int */
if (!strncmp (line, "element vertex", 14)) {
sscanf(line, "element vertex %i", &bunny.amount_of_vertices);
} else if (!strncmp (line, "element face", 12)) {
sscanf(line, "element face %i", &bunny.amount_of_faces);
}
else if (!strncmp(line, "end_header", 10)) {
break;
}
}
#ifdef DEBUG
printf("file format says %i vertices, %i faces\n",
bunny.amount_of_vertices,
bunny.amount_of_faces);
#endif
//read vertices
int i;
float x;
float y;
float z;
//create vertex table for pushing into GLSL
bunny.vertices = (float *) malloc(bunny.amount_of_vertices * 3 * sizeof(float));
bunny.tex_coordinates = (float *) malloc(bunny.amount_of_vertices * 2 *
sizeof(float)); //only u and v for texture
//set the vertex coordinates into bunny.vertices
for(i = 0; i < bunny.amount_of_vertices; i++) {
if(fgets(line, LINE_LENGTH, ply) != NULL)
{
sscanf(line, "%f %f %f", &x, &y, &z);
bunny.vertices[(3*i)] = x;
bunny.vertices[(3*i)+1] = y;
bunny.vertices[(3*i)+2] = z;
//trivial x=u, y=v mapping
bunny.tex_coordinates[2*i] = x;
bunny.tex_coordinates[2*i+1] = y;
}
}
//the indices of the faces is needed for what?
bunny.faces_indices = (int *) malloc(bunny.amount_of_faces * 3 * sizeof(int));
//we need the face normal to be able to shade the faces correclty
bunny.faces_normals = (float *) malloc(bunny.amount_of_faces * 3 * sizeof(float));
//the vertex normals
bunny.vertex_normals = (float *) malloc(bunny.amount_of_vertices * 3 * sizeof(float));
//count the face normals
int amount_of_face_indices;
int a, b, c;
int * amount_of_faces_per_vertex = (int *) malloc(bunny.amount_of_vertices * sizeof(int));
for(i = 0; i < bunny.amount_of_vertices; i++) {
amount_of_faces_per_vertex[i] = 0;
}
bool * visited = (bool*) malloc(bunny.amount_of_faces * sizeof(bool*));
for(i = 0; i < bunny.amount_of_faces; i++) {
visited[i] = false;
}
int ** vertex_in_faces =(int**) malloc(bunny.amount_of_vertices * sizeof(int*));
for (i = 0; i < bunny.amount_of_vertices; i++){
vertex_in_faces[i] = (int *)malloc(9 * sizeof(int)); //hope this is enough
int j;
for (j = 0; j < 9; j++) {
vertex_in_faces[i][j] = -1;
}
}
//todoo, free vertex_in_faces and visited after they're not needed
for(i = 0; i < bunny.amount_of_faces; i++) {
fscanf (ply, "%i %i %i %i", &amount_of_face_indices, &a, &b, &c);
if (amount_of_face_indices == 3) //we're only interested in triangels
{
//save the index of each vertex
bunny.faces_indices[3*i] = a;
bunny.faces_indices[(3*i)+1] = b;
bunny.faces_indices[(3*i)+2] = c;
vertex_in_faces[a][amount_of_faces_per_vertex[a]] = i;
vertex_in_faces[b][amount_of_faces_per_vertex[b]] = i;
vertex_in_faces[c][amount_of_faces_per_vertex[c]] = i;
amount_of_faces_per_vertex[a]++;
amount_of_faces_per_vertex[b]++;
amount_of_faces_per_vertex[c]++;
}
else {
fprintf(stderr, "error, bad index data: not triangle!");
}
}
//just choose the face index where to start recursive calculation
float reference[3];
lazy_calc_normal(bunny.faces_indices[3*vertex_in_faces[0][0]],
bunny.faces_indices[3*vertex_in_faces[0][0]+1],
bunny.faces_indices[3*vertex_in_faces[0][0]+2],
reference);
//count the face normals
for(i = 0; i < bunny.amount_of_faces; i++) {
a = bunny.faces_indices[3*i];
b = bunny.faces_indices[(3*i)+1];
c = bunny.faces_indices[(3*i)+2];
//point vectors to the vertices for the triangle
float v1[3] = {bunny.vertices[(a * 3)],
bunny.vertices[(a * 3) + 1],
bunny.vertices[(a * 3) + 2]};
float v2[3] = {bunny.vertices[(b * 3)],
bunny.vertices[(b * 3) + 1],
bunny.vertices[(b * 3) + 2]};
float v3[3] = {bunny.vertices[(c * 3)],
bunny.vertices[(c * 3) + 1],
bunny.vertices[(c * 3) + 2]};
//calculate the face normal
float res[3];
calc_normal(v1, v2, v3, res);
// add the face normal to bunny
bunny.faces_normals[(i * 3)] = res[0];
bunny.faces_normals[(i * 3) + 1] = res[1];
bunny.faces_normals[(i * 3) + 2] = res[2];
}
//check the faces_normals just calculated and turn them around
//if needed
//choose a reference face (index of the reference face)
int reference_face_index = 0;
recursive_orient(reference_face_index,
bunny.faces_normals,
bunny.faces_indices,
vertex_in_faces,
visited);
//add the faces normals to the vertex normals
for(i = 0; i < bunny.amount_of_faces; i++) {
a = bunny.faces_indices[3*i];
b = bunny.faces_indices[(3*i)+1];
c = bunny.faces_indices[(3*i)+2];
float res[3] = {
bunny.faces_normals[(i * 3)],
bunny.faces_normals[(i * 3) + 1],
bunny.faces_normals[(i * 3) + 2]
};
//(only add, normalize later)
bunny.vertex_normals[(a * 3)] += res[0];
bunny.vertex_normals[(a * 3) + 1] += res[1];
bunny.vertex_normals[(a * 3) + 2] += res[2];
bunny.vertex_normals[(b * 3)] += res[0];
bunny.vertex_normals[(b * 3) + 1] += res[1];
bunny.vertex_normals[(b * 3) + 2] += res[2];
bunny.vertex_normals[(c * 3)] += res[0];
bunny.vertex_normals[(c * 3) + 1] += res[1];
bunny.vertex_normals[(c * 3) + 2] += res[2];
}
//normalize the vertex normals (average)
for(i = 0; i < bunny.amount_of_vertices; i++) {
float temp[3];
temp[0] = bunny.vertex_normals[(i * 3)];
temp[1] = bunny.vertex_normals[(i * 3) + 1];
temp[2] = bunny.vertex_normals[(i * 3) + 2];
float normalization = sqrt(temp[0] * temp[0] + temp[1] * temp[1] + temp[2] * temp[2]);
temp[0] = temp[0]/normalization;
temp[1] = temp[1]/normalization;
temp[2] = temp[2]/normalization;
bunny.vertex_normals[(i * 3)] = temp[0];
bunny.vertex_normals[(i * 3) + 1] = temp[1];
bunny.vertex_normals[(i * 3) + 2] = temp[2];
}
fclose(ply); //close file
//END PLY FILE HANDLING
return bunny;
}