001 package com.croftsoft.core.media.jogl;
002
003 import java.nio.*;
004
005 import javax.media.opengl.GL;
006 import javax.media.opengl.glu.GLU;
007
008 /***********************************************************************
009 * Library of static methods to manipulate JOGL objects.
010 *
011 * @version
012 * $Id: JoglLib.java,v 1.5 2008/09/26 23:28:14 croft Exp $
013 * @since
014 * 2008-01-27
015 * @author
016 * <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
017 ***********************************************************************/
018
019 public final class JoglLib
020 ////////////////////////////////////////////////////////////////////////
021 ////////////////////////////////////////////////////////////////////////
022 {
023
024 public static void checkError ( final GL gl )
025 ////////////////////////////////////////////////////////////////////////
026 {
027 final int errorCode = gl.glGetError ( );
028
029 if ( errorCode != GL.GL_NO_ERROR )
030 {
031 // TODO: There could be more than one error. Include multiple in
032 // the exception message. See p67, "OpenGL SuperBible", 4th Ed.
033
034 throw new RuntimeException (
035 new GLU ( ).gluErrorString ( errorCode ) );
036 }
037 }
038
039 /***********************************************************************
040 * Creates a display list of a colored cube.
041 *
042 * Destroy the display list by calling glDeleteLists() when done.
043 *
044 * @return
045 *
046 * Returns the created display list name.
047 ***********************************************************************/
048 public static int createCubeDisplayList ( final GL gl )
049 ////////////////////////////////////////////////////////////////////////
050 {
051 final int cubeDisplayList = gl.glGenLists ( 1 );
052
053 gl.glNewList ( cubeDisplayList, GL.GL_COMPILE );
054
055 final float [ ] [ ] vertices = {
056 { -1, -1, 1 }, // 0
057 { -1, 1, 1 }, // 1
058 { 1, 1, 1 }, // 2
059 { 1, -1, 1 }, // 3
060 { -1, -1, -1 }, // 4
061 { -1, 1, -1 }, // 5
062 { 1, 1, -1 }, // 6
063 { 1, -1, -1 } }; // 7
064
065 gl.glColor3f ( 1, 0, 0 );
066
067 drawPolygon ( gl, vertices, 0, 3, 2, 1 );
068
069 gl.glColor3f ( 0, 1, 0 );
070
071 drawPolygon ( gl, vertices, 2, 3, 7, 6 );
072
073 gl.glColor3f ( 0, 0, 1 );
074
075 drawPolygon ( gl, vertices, 3, 0, 4, 7 );
076
077 gl.glColor3f ( 1, 1, 0 );
078
079 drawPolygon ( gl, vertices, 1, 2, 6, 5 );
080
081 gl.glColor3f ( 0, 1, 1 );
082
083 drawPolygon ( gl, vertices, 4, 5, 6, 7 );
084
085 gl.glColor3f ( 1, 0, 1 );
086
087 drawPolygon ( gl, vertices, 5, 4, 0, 1 );
088
089 gl.glEndList ( );
090
091 return cubeDisplayList;
092 }
093
094 public static void drawPolygon (
095 final GL gl,
096 final float [ ] vertex3f0,
097 final float [ ] vertex3f1,
098 final float [ ] vertex3f2,
099 final float [ ] vertex3f3 )
100 ////////////////////////////////////////////////////////////////////////
101 {
102 gl.glBegin ( GL.GL_POLYGON );
103
104 gl.glVertex3f (
105 vertex3f0 [ 0 ],
106 vertex3f0 [ 1 ],
107 vertex3f0 [ 2 ] );
108
109 gl.glVertex3f (
110 vertex3f1 [ 0 ],
111 vertex3f1 [ 1 ],
112 vertex3f1 [ 2 ] );
113
114 gl.glVertex3f (
115 vertex3f2 [ 0 ],
116 vertex3f2 [ 1 ],
117 vertex3f2 [ 2 ] );
118
119 gl.glVertex3f (
120 vertex3f3 [ 0 ],
121 vertex3f3 [ 1 ],
122 vertex3f3 [ 2 ] );
123
124 gl.glEnd ( );
125 }
126
127 public static void drawPolygon (
128 final GL gl,
129 final float [ ] [ ] vertices3f,
130 final int vertexIndex0,
131 final int vertexIndex1,
132 final int vertexIndex2,
133 final int vertexIndex3 )
134 ////////////////////////////////////////////////////////////////////////
135 {
136 drawPolygon (
137 gl,
138 vertices3f [ vertexIndex0 ],
139 vertices3f [ vertexIndex1 ],
140 vertices3f [ vertexIndex2 ],
141 vertices3f [ vertexIndex3 ] );
142 }
143
144 public static void printInfo ( final GL gl )
145 ////////////////////////////////////////////////////////////////////////
146 {
147 // TODO: Asking for GL_SHADING_LANGUAGE_VERSION causes an error on
148 // early versions of OpenGL. Need to check OpenGL version first.
149
150 final String [ ] stringArray = new String [ ] {
151 "GL_VERSION",
152 "GL_VENDOR",
153 "GL_RENDERER",
154 // "GL_SHADING_LANGUAGE_VERSION",
155 "GL_EXTENSIONS" };
156
157 final int [ ] enumArray = new int [ ] {
158 GL.GL_VERSION,
159 GL.GL_VENDOR,
160 GL.GL_RENDERER,
161 // GL.GL_SHADING_LANGUAGE_VERSION,
162 GL.GL_EXTENSIONS };
163
164 for ( int i = 0; i < stringArray.length; i++ )
165 {
166 System.out.println ( "OpenGL: " + stringArray [ i ] + " = "
167 + gl.glGetString ( enumArray [ i ] ) );
168 }
169
170 final DoubleBuffer doubleBuffer = DoubleBuffer.allocate ( 4 );
171
172 gl.glGetDoublev ( GL.GL_CURRENT_COLOR, doubleBuffer );
173
174 System.out.println ( "OpenGL: GL_CURRENT_COLOR = ["
175 + doubleBuffer.get ( 0 ) + ", "
176 + doubleBuffer.get ( 1 ) + ", "
177 + doubleBuffer.get ( 2 ) + ", "
178 + doubleBuffer.get ( 3 ) + "]" );
179
180 final int [ ] intArray = new int [ 1 ];
181
182 gl.glGetIntegerv ( GL.GL_MAX_LIGHTS, intArray, 0 );
183
184 System.out.println ( "OpenGL: GL_MAX_LIGHTS = " + intArray [ 0 ] );
185
186 checkError ( gl );
187 }
188
189 ////////////////////////////////////////////////////////////////////////
190 ////////////////////////////////////////////////////////////////////////
191
192 private JoglLib ( )
193 ////////////////////////////////////////////////////////////////////////
194 {
195 // empty
196 }
197
198 ////////////////////////////////////////////////////////////////////////
199 ////////////////////////////////////////////////////////////////////////
200 }