开发者

Create a right-angled triangular prism in OpenGL

开发者 https://www.devze.com 2023-01-19 14:25 出处:网络
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 o

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开发者_JS百科 prism, or if there's some way to scale an equilateral triangle to turn it into a right triangle?


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消