1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
namespace varray
{
#ifndef VARRAY_INTERNAL
enum
{
ATTRIB_VERTEX = 1<<0,
ATTRIB_COLOR = 1<<1,
ATTRIB_NORMAL = 1<<2,
ATTRIB_TEXCOORD0 = 1<<3,
ATTRIB_TEXCOORD1 = 1<<4,
MAXATTRIBS = 5
};
extern vector<uchar> data;
extern void enable();
extern void begin(GLenum mode);
extern void defattrib(int type, int size, GLenum format);
template<class T>
static inline void attrib(T x)
{
T *buf = (T *)data.pad(sizeof(T));
buf[0] = x;
}
template<class T>
static inline void attrib(T x, T y)
{
T *buf = (T *)data.pad(2*sizeof(T));
buf[0] = x;
buf[1] = y;
}
template<class T>
static inline void attrib(T x, T y, T z)
{
T *buf = (T *)data.pad(3*sizeof(T));
buf[0] = x;
buf[1] = y;
buf[2] = z;
}
template<class T>
static inline void attrib(T x, T y, T z, T w)
{
T *buf = (T *)data.pad(4*sizeof(T));
buf[0] = x;
buf[1] = y;
buf[2] = z;
buf[3] = w;
}
template<size_t N, class T>
static inline void attribv(const T *v)
{
data.put((const uchar *)v, N*sizeof(T));
}
extern int end();
extern void disable();
#else
struct attribinfo
{
int type, size, formatsize, offset;
GLenum format;
attribinfo() : type(0), size(0), formatsize(0), offset(0), format(GL_FALSE) {}
bool operator==(const attribinfo &a) const
{
return type == a.type && size == a.size && format == a.format && offset == a.offset;
}
bool operator!=(const attribinfo &a) const
{
return type != a.type || size != a.size || format != a.format || offset != a.offset;
}
};
vector<uchar> data;
static attribinfo attribs[MAXATTRIBS], lastattribs[MAXATTRIBS];
static int enabled = 0, numattribs = 0, attribmask = 0, numlastattribs = 0, lastattribmask = 0, vertexsize = 0, lastvertexsize = 0;
static GLenum primtype = GL_TRIANGLES;
static uchar *lastbuf = NULL;
static bool changedattribs = false;
void enable()
{
enabled = 0;
numlastattribs = lastattribmask = lastvertexsize = 0;
lastbuf = NULL;
}
void begin(GLenum mode)
{
primtype = mode;
}
void defattrib(int type, int size, GLenum format)
{
if(type == ATTRIB_VERTEX)
{
numattribs = attribmask = 0;
vertexsize = 0;
}
changedattribs = true;
attribmask |= type;
attribinfo &a = attribs[numattribs++];
a.type = type;
a.size = size;
a.format = format;
switch(format)
{
case GL_UNSIGNED_BYTE: a.formatsize = 1; break;
case GL_BYTE: a.formatsize = 1; break;
case GL_UNSIGNED_SHORT: a.formatsize = 2; break;
case GL_SHORT: a.formatsize = 2; break;
case GL_UNSIGNED_INT: a.formatsize = 4; break;
case GL_INT: a.formatsize = 4; break;
case GL_FLOAT: a.formatsize = 4; break;
case GL_DOUBLE: a.formatsize = 8; break;
default: a.formatsize = 0; break;
}
a.formatsize *= size;
a.offset = vertexsize;
vertexsize += a.formatsize;
}
static inline void setattrib(const attribinfo &a, uchar *buf)
{
switch(a.type)
{
case ATTRIB_VERTEX:
if(!(enabled&a.type)) glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(a.size, a.format, vertexsize, buf);
break;
case ATTRIB_COLOR:
if(!(enabled&a.type)) glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(a.size, a.format, vertexsize, buf);
break;
case ATTRIB_NORMAL:
if(!(enabled&a.type)) glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(a.format, vertexsize, buf);
break;
case ATTRIB_TEXCOORD0:
if(!(enabled&a.type)) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(a.size, a.format, vertexsize, buf);
break;
case ATTRIB_TEXCOORD1:
glClientActiveTexture_(GL_TEXTURE1_ARB);
if(!(enabled&a.type)) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(a.size, a.format, vertexsize, buf);
glClientActiveTexture_(GL_TEXTURE0_ARB);
break;
}
enabled |= a.type;
}
static inline void unsetattrib(const attribinfo &a)
{
switch(a.type)
{
case ATTRIB_VERTEX:
glDisableClientState(GL_VERTEX_ARRAY);
break;
case ATTRIB_COLOR:
glDisableClientState(GL_COLOR_ARRAY);
break;
case ATTRIB_NORMAL:
glDisableClientState(GL_NORMAL_ARRAY);
break;
case ATTRIB_TEXCOORD0:
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
break;
case ATTRIB_TEXCOORD1:
glClientActiveTexture_(GL_TEXTURE1_ARB);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture_(GL_TEXTURE0_ARB);
break;
}
enabled &= ~a.type;
}
int end()
{
if(data.empty()) return 0;
uchar *buf = data.getbuf();
bool forceattribs = numattribs != numlastattribs || vertexsize != lastvertexsize || buf != lastbuf;
if(forceattribs || changedattribs)
{
int diffmask = enabled & lastattribmask & ~attribmask;
if(diffmask) loopi(numlastattribs)
{
const attribinfo &a = lastattribs[i];
if(diffmask & a.type) unsetattrib(a);
}
uchar *src = buf;
loopi(numattribs)
{
const attribinfo &a = attribs[i];
if(forceattribs || a != lastattribs[i])
{
setattrib(a, src);
lastattribs[i] = a;
}
src += a.formatsize;
}
lastbuf = buf;
numlastattribs = numattribs;
lastattribmask = attribmask;
lastvertexsize = vertexsize;
changedattribs = false;
}
int numvertexes = data.length()/vertexsize;
glDrawArrays(primtype, 0, numvertexes);
data.setsize(0);
return numvertexes;
}
void disable()
{
if(!enabled) return;
if(enabled&ATTRIB_VERTEX) glDisableClientState(GL_VERTEX_ARRAY);
if(enabled&ATTRIB_COLOR) glDisableClientState(GL_COLOR_ARRAY);
if(enabled&ATTRIB_NORMAL) glDisableClientState(GL_NORMAL_ARRAY);
if(enabled&ATTRIB_TEXCOORD0) glDisableClientState(GL_TEXTURE_COORD_ARRAY);
if(enabled&ATTRIB_TEXCOORD1)
{
glClientActiveTexture_(GL_TEXTURE1_ARB);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture_(GL_TEXTURE0_ARB);
}
enabled = 0;
}
#endif
}
|