0
|
1 /* ============================================================================
|
|
2 * Freetype GL - A C OpenGL Freetype engine
|
|
3 * Platform: Any
|
|
4 * WWW: http://code.google.com/p/freetype-gl/
|
|
5 * ----------------------------------------------------------------------------
|
|
6 * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions are met:
|
|
10 *
|
|
11 * 1. Redistributions of source code must retain the above copyright notice,
|
|
12 * this list of conditions and the following disclaimer.
|
|
13 *
|
|
14 * 2. Redistributions in binary form must reproduce the above copyright
|
|
15 * notice, this list of conditions and the following disclaimer in the
|
|
16 * documentation and/or other materials provided with the distribution.
|
|
17 *
|
|
18 * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
|
|
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
21 * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28 *
|
|
29 * The views and conclusions contained in the software and documentation are
|
|
30 * those of the authors and should not be interpreted as representing official
|
|
31 * policies, either expressed or implied, of Nicolas P. Rougier.
|
|
32 * ============================================================================
|
|
33 */
|
|
34 #include <assert.h>
|
|
35 #include <string.h>
|
|
36 #include <stdlib.h>
|
|
37 #include <stdio.h>
|
|
38 #include "vec234.h"
|
|
39 #include "platform.h"
|
|
40 #include "vertex-attribute.h"
|
|
41
|
|
42
|
|
43
|
|
44
|
|
45 // ----------------------------------------------------------------------------
|
|
46 vertex_attribute_t *
|
|
47 vertex_attribute_new( GLchar * name,
|
|
48 GLint size,
|
|
49 GLenum type,
|
|
50 GLboolean normalized,
|
|
51 GLsizei stride,
|
|
52 GLvoid *pointer )
|
|
53 {
|
|
54 vertex_attribute_t *attribute =
|
|
55 (vertex_attribute_t *) malloc (sizeof(vertex_attribute_t));
|
|
56
|
|
57 assert( size > 0 );
|
|
58
|
|
59 attribute->name = (GLchar *) strdup( name );
|
|
60 attribute->index = -1;
|
|
61 attribute->size = size;
|
|
62 attribute->type = type;
|
|
63 attribute->normalized = normalized;
|
|
64 attribute->stride = stride;
|
|
65 attribute->pointer = pointer;
|
|
66 return attribute;
|
|
67 }
|
|
68
|
|
69
|
|
70
|
|
71 // ----------------------------------------------------------------------------
|
|
72 void
|
|
73 vertex_attribute_delete( vertex_attribute_t * self )
|
|
74 {
|
|
75 assert( self );
|
|
76
|
|
77 free( self->name );
|
|
78 free( self );
|
|
79 }
|
|
80
|
|
81 #include <stddef.h>
|
|
82
|
|
83 char *
|
|
84 strndup (const char *s, size_t n)
|
|
85 {
|
|
86 char *result;
|
|
87 size_t len = strlen (s);
|
|
88
|
|
89 if (n < len)
|
|
90 len = n;
|
|
91
|
|
92 result = (char *) malloc (len + 1);
|
|
93 if (!result)
|
|
94 return 0;
|
|
95
|
|
96 result[len] = '\0';
|
|
97 return (char *) memcpy (result, s, len);
|
|
98 }
|
|
99
|
|
100
|
|
101 // ----------------------------------------------------------------------------
|
|
102 vertex_attribute_t *
|
|
103 vertex_attribute_parse( char *format )
|
|
104 {
|
|
105 GLenum type = 0;
|
|
106 int size;
|
|
107 int normalized = 0;
|
|
108 char ctype;
|
|
109 char *name;
|
|
110 vertex_attribute_t *attr;
|
|
111 char *p = strchr(format, ':');
|
|
112 if( p != NULL)
|
|
113 {
|
|
114 name = strndup(format, p-format);
|
|
115 if( *(++p) == '\0' )
|
|
116 {
|
|
117 fprintf( stderr, "No size specified for '%s' attribute\n", name );
|
|
118 free( name );
|
|
119 return 0;
|
|
120 }
|
|
121 size = *p - '0';
|
|
122
|
|
123 if( *(++p) == '\0' )
|
|
124 {
|
|
125 fprintf( stderr, "No format specified for '%s' attribute\n", name );
|
|
126 free( name );
|
|
127 return 0;
|
|
128 }
|
|
129 ctype = *p;
|
|
130
|
|
131 if( *(++p) != '\0' )
|
|
132 {
|
|
133 if( *p == 'n' )
|
|
134 {
|
|
135 normalized = 1;
|
|
136 }
|
|
137 }
|
|
138
|
|
139 }
|
|
140 else
|
|
141 {
|
|
142 fprintf(stderr, "Vertex attribute format not understood ('%s')\n", format );
|
|
143 return 0;
|
|
144 }
|
|
145
|
|
146 switch( ctype )
|
|
147 {
|
|
148 case 'b': type = GL_BYTE; break;
|
|
149 case 'B': type = GL_UNSIGNED_BYTE; break;
|
|
150 case 's': type = GL_SHORT; break;
|
|
151 case 'S': type = GL_UNSIGNED_SHORT; break;
|
|
152 case 'i': type = GL_INT; break;
|
|
153 case 'I': type = GL_UNSIGNED_INT; break;
|
|
154 case 'f': type = GL_FLOAT; break;
|
|
155 default: type = 0; break;
|
|
156 }
|
|
157
|
|
158
|
|
159 attr = vertex_attribute_new( name, size, type, normalized, 0, 0 );
|
|
160 free( name );
|
|
161 return attr;
|
|
162 }
|
|
163
|
|
164
|
|
165
|
|
166 // ----------------------------------------------------------------------------
|
|
167 void
|
|
168 vertex_attribute_enable( vertex_attribute_t *attr )
|
|
169 {
|
|
170 if( attr->index == -1 )
|
|
171 {
|
|
172 GLint program;
|
|
173 glGetIntegerv( GL_CURRENT_PROGRAM, &program );
|
|
174 if( program == 0)
|
|
175 {
|
|
176 return;
|
|
177 }
|
|
178 attr->index = glGetAttribLocation( program, attr->name );
|
|
179 if( attr->index == -1 )
|
|
180 {
|
|
181 return;
|
|
182 }
|
|
183 }
|
|
184 glEnableVertexAttribArray( attr->index );
|
|
185 glVertexAttribPointer( attr->index, attr->size, attr->type,
|
|
186 attr->normalized, attr->stride, attr->pointer );
|
|
187 }
|