-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorAttribute.sj
271 lines (232 loc) · 6.32 KB
/
vectorAttribute.sj
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
/* Freetype GL - A C OpenGL Freetype engine
*
* Distributed under the OSI-approved BSD 2-Clause License. See accompanying
* file `LICENSE` for more details.
*/
--cinclude--
/**
* Maximum number of attributes per vertex
*
* @private
*/
##define MAX_VERTEX_ATTRIBUTE 16
--cinclude--
--ctypedef--
typedef struct vertex_attribute_td vertex_attribute_t;
--ctypedef--
--cstruct--
/**
* Generic vertex attribute.
*/
struct vertex_attribute_td
{
/**
* atribute name
*/
GLchar * name;
/**
* index of the generic vertex attribute to be modified.
*/
GLuint index;
/**
* Number of components per generic vertex attribute.
*
* Must be 1, 2, 3, or 4. The initial value is 4.
*/
GLint size;
/**
* data type of each component in the array.
*
* Symbolic constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT,
* GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, or GL_DOUBLE are
* accepted. The initial value is GL_FLOAT.
*/
GLenum type;
/**
* whether fixed-point data values should be normalized (GL_TRUE) or
* converted directly as fixed-point values (GL_FALSE) when they are
* accessed.
*/
GLboolean normalized;
/**
* byte offset between consecutive generic vertex attributes.
*
* If stride is 0, the generic vertex attributes are understood to be
* tightly packed in the array. The initial value is 0.
*/
GLsizei stride;
/**
* pointer to the first component of the first attribute element in the
* array.
*/
GLvoid * pointer;
/**
* pointer to the function that enable this attribute.
*/
void ( * enable )(void *);
};
--cstruct--
--cdefine--
/**
* Create an attribute from the given parameters.
*
* @param size number of component
* @param type data type
* @param normalized Whether fixed-point data values should be normalized
(GL_TRUE) or converted directly as fixed-point values
(GL_FALSE) when they are accessed.
* @param stride byte offset between consecutive attributes.
* @param pointer pointer to the first component of the first attribute
* element in the array.
* @return a new initialized vertex attribute.
*
* @private
*/
vertex_attribute_t *
vertex_attribute_new( GLchar * name,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei stride,
GLvoid *pointer );
/**
* Delete a vertex attribute.
*
* @param self a vertex attribute
*
*/
void
vertex_attribute_delete( vertex_attribute_t * self );
/**
* Create an attribute from the given description.
*
* @param format Format string specifies the format of a vertex attribute.
* @return an initialized vertex attribute
*
* @private
*/
vertex_attribute_t *
vertex_attribute_parse( char *format );
/**
* Enable a vertex attribute.
*
* @param attr a vertex attribute
*
* @private
*/
void
vertex_attribute_enable( vertex_attribute_t *attr );
--cdefine--
--cfunction--
#include(<assert.h>)
#include(<string.h>)
#include(<stdlib.h>)
#include(<stdio.h>)
// ----------------------------------------------------------------------------
vertex_attribute_t *
vertex_attribute_new( GLchar * name,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei stride,
GLvoid *pointer )
{
vertex_attribute_t *attribute =
(vertex_attribute_t *) malloc (sizeof(vertex_attribute_t));
assert( size > 0 );
attribute->name = (GLchar *) strdup( name );
attribute->index = -1;
attribute->size = size;
attribute->type = type;
attribute->normalized = normalized;
attribute->stride = stride;
attribute->pointer = pointer;
return attribute;
}
// ----------------------------------------------------------------------------
void
vertex_attribute_delete( vertex_attribute_t * self )
{
assert( self );
free( self->name );
free( self );
}
// ----------------------------------------------------------------------------
vertex_attribute_t *
vertex_attribute_parse( char *format )
{
GLenum type = 0;
int size;
int normalized = 0;
char ctype;
char *name;
vertex_attribute_t *attr;
char *p = strchr(format, ':');
if( p != NULL)
{
name = strndup(format, p-format);
if( *(++p) == '\0' )
{
fprintf( stderr, "No size specified for '%s' attribute\n", name );
free( name );
return 0;
}
size = *p - '0';
if( *(++p) == '\0' )
{
fprintf( stderr, "No format specified for '%s' attribute\n", name );
free( name );
return 0;
}
ctype = *p;
if( *(++p) != '\0' )
{
if( *p == 'n' )
{
normalized = 1;
}
}
}
else
{
fprintf(stderr, "Vertex attribute format not understood ('%s')\n", format );
return 0;
}
switch( ctype )
{
case 'b': type = GL_BYTE; break;
case 'B': type = GL_UNSIGNED_BYTE; break;
case 's': type = GL_SHORT; break;
case 'S': type = GL_UNSIGNED_SHORT; break;
case 'i': type = GL_INT; break;
case 'I': type = GL_UNSIGNED_INT; break;
case 'f': type = GL_FLOAT; break;
default: type = 0; break;
}
attr = vertex_attribute_new( name, size, type, normalized, 0, 0 );
free( name );
return attr;
}
// ----------------------------------------------------------------------------
void
vertex_attribute_enable( vertex_attribute_t *attr )
{
if( attr->index == -1 )
{
GLint program;
glGetIntegerv( GL_CURRENT_PROGRAM, &program );
if( program == 0)
{
return;
}
attr->index = glGetAttribLocation( program, attr->name );
if( attr->index == -1 )
{
return;
}
}
glEnableVertexAttribArray( attr->index );
glVertexAttribPointer( attr->index, attr->size, attr->type,
attr->normalized, attr->stride, attr->pointer );
}
--cfunction--