0
\$\begingroup\$

Is there a simple way to draw a triangular prism in opengl?

I'm using lighting and a non-orthographic camera. I basically need a 3d object, not just vertices.

edit: well I've tried searching, and I tried doing it with two cubes. One that is rotated at an angle from the other, but logically that doesn't work. I have to get an object with angles less than 90 degrees.

enter image description here

I basically need something like this:

enter image description here

\$\endgroup\$
2
\$\begingroup\$

You can just provide the vertices yourself:

// bottom
0, 1, -1/2
√3 / 2, -1/2, -1/2
-√3 / 2, -1/2, -1/2

//Top
0, 1, 1/2
√3 / 2, -1/2, 1/2
-√3 / 2, -1/2, 1/2

Then the indices:

// Bottom
0, 1, 2,

// Top
3, 4, 5,

// Side1
0, 1, 3,
3, 1, 4,

// Side 2
1, 2, 4,
4, 2, 5,

// Side 3
2, 0, 5,
5, 0, 3,
\$\endgroup\$
  • \$\begingroup\$ so I just have to use glVertex3f() for all the vertices to draw it? Is there no other way? Some kind of an object like the cube? \$\endgroup\$ – Mina Michael Nov 13 '17 at 15:59
  • 1
    \$\begingroup\$ @MinaMichael First, you shouldn't use OpenGL 1, it's deprecated and some GPUs don't support it anymore. Anyways, you need to call them based on the indices, how do you draw a cube? \$\endgroup\$ – Bálint Nov 13 '17 at 16:14
0
\$\begingroup\$

So Bálint's advice works. Here's how to do it in code. You can define a function that draws a triangular prism at the centre using glVertex3f() functions

void triangularPrism()
{
    glBegin(GL_QUADS);
        glVertex3f(0.5, 0, 0.5);
        glVertex3f(0.5, 0, -0.5);
        glVertex3f(-0.5, 0, -0.5);
        glVertex3f(-0.5, 0, 0.5);

        glVertex3f(0.5,0,-0.5);
        glVertex3f(0.5,1,-0.5);
        glVertex3f(-0.5,1,-0.5);
        glVertex3f(-0.5,0,-0.5);

        glVertex3f(0.5,1,-0.5);
        glVertex3f(-0.5,1,-0.5);
        glVertex3f(-0.5,0,0.5);
        glVertex3f(0.5,0,0.5);
    glEnd();
    glBegin(GL_TRIANGLES);
        glVertex3f(0.5,0,0.5);
        glVertex3f(0.5,1,-0.5);
        glVertex3f(0.5,0,-0.5);

        glVertex3f(-0.5,0,0.5);
        glVertex3f(-0.5,1,-0.5);
        glVertex3f(-0.5,0,-0.5);
    glEnd();
}

Then this function can be used in the glutDisplayFunc() just like the other primitives. You can translate it, scale it, rotate it and so forth. Example:

glPushMatrix();
    glTranslated(1.5,1.1,2.75);
    glRotated(180,0,1,0);
    glRotated(flapsDegree, -1,0,0);
    glScaled(0.5,0.8,0.1);
    triangularPrism();
glPopMatrix();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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