Sign in to follow this  
SFCBias

Data transfer into VBO

Recommended Posts

I've had working VBOs for a while now. And Now im trying to load a terrain into and array of heights and then load them into my vertex structure to be rendered from the VBO, however when i run the code in immediate mode, it looks good and when its through the VBO. All the points don't connect.

In immediate mode this is the code

[code] for (int i = 0; i < terrainGridLength - 1; i++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int j = 0; j < terrainGridWidth; j++)
{
glVertex3f(startW + j + xOffset, mHeights[(i + 1)
* terrainGridWidth + (j)] + yOffset, startL - (i + 1)
+ zOffset);

glVertex3f(startW + j + xOffset, mHeights[(i) * terrainGridWidth
+ j] + yOffset, startL - i + zOffset);
}
glEnd();
}[/code]


But if I transfer the data with this

[code] //Now lets copy the terrain data into our vertex structure so we can render;
for (int i = 0, marker = 0; i < mWidth; i++)
{
for (int j = 0; j < mHeight; j++, marker++)
{

mVertexData[marker].positions[0] = startW + j + xOffset;
mVertexData[marker].positions[1] = mHeights[(i + 1) * terrainGridWidth
+ (j)] + yOffset;
mVertexData[marker].positions[2] = startL - (i + 1) + zOffset;

mVertexData[marker + 1].positions[0] = startW + j + xOffset;

mVertexData[marker + 1].positions[1] = mHeights[(i) * terrainGridWidth
+ j] + yOffset;
mVertexData[marker + 1].positions[2] = startL - i + zOffset;
}
}
[/code]

I fail to see the difference rendering with:

[code]mVBO->Bind();
// Set the state of what we are drawing (I don't think order matters here, but I like to do it in the same
// order I set the pointers
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

// Resetup our pointers. This doesn't reinitialise any data, only how we walk through it
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(20));
glColorPointer(4, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(32));
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));

glDrawArrays(GL_TRIANGLE_STRIP, 0, mTerrainSize);

// Disable our client state back to normal drawing
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

mVBO->disable();[/code]


Any thoughts ?

Share this post


Link to post
Share on other sites
The problem is:
[quote]
//original
for (int i = 0; i < terrainGridLength - 1; i++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int j = 0; j < terrainGridWidth; j++)
{
[...]
[/quote]
The vbo isnt doing that. Notice that you do a strip per part of the terrain. the vbo does this as if you had this with immediate mode:
[quote]
//this is what's happening when you draw using the vbo (in terms of immediate)
glBegin(GL_TRIANGLE_STRIP);
for (int i = 0; i < terrainGridLength - 1; i++)
{
for (int j = 0; j < terrainGridWidth; j++)
{
[...]
}
}
[/quote]

which will attempt to connect all the points regardless of vertex position.

I know nvidia has an extension that has a way to address this but I'm not sure about amd. Other than that you can switch to using GL_TRIANGLES instead or use glDrawArraysRange.

Share this post


Link to post
Share on other sites
No that's not the problem as I just tried compiling that. It give the same results. I've determined that it has to do with how im putting the vertices into my structure. Im missing something muy importante that is causing it to be choppy.

Share this post


Link to post
Share on other sites
What I posted wasn't going to solve the problem. It was just showing what your vbo is doing in terms of immediate mode. I can only think to use primitive restart (which I think is ext only from nvidia I haven't used it) or use triangles instead. I personally use triangles with an element array list for my terrain.

Share this post


Link to post
Share on other sites
[quote name='Asem' timestamp='1298381042' post='4777509']
What I posted wasn't going to solve the problem. It was just showing what your vbo is doing in terms of immediate mode.
[/quote]


Well even if that was what it was doing, It wouldn't make a difference. I tried rendering what you said in immediate mode (what you said my VBO was doing) and I still got the same working results. Thats what pointed me to my vertices being wrong somehow.


And I looked into the indices but was unsure how to generate them only from a heightmap

Share this post


Link to post
Share on other sites
when iterating the marker it should be marker+=2.
You write over previous vertices positions with marker++.
at the very least it should show better.

still this:
[quote]
glBegin(GL_TRIANGLE_STRIP);
for (int i = 0; i < terrainGridLength - 1; i++)
{
for (int j = 0; j < terrainGridWidth; j++)
{
[...]
}
}
glEnd();
[/quote]


which looks correct at first (from you testing it) but really looks like this: (you can check it with glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); )
(if the terrain was flat)

[img]http://img692.imageshack.us/img692/6062/terrain2e.jpg[/img]

try this out:
[quote]

terrainGridLength = 10
terrainGridWidth = 20

for (int i = 0, marker = 0; i < terrainGridLength; i++)
{
for (int j = 0; j < terrainGridWidth/2; j++, marker+=2)
{
mVertexData[marker].positions[0] = startW + j + xOffset;
mVertexData[marker].positions[1] = startL - (i + 1 + k) + zOffset;
mVertexData[marker].positions[2] = 0.0f;

mVertexData[marker + 1].positions[0] = startW + j + xOffset;
mVertexData[marker + 1].positions[1] = startL - i - k + zOffset;
mVertexData[marker + 1].positions[2] = 0.0f;
}
}
[/quote]
I get the same thing above

Share this post


Link to post
Share on other sites
Yes i thought that about the marker++ but when I tried marker += 2 it crashed and i attributed that to the fact the i would be writing outside of the allocated bounds of memory in the vertex data.

SO should I allocate more space in the VertexData?

Share this post


Link to post
Share on other sites
[quote name='SFCBias' timestamp='1298387748' post='4777539']
Yes i thought that about the marker++ but when I tried marker += 2 it crashed and i attributed that to the fact the i would be writing outside of the allocated bounds of memory in the vertex data.

SO should I allocate more space in the VertexData?
[/quote]

did you terrainGridWidth/2. it would go out of range without this.

[quote]
for (int i = 0, marker = 0; i < terrainGridLength; i++)
{
for (int j = 0; j < [b]terrainGridWidth/2[/b]; j++, marker+=2)
{
[...][/quote]

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