00001
00007 #include "OpenGLObject.h"
00008
00009 GLenum arrayGLPrimitive[] = { GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_QUADS, GL_QUAD_STRIP };
00010 GLenum arrayGLType[] = { GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY };
00011 GLenum arrayGLFormat[] = { GL_FLOAT, GL_UNSIGNED_BYTE };
00012
00013 OpenGLObject::OpenGLObject() {
00014 vertexBufferID = 0;
00015 indiceBufferID = 0;
00016 }
00017
00018 OpenGLObject::~OpenGLObject() {
00019 }
00020
00021 bool OpenGLObject::createVertexBuffer() {
00022 if (GL_ARB_vertex_buffer_object_supported) {
00023 glGenBuffersARB(1, &vertexBufferID);
00024 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBufferID);
00025 glBufferDataARB(GL_ARRAY_BUFFER_ARB, nVertices * vertexSize, vertices, GL_STATIC_DRAW_ARB);
00026
00027 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
00028
00029 if (nIndices > 0) {
00030 glGenBuffersARB(1, &indiceBufferID);
00031 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indiceBufferID);
00032 glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * indiceSize, indices, GL_STATIC_DRAW_ARB);
00033
00034 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
00035 }
00036
00037 LOG_SUCCESS(("Vertex buffer uploaded"));
00038 }
00039 else
00040 LOG_ERROR(("Unable to upload vertex buffer"));
00041
00042 return true;
00043 }
00044
00045 bool OpenGLObject::clearVertexBuffer() {
00046 if (GL_ARB_vertex_buffer_object_supported) {
00047 if (vertexBufferID != 0) glDeleteBuffersARB(1, &vertexBufferID);
00048 if (indiceBufferID != 0) glDeleteBuffersARB(1, &indiceBufferID);
00049
00050 LOG_SUCCESS(("Vertex buffer cleared"));
00051 }
00052 else
00053 LOG_ERROR(("Unable to clear vertex buffer"));
00054
00055 return true;
00056 }
00057
00058 void OpenGLObject::draw() {
00059 unsigned int i;
00060 unsigned int nFormats = formats.getCount();
00061
00062 if (GL_ARB_vertex_buffer_object_supported && vertexBufferID != 0) {
00063 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBufferID);
00064 }
00065
00066
00067 for (i=0; i<nFormats; i++) {
00068 Format format = formats[i];
00069 GLenum form = arrayGLFormat[format.attFormat];
00070 GLenum type = arrayGLType[format.attType];
00071
00072
00073 char *bsp = (vertexBufferID != 0)? NULL : vertices;
00074
00075 switch (format.attType) {
00076 case ATT_VERTEX:
00077 glVertexPointer(format.size, form, vertexSize, bsp + format.offset);
00078 break;
00079 case ATT_NORMAL:
00080 glNormalPointer(form, vertexSize, bsp + format.offset);
00081 break;
00082 case ATT_COLOR:
00083 glColorPointer(format.size, form, vertexSize, bsp + format.offset);
00084 break;
00085 case ATT_TEX:
00086 glTexCoordPointer(format.size, form, vertexSize, bsp + format.offset);
00087 }
00088 glEnableClientState(type);
00089 }
00090
00091
00092 if (nIndices == 0) {
00093 glDrawArrays(arrayGLPrimitive[primitive], 0, nVertices);
00094 }
00095 else {
00096 if (indiceBufferID != 0) glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indiceBufferID);
00097
00098 glDrawElements(arrayGLPrimitive[primitive], nIndices, GL_UNSIGNED_SHORT, (indiceBufferID != 0)? NULL : indices);
00099
00100 if (indiceBufferID != 0) glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
00101 }
00102
00103
00104 for (i=0; i<nFormats; i++) {
00105 glDisableClientState(arrayGLType[formats[i].attType]);
00106 }
00107
00108 if (GL_ARB_vertex_buffer_object_supported && vertexBufferID != 0) {
00109 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
00110 }
00111 }