Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a simulation program and now I'd like to visualize the results in 3D.

All I need is N tetrahedrons moving inside rectangular cuboid. I have already calculated coordinates of tetrahedrons at each step. Possibility to move camera would be nice :)

So, I'm searching for a code sample I can adopt or docs.

share|improve this question
This question is exceedingly broad and under-specified. – Josh Petrie Jan 16 '12 at 18:05
Josh, this is because I never used OpenGL before. I described all I need. Language and platform doesn't matter for me, so I didn't specify them. And Gtoknu's answer is exactly what I wanted. – OCyril Jan 16 '12 at 18:28
That's fine; it is, however, a poor fit for this site (see the FAQ) because it's not a specific or directed enough problem. I voted to close the question, I was just letting you know why. – Josh Petrie Jan 16 '12 at 18:31
up vote 3 down vote accepted

Assuming you know how to draw to the OpenGL, I'll just give you the relevant parts:

Make a tetrahedron class, to build up the vertices, use this info to construct the class:


v0 = 0, 1, 0  
v1 = 0, 0, 1  
v2 = 1, 0, 0  
v3 = -1, 0, 0  


t0 = v1, v2, v3  
t1 = v1, v2, v0  
t2 = v1, v3, v0  
t3 = v2, v3, v0

and in the draw method does something like this pseudo-code:

        glTranslatef(x, y);
        glScale(scalefactor, scalefactor, scalefactor);

Well, you can actually draw with a lot of methods, there are glBegin~glEnd, glDrawArrays, glDrawElements, choose yours.

And i don't mentioned rotation because it can be done in a lot of ways too, you haven't cited it in your question, so i let it out.

For your camera, take this simple as example:

class Camera
    float x, y, z;
    void draw()
    glTranslatef(-x, -y, -z);

Simple huh? If you dont know yet, the basis of a camera in OpenGL(and maybe in every lowlevel GPU API) is just move your world in the opposite direction. May look strange, but if you think hard on it, it will look fine!

Outside, you can make a linked list of tetrahedrons to initialize your code.

I dont know how you'll get your input(as i dont know your platform, language, you should cite those to help on answers), but here's a simple pseudo code to move the camera:

void keyPress(keyCode key, boolean pressed)
        case DOWNKEY:
            yacceleration = (pressed ? -15 : 0)
        case UPKEY:
            yacceleration = (pressed ? +15 : 0)
        case LEFTKEY:
            xacceleration = (pressed ? -15 : 0)
        case RIGHTKEY:
            xacceleration = (pressed ? +15 : 0)
        case PAGEUPKEY:
            zacceleration = (pressed ? +15 : 0)
        case PAGEDOWNKEY:
            zacceleration = (pressed ? -15 : 0)

Tedious and simple camera movement.

for your update method, do this:

    camera.x += xacceleration;
    camera.y += yacceleration;
    camera.z += zacceleration;


The fun part, the main draw code!

    foreach(Tetrahedron t in tetralist)

Very simple, but functional.

Just to mention, i haven't tested any of this, just theory to make you find your way.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.