Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
1  
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
add comment

1 Answer 1

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:

vertices:

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

triangles:

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:

draw()
{
    glPushMatrix();
        glTranslatef(x, y);
        glScale(scalefactor, scalefactor, scalefactor);
        glSomeMethodToDraw();
    glPopMatrix();
}

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()
    {
        glMatrixMode(GL10.GL_MODELVIEW);
    glLoadIdentity();
    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)
{
    switch(key)
    {
        case DOWNKEY:
            yacceleration = (pressed ? -15 : 0)
        break;
        case UPKEY:
            yacceleration = (pressed ? +15 : 0)
        break;
        case LEFTKEY:
            xacceleration = (pressed ? -15 : 0)
        break;
        case RIGHTKEY:
            xacceleration = (pressed ? +15 : 0)
        break;
        case PAGEUPKEY:
            zacceleration = (pressed ? +15 : 0)
        break;
        case PAGEDOWNKEY:
            zacceleration = (pressed ? -15 : 0)
        break;
    }
}

Tedious and simple camera movement.

for your update method, do this:

update()
{
    camera.x += xacceleration;
    camera.y += yacceleration;
    camera.z += zacceleration;
}

(:

The fun part, the main draw code!

draw()
{
    camera.draw();
    foreach(Tetrahedron t in tetralist)
    {
        t.draw();
    }
}

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
add comment

Your Answer

 
discard

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.