VBO questions. Please help

Apprentice
Posts: 13
Joined: 2007.10
Post: #1
I have tried a couple VBO examples which worked fine. My question is what if I only bind vetex and normal to graphic card, but colors are still in nonVBO buffer. Does this work? I played a little and it crashes. Is this true?

codes look like this:
Code:
BuildVBOs(); bind all-vertex, normals, colors
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
if (m_nVBOVertices && m_nVBONormals)
{
  LI_glBindBuffer( LI_GL_ARRAY_BUFFER, m_nVBOVertices );
  glVertexPointer( 3, GL_FLOAT, 0, (GLfloat *)NULL );        
  LI_glBindBuffer( LI_GL_ARRAY_BUFFER, m_nVBONormals );
  glNormalPointer( GL_FLOAT, 0, (GLfloat *)NULL );
}
else
{        
  glVertexPointer(3, GL_FLOAT, (sizeof(GLfloat)*3), m_vertices);
  glNormalPointer(GL_FLOAT, (sizeof(GLfloat)*3), m_vnormals);
}
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer (3, GL_UNSIGNED_BYTE, (sizeof(GLubyte)*3), m_vcolors);
...
glDrawElements(...)
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

It works if I bind all of them, but it crashes if i do something like above.

Thanks for your response in advance.

-myfeng
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Make sure you glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); before you make any gl*Pointer calls that you don't want to come out of a VBO.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2007.10
Post: #3
Thanks. that fixes the crash. Smile
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2007.10
Post: #4
hi OneSadCookie, Can I ask you one more question?
I am doing 3D mesh rendering.. VBOs help a lot. but it's very slow(even slower than noVBOs) if I also send colors to the graphic card. Do you have any ideas about this problesm? Thanks.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
I've never seen anything like that. You'll need to post code, and tell us what GPU, CPU, and OS you're using.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2007.10
Post: #6
This is my code:
Code:
bool C3DMesh::BuildVBOs()
{
    clearVBOs();
    // Generate And Bind The Vertex Buffer
    glGenBuffers( 1, &m_nVBOVertices );                            // Get A Valid Name
    glBindBuffer( GL_ARRAY_BUFFER, m_nVBOVertices );            // Bind The Buffer
    // Load The Data
    glBufferDataARB( GL_ARRAY_BUFFER, m_numVertices*3*sizeof(GLfloat), m_vertices, GL_STATIC_DRAW );
    int bufferSize = 0;
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);
    if(m_numVertices*3*sizeof(GLfloat) != bufferSize)
    {
        clearVBOs();
        return false;
    }    

    glGenBuffers( 1, &m_nVBONormals );                            // Get A Valid Name
    glBindBuffer( GL_ARRAY_BUFFER, m_nVBONormals );            // Bind The Buffer
    glBufferDataARB( GL_ARRAY_BUFFER, m_numVertices*3*sizeof(GLfloat), m_vnormals, GL_STATIC_DRAW );
    bufferSize = 0;
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);
    if(m_numVertices*3*sizeof(GLfloat) != bufferSize)
    {
        clearVBOs();
        return false;
    }    
    
    glGenBuffers( 1, &m_nVBOColors );                            // Get A Valid Name
    glBindBuffer( GL_ARRAY_BUFFER, m_nVBOColors );            // Bind The Buffer
    glBufferDataARB( GL_ARRAY_BUFFER, m_numVertices*3*sizeof(GLubyte), m_vcolors, GL_STATIC_DRAW );
    bufferSize = 0;
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);
    if(m_numVertices*3*sizeof(GLubyte) != bufferSize)
    {
        clearVBOs();
        return false;
    }    
    return true;
}

