Sign in to follow this  
Kobayashi

OpenGL Problem with Vertex Buffers

Recommended Posts

Hello I am recently trying to create terrain viewer in C# using OpenGL(Colin Fahey class - CPF.GRV10.GRControl.cs). After few hours of searching and asking questions i finally created vertex buffers: _terview._terrain_data is type of CVert[_number_of_verticies] _terview._vertex_buffer i type of int[1] CVert is a struct with float x,y,z;
gr.glGenBuffers(1, _terview._vertex_buffer);
gr.glBindBuffer(GR.GL_ARRAY_BUFFER, _terview._vertex_buffer[0]);

unsafe
{
  fixed (CVert* _terrdata = _terview._terrain_data)
  {
       gr.glBufferData(GR.GL_ARRAY_BUFFER,
                      _number_of_verticies * 3 * sizeof(float),
                      (IntPtr)_terrdata,
                       GR.GL_STATIC_DRAW_ARB);
  }
}



And this is how i am displaying vbo: _null is int[1] where [0] = 0;
gr.glEnableClientState(GR.GL_VERTEX_ARRAY);

     gr.glBindBuffer(GR.GL_ARRAY_BUFFER, _terview._vertex_buffer[0]);
     gr.glVertexPointer(3, GR.GL_FLOAT, 0, _null);
     gr.glDrawArrays(GR.GL_TRIANGLES, 0, _number_of_verticies);

gr.glDisableClientState(GR.GL_VERTEX_ARRAY);


And this is how i am creating terrain data ( its from NEHE VBO tutorial ):
for (int _x = 0; _x < 512; _x += 4)
{
     for (int _z = 0; _z < 512; _z += 4)
     {
         for (int _Tri = 0; _Tri < 6; _Tri++)
         {
         float flX =(float)_x+((_Tri==1||_Tri ==2||_Tri==5)?4.0f:0.0f);
         float flZ = (float)_z +(_Tri==2||_Tri==4||_Tri==5)?4.0f:0.0f);
         _terview._terrain_data[_index].x=flX -(512/2);
         _terview._terrain_data[_index].y=   
         _terview.GetTerrainHeightFromDataFile((int)flX,(int)flZ)*4.0f;
         _terview._terrain_data[_index].z = flZ-(512/2);


         _index++;
         }
     }
}

And it is not working - its displaying some weird stuff, there is no terrain 512/2x512/2 as it should be, but some weird, hard to identify stuff. I think its something wrong with displaying data from buffer but i am not sure of it, because i think i done everything fine with binding buffers and copy data into buffer. If someone ever tried to do such a thing ( i mean VBO's in c#), maybe he can help me with this...

Share this post


Link to post
Share on other sites
At a glance the code looks fine. Have you tried populating the vbo with one predictable triangle and seeing what you get. That way you can at least test the data structure and rendering parts of your code.

Share this post


Link to post
Share on other sites
Found the source of error:
instead of:
gr.glVertexPointer(3, GR.GL_FLOAT, 0, _null);

Should be:
gr.glVertexPointer(3, GR.GL_FLOAT, 0, IntPtr.Zero);

or

gr.glVertexPointer(3, GR.GL_FLOAT, 0, (IntPtr)_null[0]);

F#@$@#$ mistake:D

[Edited by - Kobayashi on May 26, 2007 8:35:09 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this