Link to home
Create AccountLog in
Avatar of Terminator4
Terminator4

asked on

Fast Opengl wireFrame grid

I guys heres a pseudo code of a OpenGL 4 section by 4 section by 4 section wireframe grid:
Can you guys help me to complete it? Ill give points

I need this code to run very fast.
Optimizations...

renderWireFrameCubeGrid(){
//render cube shaped wireframe grid of 4*4*4
}

Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

The most optimized way would be:

 - To use GL_LINE_STRIP and/or GL_LINE_LOOP where possible;
 - Hardcode the vertices (rather than use iteration) - [then just use glScale*() to change the size];

and:

 - Use display lists for further program optimization.
Avatar of Terminator4
Terminator4

ASKER

ok can you code that please
ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
cool. But can you put comment on each line loop so i can understand what you are actually drawing? I'm confused.

I understand here.. ok well can the code be adjusted so I can choose the number of division on each side?
> can the code be adjusted so I can choose the number of division on each side?

Certainly.

The reason I had not made room for the implementation of multiple divisions, is because it would require iterations - which will add slight overhead to your program (the opposite of optimization ;)).

However, this should perform reasonably well:


void renderWireFrameCubeGrid(int k)
{
            float o=k/2.0f;
            int i;
           
            glTranslatef(-o,-o,o);
           
           
            for(i=0; i<k; i++)
            {
                glBegin(GL_LINE_LOOP);
                glVertex3f(0,i,0);
                glVertex3f(k,i,0);
                glVertex3f(k,i,-k);
                glVertex3f(0,i,-k);
                glEnd();
            }
            for(i=0; i<=k; i++)
            {
                glBegin(GL_LINE_LOOP);
                glVertex3f(0,0,-i);
                glVertex3f(0,k,-i);
                glVertex3f(k,k,-i);
                glVertex3f(k,0,-i);
                glEnd();
            }
            for(i=0; i<=k; i++)
            {
                glBegin(GL_LINE_LOOP);
                glVertex3f(i,0,0);
                glVertex3f(i,k,0);
                glVertex3f(i,k,-k);
                glVertex3f(i,0,-k);
                glEnd();
            }
           
            glTranslatef(o,o,-o);
}


As an example: for a 6x6x6 cube, call:

renderWireFrameCubeGrid(6);
You could possibly improve performance by using display lists;

Firstly you define the display list -- this should be done outside of the Display() function - generally in the Init() function:

// create the display list:
listN6 = glGenLists(1);
glNewList(listN, GL_COMPILE);
renderWireFrameCubeGrid(6);
glEndList();


Where listN is defined globally (outside of any functions), like so:

GLuint listN;


Then to display this item in your Display() function:

// [translate, rotate, scale, .., here if necessary], then load the list (draw the cube):
glCallList(listN6);


This should eliminate a fair bit of math that the computer would otherwise have to keep doing on each graphic-loop.
Sorry, I messed up the variable name a bit;
all occurances of "listN" should be "listN6" [in this example].