vertex-attribute.c 5.15 KB
Newer Older
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
/* ============================================================================
 * Freetype GL - A C OpenGL Freetype engine
 * Platform:    Any
 * WWW:         http://code.google.com/p/freetype-gl/
 * ----------------------------------------------------------------------------
 * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of Nicolas P. Rougier.
 * ============================================================================
 */
34 35 36 37 38
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "vec234.h"
39
#include "platform.h"
40 41 42 43 44
#include "vertex-attribute.h"



// ----------------------------------------------------------------------------
45 46 47 48 49 50 51
vertex_attribute_t *
vertex_attribute_new( GLchar * name,
                      GLint size,
                      GLenum type,
                      GLboolean normalized,
                      GLsizei stride,
                      GLvoid *pointer )
52
{
53 54
    vertex_attribute_t *attribute =
        (vertex_attribute_t *) malloc (sizeof(vertex_attribute_t));
55

56 57
    assert( size > 0 );

58 59 60 61 62 63 64 65
    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;
66 67 68 69 70 71
}



// ----------------------------------------------------------------------------
void
72
vertex_attribute_delete( vertex_attribute_t * self )
73
{
74
    assert( self );
75

76 77
    free( self->name );
    free( self );
78 79 80 81 82 83 84 85
}



// ----------------------------------------------------------------------------
vertex_attribute_t *
vertex_attribute_parse( char *format )
{
86
    GLenum type = 0;
87 88 89 90
    int size;
    int normalized = 0;
    char ctype;
    char *name;
91
    vertex_attribute_t *attr;
92 93
    char *p = strchr(format, ':');
    if( p != NULL)
94
    {
95 96 97 98 99 100 101 102 103 104
        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' ) 
105
        {
106 107 108
            fprintf( stderr, "No format specified for '%s' attribute\n", name );
            free( name );
            return 0;
109
        }
110 111 112
        ctype = *p;

        if( *(++p) != '\0' ) 
113
        {
114 115 116 117
            if( *p == 'n' )
            {
                normalized = 1;
            }
118
        }
119

120
    }
121
    else
122
    {
123 124
        fprintf(stderr, "Vertex attribute format not understood ('%s')\n", format );   
        return 0;
125 126
    }

127 128 129 130 131 132 133 134 135 136 137 138 139
    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;
    }


140
    attr = vertex_attribute_new( name, size, type, normalized, 0, 0 );
141 142
    free( name );
    return attr;
143 144 145 146 147
}



// ----------------------------------------------------------------------------
148 149
void
vertex_attribute_enable( vertex_attribute_t *attr )
150
{
151
    if( attr->index == -1 )
152
    {
153 154 155
        GLint program;
        glGetIntegerv( GL_CURRENT_PROGRAM, &program );
        if( program == 0)
156
        {
157 158 159 160 161 162
            return;
        }
        attr->index = glGetAttribLocation( program, attr->name );
        if( attr->index == -1 )
        {
            return;
163 164
        }
    }
165 166 167
    glEnableVertexAttribArray( attr->index );
    glVertexAttribPointer( attr->index, attr->size, attr->type,
                           attr->normalized, attr->stride, attr->pointer );
168
}