1 /*
2  * Copyright (c) 2002-2008 LWJGL Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'LWJGL' nor the names of
17  *   its contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.lwjgl.util.glu;
33 
34 import java.nio.ByteBuffer;
35 import java.nio.FloatBuffer;
36 import java.nio.IntBuffer;
37 
38 import org.lwjgl.opengl.Util;
39 import org.lwjgl.util.glu.tessellation.GLUtessellatorImpl;
40 
41 import static org.lwjgl.opengl.GL11.*;
42 
43 /**
44  * GLU.java
45  *
46  *
47  * Created 23-dec-2003
48  * @author Erik Duijs
49  */
50 public class GLU {
51 	static final float PI = (float)Math.PI;
52 
53 	/* Errors: (return value 0 = no error) */
54 	public static final int GLU_INVALID_ENUM        = 100900;
55 	public static final int GLU_INVALID_VALUE       = 100901;
56 	public static final int GLU_OUT_OF_MEMORY       = 100902;
57 	public static final int GLU_INCOMPATIBLE_GL_VERSION     = 100903;
58 
59 	/* StringName */
60 	public static final int GLU_VERSION             = 100800;
61 	public static final int GLU_EXTENSIONS          = 100801;
62 
63 	/* Boolean */
64 	public static final boolean GLU_TRUE                = true;
65 	public static final boolean GLU_FALSE               = false;
66 
67 
68 	/****           Quadric constants               ****/
69 
70 	/* QuadricNormal */
71 	public static final int GLU_SMOOTH              = 100000;
72 	public static final int GLU_FLAT                = 100001;
73 	public static final int GLU_NONE                = 100002;
74 
75 	/* QuadricDrawStyle */
76 	public static final int GLU_POINT               = 100010;
77 	public static final int GLU_LINE                = 100011;
78 	public static final int GLU_FILL                = 100012;
79 	public static final int GLU_SILHOUETTE          = 100013;
80 
81 	/* QuadricOrientation */
82 	public static final int GLU_OUTSIDE             = 100020;
83 	public static final int GLU_INSIDE              = 100021;
84 
85 	/* Callback types: */
86 	/*      ERROR               = 100103 */
87 
88 
89 	/****           Tesselation constants           ****/
90 
91 	public static final double GLU_TESS_MAX_COORD              = 1.0e150;
92 	public static final double TESS_MAX_COORD              = 1.0e150;
93 
94 	/* TessProperty */
95 	public static final int GLU_TESS_WINDING_RULE           = 100140;
96 	public static final int GLU_TESS_BOUNDARY_ONLY          = 100141;
97 	public static final int GLU_TESS_TOLERANCE              = 100142;
98 
99 	/* TessWinding */
100 	public static final int GLU_TESS_WINDING_ODD            = 100130;
101 	public static final int GLU_TESS_WINDING_NONZERO        = 100131;
102 	public static final int GLU_TESS_WINDING_POSITIVE       = 100132;
103 	public static final int GLU_TESS_WINDING_NEGATIVE       = 100133;
104 	public static final int GLU_TESS_WINDING_ABS_GEQ_TWO    = 100134;
105 
106 	/* TessCallback */
107 	public static final int GLU_TESS_BEGIN          = 100100;  /* void (CALLBACK*)(GLenum    type)  */
108 	public static final int GLU_TESS_VERTEX         = 100101;  /* void (CALLBACK*)(void      *data) */
109 	public static final int GLU_TESS_END            = 100102;  /* void (CALLBACK*)(void)            */
110 	public static final int GLU_TESS_ERROR          = 100103;  /* void (CALLBACK*)(GLenum    errno) */
111 	public static final int GLU_TESS_EDGE_FLAG      = 100104;  /* void (CALLBACK*)(GLboolean boundaryEdge)  */
112 	public static final int GLU_TESS_COMBINE        = 100105;  /* void (CALLBACK*)(GLdouble  coords[3],
113 	                                                            void      *data[4],
114 	                                                            GLfloat   weight[4],
115 	                                                            void      **dataOut)     */
116 	public static final int GLU_TESS_BEGIN_DATA     = 100106;  /* void (CALLBACK*)(GLenum    type,
117 	                                                            void      *polygon_data) */
118 	public static final int GLU_TESS_VERTEX_DATA    = 100107;  /* void (CALLBACK*)(void      *data,
119 	                                                            void      *polygon_data) */
120 	public static final int GLU_TESS_END_DATA       = 100108;  /* void (CALLBACK*)(void      *polygon_data) */
121 	public static final int GLU_TESS_ERROR_DATA     = 100109;  /* void (CALLBACK*)(GLenum    errno,
122 	                                                            void      *polygon_data) */
123 	public static final int GLU_TESS_EDGE_FLAG_DATA = 100110;  /* void (CALLBACK*)(GLboolean boundaryEdge,
124 	                                                            void      *polygon_data) */
125 	public static final int GLU_TESS_COMBINE_DATA   = 100111;  /* void (CALLBACK*)(GLdouble  coords[3],
126 	                                                            void      *data[4],
127 	                                                            GLfloat   weight[4],
128 	                                                            void      **dataOut,
129 	                                                            void      *polygon_data) */
130 
131 	/* TessError */
132 	public static final int GLU_TESS_ERROR1     = 100151;
133 	public static final int GLU_TESS_ERROR2     = 100152;
134 	public static final int GLU_TESS_ERROR3     = 100153;
135 	public static final int GLU_TESS_ERROR4     = 100154;
136 	public static final int GLU_TESS_ERROR5     = 100155;
137 	public static final int GLU_TESS_ERROR6     = 100156;
138 	public static final int GLU_TESS_ERROR7     = 100157;
139 	public static final int GLU_TESS_ERROR8     = 100158;
140 
141 	public static final int GLU_TESS_MISSING_BEGIN_POLYGON  = GLU_TESS_ERROR1;
142 	public static final int GLU_TESS_MISSING_BEGIN_CONTOUR  = GLU_TESS_ERROR2;
143 	public static final int GLU_TESS_MISSING_END_POLYGON    = GLU_TESS_ERROR3;
144 	public static final int GLU_TESS_MISSING_END_CONTOUR    = GLU_TESS_ERROR4;
145 	public static final int GLU_TESS_COORD_TOO_LARGE        = GLU_TESS_ERROR5;
146 	public static final int GLU_TESS_NEED_COMBINE_CALLBACK  = GLU_TESS_ERROR6;
147 
148 	/****           NURBS constants                 ****/
149 
150 	/* NurbsProperty */
151 	public static final int GLU_AUTO_LOAD_MATRIX    = 100200;
152 	public static final int GLU_CULLING             = 100201;
153 	public static final int GLU_SAMPLING_TOLERANCE  = 100203;
154 	public static final int GLU_DISPLAY_MODE        = 100204;
155 	public static final int GLU_PARAMETRIC_TOLERANCE        = 100202;
156 	public static final int GLU_SAMPLING_METHOD             = 100205;
157 	public static final int GLU_U_STEP                      = 100206;
158 	public static final int GLU_V_STEP                      = 100207;
159 
160 	/* NurbsSampling */
161 	public static final int GLU_PATH_LENGTH                 = 100215;
162 	public static final int GLU_PARAMETRIC_ERROR            = 100216;
163 	public static final int GLU_DOMAIN_DISTANCE             = 100217;
164 
165 
166 	/* NurbsTrim */
167 	public static final int GLU_MAP1_TRIM_2         = 100210;
168 	public static final int GLU_MAP1_TRIM_3         = 100211;
169 
170 	/* NurbsDisplay */
171 	/*      FILL                = 100012 */
172 	public static final int GLU_OUTLINE_POLYGON     = 100240;
173 	public static final int GLU_OUTLINE_PATCH       = 100241;
174 
175 	/* NurbsCallback */
176 	/*      ERROR               = 100103 */
177 
178 	/* NurbsErrors */
179 	public static final int GLU_NURBS_ERROR1        = 100251;
180 	public static final int GLU_NURBS_ERROR2        = 100252;
181 	public static final int GLU_NURBS_ERROR3        = 100253;
182 	public static final int GLU_NURBS_ERROR4        = 100254;
183 	public static final int GLU_NURBS_ERROR5        = 100255;
184 	public static final int GLU_NURBS_ERROR6        = 100256;
185 	public static final int GLU_NURBS_ERROR7        = 100257;
186 	public static final int GLU_NURBS_ERROR8        = 100258;
187 	public static final int GLU_NURBS_ERROR9        = 100259;
188 	public static final int GLU_NURBS_ERROR10       = 100260;
189 	public static final int GLU_NURBS_ERROR11       = 100261;
190 	public static final int GLU_NURBS_ERROR12       = 100262;
191 	public static final int GLU_NURBS_ERROR13       = 100263;
192 	public static final int GLU_NURBS_ERROR14       = 100264;
193 	public static final int GLU_NURBS_ERROR15       = 100265;
194 	public static final int GLU_NURBS_ERROR16       = 100266;
195 	public static final int GLU_NURBS_ERROR17       = 100267;
196 	public static final int GLU_NURBS_ERROR18       = 100268;
197 	public static final int GLU_NURBS_ERROR19       = 100269;
198 	public static final int GLU_NURBS_ERROR20       = 100270;
199 	public static final int GLU_NURBS_ERROR21       = 100271;
200 	public static final int GLU_NURBS_ERROR22       = 100272;
201 	public static final int GLU_NURBS_ERROR23       = 100273;
202 	public static final int GLU_NURBS_ERROR24       = 100274;
203 	public static final int GLU_NURBS_ERROR25       = 100275;
204 	public static final int GLU_NURBS_ERROR26       = 100276;
205 	public static final int GLU_NURBS_ERROR27       = 100277;
206 	public static final int GLU_NURBS_ERROR28       = 100278;
207 	public static final int GLU_NURBS_ERROR29       = 100279;
208 	public static final int GLU_NURBS_ERROR30       = 100280;
209 	public static final int GLU_NURBS_ERROR31       = 100281;
210 	public static final int GLU_NURBS_ERROR32       = 100282;
211 	public static final int GLU_NURBS_ERROR33       = 100283;
212 	public static final int GLU_NURBS_ERROR34       = 100284;
213 	public static final int GLU_NURBS_ERROR35       = 100285;
214 	public static final int GLU_NURBS_ERROR36       = 100286;
215 	public static final int GLU_NURBS_ERROR37       = 100287;
216 
217 	/* Contours types -- obsolete! */
218 	public static final int GLU_CW          = 100120;
219 	public static final int GLU_CCW         = 100121;
220 	public static final int GLU_INTERIOR    = 100122;
221 	public static final int GLU_EXTERIOR    = 100123;
222 	public static final int GLU_UNKNOWN     = 100124;
223 
224 	/* Names without "TESS_" prefix */
225 	public static final int GLU_BEGIN       = GLU_TESS_BEGIN;
226 	public static final int GLU_VERTEX      = GLU_TESS_VERTEX;
227 	public static final int GLU_END         = GLU_TESS_END;
228 	public static final int GLU_ERROR       = GLU_TESS_ERROR;
229 	public static final int GLU_EDGE_FLAG   = GLU_TESS_EDGE_FLAG;
230 
231 	/**
232 	 * Method gluLookAt
233 	 * @param eyex
234 	 * @param eyey
235 	 * @param eyez
236 	 * @param centerx
237 	 * @param centery
238 	 * @param centerz
239 	 * @param upx
240 	 * @param upy
241 	 * @param upz
242 	 */
gluLookAt( float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)243 	public static void gluLookAt(
244 			float eyex,
245 			float eyey,
246 			float eyez,
247 			float centerx,
248 			float centery,
249 			float centerz,
250 			float upx,
251 			float upy,
252 			float upz) {
253 
254 		Project.gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);
255 	}
256 
257 	/**
258 	 * Method gluOrtho2D
259 	 * @param left
260 	 * @param right
261 	 * @param bottom
262 	 * @param top
263 	 */
gluOrtho2D( float left, float right, float bottom, float top)264 	public static void gluOrtho2D(
265 			float left,
266 			float right,
267 			float bottom,
268 			float top) {
269 
270 		glOrtho(left, right, bottom, top, -1.0, 1.0);
271 	}
272 
273 	/**
274 	 * Method gluPerspective
275 	 * @param fovy
276 	 * @param aspect
277 	 * @param zNear
278 	 * @param zFar
279 	 */
gluPerspective( float fovy, float aspect, float zNear, float zFar)280 	public static void gluPerspective(
281 			float fovy,
282 			float aspect,
283 			float zNear,
284 			float zFar) {
285 
286 		Project.gluPerspective(fovy, aspect, zNear, zFar);
287 	}
288 
289 	/**
290 	 * Method gluProject
291 	 * @param objx
292 	 * @param objy
293 	 * @param objz
294 	 * @param modelMatrix
295 	 * @param projMatrix
296 	 * @param viewport
297 	 * @param win_pos
298 	 */
gluProject(float objx, float objy, float objz, FloatBuffer modelMatrix, FloatBuffer projMatrix, IntBuffer viewport, FloatBuffer win_pos)299 	public static boolean gluProject(float objx, float objy, float objz,
300 			FloatBuffer modelMatrix,
301 			FloatBuffer projMatrix,
302 			IntBuffer viewport,
303 			FloatBuffer win_pos)
304 	{
305 		return Project.gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, win_pos);
306 	}
307 
308 	/**
309 	 * Method gluUnproject
310 	 * @param winx
311 	 * @param winy
312 	 * @param winz
313 	 * @param modelMatrix
314 	 * @param projMatrix
315 	 * @param viewport
316 	 * @param obj_pos
317 	 */
gluUnProject(float winx, float winy, float winz, FloatBuffer modelMatrix, FloatBuffer projMatrix, IntBuffer viewport, FloatBuffer obj_pos)318 	public static boolean gluUnProject(float winx, float winy, float winz,
319 			FloatBuffer modelMatrix,
320 			FloatBuffer projMatrix,
321 			IntBuffer viewport,
322 			FloatBuffer obj_pos)
323 	{
324 		return Project.gluUnProject(winx, winy, winz, modelMatrix, projMatrix, viewport, obj_pos);
325 	}
326 
327 	/**
328 	 * Method gluPickMatrix
329 	 * @param x
330 	 * @param y
331 	 * @param width
332 	 * @param height
333 	 * @param viewport
334 	 */
gluPickMatrix( float x, float y, float width, float height, IntBuffer viewport)335 	public static void gluPickMatrix(
336 			float x,
337 			float y,
338 			float width,
339 			float height,
340 			IntBuffer viewport) {
341 
342 		Project.gluPickMatrix(x, y, width, height, viewport);
343 	}
344 
345 	/**
346 	 * Method gluGetString.
347 	 * @param name
348 	 * @return String
349 	 */
gluGetString(int name)350 	public static String gluGetString(int name) {
351 		return Registry.gluGetString(name);
352 	}
353 
354 	/**
355 	 * Method gluCheckExtension.
356 	 * @param extName
357 	 * @param extString
358 	 * @return boolean
359 	 */
gluCheckExtension(String extName, String extString)360 	public static boolean gluCheckExtension(String extName, String extString) {
361 		return Registry.gluCheckExtension(extName, extString);
362 	}
363 
364 	/**
365 	 * Method gluBuild2DMipmaps
366 	 * @param target
367 	 * @param components
368 	 * @param width
369 	 * @param height
370 	 * @param format
371 	 * @param type
372 	 * @param data
373 	 * @return int
374 	 */
gluBuild2DMipmaps( int target, int components, int width, int height, int format, int type, ByteBuffer data)375 	public static int gluBuild2DMipmaps(
376 			int target,
377 			int components,
378 			int width,
379 			int height,
380 			int format,
381 			int type,
382 			ByteBuffer data) {
383 
384 		return MipMap.gluBuild2DMipmaps(target, components, width, height, format, type, data);
385 	}
386 
387 	/**
388 	 * Method gluScaleImage.
389 	 * @param format
390 	 * @param widthIn
391 	 * @param heightIn
392 	 * @param typeIn
393 	 * @param dataIn
394 	 * @param widthOut
395 	 * @param heightOut
396 	 * @param typeOut
397 	 * @param dataOut
398 	 * @return int
399 	 */
gluScaleImage( int format, int widthIn, int heightIn, int typeIn, ByteBuffer dataIn, int widthOut, int heightOut, int typeOut, ByteBuffer dataOut)400 	public static int gluScaleImage(
401 			int format,
402 			int widthIn,
403 			int heightIn,
404 			int typeIn,
405 			ByteBuffer dataIn,
406 			int widthOut,
407 			int heightOut,
408 			int typeOut,
409 			ByteBuffer dataOut) {
410 
411 		return MipMap.gluScaleImage(format, widthIn, heightIn, typeIn, dataIn, widthOut, heightOut, typeOut, dataOut);
412 	}
413 
gluErrorString(int error_code)414 	public static String gluErrorString(int error_code) {
415 		switch (error_code) {
416 			case GLU_INVALID_ENUM:
417 				return "Invalid enum (glu)";
418 			case GLU_INVALID_VALUE:
419 				return "Invalid value (glu)";
420 			case GLU_OUT_OF_MEMORY:
421 				return "Out of memory (glu)";
422 			default:
423 				return Util.translateGLErrorString(error_code);
424 		}
425 	}
426 
gluNewTess()427 	public static GLUtessellator gluNewTess() {
428 		return new GLUtessellatorImpl();
429 	}
430 }
431