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
}

Game Programming

Avatar of undefined
Last Comment
InteractiveMind
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Terminator4
Terminator4

ASKER

cool. But can you put comment on each line loop so i can understand what you are actually drawing? I'm confused.

Avatar of Terminator4
Terminator4

ASKER

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].
Game Programming
Game Programming

Game programming is the software development of video games. Game programming requires substantial skill in software engineering as well as specialization in simulation, computer graphics, artificial intelligence, physics, audio programming, and input. Like other software, game development programs are generated from source code to the actual program by a compiler. Source code can be developed with almost any text editor, but most professional game programmers use a full integrated development environment (IDE).

6K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo