# JOGL 3D Triangle

In this section, we will learn how to create a three-dimensional triangle with the help of simple example. We will also rotate this triangle to see all its sides.

J3DTriangle.java

1. package com.tpoint.jogl;
2.
3. import javax.media.opengl.*;
4. import javax.media.opengl.awt.GLCanvas;
5. import javax.media.opengl.glu.GLU;
6.
7. import javax.swing.JFrame;
8.
9. import com.jogamp.opengl.util.FPSAnimator;
10.
11. public class J3DTriangle implements GLEventListener {
12.
13. private GLU glu = new GLU();
14. private float rotation = 0.0f;
15.
16.    @Override
17. public void display(GLAutoDrawable drawable) {
18. final GL2 gl = drawable.getGL().getGL2();
19.
20.
21. gl.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
23.       // Move triangle
24. gl.glTranslatef( -0.5f, 0.0f, -6.0f );
25. gl.glRotatef( rotation, 0.0f, 1.0f, 0.0f );
26. gl.glBegin( GL2.GL_TRIANGLES );
27.
28.
29.       // Front
30. gl.glColor3f( 1.0f, 1.0f, 0.0f ); // Yellow
31. gl.glVertex3f( 1.5f, 2f, 0.0f ); // Top Of Triangle
32.
33. gl.glColor3f( 0.0f, 1.5f, 0.0f ); // Green
34. gl.glVertex3f( -1.5f, -1.5f, 1.5f ); // Left Of Triangle
35.
36. gl.glColor3f( 1.0f, 0.0f, 1.0f ); // Purple
37. gl.glVertex3f( 1.5f, -1.5f, 1.5f ); // Right Of Triangle
38.
39.       // Right
40. gl.glColor3f( 1.0f, 1.0f, 0.0f ); // Yellow
41. gl.glVertex3f( 1.5f, 2.0f, 0.0f ); // Top Of Triangle
42.
43. gl.glColor3f( 1.0f, 0.0f, 1.0f ); // Purple
44. gl.glVertex3f( 1.5f, -1.5f, 1.5f ); // Left Of Triangle
45.
46. gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Green
47. gl.glVertex3f( 1.5f, -1.5f, -1.5f ); // Right Of Triangle
48.
49.       // Back
50. gl.glColor3f( 1.0f, 1.0f, 0.0f ); // Yellow
51. gl.glVertex3f( 1.5f, 2.0f, 0.0f ); // Top Of Triangle
52.
53. gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Green
54. gl.glVertex3f( 1.5f, -1.5f, -1.5f ); // Left Of Triangle
55.
56. gl.glColor3f( 1.0f, 0.0f, 1.0f ); // Purple
57. gl.glVertex3f( -1.5f, -1.5f, -1.5f ); // Right Of Triangle
58.
59.       //left
60. gl.glColor3f( 1.0f, 1.0f, 0.0f ); // Yellow
61. gl.glVertex3f( 1.5f, 2.0f, 0.0f ); // Top Of Triangle
62.
63. gl.glColor3f( 1.0f, 0.0f, 1.0f ); // Purple
64. gl.glVertex3f( -1.5f, -1.5f, -1.5f ); // Left Of Triangle
65.
66. gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Green
67. gl.glVertex3f( -1.5f, -1.5f, 1.5f ); // Right Of Triangle
68.
69. gl.glEnd();
70. gl.glFlush();
71. rotation += 0.6f;
72.    }
73.
74.    @Override
75. public void dispose( GLAutoDrawable drawable ) {
76.       //method body
77.    }
78.
79.    @Override
80. public void init( GLAutoDrawable drawable ) {
81.       //method body
82.    }
83.
84.    @Override
85. public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) {
86.
87. final GL2 gl = drawable.getGL().getGL2();
88. if(height<=0)
89. height = 1;
90.
91. final float h = ( float ) width / ( float ) height;
92. gl.glViewport( 00, width, height );
93. gl.glMatrixMode( GL2.GL_PROJECTION );
95.
96. glu.gluPerspective( 45.0f, h, 1.020.0 );
97. gl.glMatrixMode( GL2.GL_MODELVIEW );
99.    }
100.
101. public static void main( String[] args ) {
102.
103. final GLProfile gp = GLProfile.get( GLProfile.GL2 );
104. GLCapabilities cap = new GLCapabilities(gp );
105.
106. final GLCanvas gc = new GLCanvas( cap );
107.       J3DTriangle triangle = new J3DTriangle();
108.
110. gc.setSize( 400400 );
111.
112. final JFrame frame = new JFrame( "3D Triangle" );
113.