matthias's picture

Having Problems with VBO in c#.net

Hello,

i'm having a little trouble displaying a point-cloud i have... (at least i have the problem using VBO)
i have it working, but it's drawing the point-cloud directly.
Anyway, i would like to use VBO instead (since i expect better performance from it), and i think that's the correct way to display it.
However, this is the code i'm using - but it's displaying nothing exept a white window.
Maybe somone could point me to the right direction, what exactly i'm doing wrong, or how i could best debug the code (i don't really know where i could see if the bufferdata was written correctly or not). (anyway, debugging shows that "drawarrays" is the last step if the pointcloud-array contains anything)

This question may sound silly to most of you, but i had big problems with the tutorial, went over it time after time, but there is no complete sample included (at least as far as i could see), and i was never able to display anything using "drawarray".

The used Language is c#.net in 2010 Express and i'm using the glControl for the display.

 private void glControl1_Load(object sender, EventArgs e)
        {
            GL.ClearColor(Color.White);           
             int w = glControl1.Width;
            int h = glControl1.Height;
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(0, w, h, 0, 0, 2000); 
            GL.Viewport(0,0, w, h); 
        }

This is how i initialize the VBO:

        private void button7_Click(object sender, EventArgs e)
        {        
            GL.EnableClientState(EnableCap.VertexArray);
            GL.Enable(EnableCap.DepthTest);
            uint[] VBOid = new uint[2];
 
            Vector3[] vertices = new Vector3[globals.point_cloud.Count]; 
 
            for (int i = 0; i < globals.point_cloud.Count; i++)
            {
                  //converting the point_cloud (List<Vector3> to the array vertices
                vertices[i] = globals.point_cloud[i];
            }
 
            GL.GenBuffers(2, VBOid);
            GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length), vertices, BufferUsageHint.StaticDraw);
 
            vbo_size = vertices.Length;
 
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.DisableVertexAttribArray(vbo_id);
        }

this is called in the "paint"-event from the glcontrol:

       private void render()
        {
               GL.Clear(ClearBufferMask.ColorBufferBit |
                        ClearBufferMask.DepthBufferBit |
                        ClearBufferMask.StencilBufferBit);
 
               GL.MatrixMode(MatrixMode.Modelview);
 
               GL.EnableClientState(EnableCap.VertexArray);
 
               GL.Translate(move_x, move_y, -move_z); 
               GL.Translate(-rotationY * move_z / (float)100, rotationX * move_z / (float)100, 0);
 
               GL.Rotate(rotationX, Vector3.UnitX); 
 
               GL.Rotate(rotationY, Vector3.UnitY);
 
               GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_id);
 
               GL.Color3(Color.Black);
               GL.VertexPointer(3, VertexPointerType.Float, 3 * sizeof(float), new IntPtr(0));
               GL.DrawArrays(BeginMode.Points, 0, globals.point_cloud.Count);
             // the program only goes past this point if the array is empty. Any other case, it's stopping at "drawarrays" and calling "paint" again.
               GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
               GL.DisableClientState(EnableCap.VertexArray);
               GL.DisableVertexAttribArray(vbo_id);
 
            glControl1.SwapBuffers();
 
        }

However, i think the problem is that GL.DrawArrays(BeginMode.Points, 0, globals.point_cloud.Count); is not working correctly, at least not if the vertices-array contains any points. In that case, all other steps inside the "render"-function are not called whatsoever (like DrawArrays would call some kind of return for the function)

the following is the code that displays the point-cloud quite well... so the point-cloud should be ok as i have it...(but i'm having no problems with it so far).

 
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
 
            GL.Translate(move_x, move_y, -move_z); // position triangle according to our x variable
            GL.Translate(-rotationY * move_z / (float)100, rotationX * move_z / (float)100, 0);
 
            GL.Rotate(rotationX, Vector3.UnitX);  
 
            GL.Rotate(rotationY, Vector3.UnitY); 
 
            GL.Color3(Color.Blue);
            GL.Begin(BeginMode.Points);
 
            for (int i = 0; i < globals.point_cloud.Count; i += 1)
            {
                GL.Vertex3(globals.point_cloud[i]);
            }
            GL.End();

i hope i was able to explain a little where i'm having problems and somone is able to point out my mistakes.

greetings
Matthias


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Robmaister's picture

Your issue is with how you're passing data to GL.BufferData. The size parameter is size in bytes, not the number of elements in the array.

GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw);

http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml