Revision: 20411
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20411
Author:   dfelinto
Date:     2009-05-25 23:29:50 +0200 (Mon, 25 May 2009)

Log Message:
-----------
Missing files from last commit (Patch:[#17523] BGE VBO patch by Samuel Anjam 
(toonist))
- patch updated for recent trunk.

This patch implement a VBO option to handle skinned meshes.
It doesn't give better performance than Display List (at least not in 
un-skinned meshes).

But this is an important addition to in the near future implement full GLSL 
skinned objects.

Modified Paths:
--------------
    
branches/bb_dev/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.cpp
    
branches/bb_dev/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.h

Modified: 
branches/bb_dev/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.cpp
===================================================================
--- 
branches/bb_dev/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.cpp
        2009-05-25 21:26:30 UTC (rev 20410)
+++ 
branches/bb_dev/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.cpp
        2009-05-25 21:29:50 UTC (rev 20411)
@@ -103,6 +103,169 @@
        RAS_OpenGLRasterizer::Exit();
 }
 
+void RAS_VAOpenGLRasterizer::ClearVboSlot(class RAS_VboSlot *slot)
+{
+       if(!GLEW_ARB_vertex_buffer_object) return;
+       if(slot->m_verts) delete slot->m_verts;
+       if(slot->m_vertVbo) glDeleteBuffers(1, &slot->m_vertVbo);
+       if(slot->m_indexVbo) glDeleteBuffers(1, &slot->m_indexVbo);
+       if(slot->m_normalVbo) glDeleteBuffers(1, &slot->m_normalVbo);
+       if(slot->m_tangentVbo) glDeleteBuffers(1, &slot->m_tangentVbo);
+       if(slot->m_colorVbo) glDeleteBuffers(1, &slot->m_colorVbo);
+       if(slot->m_texCoordVbo[0]) glDeleteBuffers(1, &slot->m_texCoordVbo[0]);
+       if(slot->m_texCoordVbo[1]) glDeleteBuffers(1, &slot->m_texCoordVbo[1]);
+
+       slot->m_verts = 0;
+       slot->m_vertVbo = 0;
+       slot->m_indexVbo = 0;
+       slot->m_normalVbo = 0;
+       slot->m_tangentVbo = 0;
+       slot->m_colorVbo = 0;
+       slot->m_texCoordVbo[0] = 0;
+       slot->m_texCoordVbo[1] = 0;
+}
+
+void RAS_VAOpenGLRasterizer::InitVboSlot(class RAS_DisplayArray* array, class 
RAS_MeshSlot *ms)
+{
+       if(array->m_vertex.size() == 0 || array->m_index.size() == 0
+               || !GLEW_ARB_vertex_buffer_object) return;
+       // clean up any previous data before creating a new slot
+       if(array->m_vboSlot) delete array->m_vboSlot;
+       array->m_vboSlot = new RAS_VboSlot(this);
+       /* uploading data to vertex buffer objects doesn't allow
+       stride so we have to grab the data from RAS_TexVert and
+       initialize new arrays which we then use to upload the
+       data to the vbos*/
+       float *normals = 0;
+       float *tangents = 0;
+       float *texCoords0 = 0;
+       float *texCoords1 = 0;
+       unsigned char *colors = 0;
+       bool isColors = glIsEnabled(GL_COLOR_ARRAY);
+       array->m_vboSlot->m_verts = new float[array->m_vertex.size()*3];
+       normals = new float[array->m_vertex.size()*3];
+       tangents = new float[array->m_vertex.size()*4];
+       texCoords0 = new float[array->m_vertex.size()*2];
+       texCoords1 = new float[array->m_vertex.size()*2];
+       colors = new unsigned char[array->m_vertex.size()*4];
+       // upload the data
+       unsigned int num0 = 0;
+       unsigned int num1 = 0;
+       unsigned int num2 = 0;
+       unsigned int vertnum = 0;
+       for(vertnum = 0; vertnum < array->m_vertex.size(); vertnum++)
+       {
+               memcpy(&array->m_vboSlot->m_verts[num0], 
array->m_vertex[vertnum].getXYZ(), sizeof(float)*3);
+               memcpy(&normals[num0], array->m_vertex[vertnum].getNormal(), 
sizeof(float)*3);
+               memcpy(&texCoords0[num1], array->m_vertex[vertnum].getUV1(), 
sizeof(float)*2);
+               memcpy(&texCoords1[num1], array->m_vertex[vertnum].getUV2(), 
sizeof(float)*2);
+               memcpy(&tangents[num2], array->m_vertex[vertnum].getTangent(), 
sizeof(float)*4);
+               memcpy(&colors[num2], array->m_vertex[vertnum].getRGBA(), 
sizeof(int));
+               num0+=3;
+               num1+=2;
+               num2+=4;
+       }
+       // create the vertex buffer objects
+       glGenBuffersARB(1, &array->m_vboSlot->m_vertVbo);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, array->m_vboSlot->m_vertVbo);
+       glBufferDataARB(GL_ARRAY_BUFFER_ARB, 
sizeof(float)*array->m_vertex.size()*3,
+               array->m_vboSlot->m_verts, GL_DYNAMIC_DRAW_ARB);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+       glGenBuffersARB(1, &array->m_vboSlot->m_normalVbo);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, array->m_vboSlot->m_normalVbo);
+       glBufferDataARB(GL_ARRAY_BUFFER_ARB, 
sizeof(float)*array->m_vertex.size()*3,
+               normals, GL_STATIC_DRAW_ARB);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+       glGenBuffersARB(1, &array->m_vboSlot->m_tangentVbo);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, array->m_vboSlot->m_tangentVbo);
+       glBufferDataARB(GL_ARRAY_BUFFER_ARB, 
sizeof(float)*array->m_vertex.size()*4,
+               tangents, GL_STATIC_DRAW_ARB);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+       glGenBuffersARB(1, &array->m_vboSlot->m_texCoordVbo[0]);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, 
array->m_vboSlot->m_texCoordVbo[0]);
+       glBufferDataARB(GL_ARRAY_BUFFER_ARB, 
sizeof(float)*array->m_vertex.size()*2,
+               texCoords0, GL_STATIC_DRAW_ARB);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+       glGenBuffersARB(1, &array->m_vboSlot->m_texCoordVbo[1]);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, 
array->m_vboSlot->m_texCoordVbo[1]);
+       glBufferDataARB(GL_ARRAY_BUFFER_ARB, 
sizeof(float)*array->m_vertex.size()*2,
+               texCoords1, GL_STATIC_DRAW_ARB);
+       glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+       if(isColors)
+       {
+               glGenBuffersARB(1, &array->m_vboSlot->m_colorVbo);
+               glBindBufferARB(GL_ARRAY_BUFFER_ARB, 
array->m_vboSlot->m_colorVbo);
+               glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(unsigned 
char)*array->m_vertex.size()*4,
+                       colors, GL_STATIC_DRAW_ARB);
+               glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+       }
+       glGenBuffersARB(1, &array->m_vboSlot->m_indexVbo);
+       glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 
array->m_vboSlot->m_indexVbo);
+       glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(unsigned 
short)*array->m_index.size(),
+               &array->m_index[0], GL_STATIC_DRAW_ARB);
+       glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+       // clean up
+       delete[] array->m_vboSlot->m_verts;
+       array->m_vboSlot->m_verts = 0;
+       delete[] normals;
+       delete[] texCoords0;
+       delete[] texCoords1;
+       delete[] colors;
+}
+
+void RAS_VAOpenGLRasterizer::UpdateMeshSlotData(class RAS_MeshSlot *ms,
+       const bool &anim, const bool &zsort)
+{
+       if(!GLEW_ARB_vertex_buffer_object) return;
+       RAS_MeshSlot::iterator it;
+       unsigned int vertnum = 0;
+       unsigned int num = 0;
+       RAS_VboSlot *slot = 0;
+       int usage = 0;
+       for(ms->begin(it); !ms->end(it); ms->next(it)) {
+               if(it.totindex == 0)
+                       continue;
+               if(!it.array->m_vboSlot) continue;
+               slot = it.array->m_vboSlot;
+               if(anim)
+               {
+                       if(!slot->m_verts)
+                               slot->m_verts = new 
float[it.array->m_vertex.size()*3];
+                       for(vertnum = 0, num = 0; vertnum < 
it.array->m_vertex.size(); vertnum++)
+                       {
+                               memcpy(&slot->m_verts[num],
+                                       it.array->m_vertex[vertnum].getXYZ(), 
sizeof(float)*3);
+                               num+=3;
+                       }
+                       glBindBufferARB(GL_ARRAY_BUFFER_ARB, slot->m_vertVbo);
+                       glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0,
+                               sizeof(float)*it.array->m_vertex.size()*3, 
slot->m_verts);
+                       glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+               }
+               if(zsort)
+               {
+                       // make sure that we have a dynamic index
+                       glGetBufferParameteriv(slot->m_indexVbo, 
GL_BUFFER_USAGE_ARB, &usage);
+                       if(usage != GL_DYNAMIC_DRAW_ARB)
+                       {
+                               glGenBuffersARB(1, &slot->m_indexVbo);
+                               glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 
slot->m_indexVbo);
+                               glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
+                                       sizeof(unsigned 
short)*it.array->m_index.size(),
+                                       &it.array->m_index[0], 
GL_DYNAMIC_DRAW_ARB);
+                               glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+                       }
+                       else
+                       {
+                               glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 
slot->m_indexVbo);
+                               glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 
0,
+                                       sizeof(unsigned 
short)*it.array->m_index.size(), &it.array->m_index[0]);
+                               glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+                       }
+               }
+       }
+}
+
 void RAS_VAOpenGLRasterizer::IndexPrimitives(RAS_MeshSlot& ms)
 {
        static const GLsizei stride = sizeof(RAS_TexVert);
@@ -148,16 +311,49 @@
                else
                        glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
 
-               glVertexPointer(3, GL_FLOAT, stride, it.vertex->getXYZ());
-               glNormalPointer(GL_FLOAT, stride, it.vertex->getNormal());
-               if(!wireframe) {
-                       glTexCoordPointer(2, GL_FLOAT, stride, 
it.vertex->getUV1());
-                       if(glIsEnabled(GL_COLOR_ARRAY))
-                               glColorPointer(4, GL_UNSIGNED_BYTE, stride, 
it.vertex->getRGBA());
+               if(GLEW_ARB_vertex_buffer_object) {
+                       if(!it.array->m_vboSlot) InitVboSlot(it.array, &ms);
+                       if(it.array->m_vboSlot)
+                       {
+                               glBindBuffer(GL_ARRAY_BUFFER_ARB, 
it.array->m_vboSlot->m_vertVbo);
+                               glVertexPointer(3, GL_FLOAT, 0, 0);
+                               glBindBuffer(GL_ARRAY_BUFFER_ARB, 
it.array->m_vboSlot->m_normalVbo);
+                               glNormalPointer(GL_FLOAT, 0, 0);
+                               if(!wireframe) {
+                                       glBindBuffer(GL_ARRAY_BUFFER_ARB, 
it.array->m_vboSlot->m_texCoordVbo[0]);
+                                       glTexCoordPointer(2, GL_FLOAT, 0, 0);
+                                       if(glIsEnabled(GL_COLOR_ARRAY))
+                                       {
+                                               
glBindBuffer(GL_ARRAY_BUFFER_ARB, it.array->m_vboSlot->m_colorVbo);
+                                               glColorPointer(4, 
GL_UNSIGNED_BYTE, 0, 0);
+                                       }
+                               }
+                               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 
it.array->m_vboSlot->m_indexVbo);
+                               // a little clean up...
+                               glDrawElements(drawmode, it.totindex, 
GL_UNSIGNED_SHORT, 0);
+                               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
+                               glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
+                               glVertexPointer(3, GL_FLOAT, 0, 0);
+                               glNormalPointer(GL_FLOAT, 0, 0);
+                               if(!wireframe) {
+                                       glTexCoordPointer(2, GL_FLOAT, 0, 0);
+                                       if(glIsEnabled(GL_COLOR_ARRAY))
+                                               glColorPointer(4, 
GL_UNSIGNED_BYTE, 0, 0);
+                               }
+                       }
                }
 
-               // here the actual drawing takes places
-               glDrawElements(drawmode, it.totindex, GL_UNSIGNED_SHORT, 
it.index);
+               if(!it.array->m_vboSlot) {
+                       glVertexPointer(3, GL_FLOAT, stride, 
it.vertex->getXYZ());
+                       glNormalPointer(GL_FLOAT, stride, 
it.vertex->getNormal());
+                       if(!wireframe) {
+                               glTexCoordPointer(2, GL_FLOAT, stride, 
it.vertex->getUV1());
+                               if(glIsEnabled(GL_COLOR_ARRAY))
+                                       glColorPointer(4, GL_UNSIGNED_BYTE, 
stride, it.vertex->getRGBA());
+                       }
+                       // here the actual drawing takes places
+                       glDrawElements(drawmode, it.totindex, 
GL_UNSIGNED_SHORT, it.index);
+               }
        }
 
        if(!wireframe) {
@@ -172,6 +368,7 @@
        bool wireframe = m_drawingmode <= KX_WIREFRAME;
        RAS_MeshSlot::iterator it;
        GLenum drawmode;
+       int unit = 0;
 
        if (ms.m_pDerivedMesh) {
                // cannot be handled here, pass to RAS_OpenGLRasterizer
@@ -211,16 +408,52 @@
                else
                        glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
 
-               glVertexPointer(3, GL_FLOAT, stride, it.vertex->getXYZ());
-               glNormalPointer(GL_FLOAT, stride, it.vertex->getNormal());
-               if(!wireframe) {
-                       TexCoordPtr(it.vertex);
-                       if(glIsEnabled(GL_COLOR_ARRAY))
-                               glColorPointer(4, GL_UNSIGNED_BYTE, stride, 
it.vertex->getRGBA());
+               if(GLEW_ARB_vertex_buffer_object) {
+                       if(!it.array->m_vboSlot) InitVboSlot(it.array, &ms);
+                       if(it.array->m_vboSlot)
+                       {
+                               glBindBuffer(GL_ARRAY_BUFFER_ARB, 
it.array->m_vboSlot->m_vertVbo);
+                               glVertexPointer(3, GL_FLOAT, 0, 0);
+                               glBindBuffer(GL_ARRAY_BUFFER_ARB, 
it.array->m_vboSlot->m_normalVbo);
+                               glNormalPointer(GL_FLOAT, 0, 0);
+                               if(!wireframe) {
+                                       TexCoordPtr(it.array);

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to