VBO generation at runtime

Started by
5 comments, last by jaronimo 13 years, 5 months ago
Hi everyone!

I'm currently trying to implement a random terrain generator using OpenGL.. and since I wanna go with the times I'm planning to do the whole thing in OpenGL 3.0 and therefore use VBOs.
since the user will be able to adjust the terrain size at runtime, the point grid VBO will not have a fixed size.. so I'm trying to come up with an algorithm that calculates a vertex grid of resolution n*n (n is user specified) and also calculates all the indices which I guess is the tricky part..

what I wanted to ask: is there an established way in which this sort of thing should/could be done? I'm currently trying to figure something out.. but I don't want to reinvent the wheel, so if anyone has any suggestions, please let me know!

thanks,
jaronimo
Advertisement
You could've raycast the terrain in screen space with just a shader and a heightmap.
You can modify the texture fast, and it can give a real neat-looking result while circumventing the need for vertices almost completely.
sounds intriguing! but after googling around for quite a while now, I think I'll try the VBO approach since I'm not really familiar with raycasting.. or do you know a good tutorial?

for example, with raycasting, would it be possible to rotate the heightmap? or could I just look at it from my camera position?
Quote:Original post by jaronimo
sounds intriguing! but after googling around for quite a while now, I think I'll try the VBO approach since I'm not really familiar with raycasting.. or do you know a good tutorial?

for example, with raycasting, would it be possible to rotate the heightmap? or could I just look at it from my camera position?


Yes it will be possible to rotate as much as you like. This old technique should work for 4DOF at first, as it uses a simple column approach.
That's the advantage with heightmaps here, when each column of pixels cast
we can guarantee that (if we go from the bottom to the top of screen) the point where we hit the landscape is further away every time, and we can approximate the hit spot every time after the first ray.

It's really simple, and this tutorial, though a little old, explains the technique just fine. You can do what it does in a fragment shader;

Voxel terrain on flipcode

This is also good:

Voxel terrain on codermind

But more modern techniques are ready for 6DOF translations, but I can't find any tutorials on that for you. You should give the above articles a go.
so, I continued with the VBO approach anyway, since I can use parts of my code for another course this way.

...and I hit a roadblock that I can't seem to get past.. I'm quite sure it's a noob problem too, but I really can't figure it out...

my program has no problem rendering this points array for my VBO:

GLfloat points[] = { 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
2.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
2.0f, 1.0f, 0.0f, 1.0f,
0.0f, 2.0f, 0.0f, 1.0f,
1.0f, 2.0f, 0.0f, 1.0f,
2.0f, 2.0f, 0.0f, 1.0f};

but when I try this instead, nothing is rendered:

GLfloat* points = generateVBO(dimension);

generateVBO() looks like this:

GLfloat* generateVBO(int dim){

GLfloat genPoints[] = { 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
2.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
2.0f, 1.0f, 0.0f, 1.0f,
0.0f, 2.0f, 0.0f, 1.0f,
1.0f, 2.0f, 0.0f, 1.0f,
2.0f, 2.0f, 0.0f, 1.0f};

return genPoints;
}

of course this is not the original code but I dumbed it down to this to find out why it is not rendering.. I thought, this HAS to work since the array is not generated dynamically. I just return the same array as a pointer.. but this shouldn't be a problem, should it?

i mean glBufferData() even needs a pointer to the data, so why is nothing rendering when I give it a pointer instead of an actual array?
glBufferData(GL_ARRAY_BUFFER, 24*4*sizeof(GLfloat), (GLfloat*)points, GL_STATIC_DRAW);
You need to allocate genPoints on the heap (hint, use 'new'), not on the stack. When generateVBO exits, the array genPoints is destroyed, leaving your GLfloat* pointer pointing to destroyed data.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
thanks for the tip.. I now found out what the real problem was ;(
quite embarrassing really.. it did not work because the real code had an infinite loop because I wrote i=+4 in the for loop... yikes

This topic is closed to new replies.

Advertisement