-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbm.h
171 lines (139 loc) · 3.98 KB
/
sbm.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
#ifndef __SBM_H__
#define __SBM_H__
#include <GLES2/gl2.h>
#include <cstdio>
#include <cstring>
typedef struct SBM_HEADER_t
{
unsigned int magic;
unsigned int size;
char name[64];
unsigned int num_attribs;
unsigned int num_frames;
unsigned int num_vertices;
unsigned int num_indices;
unsigned int index_type;
} SBM_HEADER;
typedef struct SBM_ATTRIB_HEADER_t
{
char name[64];
unsigned int type;
unsigned int components;
unsigned int flags;
} SBM_ATTRIB_HEADER;
typedef struct SBM_FRAME_HEADER_t
{
unsigned int first;
unsigned int count;
unsigned int flags;
} SBM_FRAME_HEADER;
typedef struct SBM_VEC4F_t
{
float x;
float y;
float z;
float w;
} SBM_VEC4F;
class SBObject
{
public:
SBObject(void)
: m_vao(0),
m_attribute_buffer(0),
m_index_buffer(0),
m_attrib(0),
m_frame(0)
{
}
virtual ~SBObject(void)
{
Free();
}
bool LoadFromSBM(const char * filename)
{
FILE * f = NULL;
f = fopen(filename, "rb");
if(f == NULL)
{
return false;
}
fseek(f, 0, SEEK_END);
size_t filesize = ftell(f);
fseek(f, 0, SEEK_SET);
m_data = new unsigned char [filesize];
size_t readsize = fread(m_data, 1, filesize, f);
if(readsize != filesize)
{
delete[] m_data;
m_data = NULL;
fclose(f);
return false;
}
fclose(f);
SBM_HEADER * header = (SBM_HEADER *)m_data;
m_raw_data = m_data + sizeof(SBM_HEADER) + header->num_attribs * sizeof(SBM_ATTRIB_HEADER) + header->num_frames * sizeof(SBM_FRAME_HEADER);
SBM_ATTRIB_HEADER * attrib_header = (SBM_ATTRIB_HEADER *)(m_data + sizeof(SBM_HEADER));
SBM_FRAME_HEADER * frame_header = (SBM_FRAME_HEADER *)(m_data + sizeof(SBM_HEADER) + header->num_attribs * sizeof(SBM_ATTRIB_HEADER));
memcpy(&m_header, header, sizeof(SBM_HEADER));
m_attrib = new SBM_ATTRIB_HEADER[header->num_attribs];
memcpy(m_attrib, attrib_header, header->num_attribs * sizeof(SBM_ATTRIB_HEADER));
m_frame = new SBM_FRAME_HEADER[header->num_frames];
memcpy(m_frame, frame_header, header->num_frames * sizeof(SBM_FRAME_HEADER));
return true;
}
bool Free(void)
{
m_index_buffer = 0;
m_attribute_buffer = 0;
m_vao = 0;
delete [] m_attrib;
m_attrib = NULL;
delete [] m_frame;
m_frame = NULL;
delete [] m_data;
return true;
}
unsigned int GetAttributeCount(void) const
{
return m_header.num_attribs;
}
const char * GetAttributeName(unsigned int index) const
{
return index < m_header.num_attribs ? m_attrib[index].name : 0;
}
unsigned int GetAttribComponents(unsigned int index) const
{
return index < m_header.num_attribs ? m_attrib[index].components : 0;
}
unsigned char* GetVertexData()
{
return m_raw_data;
}
unsigned int GetNumVertices() const
{
return m_header.num_vertices;
}
unsigned int GetFirstFrameVertex(unsigned int frame) const
{
return (frame < m_header.num_frames) ? m_frame[frame].first : 0;
}
unsigned int GetFrameVertexCount(unsigned int frame) const
{
return (frame < m_header.num_frames) ? m_frame[frame].count : 0;
}
protected:
GLuint m_vao;
GLuint m_attribute_buffer;
GLuint m_index_buffer;
GLuint m_num_attribs;
GLuint m_num_verticies;
GLuint m_vertexIndex;
GLuint m_normalIndex;
GLuint m_texCoord0Index;
SBM_HEADER m_header;
SBM_ATTRIB_HEADER * m_attrib;
SBM_FRAME_HEADER * m_frame;
unsigned char * m_data;
unsigned char * m_raw_data;
};
#endif /* __SBM_H__ */