2 #include "arraystate.h"
3 #include "bufferstate.h"
9 BufferContent::BufferContent():
14 void BufferContent::update(const ArrayState &array)
16 if(array.stride!=stride)
19 stride = array.stride;
21 for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
22 if(i->kind==array.kind && i->index==array.index)
24 if(array.size!=i->size || array.type!=i->type || array.pointer!=i->offset)
36 vector<Array>::iterator place = arrays.end();
37 for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
38 if(i->offset>array.pointer)
44 arrays.insert(place, array);
47 void BufferContent::update_elements(GLenum type)
52 array.kind = GL_ELEMENT_ARRAY_BUFFER;
54 arrays.push_back(array);
55 stride = typesize(type);
57 else if(arrays.size()>1 || arrays.front().kind!=GL_ELEMENT_ARRAY_BUFFER)
61 if(arrays.front().type!=type)
63 arrays.front().type = type;
64 stride = typesize(type);
68 string BufferContent::describe() const
74 for(vector<Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
77 if(i->kind==GL_VERTEX_ARRAY)
79 else if(i->kind==GL_NORMAL_ARRAY)
81 else if(i->kind==GL_COLOR_ARRAY)
83 else if(i->kind==GL_TEXTURE_COORD_ARRAY && i->index==0)
85 else if(i->kind==GL_ELEMENT_ARRAY_BUFFER)
88 char type[3] = { '?', 0, 0 };
91 else if(i->type==GL_DOUBLE)
93 else if(i->type==GL_INT || i->type==GL_UNSIGNED_INT)
95 else if(i->type==GL_SHORT || i->type==GL_UNSIGNED_SHORT)
97 else if(i->type==GL_BYTE || i->type==GL_UNSIGNED_BYTE)
100 if(i->type==GL_UNSIGNED_INT || i->type==GL_UNSIGNED_SHORT || i->type==GL_UNSIGNED_BYTE)
108 result += strformat("%c%d%s", kind, i->size, type);
115 BufferContent::Array::Array():
123 BufferContent::Array::Array(const ArrayState &a):
132 BufferState::BufferState():
135 usage(GL_STATIC_DRAW),
140 void BufferState::set_data(unsigned sz, const void *ptr, GLenum use)
145 data = new char[size];
147 set_sub_data(0, size, ptr);
148 content = BufferContent();
151 void BufferState::set_sub_data(unsigned off, unsigned sz, const void *ptr)
153 if(data && off+sz<=size)
155 const char *cptr = reinterpret_cast<const char *>(ptr);
156 copy(cptr, cptr+sz, data+off);
160 string BufferState::describe() const
164 const char *what = (content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER ? "indices" : "vertices");
165 return content.describe()+strformat(", %d %s (%d bytes), %s",
166 size/content.stride, what, size, describe_enum(usage, ""));
169 return strformat("%d bytes, %s", size, describe_enum(usage, ""));
173 BufferBindingState::BufferBindingState():