ArrayBuffer.cc 3.77 KB
Newer Older
1
2
3
4
5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
Robert Menzel's avatar
Robert Menzel committed
6

Robert Menzel's avatar
Robert Menzel committed
7
#include <ACGL/OpenGL/Objects/ArrayBuffer.hh>
Janis Born's avatar
Janis Born committed
8
#include <ACGL/OpenGL/Data/GeometryData.hh>
Robert Menzel's avatar
Robert Menzel committed
9
10

using namespace ACGL;
Robert Menzel's avatar
Robert Menzel committed
11
using namespace ACGL::OpenGL;
Robert Menzel's avatar
Robert Menzel committed
12

13
14
15
//
// float attributes:
//
Robert Menzel's avatar
Robert Menzel committed
16
17
18
19
void ArrayBuffer::defineAttribute(
    const std::string& _name,
    GLenum _type,
    GLint  _size,
20
21
    GLboolean _normalized,
    GLuint _divisor )
Robert Menzel's avatar
Robert Menzel committed
22
23
{
    GLuint offset = mStride;
24
    Attribute attribute = { _name, _type, _size, offset, _normalized, _divisor, GL_FALSE };
Robert Menzel's avatar
Robert Menzel committed
25
26
27
28
29
30
31
32
    defineAttribute(attribute);
}

void ArrayBuffer::defineAttributeWithPadding(
    const std::string& _name,
    GLenum _type,
    GLint  _size,
    GLuint _padding,
33
34
    GLboolean _normalized,
    GLuint _divisor )
Robert Menzel's avatar
Robert Menzel committed
35
36
{
    GLuint offset = mStride;
37
    Attribute attribute = { _name, _type, _size, offset, _normalized, _divisor, GL_FALSE };
Robert Menzel's avatar
Robert Menzel committed
38
39
40
41
42
43
44
45
46
47
48
    defineAttribute(attribute);
    // defineAttribute will shift the mStride to the end of this attribute, so we only have to
    // add the explicit padding:
    mStride += _padding;
}

void ArrayBuffer::defineAttributeWithOffset(
    const std::string& _name,
    GLenum _type,
    GLint  _size,
    GLuint _offset,
49
50
    GLboolean _normalized,
    GLuint _divisor )
Robert Menzel's avatar
Robert Menzel committed
51
{
52
    Attribute attribute = { _name, _type, _size, _offset, _normalized, _divisor, GL_FALSE };
Robert Menzel's avatar
Robert Menzel committed
53
54
55
    defineAttribute(attribute);
}

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
//
// integer attributes:
//
void ArrayBuffer::defineIntegerAttribute(
    const std::string& _name,
    GLenum _type,
    GLint  _size,
    GLboolean _normalized,
    GLuint _divisor )
{
    GLuint offset = mStride;
    Attribute attribute = { _name, _type, _size, offset, _normalized, _divisor, GL_TRUE };
    defineAttribute(attribute);
}

void ArrayBuffer::defineIntegerAttributeWithPadding(
    const std::string& _name,
    GLenum _type,
    GLint  _size,
    GLuint _padding,
    GLboolean _normalized,
    GLuint _divisor )
{
    GLuint offset = mStride;
    Attribute attribute = { _name, _type, _size, offset, _normalized, _divisor, GL_TRUE };
    defineAttribute(attribute);
    // defineAttribute will shift the mStride to the end of this attribute, so we only have to
    // add the explicit padding:
    mStride += _padding;
}

void ArrayBuffer::defineIntegerAttributeWithOffset(
    const std::string& _name,
    GLenum _type,
    GLint  _size,
    GLuint _offset,
    GLboolean _normalized,
    GLuint _divisor )
{
    Attribute attribute = { _name, _type, _size, _offset, _normalized, _divisor, GL_TRUE };
    defineAttribute(attribute);
}


//
// int/float already defined by the Attribute:
//
Robert Menzel's avatar
Robert Menzel committed
103
104
105
106
107
108
109
110
void ArrayBuffer::defineAttribute( const Attribute &_attribute )
{
    // this way attribute definitions don't have to be in order!
    GLsizei attributeWidth = getGLTypeSize(_attribute.type)*_attribute.size; // in bytes
    mStride = std::max( (GLsizei)_attribute.offset + attributeWidth, mStride);
    mAttributes.push_back( _attribute );
}

Robert Menzel's avatar
Robert Menzel committed
111
int32_t ArrayBuffer::getAttributeIndexByName(const std::string& _nameInArray) const
Robert Menzel's avatar
Robert Menzel committed
112
113
114
{
    for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
        if(mAttributes[i].name == _nameInArray)
Robert Menzel's avatar
Robert Menzel committed
115
            return (int32_t) i;
Robert Menzel's avatar
Robert Menzel committed
116
117
    return -1;
}
Janis Born's avatar
Janis Born committed
118
119
120
121
122
123
124
125
126
127
128
129
130

void ArrayBuffer::setGeometryData(SharedGeometryData _geometryData)
{
    removeAttributes();
    for(ArrayBuffer::AttributeVec::const_iterator it = _geometryData->mAttributes.begin();
        it != _geometryData->mAttributes.end();
        ++it)
    {
        defineAttribute(*it);
    }
    setStride(_geometryData->getStrideSize());
    setData(_geometryData->getSize(), _geometryData->getData());
}