openglgeometryjogl

Create a right-angled triangular prism in OpenGL


I'm learning OpenGL (specifically, using JOGL) and I'm trying to create a right-angled triangular prism. I learned you can create an equilateral prism with glutWire/SolidCylinder and a a low number of slices (3 for a triangle, obviously).

So I'm wondering if there's some other way to make a prism, or if there's some way to scale an equilateral triangle to turn it into a right triangle?


Solution

  • I additionally found that (at least in JOGL, no idea about elsewhere) glutSolidCylinder doesn't work correctly - the endplates are drawn at a different rotation than the sides, giving you a very strange looking shape.

    So I ended up just making a method that creates a unit-right-triangular prism which you can then rotate and scale as necessary. There's likely much better ways to do this, so comments are welcome:

     private void unitTriangularPrism(GL gl, boolean solid){
        // back endcap
        gl.glBegin(solid ? GL.GL_TRIANGLES : GL.GL_LINES);
        gl.glVertex3f(1f, 0f, 0f);
        gl.glVertex3f(0f, 0f, 0f);
        gl.glVertex3f(0f, 1f, 0f);
        gl.glEnd();
    
        // front endcap
        gl.glBegin(solid ? GL.GL_TRIANGLES : GL.GL_LINES);
        gl.glVertex3f(1f, 0f, 1f);
        gl.glVertex3f(0f, 0f, 1f);
        gl.glVertex3f(0f, 1f, 1f);
        gl.glEnd();
    
        // bottom
        gl.glBegin(solid ? GL.GL_QUADS : GL.GL_LINES);
        gl.glVertex3f(0f, 0f, 0f);
        gl.glVertex3f(1f, 0f, 0f);
        gl.glVertex3f(1f, 0f, 1f);
        gl.glVertex3f(0f, 0f, 1f);
        gl.glEnd();
    
        // back
        gl.glBegin(solid ? GL.GL_QUADS : GL.GL_LINES);
        gl.glVertex3f(0f, 0f, 0f);
        gl.glVertex3f(0f, 1f, 0f);
        gl.glVertex3f(0f, 1f, 1f);
        gl.glVertex3f(0f, 0f, 1f);
        gl.glEnd();
    
        // top
        gl.glBegin(solid ? GL.GL_QUADS : GL.GL_LINES);
        gl.glVertex3f(0f, 1f, 0f);
        gl.glVertex3f(1f, 0f, 0f);
        gl.glVertex3f(1f, 0f, 1f);
        gl.glVertex3f(0f, 1f, 1f);
        gl.glEnd();
    }