DrawMesh()
{
    int numTriangles = getNumOfTriangles();

    if (numTriangles)
    {
        int n = 0;
        GLenum primMode;

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        if (m_bVBOSupported && m_nVBOVertices && m_nVBONormals)
        {

            glBindBuffer( GL_ARRAY_BUFFER, m_nVBOVertices );
            glVertexPointer( 3, GL_FLOAT, 0, (GLfloat *)NULL );        

            glBindBuffer( GL_ARRAY_BUFFER, m_nVBONormals );
            glNormalPointer( GL_FLOAT, 0, (GLfloat *)NULL );

        }
        else
        {
            
            glVertexPointer(3, GL_FLOAT, (sizeof(GLfloat)*3), m_vertices);
            glNormalPointer(GL_FLOAT, (sizeof(GLfloat)*3), m_vnormals);
        }

        if (m_vcolors && m_bColor == true)
        {
            
            float opacity[2];

            glEnableClientState(GL_COLOR_ARRAY);
            if (m_bVBOSupported && m_nVBOColors)
            {
                glBindBuffer( GL_ARRAY_BUFFER, m_nVBOColors );
                glColorPointer( 3, GL_UNSIGNED_BYTE, 0, (GLubyte *)NULL );
            }
            else
                glColorPointer (3, GL_UNSIGNED_BYTE, (sizeof(GLubyte)*3), m_vcolors);

        }
    
        unsigned int i,j;
        for(i = 0; i < m_faceList.count(); i++)
        {
            n = 0;

            S3DMeshFaceSet *pFace = m_faceList.at(i);
            // Apply face color if this object does not have per vertex color
            if(m_bColor == false)
            {
                
                // Enable transparency through vertex alpha
                glColor4f (pFace->m_color[0], pFace->m_color[1], pFace->m_color[2], m_fOpacity);
            }
            for(j = 0; j < pFace->m_primCount; j++)
            {
                
                primMode =  (pFace->m_primTypes[j] == MESH_PRIM_STRIP) ? GL_TRIANGLE_STRIP : GL_TRIANGLE_FAN;
                glDrawElements(primMode, pFace->m_primLengths[j], GL_UNSIGNED_SHORT, &pFace->m_primVerts[n]);
                n += pFace->m_primLengths[j];
            }  
        }

        if (m_vcolors && m_bColor == true)
        {
            glDisableClientState(GL_COLOR_ARRAY);
            glDisable(GL_TEXTURE_1D);
        }
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
    }
}

if I set m_bVBOSupported = false, the rendering would be fine with colors.
if I set m_bVBOSupported = true, the rendering without colors is fine, but it's very very slow with colors.

Thanks.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
Tried using 4-component color?

You haven't said what CPU, GPU and OS this refers to.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2007.10
Post: #8
Sorry that I forgot to say OS and hardwares.
ok. I am using WindowXP. cpu is Pentium®4 3.4GHz and graphic card is ATI(RADEON X300 Seies)

Yes. I use 4 compoments colors
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
Your code above is using 3-component ubyte colors, which is probably a very bad size. Try changing to 4.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2007.10
Post: #10
Dear OneSadCookie,

I have to say you are a superman of openGL. It works after I changed color to 4 components. but I dont understand why I have to used 4 componets color with VBOs.

Thanks,
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
When you're not using VBOs, the data is copied to the video card by the driver during the DrawElements call. It's free to add a fourth component (for alignment reasons) at that time. That'll no doubt be slower than just sending four-component colors, but probably not so much slower that you'd notice.

When you're using VBOs, the data is sent to the card *exactly* as you specify it in BufferData. That means that if you use a format the card doesn't like, the driver has no opportunity to correct your mistake.
Quote this message in a reply
Sage
Posts: 1,234
Joined: 2002.10
Post: #12
Well, that isn't quite accurate. The rendering should succeed regardless of you providing data in a format that isn't natively accelerated. The driver will have to reformat the data for every vertex, though, negating any performance advantage you're trying to get by using VBO.

If you're crashing it could be due to a broken slow path in your Windows driver.

But OSC's suggestion is still correct-- use data formats which can be natively accelerated.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2007.10
Post: #13
Thanks for you guy's help and responses
Quote this message in a reply
Post Reply