forked from rougier/freetype-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertex-attribute.c
140 lines (119 loc) · 3.41 KB
/
vertex-attribute.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
/* Freetype GL - A C OpenGL Freetype engine
*
* Distributed under the OSI-approved BSD 2-Clause License. See accompanying
* file `LICENSE` for more details.
*/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "vec234.h"
#include "platform.h"
#include "vertex-attribute.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 );
}