-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl_loader.h
91 lines (69 loc) · 2.43 KB
/
stl_loader.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
//
// Created by Pablo Weremczuk on 4/15/20.
//
#ifndef RAYLIB_FLECS_SPINE_STL_LOADER_H
#define RAYLIB_FLECS_SPINE_STL_LOADER_H
#include <stdio.h>
#include <stdlib.h>
#include "rlgl.h"
#ifndef __uint8_t
typedef unsigned char __uint8_t;
#endif
#ifndef __uint16_t
typedef unsigned short __uint16_t;
#endif
#pragma pack(push, 1)
typedef struct vertex_info_t {
Vector3 normal;
Vector3 triangle[3];
__uint16_t attribute;
} vertex_info_t;
#pragma pack(pop)
Mesh load_stl(char *file_path) {
Mesh mesh = {0};
mesh.vboId = (unsigned int *)RL_CALLOC(7, sizeof(unsigned int));
FILE *fap = fopen(file_path, "r");
if(fap == NULL){
perror("File not found");
exit(-1);
}
unsigned int triangle_count = 0;
fseek(fap, sizeof(__uint8_t) * 80, 0);
fread(&triangle_count, sizeof(int), 1, fap);
vertex_info_t *model_info = (vertex_info_t *) RL_MALLOC(triangle_count * sizeof(vertex_info_t));
if(model_info == NULL) {
perror("Error creating model");
exit(-1);
}
mesh.vertexCount = triangle_count * 3;
mesh.triangleCount = triangle_count;
mesh.normals = (float *)RL_MALLOC(sizeof(Vector3) * triangle_count * 3);
mesh.vertices = (float *)RL_MALLOC(sizeof(Vector3) * triangle_count * 3);
mesh.texcoords = (float *)RL_MALLOC(sizeof(Vector3) * triangle_count * 3);
size_t registers_read = fread(model_info, sizeof(vertex_info_t), triangle_count, fap);
if(registers_read < triangle_count) {
printf("Error. Unable to read the expected number of triangles: %zu out of %zu", registers_read, triangle_count);
exit(-1);
} else {
printf("%zu triangles read\n", registers_read);
}
for (int i = 0, t = 0; t < triangle_count; t++) {
for(int vertex_index = 0; vertex_index < 3; vertex_index++){
mesh.vertices[i++] = model_info[t].triangle[vertex_index].x;
mesh.vertices[i++] = model_info[t].triangle[vertex_index].y;
mesh.vertices[i++] = model_info[t].triangle[vertex_index].z;
}
}
for (int i = 0, t = 0; t < triangle_count; t++) {
for(int j = 0; j < 3; j++){
mesh.normals[i++] = model_info[t].normal.x;
mesh.normals[i++] = model_info[t].normal.y;
mesh.normals[i++] = model_info[t].normal.z;
}
}
UploadMesh(&mesh, false);
RL_FREE(model_info);
fclose(fap);
return mesh;
}
#endif //RAYLIB_FLECS_SPINE_STL_LOADER_H