Sign in to follow this  
mark-w

general performace question

Recommended Posts

Hi, I'm trying to create several thousand solid cubes in my opengl scene. The cubes will all be sitting at the same base level. As the user is interacting with my application, I'd like to dynamically change the height of the cubes. I haven't programmed with opengl for a few years, but I remember we could specify some vertices to form triangles to eventually make a cube. If I want to be able to dynamically change the height of a cube geometry, I'd basically have to trash all the existing cube geometry and recreate every cube with the new heights right? There's no way to tell an existing 'cube' to change its height? If that's so, would it be possible to keep recreating these cubes twice a second let's say for 65536, 262144, or 1048576 cubes? I'm basically trying to make a height map that can be interactively changed by the user, but the height map may have dimensions of up to 1024 x 1024 cubes. Thanks

Share this post


Link to post
Share on other sites
You can just change the geometry but I think that would slow down the program... use the glTranslate3f(float x, float y, float z) command before the rendering of the cube you want to move and use the amount of movement you want in each direction and voila you moved a cube

Share this post


Link to post
Share on other sites
Hi win_crook,

So I was following the nehe example framwork for drawing a simple polygon. There is a function where we do the actual drawing, here I'm just drawing a plane:

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);

glColor3f(0.5f,0.5f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);

glEnd();
return TRUE;
}

Now I am getting worried as I may have to draw 1024 x 1024 cubes inside this function - how horribly slow would that be to have to generate all the polygons everytime the draw() function is called? Is there a better way to do it? Again, I'm just starting to look at opengl again and am trying to find the best way to do this.

int DrawGLScene(GLvoid)
{
// ...

for (int y = 0; y < 1024; y++)
for (int x = 0; x < 1024; x++) {
CreateCube();
}

// ...
}

Thanks

Share this post


Link to post
Share on other sites
or better yet just model one cube, store it in a binary file, read it in at runtime and instance it for each of your objects. You should have some runtime representation of geomety in your engine, likely it's own class. Then you can just modify the geometry of each object as need demands. But have each object's vertices in relative positions and then just store a transform matrix for each object with which you render it. no need to actually modify the vertices if all your doing is translating/rotating the cube.

-me

Share this post


Link to post
Share on other sites
Hi guys,

Sorry I am confused on this - how can I draw them only once, then somehow save that and use that storage to draw them when each cube's height changes?

I thought for everytime the frame is drawn, I would have to manually change the vertex points of the geometry that makes up each cube? (modifications done inside the draw() function).

Can you explain how it would be possible to call that draw function only once, then somehow use that geometry later on when the heights of individual cubes are changing?

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by mark-w
Hi guys,

Sorry I am confused on this - how can I draw them only once, then somehow save that and use that storage to draw them when each cube's height changes?

I thought for everytime the frame is drawn, I would have to manually change the vertex points of the geometry that makes up each cube? (modifications done inside the draw() function).

Can you explain how it would be possible to call that draw function only once, then somehow use that geometry later on when the heights of individual cubes are changing?

Thanks


A "geometry" should only hold information about the shape (and possibly size) of a renderable thing, not an absolute position in world space. What you do is give the cube's draw() method some parameters to say where to draw the cube.

That said, drawing all the cubes for a *height map* is excessive. In any given column of cubes, you won't have any "gaps" below the bottom cube, so you could instead draw a rectangular prism from the top "cube" down to the bottom of the stack. But even then - why not just use a mesh across the top of the heightmap?

Share this post


Link to post
Share on other sites
in kinda-pseudo code. I forget the actual openGL function names for doing a matrix transform, setting the vertex pointer, and setting the index pointer.


class Model
{
private:
//this is the list of vertices as if the model were at (0,0,0)
Vertex *mVertices;

//this is the vertex draw order
unsigned int *mIndices;

//this holds the world position and orientation of the model
Matrix mTransform;
};


std::vector<Model*> gameModels;
std::vector<Model*>::iterator ite = gameModels.end();
for ( std::vector<Model*>::iterator it = gameModels.begin; it != ite; ++it )
{
<appropriateOpenGLMatrixFunction>(mTransform);
<appropriateOpenGLVertexListFunction>(mVertices);
<appropriateOpenGLIndexListFunction>(mIndices);
}


Share this post


Link to post
Share on other sites
Are you asking us how slow it would be to rerender a scene filled with the squares? You should be rerendering the scene every frame, if you want it to be interactive at all...

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