Thread: [OGL] Vertex buffer objects: not rendering.

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071

    [OGL] Vertex buffer objects: not rendering.

    So, I've decided to switch my renderer over to vertex buffer objects, to help speed things up a bit.

    I've formatted the arrays every which way, but for whatever reason, the thing isn't rendering.

    Note: Code is in C++/CLI. The concept is the same. I can post a standard C++ version if you wish, just let me know.

    In CModel:
    Code:
    //..//
    public:
    int vertexBufferID;
    array<float> ^vertexBuffer;
    //..//
    CreateBuffers() [Member of CModel]:
    Code:
    void CModel::CreateBuffers(){
    	vertexBuffer = gcnew array<float>(numVertices*9);
    	int vertIndex = 0;
    	for(int i=0; i<numMeshes; i++){
    		for(int j=0; j<mesh[i].numTriangles; j++){
    			int triangleIndex = mesh[i].triangleIndices[j];
    			const Triangle *triPtr = &triangle[triangleIndex];
    			int index[3];
    
    			index[0] = triPtr->vertexIndices[0];
    			index[1] = triPtr->vertexIndices[1];
    			index[2] = triPtr->vertexIndices[2];
    			
    			vertexBuffer[vertIndex+0] = vertex[index[0]].location[0];
    			vertexBuffer[vertIndex+1] = vertex[index[0]].location[1];
    			vertexBuffer[vertIndex+2] = vertex[index[0]].location[2];
    
    			vertexBuffer[vertIndex+3] = vertex[index[1]].location[0];
    			vertexBuffer[vertIndex+4] = vertex[index[1]].location[1];
    			vertexBuffer[vertIndex+5] = vertex[index[1]].location[2];
    
    			vertexBuffer[vertIndex+6] = vertex[index[2]].location[0];
    			vertexBuffer[vertIndex+7] = vertex[index[2]].location[1];
    			vertexBuffer[vertIndex+8] = vertex[index[2]].location[2];
    
    			vertIndex += 9;
    		}
    	}
    
    	int objectArraySizeOut;
    	int objectArraySize = vertexBuffer->Length*sizeof(float);
    	Gl::glGenBuffersARB(1, vertexBufferID);
    	Gl::glBindBufferARB(Gl::GL_ARRAY_BUFFER_ARB, vertexBufferID);
    	Gl::glBufferDataARB(Gl::GL_ARRAY_BUFFER_ARB, objectArraySize, vertexBuffer, Gl::GL_STATIC_DRAW_ARB);
    	
    	Gl::glGetBufferParameteriv(Gl::GL_ARRAY_BUFFER_ARB, Gl::GL_BUFFER_SIZE_ARB, objectArraySizeOut);
    	if(objectArraySizeOut <= 0){
    		CUtility::Log("Buffer error.");
    	}
    }
    Rendering code:
    Code:
    Gl::glEnableClientState(Gl::GL_VERTEX_ARRAY);
    
    Gl::glBindBufferARB(Gl::GL_ARRAY_BUFFER_ARB, vertexBufferID);
    Gl::glVertexPointer(3, Gl::GL_FLOAT, 0, IntPtr::Zero);
    Gl::glDrawArrays(Gl::GL_TRIANGLES, 0, numVertices);
    
    Gl::glDisableClientState(Gl::GL_VERTEX_ARRAY);
    CreateBuffers() is called on object init, after model data has been loaded.

    I've probably done something terribly stupid, but I can't figure out what. I've also tried using a multi-dimentional array to hold the buffer data, with the first dimention being the vertex index, and the second being the x,y or z, but it didn't work either.

    Thanks.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Eh, nevermind. I was passing the buffer the address of the array, not the address of the first index of the array.

    Code:
    Gl::glBufferDataARB(Gl::GL_ARRAY_BUFFER_ARB, objectArraySize, vertexBuffer[0], Gl::GL_STATIC_DRAW_ARB);
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Way to go.

    I never make mistakes like that.

    Yeah right. Usually those are the ones that take me hours to solve.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DX9 visualization problem
    By darcome in forum Game Programming
    Replies: 4
    Last Post: 03-05-2003, 12:40 PM
  2. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM
  3. draw to a BITMAP* argument - allegro and objects again
    By MadHatter in forum Game Programming
    Replies: 13
    Last Post: 12-13-2002, 06:51 AM
  4. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM
  5. Using D3DFVF_XYZRHW flag for a Vertex Buffer
    By lightatdawn in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2001, 02:48 PM