~ubuntu-branches/debian/wheezy/gource/wheezy

« back to all changes in this revision

Viewing changes to src/core/vbo.cpp

  • Committer: Package Import Robot
  • Author(s): Andrew Caudwell
  • Date: 2012-04-24 11:25:45 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20120424112545-18fbnycu9xrsl4s5
Tags: 0.38-1
* New upstream release (closes: #667189)
* New build dependencies on libglm-dev and libboost-filesystem-dev. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
//quadbuf
31
31
 
32
 
quadbuf::quadbuf(int data_size) : data_size(data_size) {
 
32
quadbuf::quadbuf(int vertex_capacity) : vertex_capacity(vertex_capacity) {
33
33
    vertex_count = 0;
34
 
    curr_buffer =-1;
35
 
 
36
 
    buffers.resize(1);
37
 
 
38
 
    data = data_size > 0 ? new quadbuf_vertex[data_size] : 0;
 
34
 
 
35
    data = vertex_capacity > 0 ? new quadbuf_vertex[vertex_capacity] : 0;
39
36
 
40
37
    //fprintf(stderr, "size of quadbuf_vertex = %d\n", sizeof(quadbuf_vertex));
41
38
}
44
41
    if(data!=0) delete[] data;
45
42
}
46
43
 
 
44
void quadbuf::unload() {
 
45
    buf.unload();
 
46
}
 
47
 
47
48
void quadbuf::resize(int new_size) {
48
49
 
49
50
    quadbuf_vertex* _data = data;
50
51
 
51
52
    data = new quadbuf_vertex[new_size];
52
53
 
53
 
    for(int i=0;i<data_size;i++) {
 
54
    for(int i=0;i<vertex_capacity;i++) {
54
55
        data[i] = _data[i];
55
56
    }
56
57
 
57
 
    data_size = new_size;
 
58
    vertex_capacity = new_size;
58
59
 
59
60
    if(_data != 0) delete[] _data;
60
61
}
69
70
}
70
71
 
71
72
size_t quadbuf::capacity() {
72
 
    return data_size;
 
73
    return vertex_capacity;
73
74
}
74
75
 
75
76
size_t quadbuf::texture_changes() {
76
77
    return textures.size();
77
78
}
78
79
 
79
 
vec4f quadbuf_default_texcoord(0.0f, 0.0f, 1.0f, 1.0f);
 
80
vec4 quadbuf_default_texcoord(0.0f, 0.0f, 1.0f, 1.0f);
80
81
 
81
 
void quadbuf::add(GLuint textureid, const vec2f& pos, const vec2f& dims, const vec4f& colour) {
 
82
void quadbuf::add(GLuint textureid, const vec2& pos, const vec2& dims, const vec4& colour) {
82
83
    add(textureid, pos, dims, colour, quadbuf_default_texcoord);
83
84
}
84
85
 
85
 
void quadbuf::add(GLuint textureid, const vec2f& pos, const vec2f& dims, const vec4f& colour, const vec4f& texcoord) {
 
86
void quadbuf::add(GLuint textureid, const vec2& pos, const vec2& dims, const vec4& colour, const vec4& texcoord) {
86
87
    //debugLog("%d: %.2f, %.2f, %.2f, %.2f\n", i, pos.x, pos.y, dims.x, dims.y);
87
88
 
88
 
    quadbuf_vertex v1(pos,                       colour, vec2f(texcoord.x, texcoord.y));
89
 
    quadbuf_vertex v2(pos + vec2f(dims.x, 0.0f), colour, vec2f(texcoord.z, texcoord.y));
90
 
    quadbuf_vertex v3(pos + dims,                colour, vec2f(texcoord.z, texcoord.w));
91
 
    quadbuf_vertex v4(pos + vec2f(0.0f, dims.y), colour, vec2f(texcoord.x, texcoord.w));
 
89
    quadbuf_vertex v1(pos,                       colour, vec2(texcoord.x, texcoord.y));
 
90
    quadbuf_vertex v2(pos + vec2(dims.x, 0.0f), colour, vec2(texcoord.z, texcoord.y));
 
91
    quadbuf_vertex v3(pos + dims,                colour, vec2(texcoord.z, texcoord.w));
 
92
    quadbuf_vertex v4(pos + vec2(0.0f, dims.y), colour, vec2(texcoord.x, texcoord.w));
92
93
 
93
94
    int i = vertex_count;
94
95
 
95
96
    vertex_count += 4;
96
97
 
97
 
    if(vertex_count > data_size) {
 
98
    if(vertex_count > vertex_capacity) {
98
99
        resize(vertex_count*2);
99
100
    }
100
101
 
114
115
 
115
116
    vertex_count += 4;
116
117
 
117
 
    if(vertex_count > data_size) {
 
118
    if(vertex_count > vertex_capacity) {
118
119
        resize(vertex_count*2);
119
120
    }
120
121
 
131
132
void quadbuf::update() {
132
133
    if(vertex_count==0) return;
133
134
 
134
 
    curr_buffer = (curr_buffer + 1) % buffers.size();
135
 
 
136
 
    quadbuf_buffer* buf = &(buffers[curr_buffer]);
137
 
 
138
 
    if(!buf->id) {
139
 
        glGenBuffers(1, &(buf->id));
140
 
    }
141
 
 
142
 
    glBindBuffer(GL_ARRAY_BUFFER, buf->id);
 
135
    buf.bind();
143
136
 
144
137
    //recreate buffer if less than the vertex_count
145
 
    if(buf->size < vertex_count) {
146
 
        buf->size = data_size;
147
 
        glBufferData(GL_ARRAY_BUFFER, buf->size*sizeof(quadbuf_vertex), &(data[0].pos.x), GL_DYNAMIC_DRAW);
148
 
    } else {
149
 
        glBufferSubData(GL_ARRAY_BUFFER, 0, vertex_count*sizeof(quadbuf_vertex), &(data[0].pos.x));
150
 
    }
151
 
 
152
 
    glBindBuffer(GL_ARRAY_BUFFER, 0);
 
138
    buf.buffer( vertex_count, sizeof(quadbuf_vertex), vertex_capacity, &(data[0].pos.x), GL_DYNAMIC_DRAW );
 
139
    
 
140
    buf.unbind();
153
141
}
154
142
 
155
143
void quadbuf::draw() {
156
 
    if(vertex_count==0 || curr_buffer==-1) return;
157
 
 
158
 
    glBindBuffer(GL_ARRAY_BUFFER, buffers[curr_buffer].id);
 
144
    if(vertex_count==0) return;
 
145
   
 
146
    buf.bind();
159
147
 
160
148
    glEnableClientState(GL_VERTEX_ARRAY);
161
149
    glEnableClientState(GL_COLOR_ARRAY);
196
184
    glDisableClientState(GL_COLOR_ARRAY);
197
185
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
198
186
 
199
 
    glBindBuffer(GL_ARRAY_BUFFER, 0);
 
187
    buf.unbind();
200
188
}