00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "platform/vbuffer.hpp"
00035 #include "platform/lowlevel.hpp"
00036
00037 #include <cstring>
00038
00039 namespace gsgl
00040 {
00041
00042 using namespace data;
00043
00044 namespace platform
00045 {
00046
00047 vbuffer_base::vbuffer_base(const int & target, const int & gl_mode)
00048 : target(target), gl_mode(gl_mode), opengl_id(0),
00049 prev_size(0), lowest_dirty_index(INT_MAX), highest_dirty_index(-1)
00050 {
00051 }
00052
00053
00054 vbuffer_base::~vbuffer_base()
00055 {
00056 unload();
00057 }
00058
00059
00060 void vbuffer_base::load()
00061 {
00062 glGenBuffers(1, (GLuint *) &opengl_id); CHECK_GL_ERRORS();
00063 if (!opengl_id)
00064 throw opengl_exception(__FILE__, __LINE__, L"Unable to generate buffer id: %hs", gluErrorString(glGetError()));
00065 }
00066
00067
00068 void vbuffer_base::unload()
00069 {
00070 if (opengl_id)
00071 {
00072 glDeleteBuffers(1, (GLuint *) &opengl_id); CHECK_GL_ERRORS();
00073 opengl_id = 0;
00074 }
00075 }
00076
00077
00078 void vbuffer_base::bind()
00079 {
00080 if (!opengl_id)
00081 load();
00082
00083 glBindBuffer(target, opengl_id); CHECK_GL_ERRORS();
00084
00085
00086 if (buffer_size() != prev_size)
00087 {
00088 glBufferData(target, buffer_size() * element_size(), get_ptr(), gl_mode); CHECK_GL_ERRORS();
00089 prev_size = buffer_size();
00090 }
00091 else if (lowest_dirty_index != INT_MAX || highest_dirty_index != -1)
00092 {
00093 if (lowest_dirty_index == INT_MAX)
00094 lowest_dirty_index = 0;
00095 if (highest_dirty_index == -1)
00096 highest_dirty_index = buffer_size()-1;
00097
00098 if (gl_mode == GL_STATIC_DRAW)
00099 {
00100 glBufferData(target, prev_size, get_ptr(), gl_mode); CHECK_GL_ERRORS();
00101 }
00102 else
00103 {
00104 glBufferSubData(target,
00105 lowest_dirty_index * element_size(),
00106 ((highest_dirty_index - lowest_dirty_index)+1) * element_size(),
00107 ((char *)get_ptr()) + (lowest_dirty_index * element_size())); CHECK_GL_ERRORS();
00108 }
00109 }
00110
00111 highest_dirty_index = -1;
00112 lowest_dirty_index = INT_MAX;
00113 }
00114
00115
00116 void vbuffer_base::unbind()
00117 {
00118 glBindBuffer(target, 0);
00119 }
00120
00121
00122
00123
00124 index_buffer::index_buffer(const vbuffer::update_mode & mode, const gsgl::index_t initial_capacity)
00125 : vbuffer_mixin<unsigned int>(GL_ELEMENT_ARRAY_BUFFER, mode == vbuffer::STATIC ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW, initial_capacity)
00126 {
00127 }
00128
00129
00130 index_buffer::~index_buffer()
00131 {
00132 }
00133
00134
00135
00136
00137 vertex_buffer::vertex_buffer(const vbuffer::update_mode & mode, const gsgl::index_t initial_capacity)
00138 : vbuffer_mixin<float>(GL_ARRAY_BUFFER, mode == vbuffer::STATIC ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW, initial_capacity)
00139 {
00140 }
00141
00142
00143 vertex_buffer::~vertex_buffer()
00144 {
00145 }
00146
00147
00148
00149 namespace vbuffer
00150 {
00151
00152
00153 }
00154
00155
00156 }
00157
00158 }
00159