1 /*
2     Copyright (c) 2011 Andrew Caudwell (acaudwell@gmail.com)
3     All rights reserved.
4 
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8     1. Redistributions of source code must retain the above copyright
9        notice, this list of conditions and the following disclaimer.
10     2. Redistributions in binary form must reproduce the above copyright
11        notice, this list of conditions and the following disclaimer in the
12        documentation and/or other materials provided with the distribution.
13     3. The name of the author may not be used to endorse or promote products
14        derived from this software without specific prior written permission.
15 
16     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef CORE_VBO_H
29 #define CORE_VBO_H
30 
31 
32 #include <stack>
33 #include <vector>
34 
35 #include "gl.h"
36 #include "vectors.h"
37 #include "logger.h"
38 
39 class VBO {
40 public:
41     GLuint id;
42     GLenum buffer_type;
43 
44     int capacity;
45 
buffer_type(buffer_type)46     VBO(GLenum buffer_type = GL_ARRAY_BUFFER) : buffer_type(buffer_type) {
47         capacity = 0;
48         id       = 0;
49     }
50 
~VBO()51     ~VBO() {
52         unload();
53     }
54 
init()55     void init() {
56         if(!id) glGenBuffers(1, &id);
57     }
58 
unload()59     void unload() {
60         capacity = 0;
61         if(id != 0) {
62             glDeleteBuffers(1, &id);
63             id = 0;
64         }
65     }
66 
bind()67     void bind() {
68         if(!id) init();
69         glBindBuffer(buffer_type, id);
70     }
71 
buffer(int item_count,int item_size,int item_capacity,GLvoid * data,GLenum usage)72     void buffer(int item_count, int item_size, int item_capacity, GLvoid* data, GLenum usage) {
73 
74         bind();
75 
76         if(capacity < item_count) {
77             capacity = item_capacity;
78             glBufferData(buffer_type, capacity * item_size, data, usage);
79         } else {
80             glBufferSubData(buffer_type, 0, item_count * item_size, data);
81         }
82 
83         unbind();
84     }
85 
unbind()86     void unbind() {
87         glBindBuffer(buffer_type, 0);
88     }
89 };
90 
91 //note this should be 32 bytes (8x4 bytes)
92 class quadbuf_vertex {
93 public:
quadbuf_vertex()94     quadbuf_vertex() {};
quadbuf_vertex(const vec2 & pos,const vec4 & colour,const vec2 & texcoord)95     quadbuf_vertex(const vec2& pos, const vec4& colour, const vec2& texcoord) : pos(pos), colour(colour), texcoord(texcoord) {};
96 
97     vec2 pos;
98     vec4 colour;
99     vec2 texcoord;
100 };
101 
102 //maintain ranges corresponding to each texture
103 class quadbuf_tex {
104 public:
quadbuf_tex()105     quadbuf_tex() {};
quadbuf_tex(int start_index,GLuint textureid)106     quadbuf_tex(int start_index, GLuint textureid) : start_index(start_index), textureid(textureid) {};
107     int start_index;
108     GLuint textureid;
109 };
110 
111 class quadbuf {
112 
113     quadbuf_vertex* data;
114     int vertex_capacity;
115 
116     std::vector<quadbuf_tex> textures;
117 
118     VBO buf;
119 
120     int vertex_count;
121 
122     void resize(int new_size);
123 public:
124     quadbuf(int data_size = 0);
125     ~quadbuf();
126 
127     void unload();
128     void reset();
129 
130     size_t vertices();
131     size_t capacity();
132     size_t texture_changes();
133 
134     void add(GLuint textureid, const vec2& pos, const vec2& dims, const vec4& colour);
135     void add(GLuint textureid, const vec2& pos, const vec2& dims, const vec4& colour, const vec4& texcoord);
136     void add(GLuint textureid, const quadbuf_vertex& v1, const quadbuf_vertex& v2, const quadbuf_vertex& v3, const quadbuf_vertex& v4);
137 
138     void update();
139     void draw();
140 };
141 
142 #endif
143