Odub
Odub

Reputation: 33

Drawing tetrahedron in openGL + SDL 2.0

So I'm pretty new to openGL programming and am just going over the basics for now. I know I should be using VBOs and stuff but wanted to get a little foundation first. I wont present you with all the code just the stuff that draws and sets the scene.

Heres a little code for setting up my camera:

glClearColor(0.0f, 0.0f, 0.0f, 0.5f);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, width / height, 1, 1000);
glEnable(GL_DEPTH_TEST);

// Move the camera back to view the scene
glTranslatef(0.0f, 0.0f, -5.0f);

I tried to create it around the origin like so (also I never draw the bottom face) :

void drawtetrahedron(GLfloat angle)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(angle, 0.0f, 1.0f, 0.0f);

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f); //FRONT
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);

glColor3f(0.0f, 1.0f, 0.0f); //RIGHT
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);

glColor3f(0.0f, 0.0f, 1.0f); //LEFT
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(0.0f, -1.0f, -1.0f);

glEnd();

}

When my window first comes up the red triangle looks fine, but as I rotate it the shape looks a little distorted. If I rotate all the way around (where I cant see the red face at all) it looks normal... What am I missing here?

Heres where it starts to look weird

Also any pointers on openGL stuff I'm doing incorrectly (or in general) are greatly appreciated! :D

Upvotes: 0

Views: 3297

Answers (1)

Ferdi265
Ferdi265

Reputation: 2969

I don't know if this is what you consider a wierd looking shape, but your shape doesn't seem to be a regular Tetrahedron: The 3 Corners of the base don't have the same distance to the top corner (the two front corners have a distance of sqrt(6) to the top corner, while the back corner has a distance of sqrt(5)). the distance on the base is off too: the front corners have a distance of sqrt(2) while the distance between any front corner and the back corner is sqrt(3).

An example for a regular tetrahedron would be:

(Please note that these coordinates don't have a base parallel to the xz plane)

(1,1,1)(1,-1,-1)(-1,1,-1)(-1,-1,1)

Your code itself looks to be ok. (Except for the translating the projection matrix) I, myself prefer to create code blocks after push/popmatrix and glbegin/end (these things { ... }), but that's just to keep my code easy to read.

Also, as a general rule of thumb, in opengl you don't move the camera: you move everything else. (That's why translating negative z moves objects away from you, translating positive x makes them move right and so on...)

Upvotes: 1

Related Questions