Results 1 to 7 of 7

Thread: VBO Map-Unmap query

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2007
    Location
    Birmingham, UK
    Posts
    12

    VBO Map-Unmap query

    I am working with VBO's for a new project, I want to use flexible maaped VBO's which are similar to the Direct3D Lock() Unlock() mechanism. I have written some code below, which Maps and Unmaps the vertex data on the fly in the same way as the D3D lock, but it overwrites over the previous VBO vertex data... Here is the rough brief example of what i'm implementing...
    Code :
    //Vertex structure
    struct Vertex3D
    {
    	float x;
    	float y;
    	float z;
    	float u;
    	float v;
    };
     
    class VertexBuffer
    {
    	public:
    	VertexBuffer(){}
    	~VertexBuffer(){}
     
    	void InitialiseBuffer(Vuint buffer_size)
    	{
    		//NOW INTIAILISING VERTEX DATA FOR VBO
    		glGenBuffers(1, &ID);
    		glBindBuffer(GL_ARRAY_BUFFER,ID);
    		glBufferData(GL_ARRAY_BUFFER,buffer_size, NULL, V_VERT_BUF_STATIC);
     
    		glEnableClientState(GL_VERTEX_ARRAY);
    		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    		glVertexPointer(3, GL_FLOAT, sizeof(Vertex3D), BUFFER_OFFSET(0));
    		glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex3D), BUFFER_OFFSET(12));
    		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    		glDisableClientState(GL_VERTEX_ARRAY);
    	}
     
    	//MAPPING FUNCTION MEANT TO WORK LIKE LOCK FUNCTION IN DIRECT3D, BUT SHORTENED TO MAKE IT ONLY WRITE TO VERTEX BUFFER
    	void* MapBuffer()
    	{
    		glBindBufferARB(GL_ARRAY_BUFFER_ARB,ID);
    		return glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY_ARB);
    	}
     
    	void UnmapBuffer()
    	{
    		glBindBuffer(GL_ARRAY_BUFFER,ID);
    		glUnmapBuffer(GL_ARRAY_BUFFER);
    		glBindBufferARB(GL_ARRAY_BUFFER,0);
    	}
     
    	void BindBuffer()
    	{
    		glBindBuffer(GL_ARRAY_BUFFER, ID);
    	}
     
    	void Destroy()
    	{
    		glDeleteBuffers(1, &ID);
    	}
     
    	GLuint ID;
    };

    Now I am going to use the class to bind buffers

    Code :
    //PLACE VERTICES INTO VECTOR CONTAINER
    vector<Vertex3D>	points_one;
    vector<Vertex3D>	points_two;
     
    VertexBuffer	*buffer_one;
    VertexBuffer	*buffer_two;
     
    buffer_one = new VertexBuffer();
    buffer_two = new VertexBuffer();
     
    Vuint buffer_size_one = points_one.size()*sizeof(Vertex3D);
    Vuint buffer_size_two = points_two.size()*sizeof(Vertex3D);
     
    buffer_one->InitialiseBuffer(buffer_size_one);
    VertexBuffer* membuffer_one = (VertexBuffer*)buffer_one->MapBuffer();
    for(vector<Vertex3D>::iterator it = points_one.begin(); it!=points_one.end(); it++)
    {
    membuffer_one->x	=	it->x;
    membuffer_one->y	=	it->y;
    membuffer_one->z	=	it->z;
    membuffer_one->u	=	it->u;
    membuffer_one->v	=	it->v;
    membuffer_one++;
    }
    buffer_one->UnmapBuffer();
     
     
    buffer_two->InitialiseBuffer(buffer_size_two);
    VertexBuffer* membuffer_two = (VertexBuffer*)buffer_two->MapBuffer();
    for(vector<Vertex3D>::iterator it = points_two.begin(); it!=points_two.end(); it++)
    {
    	membuffer_two->x	=	it->x;
    	membuffer_two->y	=	it->y;
    	membuffer_two->z	=	it->z;
    	membuffer_two->u	=	it->u;
    	membuffer_two->v	=	it->v;
    	membuffer_two++;
    }
    buffer_two->UnmapBuffer();
    Code :
    buffer_one->BindBuffer();
    //RENDER VERTEX BUFFER THROUGH GL DRAWARRAYS
     
    buffer_two->BindBuffer();
    //AGAIN RENDER VERTEX BUFFER THROUGH GL DRAWARRAYS

    Now if I render and bind VBOs, the only vertex buffer that renders is the second binded vertex mesh during both instances... Could anyone give me advice on how to map it the same way as D3D.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Dec 2000
    Location
    Reutlingen, Germany
    Posts
    2,042

    Re: VBO Map-Unmap query

    Hm, i just quickly glanced at it, but one thing that could be a problem, is that you call glVertexPointer as the first thing to set up your arrays. Make it the LAST function to be called, most drivers do all the vertex array setup in that function call, so maybe that's the reaseon, that it only works properly the second time.

    Jan.
    GLIM - Immediate Mode Emulation for GL3

  3. #3
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: VBO Map-Unmap query

    I did not look deeply in your code, but I do not understand why you see something with the 2nd VBO. Does this one uses texture mapping?
    I have seen something that is wrong to me, you use texture coordinates buffer objects but you do not set which vertex array client state to be modified by glTexCoordPointer. You have to call glClientActiveTexture, this way, before drawing something:

    Code :
        glClientActiveTexture( GL_TEXTURE0_ARB );
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        // bind a vbo that contains texture coordinates
        // or call glTexCoordPointer with the array size and address
        // to use as a simple vertex array
        glBindBuffer( GL_ARRAY_BUFFER, texCoordBuffer );
        glTexCoordPointer(2, GL_FLOAT, 0, NULL);
     
        glActiveTexture( GL_TEXTURE0_ARB );
        glEnable( GL_TEXTURE_2D );
     
        glBindTexture( ... );
     
        // draw something
        ...
        // disable client states
        ...

  4. #4
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: VBO Map-Unmap query

    i had similar problem where i was binding the vertex -index buffers after all gl pointer calls. glBind them first and then specify the vertex stream solved the problem. maybe something similar here.

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2007
    Location
    Birmingham, UK
    Posts
    12

    Re: VBO Map-Unmap query

    Thanks guys, its all sorted... Happy Xmas to you all

  6. #6
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: VBO Map-Unmap query

    Great! Thanks you too! <8D

  7. #7
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: VBO Map-Unmap query

    still would like to know what the problem was. ;D merry xmas!

Similar Threads

  1. Use of map/unmap API
    By brijesh.jadav in forum OpenVX
    Replies: 0
    Last Post: 07-22-2016, 05:58 AM
  2. Slow JavaCL CLBuffer map/unmap operations
    By chippies in forum OpenCL
    Replies: 1
    Last Post: 10-08-2012, 08:55 PM
  3. Vbo map & unmap dumps garbage when using new?
    By th3w1zard in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-19-2010, 06:34 AM
  4. Unmap crash with map_read
    By matrem in forum OpenCL
    Replies: 0
    Last Post: 11-26-2009, 03:57 AM
  5. GPU Query
    By loriordan in forum OpenCL
    Replies: 1
    Last Post: 11-20-2009, 01:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean