diff options
author | Brian Paul <[email protected]> | 2003-08-22 20:11:43 +0000 |
---|---|---|
committer | Brian Paul <[email protected]> | 2003-08-22 20:11:43 +0000 |
commit | 5df82c82bd53db90eb72c5aad4dd20cf6f1116b1 (patch) | |
tree | f04fc69df71104df2a4cec03346abc3d4c3f4bbb /src/glu/mini | |
parent | 1a84876d7907df90add3f59d3396ce0bbb905040 (diff) |
patch to import Jon Smirl's work from Bitkeeper
Diffstat (limited to 'src/glu/mini')
-rw-r--r-- | src/glu/mini/Makefile.X11 | 65 | ||||
-rw-r--r-- | src/glu/mini/all.h | 55 | ||||
-rw-r--r-- | src/glu/mini/glu.c | 417 | ||||
-rw-r--r-- | src/glu/mini/gluP.h | 142 | ||||
-rw-r--r-- | src/glu/mini/mipmap.c | 764 | ||||
-rw-r--r-- | src/glu/mini/nurbs.c | 158 | ||||
-rw-r--r-- | src/glu/mini/nurbs.h | 253 | ||||
-rw-r--r-- | src/glu/mini/nurbscrv.c | 133 | ||||
-rw-r--r-- | src/glu/mini/polytest.c | 938 | ||||
-rw-r--r-- | src/glu/mini/project.c | 402 | ||||
-rw-r--r-- | src/glu/mini/quadric.c | 774 | ||||
-rw-r--r-- | src/glu/mini/tess.c | 328 | ||||
-rw-r--r-- | src/glu/mini/tess.h | 108 | ||||
-rw-r--r-- | src/glu/mini/tesselat.c | 407 |
14 files changed, 4944 insertions, 0 deletions
diff --git a/src/glu/mini/Makefile.X11 b/src/glu/mini/Makefile.X11 new file mode 100644 index 00000000000..d0ef2e50f06 --- /dev/null +++ b/src/glu/mini/Makefile.X11 @@ -0,0 +1,65 @@ + + +TOP = ../../.. + +default: linux-solo + +LIBDIR = $(TOP)/lib + +INCLUDES = -I$(TOP)/include +CFLAGS = -c -g $(INCLUDES) -MD + +SOURCES = glu.c \ + mipmap.c \ + nurbs.c \ + polytest.c \ + project.c \ + quadric.c \ + tess.c \ + tesselat.c + + +OBJS = $(SOURCES:.c=.o) + +LIBS=-L$(TOP)/lib -lGL -lm + +##### RULES ##### + +.c.o: + $(CC) -c $(INCLUDES) $(CFLAGS) $(DEFINES) $< -o $@ + +.S.o: + $(CC) -c $(INCLUDES) $(CFLAGS) $(DEFINES) $< -o $@ + + +##### TARGETS ##### + +targets: depend libGLU.so.1.1 install + +libGLU.so.1.1: $(OBJS) Makefile.X11 + gcc -shared -Wl,-soname,libGLU.so -Wl,-Bsymbolic $(OBJS) $(LIBS) -o $@ + +install: + rm -f $(TOP)/lib/libGLU.so* + install -D libGLU.so.1.1 $(TOP)/lib/libGLU.so.1.1 + ln -s libGLU.so.1.1 $(TOP)/lib/libGLU.so.1 + ln -s libGLU.so.1 $(TOP)/lib/libGLU.so + +# Run 'make -f Makefile.X11 dep' to update the dependencies if you change +# what's included by any source file. +depend: $(SOURCES) + makedepend -fdepend -Y $(INCLUDES) \ + $(SOURCES) + +# Emacs tags +tags: + etags `find . -name \*.[ch]` `find ../include` + + +# Remove .o and backup files +clean: + -rm -f *.o *~ *.o *~ *.so + +include $(TOP)/Make-config + +include depend diff --git a/src/glu/mini/all.h b/src/glu/mini/all.h new file mode 100644 index 00000000000..d626bee937f --- /dev/null +++ b/src/glu/mini/all.h @@ -0,0 +1,55 @@ +/* $Id: all.h,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * This file includes all .h files needed for the GLU source code for + * the purpose of precompiled headers. + * + * If the preprocessor symbol PCH is defined at compile time then each + * of the .c files will #include "all.h" only, instead of a bunch of + * individual .h files. + */ + + +#ifndef GLU_ALL_H +#define GLU_ALL_H + + +#ifndef PC_HEADER +This is an error. all.h should be included only if PCH is defined. +#endif + + +#include <assert.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "GL/gl.h" +#include "GL/glu.h" +#include "gluP.h" +#include "nurbs.h" +#include "tess.h" + + +#endif /*GLU_ALL_H */ diff --git a/src/glu/mini/glu.c b/src/glu/mini/glu.c new file mode 100644 index 00000000000..5c7722c5f0b --- /dev/null +++ b/src/glu/mini/glu.c @@ -0,0 +1,417 @@ +/* $Id: glu.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.5 + * Copyright (C) 1995-2001 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <assert.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "gluP.h" +#endif + + +/* + * Miscellaneous utility functions + */ + + +#ifndef M_PI +#define M_PI 3.1415926536 +#endif +#define EPS 0.00001 + +#ifndef GLU_INCOMPATIBLE_GL_VERSION +#define GLU_INCOMPATIBLE_GL_VERSION 100903 +#endif + + +void GLAPIENTRY +gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, + GLdouble centerx, GLdouble centery, GLdouble centerz, + GLdouble upx, GLdouble upy, GLdouble upz) +{ + GLfloat m[16]; + GLfloat x[3], y[3], z[3]; + GLfloat mag; + + /* Make rotation matrix */ + + /* Z vector */ + z[0] = eyex - centerx; + z[1] = eyey - centery; + z[2] = eyez - centerz; + mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]); + if (mag) { /* mpichler, 19950515 */ + z[0] /= mag; + z[1] /= mag; + z[2] /= mag; + } + + /* Y vector */ + y[0] = upx; + y[1] = upy; + y[2] = upz; + + /* X vector = Y cross Z */ + x[0] = y[1] * z[2] - y[2] * z[1]; + x[1] = -y[0] * z[2] + y[2] * z[0]; + x[2] = y[0] * z[1] - y[1] * z[0]; + + /* Recompute Y = Z cross X */ + y[0] = z[1] * x[2] - z[2] * x[1]; + y[1] = -z[0] * x[2] + z[2] * x[0]; + y[2] = z[0] * x[1] - z[1] * x[0]; + + /* mpichler, 19950515 */ + /* cross product gives area of parallelogram, which is < 1.0 for + * non-perpendicular unit-length vectors; so normalize x, y here + */ + + mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]); + if (mag) { + x[0] /= mag; + x[1] /= mag; + x[2] /= mag; + } + + mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]); + if (mag) { + y[0] /= mag; + y[1] /= mag; + y[2] /= mag; + } + +#define M(row,col) m[col*4+row] + M(0, 0) = x[0]; + M(0, 1) = x[1]; + M(0, 2) = x[2]; + M(0, 3) = 0.0; + M(1, 0) = y[0]; + M(1, 1) = y[1]; + M(1, 2) = y[2]; + M(1, 3) = 0.0; + M(2, 0) = z[0]; + M(2, 1) = z[1]; + M(2, 2) = z[2]; + M(2, 3) = 0.0; + M(3, 0) = 0.0; + M(3, 1) = 0.0; + M(3, 2) = 0.0; + M(3, 3) = 1.0; +#undef M + glMultMatrixf(m); + + /* Translate Eye to Origin */ + glTranslatef(-eyex, -eyey, -eyez); + +} + + + +void GLAPIENTRY +gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) +{ + glOrtho(left, right, bottom, top, -1.0, 1.0); +} + + + +static void +frustum(GLfloat left, GLfloat right, + GLfloat bottom, GLfloat top, + GLfloat nearval, GLfloat farval) +{ + GLfloat x, y, a, b, c, d; + GLfloat m[16]; + + x = (2.0 * nearval) / (right - left); + y = (2.0 * nearval) / (top - bottom); + a = (right + left) / (right - left); + b = (top + bottom) / (top - bottom); + c = -(farval + nearval) / ( farval - nearval); + d = -(2.0 * farval * nearval) / (farval - nearval); + +#define M(row,col) m[col*4+row] + M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F; + M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F; + M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d; + M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F; +#undef M + + glMultMatrixf(m); +} + + +void GLAPIENTRY +gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) +{ + GLfloat xmin, xmax, ymin, ymax; + + ymax = zNear * tan(fovy * M_PI / 360.0); + ymin = -ymax; + xmin = ymin * aspect; + xmax = ymax * aspect; + + /* don't call glFrustum() because of error semantics (covglu) */ + frustum(xmin, xmax, ymin, ymax, zNear, zFar); +} + + + +void GLAPIENTRY +gluPickMatrix(GLdouble x, GLdouble y, + GLdouble width, GLdouble height, GLint viewport[4]) +{ + GLfloat m[16]; + GLfloat sx, sy; + GLfloat tx, ty; + + sx = viewport[2] / width; + sy = viewport[3] / height; + tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width; + ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height; + +#define M(row,col) m[col*4+row] + M(0, 0) = sx; + M(0, 1) = 0.0; + M(0, 2) = 0.0; + M(0, 3) = tx; + M(1, 0) = 0.0; + M(1, 1) = sy; + M(1, 2) = 0.0; + M(1, 3) = ty; + M(2, 0) = 0.0; + M(2, 1) = 0.0; + M(2, 2) = 1.0; + M(2, 3) = 0.0; + M(3, 0) = 0.0; + M(3, 1) = 0.0; + M(3, 2) = 0.0; + M(3, 3) = 1.0; +#undef M + + glMultMatrixf(m); +} + + + +const GLubyte *GLAPIENTRY +gluErrorString(GLenum errorCode) +{ + static char *tess_error[] = { + "missing gluBeginPolygon", + "missing gluBeginContour", + "missing gluEndPolygon", + "missing gluEndContour", + "misoriented or self-intersecting loops", + "coincident vertices", + "colinear vertices", + "FIST recovery process fatal error" + }; + static char *nurbs_error[] = { + "spline order un-supported", + "too few knots", + "valid knot range is empty", + "decreasing knot sequence knot", + "knot multiplicity greater than order of spline", + "endcurve() must follow bgncurve()", + "bgncurve() must precede endcurve()", + "missing or extra geometric data", + "can't draw pwlcurves", + "missing bgncurve()", + "missing bgnsurface()", + "endtrim() must precede endsurface()", + "bgnsurface() must precede endsurface()", + "curve of improper type passed as trim curve", + "bgnsurface() must precede bgntrim()", + "endtrim() must follow bgntrim()", + "bgntrim() must precede endtrim()", + "invalid or missing trim curve", + "bgntrim() must precede pwlcurve()", + "pwlcurve referenced twice", + "pwlcurve and nurbscurve mixed", + "improper usage of trim data type", + "nurbscurve referenced twice", + "nurbscurve and pwlcurve mixed", + "nurbssurface referenced twice", + "invalid property", + "endsurface() must follow bgnsurface()", + "misoriented trim curves", + "intersecting trim curves", + "UNUSED", + "unconnected trim curves", + "unknown knot error", + "negative vertex count encountered", + "negative byte-stride encountered", + "unknown type descriptor", + "null control array or knot vector", + "duplicate point on pwlcurve" + }; + + /* GL Errors */ + if (errorCode == GL_NO_ERROR) { + return (GLubyte *) "no error"; + } + else if (errorCode == GL_INVALID_VALUE) { + return (GLubyte *) "invalid value"; + } + else if (errorCode == GL_INVALID_ENUM) { + return (GLubyte *) "invalid enum"; + } + else if (errorCode == GL_INVALID_OPERATION) { + return (GLubyte *) "invalid operation"; + } + else if (errorCode == GL_STACK_OVERFLOW) { + return (GLubyte *) "stack overflow"; + } + else if (errorCode == GL_STACK_UNDERFLOW) { + return (GLubyte *) "stack underflow"; + } + else if (errorCode == GL_OUT_OF_MEMORY) { + return (GLubyte *) "out of memory"; + } + /* GLU Errors */ + else if (errorCode == GLU_NO_ERROR) { + return (GLubyte *) "no error"; + } + else if (errorCode == GLU_INVALID_ENUM) { + return (GLubyte *) "invalid enum"; + } + else if (errorCode == GLU_INVALID_VALUE) { + return (GLubyte *) "invalid value"; + } + else if (errorCode == GLU_OUT_OF_MEMORY) { + return (GLubyte *) "out of memory"; + } + else if (errorCode == GLU_INCOMPATIBLE_GL_VERSION) { + return (GLubyte *) "incompatible GL version"; + } + else if (errorCode >= GLU_TESS_ERROR1 && errorCode <= GLU_TESS_ERROR8) { + return (GLubyte *) tess_error[errorCode - GLU_TESS_ERROR1]; + } + else if (errorCode >= GLU_NURBS_ERROR1 && errorCode <= GLU_NURBS_ERROR37) { + return (GLubyte *) nurbs_error[errorCode - GLU_NURBS_ERROR1]; + } + else { + return NULL; + } +} + + + +/* + * New in GLU 1.1 + */ + +const GLubyte *GLAPIENTRY +gluGetString(GLenum name) +{ + static char *extensions = "GL_EXT_abgr"; + static char *version = "1.1 Mesa 3.5"; + + switch (name) { + case GLU_EXTENSIONS: + return (GLubyte *) extensions; + case GLU_VERSION: + return (GLubyte *) version; + default: + return NULL; + } +} + + + +#if 0 /* gluGetProcAddressEXT not finalized yet! */ + +#ifdef __cplusplus + /* for BeOS R4.5 */ +void GLAPIENTRY(*gluGetProcAddressEXT(const GLubyte * procName)) (...) +#else +void (GLAPIENTRY * gluGetProcAddressEXT(const GLubyte * procName)) () +#endif +{ + struct proc + { + const char *name; + void *address; + }; + static struct proc procTable[] = { + {"gluGetProcAddressEXT", (void *) gluGetProcAddressEXT}, /* me! */ + + /* new 1.1 functions */ + {"gluGetString", (void *) gluGetString}, + + /* new 1.2 functions */ + {"gluTessBeginPolygon", (void *) gluTessBeginPolygon}, + {"gluTessBeginContour", (void *) gluTessBeginContour}, + {"gluTessEndContour", (void *) gluTessEndContour}, + {"gluTessEndPolygon", (void *) gluTessEndPolygon}, + {"gluGetTessProperty", (void *) gluGetTessProperty}, + + /* new 1.3 functions */ + + {NULL, NULL} + }; + GLuint i; + + for (i = 0; procTable[i].address; i++) { + if (strcmp((const char *) procName, procTable[i].name) == 0) + return (void (GLAPIENTRY *) ()) procTable[i].address; + } + + return NULL; +} + +#endif + + + +/* + * New in GLU 1.3 + */ +#ifdef GLU_VERSION_1_3 +GLboolean GLAPIENTRY +gluCheckExtension(const GLubyte *extName, const GLubyte * extString) +{ + assert(extName); + assert(extString); + { + const int len = strlen((const char *) extName); + const char *start = (const char *) extString; + + while (1) { + const char *c = strstr(start, (const char *) extName); + if (!c) + return GL_FALSE; + + if ((c == start || c[-1] == ' ') && (c[len] == ' ' || c[len] == 0)) + return GL_TRUE; + + start = c + len; + } + } +} +#endif diff --git a/src/glu/mini/gluP.h b/src/glu/mini/gluP.h new file mode 100644 index 00000000000..85fbc33c625 --- /dev/null +++ b/src/glu/mini/gluP.h @@ -0,0 +1,142 @@ +/* $Id: gluP.h,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * This file allows the GLU code to be compiled either with the Mesa + * headers or with the real OpenGL headers. + */ + + +#ifndef GLUP_H +#define GLUP_H + + +#include <GL/gl.h> +#include <GL/glu.h> +#include <string.h> + + +#if defined(_WIN32) && !defined(__WIN32__) +# define __WIN32__ +#endif + +#if !defined(OPENSTEP) && (defined(__WIN32__) || defined(__CYGWIN__)) +# pragma warning( disable : 4068 ) /* unknown pragma */ +# pragma warning( disable : 4710 ) /* function 'foo' not inlined */ +# pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */ +# pragma warning( disable : 4127 ) /* conditional expression is constant */ +# if defined(MESA_MINWARN) +# pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */ +# pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */ +# pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */ +# pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */ +# pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */ +# endif +# if defined(_MSC_VER) && defined(BUILD_GL32) /* tag specify we're building mesa as a DLL */ +# define GLAPI __declspec(dllexport) +# define WGLAPI __declspec(dllexport) +# elif defined(_MSC_VER) && defined(_DLL) /* tag specifying we're building for DLL runtime support */ +# define GLAPI __declspec(dllimport) +# define WGLAPI __declspec(dllimport) +# else /* for use with static link lib build of Win32 edition only */ +# define GLAPI extern +# define WGLAPI __declspec(dllimport) +# endif /* _STATIC_MESA support */ +# define GLAPIENTRY __stdcall +# define GLAPIENTRYP __stdcall * +# define GLCALLBACK __stdcall +# define GLCALLBACKP __stdcall * +# if defined(__CYGWIN__) +# define GLCALLBACKPCAST * +# else +# define GLCALLBACKPCAST __stdcall * +# endif +# define GLWINAPI __stdcall +# define GLWINAPIV __cdecl +#else +/* non-Windows compilation */ +# define GLAPI extern +# define GLAPIENTRY +# define GLAPIENTRYP * +# define GLCALLBACK +# define GLCALLBACKP * +# define GLCALLBACKPCAST * +# define GLWINAPI +# define GLWINAPIV +#endif /* WIN32 / CYGWIN bracket */ + +/* compatibility guard so we don't need to change client code */ + +#if defined(_WIN32) && !defined(_WINDEF_) && !defined(_GNU_H_WINDOWS32_BASE) && !defined(OPENSTEP) +# define CALLBACK GLCALLBACK +typedef int (GLAPIENTRY *PROC)(); +typedef void *HGLRC; +typedef void *HDC; +typedef unsigned long COLORREF; +#endif + +#if defined(_WIN32) && !defined(_WINGDI_) && !defined(_GNU_H_WINDOWS32_DEFINES) && !defined(OPENSTEP) +# define WGL_FONT_LINES 0 +# define WGL_FONT_POLYGONS 1 +#ifndef _GNU_H_WINDOWS32_FUNCTIONS +# ifdef UNICODE +# define wglUseFontBitmaps wglUseFontBitmapsW +# define wglUseFontOutlines wglUseFontOutlinesW +# else +# define wglUseFontBitmaps wglUseFontBitmapsA +# define wglUseFontOutlines wglUseFontOutlinesA +# endif /* !UNICODE */ +#endif /* _GNU_H_WINDOWS32_FUNCTIONS */ +typedef struct tagLAYERPLANEDESCRIPTOR LAYERPLANEDESCRIPTOR, *PLAYERPLANEDESCRIPTOR, *LPLAYERPLANEDESCRIPTOR; +typedef struct _GLYPHMETRICSFLOAT GLYPHMETRICSFLOAT, *PGLYPHMETRICSFLOAT, *LPGLYPHMETRICSFLOAT; +typedef struct tagPIXELFORMATDESCRIPTOR PIXELFORMATDESCRIPTOR, *PPIXELFORMATDESCRIPTOR, *LPPIXELFORMATDESCRIPTOR; +#include <gl/mesa_wgl.h> +#endif + + + +#ifndef GLU_TESS_ERROR9 + /* If we're using the real OpenGL header files... */ +# define GLU_TESS_ERROR9 100159 +#endif + + +#define GLU_NO_ERROR GL_NO_ERROR + + +/* for Sun: */ +#ifdef SUNOS4 +#define MEMCPY( DST, SRC, BYTES) \ + memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) ) +#else +#define MEMCPY( DST, SRC, BYTES) \ + memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) ) +#endif + + +#ifndef NULL +# define NULL 0 +#endif + + +#endif diff --git a/src/glu/mini/mipmap.c b/src/glu/mini/mipmap.c new file mode 100644 index 00000000000..97297729e78 --- /dev/null +++ b/src/glu/mini/mipmap.c @@ -0,0 +1,764 @@ +/* $Id: mipmap.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.4 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <assert.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include "gluP.h" +#endif + + +/* + * Compute ceiling of integer quotient of A divided by B: + */ +#define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 ) + + + +#ifdef EPSILON +#undef EPSILON +#endif +#define EPSILON 0.001 + + +/* To work around optimizer bug in MSVC4.1 */ +#if defined(__WIN32__) && !defined(OPENSTEP) +void +dummy(GLuint j, GLuint k) +{ +} +#else +#define dummy(J, K) +#endif + + +GLint GLAPIENTRY +gluScaleImage(GLenum format, + GLsizei widthin, GLsizei heightin, + GLenum typein, const void *datain, + GLsizei widthout, GLsizei heightout, + GLenum typeout, void *dataout) +{ + GLint components, i, j, k; + GLfloat *tempin, *tempout, f; + GLfloat sx, sy; + GLint unpackrowlength, unpackalignment, unpackskiprows, unpackskippixels; + GLint packrowlength, packalignment, packskiprows, packskippixels; + GLint sizein, sizeout; + GLint rowstride, rowlen; + + + /* Determine number of components per pixel */ + switch (format) { + case GL_COLOR_INDEX: + case GL_STENCIL_INDEX: + case GL_DEPTH_COMPONENT: + case GL_RED: + case GL_GREEN: + case GL_BLUE: + case GL_ALPHA: + case GL_LUMINANCE: + components = 1; + break; + case GL_LUMINANCE_ALPHA: + components = 2; + break; + case GL_RGB: + case GL_BGR: + components = 3; + break; + case GL_RGBA: + case GL_BGRA: +#ifdef GL_EXT_abgr + case GL_ABGR_EXT: +#endif + components = 4; + break; + default: + return GLU_INVALID_ENUM; + } + + /* Determine bytes per input datum */ + switch (typein) { + case GL_UNSIGNED_BYTE: + sizein = sizeof(GLubyte); + break; + case GL_BYTE: + sizein = sizeof(GLbyte); + break; + case GL_UNSIGNED_SHORT: + sizein = sizeof(GLushort); + break; + case GL_SHORT: + sizein = sizeof(GLshort); + break; + case GL_UNSIGNED_INT: + sizein = sizeof(GLuint); + break; + case GL_INT: + sizein = sizeof(GLint); + break; + case GL_FLOAT: + sizein = sizeof(GLfloat); + break; + case GL_BITMAP: + /* not implemented yet */ + default: + return GL_INVALID_ENUM; + } + + /* Determine bytes per output datum */ + switch (typeout) { + case GL_UNSIGNED_BYTE: + sizeout = sizeof(GLubyte); + break; + case GL_BYTE: + sizeout = sizeof(GLbyte); + break; + case GL_UNSIGNED_SHORT: + sizeout = sizeof(GLushort); + break; + case GL_SHORT: + sizeout = sizeof(GLshort); + break; + case GL_UNSIGNED_INT: + sizeout = sizeof(GLuint); + break; + case GL_INT: + sizeout = sizeof(GLint); + break; + case GL_FLOAT: + sizeout = sizeof(GLfloat); + break; + case GL_BITMAP: + /* not implemented yet */ + default: + return GL_INVALID_ENUM; + } + + /* Get glPixelStore state */ + glGetFloatv(GL_UNPACK_ROW_LENGTH, &f); unpackrowlength = (int)f; + glGetFloatv(GL_UNPACK_ALIGNMENT, &f); unpackalignment = (int)f; + glGetFloatv(GL_UNPACK_SKIP_ROWS, &f); unpackskiprows = (int)f; + glGetFloatv(GL_UNPACK_SKIP_PIXELS, &f); unpackskippixels = (int)f; + glGetFloatv(GL_PACK_ROW_LENGTH, &f); packrowlength = (int)f; + glGetFloatv(GL_PACK_ALIGNMENT, &f); packalignment = (int)f; + glGetFloatv(GL_PACK_SKIP_ROWS, &f); packskiprows = (int)f; + glGetFloatv(GL_PACK_SKIP_PIXELS, &f); packskippixels = (int)f; + + /* Allocate storage for intermediate images */ + tempin = (GLfloat *) malloc(widthin * heightin + * components * sizeof(GLfloat)); + if (!tempin) { + return GLU_OUT_OF_MEMORY; + } + tempout = (GLfloat *) malloc(widthout * heightout + * components * sizeof(GLfloat)); + if (!tempout) { + free(tempin); + return GLU_OUT_OF_MEMORY; + } + + + /* + * Unpack the pixel data and convert to floating point + */ + + if (unpackrowlength > 0) { + rowlen = unpackrowlength; + } + else { + rowlen = widthin; + } + if (sizein >= unpackalignment) { + rowstride = components * rowlen; + } + else { + rowstride = unpackalignment / sizein + * CEILING(components * rowlen * sizein, unpackalignment); + } + + switch (typein) { + case GL_UNSIGNED_BYTE: + k = 0; + for (i = 0; i < heightin; i++) { + GLubyte *ubptr = (GLubyte *) datain + + i * rowstride + + unpackskiprows * rowstride + unpackskippixels * components; + for (j = 0; j < widthin * components; j++) { + dummy(j, k); + tempin[k++] = (GLfloat) * ubptr++; + } + } + break; + case GL_BYTE: + k = 0; + for (i = 0; i < heightin; i++) { + GLbyte *bptr = (GLbyte *) datain + + i * rowstride + + unpackskiprows * rowstride + unpackskippixels * components; + for (j = 0; j < widthin * components; j++) { + dummy(j, k); + tempin[k++] = (GLfloat) * bptr++; + } + } + break; + case GL_UNSIGNED_SHORT: + k = 0; + for (i = 0; i < heightin; i++) { + GLushort *usptr = (GLushort *) datain + + i * rowstride + + unpackskiprows * rowstride + unpackskippixels * components; + for (j = 0; j < widthin * components; j++) { + dummy(j, k); + tempin[k++] = (GLfloat) * usptr++; + } + } + break; + case GL_SHORT: + k = 0; + for (i = 0; i < heightin; i++) { + GLshort *sptr = (GLshort *) datain + + i * rowstride + + unpackskiprows * rowstride + unpackskippixels * components; + for (j = 0; j < widthin * components; j++) { + dummy(j, k); + tempin[k++] = (GLfloat) * sptr++; + } + } + break; + case GL_UNSIGNED_INT: + k = 0; + for (i = 0; i < heightin; i++) { + GLuint *uiptr = (GLuint *) datain + + i * rowstride + + unpackskiprows * rowstride + unpackskippixels * components; + for (j = 0; j < widthin * components; j++) { + dummy(j, k); + tempin[k++] = (GLfloat) * uiptr++; + } + } + break; + case GL_INT: + k = 0; + for (i = 0; i < heightin; i++) { + GLint *iptr = (GLint *) datain + + i * rowstride + + unpackskiprows * rowstride + unpackskippixels * components; + for (j = 0; j < widthin * components; j++) { + dummy(j, k); + tempin[k++] = (GLfloat) * iptr++; + } + } + break; + case GL_FLOAT: + k = 0; + for (i = 0; i < heightin; i++) { + GLfloat *fptr = (GLfloat *) datain + + i * rowstride + + unpackskiprows * rowstride + unpackskippixels * components; + for (j = 0; j < widthin * components; j++) { + dummy(j, k); + tempin[k++] = *fptr++; + } + } + break; + default: + return GLU_INVALID_ENUM; + } + + + /* + * Scale the image! + */ + + if (widthout > 1) + sx = (GLfloat) (widthin - 1) / (GLfloat) (widthout - 1); + else + sx = (GLfloat) (widthin - 1); + if (heightout > 1) + sy = (GLfloat) (heightin - 1) / (GLfloat) (heightout - 1); + else + sy = (GLfloat) (heightin - 1); + +/*#define POINT_SAMPLE*/ +#ifdef POINT_SAMPLE + for (i = 0; i < heightout; i++) { + GLint ii = i * sy; + for (j = 0; j < widthout; j++) { + GLint jj = j * sx; + + GLfloat *src = tempin + (ii * widthin + jj) * components; + GLfloat *dst = tempout + (i * widthout + j) * components; + + for (k = 0; k < components; k++) { + *dst++ = *src++; + } + } + } +#else + if (sx < 1.0 && sy < 1.0) { + /* magnify both width and height: use weighted sample of 4 pixels */ + GLint i0, i1, j0, j1; + GLfloat alpha, beta; + GLfloat *src00, *src01, *src10, *src11; + GLfloat s1, s2; + GLfloat *dst; + + for (i = 0; i < heightout; i++) { + i0 = i * sy; + i1 = i0 + 1; + if (i1 >= heightin) + i1 = heightin - 1; +/* i1 = (i+1) * sy - EPSILON;*/ + alpha = i * sy - i0; + for (j = 0; j < widthout; j++) { + j0 = j * sx; + j1 = j0 + 1; + if (j1 >= widthin) + j1 = widthin - 1; +/* j1 = (j+1) * sx - EPSILON; */ + beta = j * sx - j0; + + /* compute weighted average of pixels in rect (i0,j0)-(i1,j1) */ + src00 = tempin + (i0 * widthin + j0) * components; + src01 = tempin + (i0 * widthin + j1) * components; + src10 = tempin + (i1 * widthin + j0) * components; + src11 = tempin + (i1 * widthin + j1) * components; + + dst = tempout + (i * widthout + j) * components; + + for (k = 0; k < components; k++) { + s1 = *src00++ * (1.0 - beta) + *src01++ * beta; + s2 = *src10++ * (1.0 - beta) + *src11++ * beta; + *dst++ = s1 * (1.0 - alpha) + s2 * alpha; + } + } + } + } + else { + /* shrink width and/or height: use an unweighted box filter */ + GLint i0, i1; + GLint j0, j1; + GLint ii, jj; + GLfloat sum, *dst; + + for (i = 0; i < heightout; i++) { + i0 = i * sy; + i1 = i0 + 1; + if (i1 >= heightin) + i1 = heightin - 1; +/* i1 = (i+1) * sy - EPSILON; */ + for (j = 0; j < widthout; j++) { + j0 = j * sx; + j1 = j0 + 1; + if (j1 >= widthin) + j1 = widthin - 1; +/* j1 = (j+1) * sx - EPSILON; */ + + dst = tempout + (i * widthout + j) * components; + + /* compute average of pixels in the rectangle (i0,j0)-(i1,j1) */ + for (k = 0; k < components; k++) { + sum = 0.0; + for (ii = i0; ii <= i1; ii++) { + for (jj = j0; jj <= j1; jj++) { + sum += *(tempin + (ii * widthin + jj) * components + k); + } + } + sum /= (j1 - j0 + 1) * (i1 - i0 + 1); + *dst++ = sum; + } + } + } + } +#endif + + + /* + * Return output image + */ + + if (packrowlength > 0) { + rowlen = packrowlength; + } + else { + rowlen = widthout; + } + if (sizeout >= packalignment) { + rowstride = components * rowlen; + } + else { + rowstride = packalignment / sizeout + * CEILING(components * rowlen * sizeout, packalignment); + } + + switch (typeout) { + case GL_UNSIGNED_BYTE: + k = 0; + for (i = 0; i < heightout; i++) { + GLubyte *ubptr = (GLubyte *) dataout + + i * rowstride + + packskiprows * rowstride + packskippixels * components; + for (j = 0; j < widthout * components; j++) { + dummy(j, k + i); + *ubptr++ = (GLubyte) tempout[k++]; + } + } + break; + case GL_BYTE: + k = 0; + for (i = 0; i < heightout; i++) { + GLbyte *bptr = (GLbyte *) dataout + + i * rowstride + + packskiprows * rowstride + packskippixels * components; + for (j = 0; j < widthout * components; j++) { + dummy(j, k + i); + *bptr++ = (GLbyte) tempout[k++]; + } + } + break; + case GL_UNSIGNED_SHORT: + k = 0; + for (i = 0; i < heightout; i++) { + GLushort *usptr = (GLushort *) dataout + + i * rowstride + + packskiprows * rowstride + packskippixels * components; + for (j = 0; j < widthout * components; j++) { + dummy(j, k + i); + *usptr++ = (GLushort) tempout[k++]; + } + } + break; + case GL_SHORT: + k = 0; + for (i = 0; i < heightout; i++) { + GLshort *sptr = (GLshort *) dataout + + i * rowstride + + packskiprows * rowstride + packskippixels * components; + for (j = 0; j < widthout * components; j++) { + dummy(j, k + i); + *sptr++ = (GLshort) tempout[k++]; + } + } + break; + case GL_UNSIGNED_INT: + k = 0; + for (i = 0; i < heightout; i++) { + GLuint *uiptr = (GLuint *) dataout + + i * rowstride + + packskiprows * rowstride + packskippixels * components; + for (j = 0; j < widthout * components; j++) { + dummy(j, k + i); + *uiptr++ = (GLuint) tempout[k++]; + } + } + break; + case GL_INT: + k = 0; + for (i = 0; i < heightout; i++) { + GLint *iptr = (GLint *) dataout + + i * rowstride + + packskiprows * rowstride + packskippixels * components; + for (j = 0; j < widthout * components; j++) { + dummy(j, k + i); + *iptr++ = (GLint) tempout[k++]; + } + } + break; + case GL_FLOAT: + k = 0; + for (i = 0; i < heightout; i++) { + GLfloat *fptr = (GLfloat *) dataout + + i * rowstride + + packskiprows * rowstride + packskippixels * components; + for (j = 0; j < widthout * components; j++) { + dummy(j, k + i); + *fptr++ = tempout[k++]; + } + } + break; + default: + return GLU_INVALID_ENUM; + } + + + /* free temporary image storage */ + free(tempin); + free(tempout); + + return 0; +} + + + +/* + * Return the largest k such that 2^k <= n. + */ +static GLint +ilog2(GLint n) +{ + GLint k; + + if (n <= 0) + return 0; + for (k = 0; n >>= 1; k++); + return k; +} + + + +/* + * Find the value nearest to n which is also a power of two. + */ +static GLint +round2(GLint n) +{ + GLint m; + + for (m = 1; m < n; m *= 2); + + /* m>=n */ + if (m - n <= n - m / 2) { + return m; + } + else { + return m / 2; + } +} + + +/* + * Given an pixel format and data type, return the number of bytes to + * store one pixel. + */ +static GLint +bytes_per_pixel(GLenum format, GLenum type) +{ + GLint n, m; + + switch (format) { + case GL_COLOR_INDEX: + case GL_STENCIL_INDEX: + case GL_DEPTH_COMPONENT: + case GL_RED: + case GL_GREEN: + case GL_BLUE: + case GL_ALPHA: + case GL_LUMINANCE: + n = 1; + break; + case GL_LUMINANCE_ALPHA: + n = 2; + break; + case GL_RGB: + case GL_BGR: + n = 3; + break; + case GL_RGBA: + case GL_BGRA: +#ifdef GL_EXT_abgr + case GL_ABGR_EXT: +#endif + n = 4; + break; + default: + n = 0; + } + + switch (type) { + case GL_UNSIGNED_BYTE: + m = sizeof(GLubyte); + break; + case GL_BYTE: + m = sizeof(GLbyte); + break; + case GL_BITMAP: + m = 1; + break; + case GL_UNSIGNED_SHORT: + m = sizeof(GLushort); + break; + case GL_SHORT: + m = sizeof(GLshort); + break; + case GL_UNSIGNED_INT: + m = sizeof(GLuint); + break; + case GL_INT: + m = sizeof(GLint); + break; + case GL_FLOAT: + m = sizeof(GLfloat); + break; + default: + m = 0; + } + + return n * m; +} + + + +/* + * WARNING: This function isn't finished and has never been tested!!!! + */ +GLint GLAPIENTRY +gluBuild1DMipmaps(GLenum target, GLint components, + GLsizei width, GLenum format, GLenum type, const void *data) +{ + return 0; +} + + + +GLint GLAPIENTRY +gluBuild2DMipmaps(GLenum target, GLint components, + GLsizei width, GLsizei height, GLenum format, + GLenum type, const void *data) +{ + GLint w, h; + GLint maxsize; + void *image, *newimage; + GLint neww, newh, level, bpp; + int error; + GLboolean done; + GLint retval = 0; + GLint unpackrowlength, unpackalignment, unpackskiprows, unpackskippixels; + GLint packrowlength, packalignment, packskiprows, packskippixels; + GLfloat f; + + if (width < 1 || height < 1) + return GLU_INVALID_VALUE; + + glGetFloatv(GL_MAX_TEXTURE_SIZE, &f); maxsize = (int)f; + + w = round2(width); + if (w > maxsize) { + w = maxsize; + } + h = round2(height); + if (h > maxsize) { + h = maxsize; + } + + bpp = bytes_per_pixel(format, type); + if (bpp == 0) { + /* probably a bad format or type enum */ + return GLU_INVALID_ENUM; + } + + /* Get current glPixelStore values */ + glGetFloatv(GL_UNPACK_ROW_LENGTH, &f); unpackrowlength = (int)f; + glGetFloatv(GL_UNPACK_ALIGNMENT, &f); unpackalignment = (int)f; + glGetFloatv(GL_UNPACK_SKIP_ROWS, &f); unpackskiprows = (int)f; + glGetFloatv(GL_UNPACK_SKIP_PIXELS, &f); unpackskippixels = (int)f; + glGetFloatv(GL_PACK_ROW_LENGTH, &f); packrowlength = (int)f; + glGetFloatv(GL_PACK_ALIGNMENT, &f); packalignment = (int)f; + glGetFloatv(GL_PACK_SKIP_ROWS, &f); packskiprows = (int)f; + glGetFloatv(GL_PACK_SKIP_PIXELS, &f); packskippixels = (int)f; + + /* set pixel packing */ + glPixelStorei(GL_PACK_ROW_LENGTH, 0); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glPixelStorei(GL_PACK_SKIP_ROWS, 0); + glPixelStorei(GL_PACK_SKIP_PIXELS, 0); + + done = GL_FALSE; + + if (w != width || h != height) { + /* must rescale image to get "top" mipmap texture image */ + image = malloc((w + 4) * h * bpp); + if (!image) { + return GLU_OUT_OF_MEMORY; + } + error = gluScaleImage(format, width, height, type, data, + w, h, type, image); + if (error) { + retval = error; + done = GL_TRUE; + } + } + else { + image = (void *) data; + } + + level = 0; + while (!done) { + if (image != data) { + /* set pixel unpacking */ + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); + glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); + } + + glTexImage2D(target, level, components, w, h, 0, format, type, image); + + if (w == 1 && h == 1) + break; + + neww = (w < 2) ? 1 : w / 2; + newh = (h < 2) ? 1 : h / 2; + newimage = malloc((neww + 4) * newh * bpp); + if (!newimage) { + return GLU_OUT_OF_MEMORY; + } + + error = gluScaleImage(format, w, h, type, image, + neww, newh, type, newimage); + if (error) { + retval = error; + done = GL_TRUE; + } + + if (image != data) { + free(image); + } + image = newimage; + + w = neww; + h = newh; + level++; + } + + if (image != data) { + free(image); + } + + /* Restore original glPixelStore state */ + glPixelStorei(GL_UNPACK_ROW_LENGTH, unpackrowlength); + glPixelStorei(GL_UNPACK_ALIGNMENT, unpackalignment); + glPixelStorei(GL_UNPACK_SKIP_ROWS, unpackskiprows); + glPixelStorei(GL_UNPACK_SKIP_PIXELS, unpackskippixels); + glPixelStorei(GL_PACK_ROW_LENGTH, packrowlength); + glPixelStorei(GL_PACK_ALIGNMENT, packalignment); + glPixelStorei(GL_PACK_SKIP_ROWS, packskiprows); + glPixelStorei(GL_PACK_SKIP_PIXELS, packskippixels); + + return retval; +} diff --git a/src/glu/mini/nurbs.c b/src/glu/mini/nurbs.c new file mode 100644 index 00000000000..93c0dd3ce23 --- /dev/null +++ b/src/glu/mini/nurbs.c @@ -0,0 +1,158 @@ +/* $Id: nurbs.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * NURBS implementation written by Bogdan Sikorski ([email protected]) + * See README2 for more info. + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <stdio.h> +#include <stdlib.h> +#include "gluP.h" +#include "nurbs.h" +#endif + + +void +call_user_error(GLUnurbsObj * nobj, GLenum error) +{ + nobj->error = error; + if (nobj->error_callback != NULL) { + (*(nobj->error_callback)) (error); + } + else { + printf("NURBS error %d %s\n", error, (char *) gluErrorString(error)); + } +} + + + +GLUnurbsObj *GLAPIENTRY +gluNewNurbsRenderer(void) +{ + GLUnurbsObj *n; + GLfloat tmp_viewport[4]; + GLint i, j; + + n = (GLUnurbsObj *) malloc(sizeof(GLUnurbsObj)); + return n; +} + + + +void GLAPIENTRY +gluDeleteNurbsRenderer(GLUnurbsObj * nobj) +{ + if (nobj) { + free(nobj); + } +} + + + +void GLAPIENTRY +gluLoadSamplingMatrices(GLUnurbsObj * nobj, + const GLfloat modelMatrix[16], + const GLfloat projMatrix[16], const GLint viewport[4]) +{ +} + + +void GLAPIENTRY +gluNurbsProperty(GLUnurbsObj * nobj, GLenum property, GLfloat value) +{ +} + + +void GLAPIENTRY +gluGetNurbsProperty(GLUnurbsObj * nobj, GLenum property, GLfloat * value) +{ +} + + + +void GLAPIENTRY +gluBeginCurve(GLUnurbsObj * nobj) +{ +} + + +void GLAPIENTRY +gluEndCurve(GLUnurbsObj * nobj) +{ +} + + +void GLAPIENTRY +gluNurbsCurve(GLUnurbsObj * nobj, GLint nknots, GLfloat * knot, + GLint stride, GLfloat * ctlarray, GLint order, GLenum type) +{ +} + + +void GLAPIENTRY +gluBeginSurface(GLUnurbsObj * nobj) +{ +} + + +void GLAPIENTRY +gluEndSurface(GLUnurbsObj * nobj) +{ +} + + +void GLAPIENTRY +gluNurbsSurface(GLUnurbsObj * nobj, + GLint sknot_count, GLfloat * sknot, + GLint tknot_count, GLfloat * tknot, + GLint s_stride, GLint t_stride, + GLfloat * ctrlarray, GLint sorder, GLint torder, GLenum type) +{ +} + + +void GLAPIENTRY +gluNurbsCallback(GLUnurbsObj * nobj, GLenum which, void (GLCALLBACK * fn) ()) +{ +} + +void GLAPIENTRY +gluBeginTrim(GLUnurbsObj * nobj) +{ +} + +void GLAPIENTRY +gluPwlCurve(GLUnurbsObj * nobj, GLint count, GLfloat * array, GLint stride, + GLenum type) +{ +} + +void GLAPIENTRY +gluEndTrim(GLUnurbsObj * nobj) +{ +} diff --git a/src/glu/mini/nurbs.h b/src/glu/mini/nurbs.h new file mode 100644 index 00000000000..c9c9c094f1a --- /dev/null +++ b/src/glu/mini/nurbs.h @@ -0,0 +1,253 @@ +/* $Id: nurbs.h,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * NURBS implementation written by Bogdan Sikorski ([email protected]) + * See README2 for more info. + */ + + +#ifndef NURBS_H +#define NURBS_H + + +#define EPSILON 1e-06 /* epsilon for double precision compares */ + +typedef enum +{ + GLU_NURBS_CURVE, GLU_NURBS_SURFACE, GLU_NURBS_TRIM, GLU_NURBS_NO_TRIM, + GLU_NURBS_TRIM_DONE, GLU_NURBS_NONE +} +GLU_nurbs_enum; + +typedef enum +{ + GLU_TRIM_NURBS, GLU_TRIM_PWL +} +GLU_trim_enum; + +typedef struct +{ + GLint sknot_count; + GLfloat *sknot; + GLint tknot_count; + GLfloat *tknot; + GLint s_stride; + GLint t_stride; + GLfloat *ctrlarray; + GLint sorder; + GLint torder; + GLint dim; + GLenum type; +} +surface_attribs; + +typedef struct +{ + surface_attribs geom; + surface_attribs color; + surface_attribs texture; + surface_attribs normal; +} +nurbs_surface; + +typedef struct +{ + GLint knot_count; + GLfloat *knot; + GLint stride; + GLfloat *ctrlarray; + GLint order; + GLint dim; + GLenum type; +} +curve_attribs; + +typedef struct +{ + GLint pt_count; + GLfloat *ctrlarray; + GLint stride; + GLint dim; + GLenum type; +} +pwl_curve_attribs; + +typedef struct +{ + curve_attribs geom; + curve_attribs color; + curve_attribs texture; + curve_attribs normal; +} +nurbs_curve; + +typedef struct trim_list_str +{ + GLU_trim_enum trim_type; + union + { + pwl_curve_attribs pwl_curve; + curve_attribs nurbs_curve; + } + curve; + struct trim_list_str *next; +} +trim_list; + +typedef struct seg_trim_str +{ + GLfloat *points; + GLint pt_cnt, seg_array_len; + struct seg_trim_str *next; +} +trim_segments; + +typedef struct nurbs_trim_str +{ + trim_list *trim_loop; + trim_segments *segments; + struct nurbs_trim_str *next; +} +nurbs_trim; + +typedef struct +{ + GLfloat model[16], proj[16], viewport[4]; +} +culling_and_sampling_str; + +struct GLUnurbs +{ + GLboolean culling; + GLenum error; + void (GLCALLBACK * error_callback) (GLenum err); + GLenum display_mode; + GLU_nurbs_enum nurbs_type; + GLboolean auto_load_matrix; + culling_and_sampling_str sampling_matrices; + GLenum sampling_method; + GLfloat sampling_tolerance; + GLfloat parametric_tolerance; + GLint u_step, v_step; + nurbs_surface surface; + nurbs_curve curve; + nurbs_trim *trim; +}; + +typedef struct +{ + GLfloat *knot; + GLint nknots; + GLfloat *unified_knot; + GLint unified_nknots; + GLint order; + GLint t_min, t_max; + GLint delta_nknots; + GLboolean open_at_begin, open_at_end; + GLfloat *new_knot; + GLfloat *alpha; +} +knot_str_type; + +typedef struct +{ + GLfloat *geom_ctrl; + GLint geom_s_stride, geom_t_stride; + GLfloat **geom_offsets; + GLint geom_s_pt_cnt, geom_t_pt_cnt; + GLfloat *color_ctrl; + GLint color_s_stride, color_t_stride; + GLfloat **color_offsets; + GLint color_s_pt_cnt, color_t_pt_cnt; + GLfloat *normal_ctrl; + GLint normal_s_stride, normal_t_stride; + GLfloat **normal_offsets; + GLint normal_s_pt_cnt, normal_t_pt_cnt; + GLfloat *texture_ctrl; + GLint texture_s_stride, texture_t_stride; + GLfloat **texture_offsets; + GLint texture_s_pt_cnt, texture_t_pt_cnt; + GLint s_bezier_cnt, t_bezier_cnt; +} +new_ctrl_type; + +extern void call_user_error(GLUnurbsObj * nobj, GLenum error); + +extern GLenum test_knot(GLint nknots, GLfloat * knot, GLint order); + +extern GLenum explode_knot(knot_str_type * the_knot); + +extern GLenum calc_alphas(knot_str_type * the_knot); + +extern GLenum calc_new_ctrl_pts(GLfloat * ctrl, GLint stride, + knot_str_type * the_knot, GLint dim, + GLfloat ** new_ctrl, GLint * ncontrol); + +extern GLenum glu_do_sampling_crv(GLUnurbsObj * nobj, GLfloat * new_ctrl, + GLint n_ctrl, GLint order, GLint dim, + GLint ** factors); + +extern GLenum glu_do_sampling_3D(GLUnurbsObj * nobj, new_ctrl_type * new_ctrl, + int **sfactors, GLint ** tfactors); + +extern GLenum glu_do_sampling_uv(GLUnurbsObj * nobj, new_ctrl_type * new_ctrl, + int **sfactors, GLint ** tfactors); + +extern GLenum glu_do_sampling_param_3D(GLUnurbsObj * nobj, + new_ctrl_type * new_ctrl, + int **sfactors, GLint ** tfactors); + +extern GLboolean fine_culling_test_2D(GLUnurbsObj * nobj, GLfloat * ctrl, + GLint n_ctrl, GLint stride, GLint dim); + +extern GLboolean fine_culling_test_3D(GLUnurbsObj * nobj, GLfloat * ctrl, + GLint s_n_ctrl, GLint t_n_ctrl, + GLint s_stride, GLint t_stride, + GLint dim); + +extern void do_nurbs_curve(GLUnurbsObj * nobj); + +extern void do_nurbs_surface(GLUnurbsObj * nobj); + +extern GLenum patch_trimming(GLUnurbsObj * nobj, new_ctrl_type * new_ctrl, + GLint * sfactors, GLint * tfactors); + +extern void collect_unified_knot(knot_str_type * dest, knot_str_type * src, + GLfloat maximal_min_knot, + GLfloat minimal_max_knot); + +extern GLenum select_knot_working_range(GLUnurbsObj * nobj, + knot_str_type * geom_knot, + knot_str_type * color_knot, + knot_str_type * normal_knot, + knot_str_type * texture_knot); + +extern void free_unified_knots(knot_str_type * geom_knot, + knot_str_type * color_knot, + knot_str_type * normal_knot, + knot_str_type * texture_knot); + + + +#endif diff --git a/src/glu/mini/nurbscrv.c b/src/glu/mini/nurbscrv.c new file mode 100644 index 00000000000..62d91b46d3f --- /dev/null +++ b/src/glu/mini/nurbscrv.c @@ -0,0 +1,133 @@ +/* $Id: nurbscrv.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * NURBS implementation written by Bogdan Sikorski ([email protected]) + * See README2 for more info. + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <math.h> +#include <stdlib.h> +#include "gluP.h" +#include "nurbs.h" +#endif + + + +/* main NURBS curve procedure */ +void +do_nurbs_curve(GLUnurbsObj * nobj) +{ + GLint geom_order, color_order = 0, normal_order = 0, texture_order = 0; + GLenum geom_type; + GLint n_ctrl; + GLfloat *new_geom_ctrl, *new_color_ctrl, *new_normal_ctrl, + *new_texture_ctrl; + GLfloat *geom_ctrl = 0, *color_ctrl = 0, *normal_ctrl = 0, *texture_ctrl = 0; + GLint *factors; + GLint i, j; + GLint geom_dim, color_dim = 0, normal_dim = 0, texture_dim = 0; + + /* test the user supplied data */ + if (test_nurbs_curves(nobj) != GLU_NO_ERROR) + return; + + if (convert_curves(nobj, &new_geom_ctrl, &n_ctrl, &new_color_ctrl, + &new_normal_ctrl, &new_texture_ctrl) != GLU_NO_ERROR) + return; + + geom_order = nobj->curve.geom.order; + geom_type = nobj->curve.geom.type; + geom_dim = nobj->curve.geom.dim; + + if (glu_do_sampling_crv(nobj, new_geom_ctrl, n_ctrl, geom_order, geom_dim, + &factors) != GLU_NO_ERROR) { + free(new_geom_ctrl); + if (new_color_ctrl) + free(new_color_ctrl); + if (new_normal_ctrl) + free(new_normal_ctrl); + if (new_texture_ctrl) + free(new_texture_ctrl); + return; + } + glEnable(geom_type); + if (new_color_ctrl) { + glEnable(nobj->curve.color.type); + color_dim = nobj->curve.color.dim; + color_ctrl = new_color_ctrl; + color_order = nobj->curve.color.order; + } + if (new_normal_ctrl) { + glEnable(nobj->curve.normal.type); + normal_dim = nobj->curve.normal.dim; + normal_ctrl = new_normal_ctrl; + normal_order = nobj->curve.normal.order; + } + if (new_texture_ctrl) { + glEnable(nobj->curve.texture.type); + texture_dim = nobj->curve.texture.dim; + texture_ctrl = new_texture_ctrl; + texture_order = nobj->curve.texture.order; + } + for (i = 0, j = 0, geom_ctrl = new_geom_ctrl; + i < n_ctrl; i += geom_order, j++, geom_ctrl += geom_order * geom_dim) { + if (fine_culling_test_2D + (nobj, geom_ctrl, geom_order, geom_dim, geom_dim)) { + color_ctrl += color_order * color_dim; + normal_ctrl += normal_order * normal_dim; + texture_ctrl += texture_order * texture_dim; + continue; + } + glMap1f(geom_type, 0.0, 1.0, geom_dim, geom_order, geom_ctrl); + if (new_color_ctrl) { + glMap1f(nobj->curve.color.type, 0.0, 1.0, color_dim, + color_order, color_ctrl); + color_ctrl += color_order * color_dim; + } + if (new_normal_ctrl) { + glMap1f(nobj->curve.normal.type, 0.0, 1.0, normal_dim, + normal_order, normal_ctrl); + normal_ctrl += normal_order * normal_dim; + } + if (new_texture_ctrl) { + glMap1f(nobj->curve.texture.type, 0.0, 1.0, texture_dim, + texture_order, texture_ctrl); + texture_ctrl += texture_order * texture_dim; + } + glMapGrid1f(factors[j], 0.0, 1.0); + glEvalMesh1(GL_LINE, 0, factors[j]); + } + free(new_geom_ctrl); + free(factors); + if (new_color_ctrl) + free(new_color_ctrl); + if (new_normal_ctrl) + free(new_normal_ctrl); + if (new_texture_ctrl) + free(new_texture_ctrl); +} diff --git a/src/glu/mini/polytest.c b/src/glu/mini/polytest.c new file mode 100644 index 00000000000..52f272a3cb4 --- /dev/null +++ b/src/glu/mini/polytest.c @@ -0,0 +1,938 @@ +/* $Id: polytest.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * This file is part of the polygon tesselation code contributed by + * Bogdan Sikorski + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <math.h> +#include <stdlib.h> +#include "gluP.h" +#include "tess.h" +#endif + + + +static GLenum store_polygon_as_contour(GLUtriangulatorObj *); +static void free_current_polygon(tess_polygon *); +static void prepare_projection_info(GLUtriangulatorObj *); +static GLdouble twice_the_polygon_area(tess_vertex *, tess_vertex *); +static GLenum verify_edge_vertex_intersections(GLUtriangulatorObj *); +void tess_find_contour_hierarchies(GLUtriangulatorObj *); +static GLenum test_for_overlapping_contours(GLUtriangulatorObj *); +static GLenum contours_overlap(tess_contour *, tess_polygon *); +static GLenum is_contour_contained_in(tess_contour *, tess_contour *); +static void add_new_exterior(GLUtriangulatorObj *, tess_contour *); +static void add_new_interior(GLUtriangulatorObj *, tess_contour *, + tess_contour *); +static void add_interior_with_hierarchy_check(GLUtriangulatorObj *, + tess_contour *, tess_contour *); +static void reverse_hierarchy_and_add_exterior(GLUtriangulatorObj *, + tess_contour *, + tess_contour *); +static GLboolean point_in_polygon(tess_contour *, GLdouble, GLdouble); +static void shift_interior_to_exterior(GLUtriangulatorObj *, tess_contour *); +static void add_exterior_with_check(GLUtriangulatorObj *, tess_contour *, + tess_contour *); +static GLenum cut_out_hole(GLUtriangulatorObj *, tess_contour *, + tess_contour *); +static GLenum merge_hole_with_contour(GLUtriangulatorObj *, + tess_contour *, tess_contour *, + tess_vertex *, tess_vertex *); + +static GLenum +find_normal(GLUtriangulatorObj * tobj) +{ + tess_polygon *polygon = tobj->current_polygon; + tess_vertex *va, *vb, *vc; + GLdouble A, B, C; + GLdouble A0, A1, A2, B0, B1, B2; + + va = polygon->vertices; + vb = va->next; + A0 = vb->location[0] - va->location[0]; + A1 = vb->location[1] - va->location[1]; + A2 = vb->location[2] - va->location[2]; + for (vc = vb->next; vc != va; vc = vc->next) { + B0 = vc->location[0] - va->location[0]; + B1 = vc->location[1] - va->location[1]; + B2 = vc->location[2] - va->location[2]; + A = A1 * B2 - A2 * B1; + B = A2 * B0 - A0 * B2; + C = A0 * B1 - A1 * B0; + if (fabs(A) > EPSILON || fabs(B) > EPSILON || fabs(C) > EPSILON) { + polygon->A = A; + polygon->B = B; + polygon->C = C; + polygon->D = + -A * va->location[0] - B * va->location[1] - C * va->location[2]; + return GLU_NO_ERROR; + } + } + tess_call_user_error(tobj, GLU_TESS_ERROR7); + return GLU_ERROR; +} + +void +tess_test_polygon(GLUtriangulatorObj * tobj) +{ + tess_polygon *polygon = tobj->current_polygon; + + /* any vertices defined? */ + if (polygon->vertex_cnt < 3) { + free_current_polygon(polygon); + return; + } + /* wrap pointers */ + polygon->last_vertex->next = polygon->vertices; + polygon->vertices->previous = polygon->last_vertex; + /* determine the normal */ + if (find_normal(tobj) == GLU_ERROR) + return; + /* compare the normals of previously defined contours and this one */ + /* first contour define ? */ + if (tobj->contours == NULL) { + tobj->A = polygon->A; + tobj->B = polygon->B; + tobj->C = polygon->C; + tobj->D = polygon->D; + /* determine the best projection to use */ + if (fabs(polygon->A) > fabs(polygon->B)) + if (fabs(polygon->A) > fabs(polygon->C)) + tobj->projection = OYZ; + else + tobj->projection = OXY; + else if (fabs(polygon->B) > fabs(polygon->C)) + tobj->projection = OXZ; + else + tobj->projection = OXY; + } + else { + GLdouble a[3], b[3]; + tess_vertex *vertex = polygon->vertices; + + a[0] = tobj->A; + a[1] = tobj->B; + a[2] = tobj->C; + b[0] = polygon->A; + b[1] = polygon->B; + b[2] = polygon->C; + + /* compare the normals */ + if (fabs(a[1] * b[2] - a[2] * b[1]) > EPSILON || + fabs(a[2] * b[0] - a[0] * b[2]) > EPSILON || + fabs(a[0] * b[1] - a[1] * b[0]) > EPSILON) { + /* not coplanar */ + tess_call_user_error(tobj, GLU_TESS_ERROR9); + return; + } + /* the normals are parallel - test for plane equation */ + if (fabs(a[0] * vertex->location[0] + a[1] * vertex->location[1] + + a[2] * vertex->location[2] + tobj->D) > EPSILON) { + /* not the same plane */ + tess_call_user_error(tobj, GLU_TESS_ERROR9); + return; + } + } + prepare_projection_info(tobj); + if (verify_edge_vertex_intersections(tobj) == GLU_ERROR) + return; + if (test_for_overlapping_contours(tobj) == GLU_ERROR) + return; + if (store_polygon_as_contour(tobj) == GLU_ERROR) + return; +} + +static GLenum +test_for_overlapping_contours(GLUtriangulatorObj * tobj) +{ + tess_contour *contour; + tess_polygon *polygon; + + polygon = tobj->current_polygon; + for (contour = tobj->contours; contour != NULL; contour = contour->next) + if (contours_overlap(contour, polygon) != GLU_NO_ERROR) { + tess_call_user_error(tobj, GLU_TESS_ERROR5); + return GLU_ERROR; + } + return GLU_NO_ERROR; +} + +static GLenum +store_polygon_as_contour(GLUtriangulatorObj * tobj) +{ + tess_polygon *polygon = tobj->current_polygon; + tess_contour *contour = tobj->contours; + + /* the first contour defined */ + if (contour == NULL) { + if ((contour = (tess_contour *) malloc(sizeof(tess_contour))) == NULL) { + tess_call_user_error(tobj, GLU_OUT_OF_MEMORY); + free_current_polygon(polygon); + return GLU_ERROR; + } + tobj->contours = tobj->last_contour = contour; + contour->next = contour->previous = NULL; + } + else { + if ((contour = (tess_contour *) malloc(sizeof(tess_contour))) == NULL) { + tess_call_user_error(tobj, GLU_OUT_OF_MEMORY); + free_current_polygon(polygon); + return GLU_ERROR; + } + contour->previous = tobj->last_contour; + tobj->last_contour->next = contour; + tobj->last_contour = contour; + contour->next = NULL; + } + /* mark all vertices in new contour as not special */ + /* and all are boundary edges */ + { + tess_vertex *vertex; + GLuint vertex_cnt, i; + + for (vertex = polygon->vertices, i = 0, vertex_cnt = + polygon->vertex_cnt; i < vertex_cnt; vertex = vertex->next, i++) { + vertex->shadow_vertex = NULL; + vertex->edge_flag = GL_TRUE; + } + } + contour->vertex_cnt = polygon->vertex_cnt; + contour->area = polygon->area; + contour->orientation = polygon->orientation; + contour->type = GLU_UNKNOWN; + contour->vertices = polygon->vertices; + contour->last_vertex = polygon->last_vertex; + polygon->vertices = polygon->last_vertex = NULL; + polygon->vertex_cnt = 0; + ++(tobj->contour_cnt); + return GLU_NO_ERROR; +} + +static void +free_current_polygon(tess_polygon * polygon) +{ + tess_vertex *vertex, *vertex_tmp; + GLuint i; + + /* free current_polygon structures */ + for (vertex = polygon->vertices, i = 0; i < polygon->vertex_cnt; i++) { + vertex_tmp = vertex->next; + free(vertex); + vertex = vertex_tmp; + } + polygon->vertices = polygon->last_vertex = NULL; + polygon->vertex_cnt = 0; +} + +static void +prepare_projection_info(GLUtriangulatorObj * tobj) +{ + tess_polygon *polygon = tobj->current_polygon; + tess_vertex *vertex, *last_vertex_ptr; + GLdouble area; + + last_vertex_ptr = polygon->last_vertex; + switch (tobj->projection) { + case OXY: + for (vertex = polygon->vertices; vertex != last_vertex_ptr; + vertex = vertex->next) { + vertex->x = vertex->location[0]; + vertex->y = vertex->location[1]; + } + last_vertex_ptr->x = last_vertex_ptr->location[0]; + last_vertex_ptr->y = last_vertex_ptr->location[1]; + break; + case OXZ: + for (vertex = polygon->vertices; vertex != last_vertex_ptr; + vertex = vertex->next) { + vertex->x = vertex->location[0]; + vertex->y = vertex->location[2]; + } + last_vertex_ptr->x = last_vertex_ptr->location[0]; + last_vertex_ptr->y = last_vertex_ptr->location[2]; + break; + case OYZ: + for (vertex = polygon->vertices; vertex != last_vertex_ptr; + vertex = vertex->next) { + vertex->x = vertex->location[1]; + vertex->y = vertex->location[2]; + } + last_vertex_ptr->x = last_vertex_ptr->location[1]; + last_vertex_ptr->y = last_vertex_ptr->location[2]; + break; + } + area = twice_the_polygon_area(polygon->vertices, polygon->last_vertex); + if (area >= 0.0) { + polygon->orientation = GLU_CCW; + polygon->area = area; + } + else { + polygon->orientation = GLU_CW; + polygon->area = -area; + } +} + +static GLdouble +twice_the_polygon_area(tess_vertex * vertex, tess_vertex * last_vertex) +{ + tess_vertex *next; + GLdouble area, x, y; + + area = 0.0; + x = vertex->x; + y = vertex->y; + vertex = vertex->next; + for (; vertex != last_vertex; vertex = vertex->next) { + next = vertex->next; + area += + (vertex->x - x) * (next->y - y) - (vertex->y - y) * (next->x - x); + } + return area; +} + +/* test if edges ab and cd intersect */ +/* if not return GLU_NO_ERROR, else if cross return GLU_TESS_ERROR8, */ +/* else if adjacent return GLU_TESS_ERROR4 */ +static GLenum +edge_edge_intersect(tess_vertex * a, + tess_vertex * b, tess_vertex * c, tess_vertex * d) +{ + GLdouble denom, r, s; + GLdouble xba, ydc, yba, xdc, yac, xac; + + xba = b->x - a->x; + yba = b->y - a->y; + xdc = d->x - c->x; + ydc = d->y - c->y; + xac = a->x - c->x; + yac = a->y - c->y; + denom = xba * ydc - yba * xdc; + r = yac * xdc - xac * ydc; + /* parallel? */ + if (fabs(denom) < EPSILON) { + if (fabs(r) < EPSILON) { + /* colinear */ + if (fabs(xba) < EPSILON) { + /* compare the Y coordinate */ + if (yba > 0.0) { + if ( + (fabs(a->y - c->y) < EPSILON + && fabs(c->y - b->y) < EPSILON) + || (fabs(a->y - d->y) < EPSILON + && fabs(d->y - b->y) < + EPSILON)) return GLU_TESS_ERROR4; + + } + else { + if ( + (fabs(b->y - c->y) < EPSILON + && fabs(c->y - a->y) < EPSILON) + || (fabs(b->y - d->y) < EPSILON + && fabs(d->y - a->y) < + EPSILON)) return GLU_TESS_ERROR4; + } + } + else { + /* compare the X coordinate */ + if (xba > 0.0) { + if ( + (fabs(a->x - c->x) < EPSILON + && fabs(c->x - b->x) < EPSILON) + || (fabs(a->x - d->x) < EPSILON + && fabs(d->x - b->x) < + EPSILON)) return GLU_TESS_ERROR4; + } + else { + if ( + (fabs(b->x - c->x) < EPSILON + && fabs(c->x - a->x) < EPSILON) + || (fabs(b->x - d->x) < EPSILON + && fabs(d->x - a->x) < + EPSILON)) return GLU_TESS_ERROR4; + } + } + } + return GLU_NO_ERROR; + } + r /= denom; + s = (yac * xba - xac * yba) / denom; + /* test if one vertex lies on other edge */ + if (((fabs(r) < EPSILON || (r < 1.0 + EPSILON && r > 1.0 - EPSILON)) && + s > -EPSILON && s < 1.0 + EPSILON) || + ((fabs(s) < EPSILON || (s < 1.0 + EPSILON && s > 1.0 - EPSILON)) && + r > -EPSILON && r < 1.0 + EPSILON)) { + return GLU_TESS_ERROR4; + } + /* test for crossing */ + if (r > -EPSILON && r < 1.0 + EPSILON && s > -EPSILON && s < 1.0 + EPSILON) { + return GLU_TESS_ERROR8; + } + return GLU_NO_ERROR; +} + +static GLenum +verify_edge_vertex_intersections(GLUtriangulatorObj * tobj) +{ + tess_polygon *polygon = tobj->current_polygon; + tess_vertex *vertex1, *last_vertex, *vertex2; + GLenum test; + + last_vertex = polygon->last_vertex; + vertex1 = last_vertex; + for (vertex2 = vertex1->next->next; + vertex2->next != last_vertex; vertex2 = vertex2->next) { + test = edge_edge_intersect(vertex1, vertex1->next, vertex2, + vertex2->next); + if (test != GLU_NO_ERROR) { + tess_call_user_error(tobj, test); + return GLU_ERROR; + } + } + for (vertex1 = polygon->vertices; + vertex1->next->next != last_vertex; vertex1 = vertex1->next) { + for (vertex2 = vertex1->next->next; + vertex2 != last_vertex; vertex2 = vertex2->next) { + test = edge_edge_intersect(vertex1, vertex1->next, vertex2, + vertex2->next); + if (test != GLU_NO_ERROR) { + tess_call_user_error(tobj, test); + return GLU_ERROR; + } + } + } + return GLU_NO_ERROR; +} + +static int +#ifdef WIN32 + __cdecl +#endif +area_compare(const void *a, const void *b) +{ + GLdouble area1, area2; + + area1 = (*((tess_contour **) a))->area; + area2 = (*((tess_contour **) b))->area; + if (area1 < area2) + return 1; + if (area1 > area2) + return -1; + return 0; +} + +void +tess_find_contour_hierarchies(GLUtriangulatorObj * tobj) +{ + tess_contour **contours; /* dinamic array of pointers */ + tess_contour *tmp_contour_ptr = tobj->contours; + GLuint cnt, i; + GLenum result; + GLboolean hierarchy_changed; + + /* any contours? */ + if (tobj->contour_cnt < 2) { + tobj->contours->type = GLU_EXTERIOR; + return; + } + if ((contours = (tess_contour **) + malloc(sizeof(tess_contour *) * (tobj->contour_cnt))) == NULL) { + tess_call_user_error(tobj, GLU_OUT_OF_MEMORY); + return; + } + for (tmp_contour_ptr = tobj->contours, cnt = 0; + tmp_contour_ptr != NULL; tmp_contour_ptr = tmp_contour_ptr->next) + contours[cnt++] = tmp_contour_ptr; + /* now sort the contours in decreasing area size order */ + qsort((void *) contours, (size_t) cnt, (size_t) sizeof(tess_contour *), + area_compare); + /* we leave just the first contour - remove others from list */ + tobj->contours = contours[0]; + tobj->contours->next = tobj->contours->previous = NULL; + tobj->last_contour = tobj->contours; + tobj->contour_cnt = 1; + /* first contour is the one with greatest area */ + /* must be EXTERIOR */ + tobj->contours->type = GLU_EXTERIOR; + tmp_contour_ptr = tobj->contours; + /* now we play! */ + for (i = 1; i < cnt; i++) { + hierarchy_changed = GL_FALSE; + for (tmp_contour_ptr = tobj->contours; + tmp_contour_ptr != NULL; tmp_contour_ptr = tmp_contour_ptr->next) { + if (tmp_contour_ptr->type == GLU_EXTERIOR) { + /* check if contour completely contained in EXTERIOR */ + result = is_contour_contained_in(tmp_contour_ptr, contours[i]); + switch (result) { + case GLU_INTERIOR: + /* now we have to check if contour is inside interiors */ + /* or not */ + /* any interiors? */ + if (tmp_contour_ptr->next != NULL && + tmp_contour_ptr->next->type == GLU_INTERIOR) { + /* for all interior, check if inside any of them */ + /* if not inside any of interiors, its another */ + /* interior */ + /* or it may contain some interiors, then change */ + /* the contained interiors to exterior ones */ + add_interior_with_hierarchy_check(tobj, + tmp_contour_ptr, + contours[i]); + } + else { + /* not in interior, add as new interior contour */ + add_new_interior(tobj, tmp_contour_ptr, contours[i]); + } + hierarchy_changed = GL_TRUE; + break; + case GLU_EXTERIOR: + /* ooops, the marked as EXTERIOR (contours[i]) is */ + /* actually an interior of tmp_contour_ptr */ + /* reverse the local hierarchy */ + reverse_hierarchy_and_add_exterior(tobj, tmp_contour_ptr, + contours[i]); + hierarchy_changed = GL_TRUE; + break; + case GLU_NO_ERROR: + break; + default: + abort(); + } + } + if (hierarchy_changed) + break; /* break from for loop */ + } + if (hierarchy_changed == GL_FALSE) { + /* disjoint with all contours, add to contour list */ + add_new_exterior(tobj, contours[i]); + } + } + free(contours); +} + +/* returns GLU_INTERIOR if inner is completey enclosed within outer */ +/* returns GLU_EXTERIOR if outer is completely enclosed within inner */ +/* returns GLU_NO_ERROR if contours are disjoint */ +static GLenum +is_contour_contained_in(tess_contour * outer, tess_contour * inner) +{ + GLenum relation_flag; + + /* set relation_flag to relation of containment of first inner vertex */ + /* regarding outer contour */ + if (point_in_polygon(outer, inner->vertices->x, inner->vertices->y)) + relation_flag = GLU_INTERIOR; + else + relation_flag = GLU_EXTERIOR; + if (relation_flag == GLU_INTERIOR) + return GLU_INTERIOR; + if (point_in_polygon(inner, outer->vertices->x, outer->vertices->y)) + return GLU_EXTERIOR; + return GLU_NO_ERROR; +} + +static GLboolean +point_in_polygon(tess_contour * contour, GLdouble x, GLdouble y) +{ + tess_vertex *v1, *v2; + GLuint i, vertex_cnt; + GLdouble xp1, yp1, xp2, yp2; + GLboolean tst; + + tst = GL_FALSE; + v1 = contour->vertices; + v2 = contour->vertices->previous; + for (i = 0, vertex_cnt = contour->vertex_cnt; i < vertex_cnt; i++) { + xp1 = v1->x; + yp1 = v1->y; + xp2 = v2->x; + yp2 = v2->y; + if ((((yp1 <= y) && (y < yp2)) || ((yp2 <= y) && (y < yp1))) && + (x < (xp2 - xp1) * (y - yp1) / (yp2 - yp1) + xp1)) + tst = (tst == GL_FALSE ? GL_TRUE : GL_FALSE); + v2 = v1; + v1 = v1->next; + } + return tst; +} + +static GLenum +contours_overlap(tess_contour * contour, tess_polygon * polygon) +{ + tess_vertex *vertex1, *vertex2; + GLuint vertex1_cnt, vertex2_cnt, i, j; + GLenum test; + + vertex1 = contour->vertices; + vertex2 = polygon->vertices; + vertex1_cnt = contour->vertex_cnt; + vertex2_cnt = polygon->vertex_cnt; + for (i = 0; i < vertex1_cnt; vertex1 = vertex1->next, i++) { + for (j = 0; j < vertex2_cnt; vertex2 = vertex2->next, j++) + if ((test = edge_edge_intersect(vertex1, vertex1->next, vertex2, + vertex2->next)) != GLU_NO_ERROR) + return test; + } + return GLU_NO_ERROR; +} + +static void +add_new_exterior(GLUtriangulatorObj * tobj, tess_contour * contour) +{ + contour->type = GLU_EXTERIOR; + contour->next = NULL; + contour->previous = tobj->last_contour; + tobj->last_contour->next = contour; + tobj->last_contour = contour; +} + +static void +add_new_interior(GLUtriangulatorObj * tobj, + tess_contour * outer, tess_contour * contour) +{ + contour->type = GLU_INTERIOR; + contour->next = outer->next; + contour->previous = outer; + if (outer->next != NULL) + outer->next->previous = contour; + outer->next = contour; + if (tobj->last_contour == outer) + tobj->last_contour = contour; +} + +static void +add_interior_with_hierarchy_check(GLUtriangulatorObj * tobj, + tess_contour * outer, + tess_contour * contour) +{ + tess_contour *ptr; + + /* for all interiors of outer check if they are interior of contour */ + /* if so, change that interior to exterior and move it of of the */ + /* interior sequence */ + if (outer->next != NULL && outer->next->type == GLU_INTERIOR) { + GLenum test; + + for (ptr = outer->next; ptr != NULL && ptr->type == GLU_INTERIOR; + ptr = ptr->next) { + test = is_contour_contained_in(ptr, contour); + switch (test) { + case GLU_INTERIOR: + /* contour is contained in one of the interiors */ + /* check if possibly contained in other exteriors */ + /* move ptr to first EXTERIOR */ + for (; ptr != NULL && ptr->type == GLU_INTERIOR; ptr = ptr->next); + if (ptr == NULL) + /* another exterior */ + add_new_exterior(tobj, contour); + else + add_exterior_with_check(tobj, ptr, contour); + return; + case GLU_EXTERIOR: + /* one of the interiors is contained in the contour */ + /* change it to EXTERIOR, and shift it away from the */ + /* interior sequence */ + shift_interior_to_exterior(tobj, ptr); + break; + case GLU_NO_ERROR: + /* disjoint */ + break; + default: + abort(); + } + } + } + /* add contour to the interior sequence */ + add_new_interior(tobj, outer, contour); +} + +static void +reverse_hierarchy_and_add_exterior(GLUtriangulatorObj * tobj, + tess_contour * outer, + tess_contour * contour) +{ + tess_contour *ptr; + + /* reverse INTERIORS to EXTERIORS */ + /* any INTERIORS? */ + if (outer->next != NULL && outer->next->type == GLU_INTERIOR) + for (ptr = outer->next; ptr != NULL && ptr->type == GLU_INTERIOR; + ptr = ptr->next) ptr->type = GLU_EXTERIOR; + /* the outer now becomes inner */ + outer->type = GLU_INTERIOR; + /* contour is the EXTERIOR */ + contour->next = outer; + if (tobj->contours == outer) { + /* first contour beeing reversed */ + contour->previous = NULL; + tobj->contours = contour; + } + else { + outer->previous->next = contour; + contour->previous = outer->previous; + } + outer->previous = contour; +} + +static void +shift_interior_to_exterior(GLUtriangulatorObj * tobj, tess_contour * contour) +{ + contour->previous->next = contour->next; + if (contour->next != NULL) + contour->next->previous = contour->previous; + else + tobj->last_contour = contour->previous; +} + +static void +add_exterior_with_check(GLUtriangulatorObj * tobj, + tess_contour * outer, tess_contour * contour) +{ + GLenum test; + + /* this contour might be interior to further exteriors - check */ + /* if not, just add as a new exterior */ + for (; outer != NULL && outer->type == GLU_EXTERIOR; outer = outer->next) { + test = is_contour_contained_in(outer, contour); + switch (test) { + case GLU_INTERIOR: + /* now we have to check if contour is inside interiors */ + /* or not */ + /* any interiors? */ + if (outer->next != NULL && outer->next->type == GLU_INTERIOR) { + /* for all interior, check if inside any of them */ + /* if not inside any of interiors, its another */ + /* interior */ + /* or it may contain some interiors, then change */ + /* the contained interiors to exterior ones */ + add_interior_with_hierarchy_check(tobj, outer, contour); + } + else { + /* not in interior, add as new interior contour */ + add_new_interior(tobj, outer, contour); + } + return; + case GLU_NO_ERROR: + /* disjoint */ + break; + default: + abort(); + } + } + /* add contour to the exterior sequence */ + add_new_exterior(tobj, contour); +} + +void +tess_handle_holes(GLUtriangulatorObj * tobj) +{ + tess_contour *contour, *hole; + GLenum exterior_orientation; + + /* verify hole orientation */ + for (contour = tobj->contours; contour != NULL;) { + exterior_orientation = contour->orientation; + for (contour = contour->next; + contour != NULL && contour->type == GLU_INTERIOR; + contour = contour->next) { + if (contour->orientation == exterior_orientation) { + tess_call_user_error(tobj, GLU_TESS_ERROR5); + return; + } + } + } + /* now cut-out holes */ + for (contour = tobj->contours; contour != NULL;) { + hole = contour->next; + while (hole != NULL && hole->type == GLU_INTERIOR) { + if (cut_out_hole(tobj, contour, hole) == GLU_ERROR) + return; + hole = contour->next; + } + contour = contour->next; + } +} + +static GLenum +cut_out_hole(GLUtriangulatorObj * tobj, + tess_contour * contour, tess_contour * hole) +{ + tess_contour *tmp_hole; + tess_vertex *v1, *v2, *tmp_vertex; + GLuint vertex1_cnt, vertex2_cnt, tmp_vertex_cnt; + GLuint i, j, k; + GLenum test = 0; + + /* find an edge connecting contour and hole not intersecting any other */ + /* edge belonging to either the contour or any of the other holes */ + for (v1 = contour->vertices, vertex1_cnt = contour->vertex_cnt, i = 0; + i < vertex1_cnt; i++, v1 = v1->next) { + for (v2 = hole->vertices, vertex2_cnt = hole->vertex_cnt, j = 0; + j < vertex2_cnt; j++, v2 = v2->next) { + /* does edge (v1,v2) intersect any edge of contour */ + for (tmp_vertex = contour->vertices, tmp_vertex_cnt = + contour->vertex_cnt, k = 0; k < tmp_vertex_cnt; + tmp_vertex = tmp_vertex->next, k++) { + /* skip edge tests for edges directly connected */ + if (v1 == tmp_vertex || v1 == tmp_vertex->next) + continue; + test = edge_edge_intersect(v1, v2, tmp_vertex, tmp_vertex->next); + if (test != GLU_NO_ERROR) + break; + } + if (test == GLU_NO_ERROR) { + /* does edge (v1,v2) intersect any edge of hole */ + for (tmp_vertex = hole->vertices, + tmp_vertex_cnt = hole->vertex_cnt, k = 0; + k < tmp_vertex_cnt; tmp_vertex = tmp_vertex->next, k++) { + /* skip edge tests for edges directly connected */ + if (v2 == tmp_vertex || v2 == tmp_vertex->next) + continue; + test = + edge_edge_intersect(v1, v2, tmp_vertex, tmp_vertex->next); + if (test != GLU_NO_ERROR) + break; + } + if (test == GLU_NO_ERROR) { + /* does edge (v1,v2) intersect any other hole? */ + for (tmp_hole = hole->next; + tmp_hole != NULL && tmp_hole->type == GLU_INTERIOR; + tmp_hole = tmp_hole->next) { + /* does edge (v1,v2) intersect any edge of hole */ + for (tmp_vertex = tmp_hole->vertices, + tmp_vertex_cnt = tmp_hole->vertex_cnt, k = 0; + k < tmp_vertex_cnt; tmp_vertex = tmp_vertex->next, k++) { + test = edge_edge_intersect(v1, v2, tmp_vertex, + tmp_vertex->next); + if (test != GLU_NO_ERROR) + break; + } + if (test != GLU_NO_ERROR) + break; + } + } + } + if (test == GLU_NO_ERROR) { + /* edge (v1,v2) is good for eliminating the hole */ + if (merge_hole_with_contour(tobj, contour, hole, v1, v2) + == GLU_NO_ERROR) + return GLU_NO_ERROR; + else + return GLU_ERROR; + } + } + } + /* other holes are blocking all possible connections of hole */ + /* with contour, we shift this hole as the last hole and retry */ + for (tmp_hole = hole; + tmp_hole != NULL && tmp_hole->type == GLU_INTERIOR; + tmp_hole = tmp_hole->next); + contour->next = hole->next; + hole->next->previous = contour; + if (tmp_hole == NULL) { + /* last EXTERIOR contour, shift hole as last contour */ + hole->next = NULL; + hole->previous = tobj->last_contour; + tobj->last_contour->next = hole; + tobj->last_contour = hole; + } + else { + tmp_hole->previous->next = hole; + hole->previous = tmp_hole->previous; + tmp_hole->previous = hole; + hole->next = tmp_hole; + } + hole = contour->next; + /* try once again - recurse */ + return cut_out_hole(tobj, contour, hole); +} + +static GLenum +merge_hole_with_contour(GLUtriangulatorObj * tobj, + tess_contour * contour, + tess_contour * hole, + tess_vertex * v1, tess_vertex * v2) +{ + tess_vertex *v1_new, *v2_new; + + /* make copies of v1 and v2, place them respectively after their originals */ + if ((v1_new = (tess_vertex *) malloc(sizeof(tess_vertex))) == NULL) { + tess_call_user_error(tobj, GLU_OUT_OF_MEMORY); + return GLU_ERROR; + } + if ((v2_new = (tess_vertex *) malloc(sizeof(tess_vertex))) == NULL) { + tess_call_user_error(tobj, GLU_OUT_OF_MEMORY); + return GLU_ERROR; + } + v1_new->edge_flag = GL_TRUE; + v1_new->data = v1->data; + v1_new->location[0] = v1->location[0]; + v1_new->location[1] = v1->location[1]; + v1_new->location[2] = v1->location[2]; + v1_new->x = v1->x; + v1_new->y = v1->y; + v1_new->shadow_vertex = v1; + v1->shadow_vertex = v1_new; + v1_new->next = v1->next; + v1_new->previous = v1; + v1->next->previous = v1_new; + v1->next = v1_new; + v2_new->edge_flag = GL_TRUE; + v2_new->data = v2->data; + v2_new->location[0] = v2->location[0]; + v2_new->location[1] = v2->location[1]; + v2_new->location[2] = v2->location[2]; + v2_new->x = v2->x; + v2_new->y = v2->y; + v2_new->shadow_vertex = v2; + v2->shadow_vertex = v2_new; + v2_new->next = v2->next; + v2_new->previous = v2; + v2->next->previous = v2_new; + v2->next = v2_new; + /* link together the two lists */ + v1->next = v2_new; + v2_new->previous = v1; + v2->next = v1_new; + v1_new->previous = v2; + /* update the vertex count of the contour */ + contour->vertex_cnt += hole->vertex_cnt + 2; + /* remove the INTERIOR contour */ + contour->next = hole->next; + if (hole->next != NULL) + hole->next->previous = contour; + free(hole); + /* update tobj structure */ + --(tobj->contour_cnt); + if (contour->last_vertex == v1) + contour->last_vertex = v1_new; + /* mark two vertices with edge_flag */ + v2->edge_flag = GL_FALSE; + v1->edge_flag = GL_FALSE; + return GLU_NO_ERROR; +} diff --git a/src/glu/mini/project.c b/src/glu/mini/project.c new file mode 100644 index 00000000000..a2747de55f2 --- /dev/null +++ b/src/glu/mini/project.c @@ -0,0 +1,402 @@ +/* $Id: project.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <stdio.h> +#include <string.h> +#include <math.h> +#include "gluP.h" +#endif + + +/* + * This code was contributed by Marc Buffat ([email protected]). + * Thanks Marc!!! + */ + + + +/* implementation de gluProject et gluUnproject */ +/* M. Buffat 17/2/95 */ + + + +/* + * Transform a point (column vector) by a 4x4 matrix. I.e. out = m * in + * Input: m - the 4x4 matrix + * in - the 4x1 vector + * Output: out - the resulting 4x1 vector. + */ +static void +transform_point(GLdouble out[4], const GLdouble m[16], const GLdouble in[4]) +{ +#define M(row,col) m[col*4+row] + out[0] = + M(0, 0) * in[0] + M(0, 1) * in[1] + M(0, 2) * in[2] + M(0, 3) * in[3]; + out[1] = + M(1, 0) * in[0] + M(1, 1) * in[1] + M(1, 2) * in[2] + M(1, 3) * in[3]; + out[2] = + M(2, 0) * in[0] + M(2, 1) * in[1] + M(2, 2) * in[2] + M(2, 3) * in[3]; + out[3] = + M(3, 0) * in[0] + M(3, 1) * in[1] + M(3, 2) * in[2] + M(3, 3) * in[3]; +#undef M +} + + + + +/* + * Perform a 4x4 matrix multiplication (product = a x b). + * Input: a, b - matrices to multiply + * Output: product - product of a and b + */ +static void +matmul(GLdouble * product, const GLdouble * a, const GLdouble * b) +{ + /* This matmul was contributed by Thomas Malik */ + GLdouble temp[16]; + GLint i; + +#define A(row,col) a[(col<<2)+row] +#define B(row,col) b[(col<<2)+row] +#define T(row,col) temp[(col<<2)+row] + + /* i-te Zeile */ + for (i = 0; i < 4; i++) { + T(i, 0) = + A(i, 0) * B(0, 0) + A(i, 1) * B(1, 0) + A(i, 2) * B(2, 0) + A(i, + 3) * + B(3, 0); + T(i, 1) = + A(i, 0) * B(0, 1) + A(i, 1) * B(1, 1) + A(i, 2) * B(2, 1) + A(i, + 3) * + B(3, 1); + T(i, 2) = + A(i, 0) * B(0, 2) + A(i, 1) * B(1, 2) + A(i, 2) * B(2, 2) + A(i, + 3) * + B(3, 2); + T(i, 3) = + A(i, 0) * B(0, 3) + A(i, 1) * B(1, 3) + A(i, 2) * B(2, 3) + A(i, + 3) * + B(3, 3); + } + +#undef A +#undef B +#undef T + MEMCPY(product, temp, 16 * sizeof(GLdouble)); +} + + + +/* + * Compute inverse of 4x4 transformation matrix. + * Code contributed by Jacques Leroy [email protected] + * Return GL_TRUE for success, GL_FALSE for failure (singular matrix) + */ +static GLboolean +invert_matrix(const GLdouble * m, GLdouble * out) +{ +/* NB. OpenGL Matrices are COLUMN major. */ +#define SWAP_ROWS(a, b) { GLdouble *_tmp = a; (a)=(b); (b)=_tmp; } +#define MAT(m,r,c) (m)[(c)*4+(r)] + + GLdouble wtmp[4][8]; + GLdouble m0, m1, m2, m3, s; + GLdouble *r0, *r1, *r2, *r3; + + r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3]; + + r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1), + r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3), + r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0, + r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1), + r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3), + r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0, + r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1), + r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3), + r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0, + r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1), + r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3), + r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0; + + /* choose pivot - or die */ + if (fabs(r3[0]) > fabs(r2[0])) + SWAP_ROWS(r3, r2); + if (fabs(r2[0]) > fabs(r1[0])) + SWAP_ROWS(r2, r1); + if (fabs(r1[0]) > fabs(r0[0])) + SWAP_ROWS(r1, r0); + if (0.0 == r0[0]) + return GL_FALSE; + + /* eliminate first variable */ + m1 = r1[0] / r0[0]; + m2 = r2[0] / r0[0]; + m3 = r3[0] / r0[0]; + s = r0[1]; + r1[1] -= m1 * s; + r2[1] -= m2 * s; + r3[1] -= m3 * s; + s = r0[2]; + r1[2] -= m1 * s; + r2[2] -= m2 * s; + r3[2] -= m3 * s; + s = r0[3]; + r1[3] -= m1 * s; + r2[3] -= m2 * s; + r3[3] -= m3 * s; + s = r0[4]; + if (s != 0.0) { + r1[4] -= m1 * s; + r2[4] -= m2 * s; + r3[4] -= m3 * s; + } + s = r0[5]; + if (s != 0.0) { + r1[5] -= m1 * s; + r2[5] -= m2 * s; + r3[5] -= m3 * s; + } + s = r0[6]; + if (s != 0.0) { + r1[6] -= m1 * s; + r2[6] -= m2 * s; + r3[6] -= m3 * s; + } + s = r0[7]; + if (s != 0.0) { + r1[7] -= m1 * s; + r2[7] -= m2 * s; + r3[7] -= m3 * s; + } + + /* choose pivot - or die */ + if (fabs(r3[1]) > fabs(r2[1])) + SWAP_ROWS(r3, r2); + if (fabs(r2[1]) > fabs(r1[1])) + SWAP_ROWS(r2, r1); + if (0.0 == r1[1]) + return GL_FALSE; + + /* eliminate second variable */ + m2 = r2[1] / r1[1]; + m3 = r3[1] / r1[1]; + r2[2] -= m2 * r1[2]; + r3[2] -= m3 * r1[2]; + r2[3] -= m2 * r1[3]; + r3[3] -= m3 * r1[3]; + s = r1[4]; + if (0.0 != s) { + r2[4] -= m2 * s; + r3[4] -= m3 * s; + } + s = r1[5]; + if (0.0 != s) { + r2[5] -= m2 * s; + r3[5] -= m3 * s; + } + s = r1[6]; + if (0.0 != s) { + r2[6] -= m2 * s; + r3[6] -= m3 * s; + } + s = r1[7]; + if (0.0 != s) { + r2[7] -= m2 * s; + r3[7] -= m3 * s; + } + + /* choose pivot - or die */ + if (fabs(r3[2]) > fabs(r2[2])) + SWAP_ROWS(r3, r2); + if (0.0 == r2[2]) + return GL_FALSE; + + /* eliminate third variable */ + m3 = r3[2] / r2[2]; + r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4], + r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], r3[7] -= m3 * r2[7]; + + /* last check */ + if (0.0 == r3[3]) + return GL_FALSE; + + s = 1.0 / r3[3]; /* now back substitute row 3 */ + r3[4] *= s; + r3[5] *= s; + r3[6] *= s; + r3[7] *= s; + + m2 = r2[3]; /* now back substitute row 2 */ + s = 1.0 / r2[2]; + r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2), + r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2); + m1 = r1[3]; + r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1, + r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1; + m0 = r0[3]; + r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0, + r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0; + + m1 = r1[2]; /* now back substitute row 1 */ + s = 1.0 / r1[1]; + r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1), + r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1); + m0 = r0[2]; + r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0, + r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0; + + m0 = r0[1]; /* now back substitute row 0 */ + s = 1.0 / r0[0]; + r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0), + r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0); + + MAT(out, 0, 0) = r0[4]; + MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6]; + MAT(out, 0, 3) = r0[7], MAT(out, 1, 0) = r1[4]; + MAT(out, 1, 1) = r1[5], MAT(out, 1, 2) = r1[6]; + MAT(out, 1, 3) = r1[7], MAT(out, 2, 0) = r2[4]; + MAT(out, 2, 1) = r2[5], MAT(out, 2, 2) = r2[6]; + MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4]; + MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6]; + MAT(out, 3, 3) = r3[7]; + + return GL_TRUE; + +#undef MAT +#undef SWAP_ROWS +} + + + +/* projection du point (objx,objy,obz) sur l'ecran (winx,winy,winz) */ +GLint GLAPIENTRY +gluProject(GLdouble objx, GLdouble objy, GLdouble objz, + const GLdouble model[16], const GLdouble proj[16], + const GLint viewport[4], + GLdouble * winx, GLdouble * winy, GLdouble * winz) +{ + /* matrice de transformation */ + GLdouble in[4], out[4]; + + /* initilise la matrice et le vecteur a transformer */ + in[0] = objx; + in[1] = objy; + in[2] = objz; + in[3] = 1.0; + transform_point(out, model, in); + transform_point(in, proj, out); + + /* d'ou le resultat normalise entre -1 et 1 */ + if (in[3] == 0.0) + return GL_FALSE; + + in[0] /= in[3]; + in[1] /= in[3]; + in[2] /= in[3]; + + /* en coordonnees ecran */ + *winx = viewport[0] + (1 + in[0]) * viewport[2] / 2; + *winy = viewport[1] + (1 + in[1]) * viewport[3] / 2; + /* entre 0 et 1 suivant z */ + *winz = (1 + in[2]) / 2; + return GL_TRUE; +} + + + +/* transformation du point ecran (winx,winy,winz) en point objet */ +GLint GLAPIENTRY +gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz, + const GLdouble model[16], const GLdouble proj[16], + const GLint viewport[4], + GLdouble * objx, GLdouble * objy, GLdouble * objz) +{ + /* matrice de transformation */ + GLdouble m[16], A[16]; + GLdouble in[4], out[4]; + + /* transformation coordonnees normalisees entre -1 et 1 */ + in[0] = (winx - viewport[0]) * 2 / viewport[2] - 1.0; + in[1] = (winy - viewport[1]) * 2 / viewport[3] - 1.0; + in[2] = 2 * winz - 1.0; + in[3] = 1.0; + + /* calcul transformation inverse */ + matmul(A, proj, model); + invert_matrix(A, m); + + /* d'ou les coordonnees objets */ + transform_point(out, m, in); + if (out[3] == 0.0) + return GL_FALSE; + *objx = out[0] / out[3]; + *objy = out[1] / out[3]; + *objz = out[2] / out[3]; + return GL_TRUE; +} + + +/* + * New in GLU 1.3 + * This is like gluUnProject but also takes near and far DepthRange values. + */ +#ifdef GLU_VERSION_1_3 +GLint GLAPIENTRY +gluUnProject4(GLdouble winx, GLdouble winy, GLdouble winz, GLdouble clipw, + const GLdouble modelMatrix[16], + const GLdouble projMatrix[16], + const GLint viewport[4], + GLclampd nearZ, GLclampd farZ, + GLdouble * objx, GLdouble * objy, GLdouble * objz, + GLdouble * objw) +{ + /* matrice de transformation */ + GLdouble m[16], A[16]; + GLdouble in[4], out[4]; + GLdouble z = nearZ + winz * (farZ - nearZ); + + /* transformation coordonnees normalisees entre -1 et 1 */ + in[0] = (winx - viewport[0]) * 2 / viewport[2] - 1.0; + in[1] = (winy - viewport[1]) * 2 / viewport[3] - 1.0; + in[2] = 2.0 * z - 1.0; + in[3] = clipw; + + /* calcul transformation inverse */ + matmul(A, projMatrix, modelMatrix); + invert_matrix(A, m); + + /* d'ou les coordonnees objets */ + transform_point(out, m, in); + if (out[3] == 0.0) + return GL_FALSE; + *objx = out[0] / out[3]; + *objy = out[1] / out[3]; + *objz = out[2] / out[3]; + *objw = out[3]; + return GL_TRUE; +} +#endif diff --git a/src/glu/mini/quadric.c b/src/glu/mini/quadric.c new file mode 100644 index 00000000000..015552e1237 --- /dev/null +++ b/src/glu/mini/quadric.c @@ -0,0 +1,774 @@ +/* $Id: quadric.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1999-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* TODO: + * texture coordinate support + * flip normals according to orientation + * there's still some inside/outside orientation bugs in possibly all + * but the sphere function + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include "gluP.h" +#endif + + + +#ifndef M_PI +# define M_PI (3.1415926) +#endif + + +/* + * Convert degrees to radians: + */ +#define DEG_TO_RAD(A) ((A)*(M_PI/180.0)) + + +/* + * Sin and Cos for degree angles: + */ +#define SIND( A ) sin( (A)*(M_PI/180.0) ) +#define COSD( A) cos( (A)*(M_PI/180.0) ) + + +/* + * Texture coordinates if texture flag is set + */ +#define TXTR_COORD(x,y) if (qobj->TextureFlag) glTexCoord2f(x,y); + + + +struct GLUquadric +{ + GLenum DrawStyle; /* GLU_FILL, LINE, SILHOUETTE, or POINT */ + GLenum Orientation; /* GLU_INSIDE or GLU_OUTSIDE */ + GLboolean TextureFlag; /* Generate texture coords? */ + GLenum Normals; /* GLU_NONE, GLU_FLAT, or GLU_SMOOTH */ + void (GLCALLBACK * ErrorFunc) (GLenum err); /* Error handler callback function */ +}; + + + +/* + * Process a GLU error. + */ +static void +quadric_error(GLUquadricObj * qobj, GLenum error, const char *msg) +{ + /* Call the error call back function if any */ + if (qobj->ErrorFunc) { + (*qobj->ErrorFunc) (error); + } + /* Print a message to stdout if MESA_DEBUG variable is defined */ + if (getenv("MESA_DEBUG")) { + fprintf(stderr, "GLUError: %s: %s\n", (char *) gluErrorString(error), + msg); + } +} + + + + +GLUquadricObj *GLAPIENTRY +gluNewQuadric(void) +{ + GLUquadricObj *q; + + q = (GLUquadricObj *) malloc(sizeof(struct GLUquadric)); + if (q) { + q->DrawStyle = GLU_FILL; + q->Orientation = GLU_OUTSIDE; + q->TextureFlag = GL_FALSE; + q->Normals = GLU_SMOOTH; + q->ErrorFunc = NULL; + } + return q; +} + + + +void GLAPIENTRY +gluDeleteQuadric(GLUquadricObj * state) +{ + if (state) { + free((void *) state); + } +} + + + +/* + * Set the drawing style to be GLU_FILL, GLU_LINE, GLU_SILHOUETTE, + * or GLU_POINT. + */ +void GLAPIENTRY +gluQuadricDrawStyle(GLUquadricObj * quadObject, GLenum drawStyle) +{ + if (quadObject && (drawStyle == GLU_FILL || drawStyle == GLU_LINE + || drawStyle == GLU_SILHOUETTE + || drawStyle == GLU_POINT)) { + quadObject->DrawStyle = drawStyle; + } + else { + quadric_error(quadObject, GLU_INVALID_ENUM, "qluQuadricDrawStyle"); + } +} + + + +/* + * Set the orientation to GLU_INSIDE or GLU_OUTSIDE. + */ +void GLAPIENTRY +gluQuadricOrientation(GLUquadricObj * quadObject, GLenum orientation) +{ + if (quadObject + && (orientation == GLU_INSIDE || orientation == GLU_OUTSIDE)) { + quadObject->Orientation = orientation; + } + else { + quadric_error(quadObject, GLU_INVALID_ENUM, "qluQuadricOrientation"); + } +} + + + +/* + * Set the error handler callback function. + */ +void GLAPIENTRY +gluQuadricCallback(GLUquadricObj * qobj, + GLenum which, void (GLCALLBACK * fn) ()) +{ + /* + * UGH, this is a mess! I thought ANSI was a standard. + */ + if (qobj && which == GLU_ERROR) { +#ifdef __CYGWIN32__ + qobj->ErrorFunc = (void (GLCALLBACKPCAST) (GLenum)) fn; +#elif defined(OPENSTEP) + qobj->ErrorFunc = (void (*)(GLenum)) fn; +#elif defined(_WIN32) + qobj->ErrorFunc = (void (GLCALLBACK *) (int)) fn; +#elif defined(__STORM__) + qobj->ErrorFunc = (void (GLCALLBACK *) (GLenum)) fn; +#elif defined(__BEOS__) + qobj->ErrorFunc = (void (*)(GLenum)) fn; +#else + qobj->ErrorFunc = (void (GLCALLBACK *) ()) fn; +#endif + } +} + + +void GLAPIENTRY +gluQuadricNormals(GLUquadricObj * quadObject, GLenum normals) +{ + if (quadObject + && (normals == GLU_NONE || normals == GLU_FLAT + || normals == GLU_SMOOTH)) { + quadObject->Normals = normals; + } +} + + +void GLAPIENTRY +gluQuadricTexture(GLUquadricObj * quadObject, GLboolean textureCoords) +{ + if (quadObject) { + quadObject->TextureFlag = textureCoords; + } +} + + + + +/* + * Call glNormal3f after scaling normal to unit length. + */ +static void +normal3f(GLfloat x, GLfloat y, GLfloat z) +{ +} + + + +void GLAPIENTRY +gluCylinder(GLUquadricObj * qobj, + GLdouble baseRadius, GLdouble topRadius, + GLdouble height, GLint slices, GLint stacks) +{ + GLdouble da, r, dr, dz; + GLfloat x, y, z, nz, nsign; + GLint i, j; + + if (qobj->Orientation == GLU_INSIDE) { + nsign = -1.0; + } + else { + nsign = 1.0; + } + + da = 2.0 * M_PI / slices; + dr = (topRadius - baseRadius) / stacks; + dz = height / stacks; + nz = (baseRadius - topRadius) / height; /* Z component of normal vectors */ + + if (qobj->DrawStyle == GLU_POINT) { + glBegin(GL_POINTS); + for (i = 0; i < slices; i++) { + x = cos(i * da); + y = sin(i * da); + normal3f(x * nsign, y * nsign, nz * nsign); + + z = 0.0; + r = baseRadius; + for (j = 0; j <= stacks; j++) { + glVertex3f(x * r, y * r, z); + z += dz; + r += dr; + } + } + glEnd(); + } + else if (qobj->DrawStyle == GLU_LINE || qobj->DrawStyle == GLU_SILHOUETTE) { + /* Draw rings */ + if (qobj->DrawStyle == GLU_LINE) { + z = 0.0; + r = baseRadius; + for (j = 0; j <= stacks; j++) { + glBegin(GL_LINE_LOOP); + for (i = 0; i < slices; i++) { + x = cos(i * da); + y = sin(i * da); + normal3f(x * nsign, y * nsign, nz * nsign); + glVertex3f(x * r, y * r, z); + } + glEnd(); + z += dz; + r += dr; + } + } + else { + /* draw one ring at each end */ + if (baseRadius != 0.0) { + glBegin(GL_LINE_LOOP); + for (i = 0; i < slices; i++) { + x = cos(i * da); + y = sin(i * da); + normal3f(x * nsign, y * nsign, nz * nsign); + glVertex3f(x * baseRadius, y * baseRadius, 0.0); + } + glEnd(); + glBegin(GL_LINE_LOOP); + for (i = 0; i < slices; i++) { + x = cos(i * da); + y = sin(i * da); + normal3f(x * nsign, y * nsign, nz * nsign); + glVertex3f(x * topRadius, y * topRadius, height); + } + glEnd(); + } + } + /* draw length lines */ + glBegin(GL_LINES); + for (i = 0; i < slices; i++) { + x = cos(i * da); + y = sin(i * da); + normal3f(x * nsign, y * nsign, nz * nsign); + glVertex3f(x * baseRadius, y * baseRadius, 0.0); + glVertex3f(x * topRadius, y * topRadius, height); + } + glEnd(); + } + else if (qobj->DrawStyle == GLU_FILL) { + GLfloat ds = 1.0 / slices; + GLfloat dt = 1.0 / stacks; + GLfloat t = 0.0; + z = 0.0; + r = baseRadius; + for (j = 0; j < stacks; j++) { + GLfloat s = 0.0; + glBegin(GL_QUAD_STRIP); + for (i = 0; i <= slices; i++) { + GLfloat x, y; + if (i == slices) { + x = sin(0.0); + y = cos(0.0); + } + else { + x = sin(i * da); + y = cos(i * da); + } + if (nsign == 1.0) { + normal3f(x * nsign, y * nsign, nz * nsign); + TXTR_COORD(s, t); + glVertex3f(x * r, y * r, z); + normal3f(x * nsign, y * nsign, nz * nsign); + TXTR_COORD(s, t + dt); + glVertex3f(x * (r + dr), y * (r + dr), z + dz); + } + else { + normal3f(x * nsign, y * nsign, nz * nsign); + TXTR_COORD(s, t); + glVertex3f(x * r, y * r, z); + normal3f(x * nsign, y * nsign, nz * nsign); + TXTR_COORD(s, t + dt); + glVertex3f(x * (r + dr), y * (r + dr), z + dz); + } + s += ds; + } /* for slices */ + glEnd(); + r += dr; + t += dt; + z += dz; + } /* for stacks */ + } +} + + + + + +void GLAPIENTRY +gluSphere(GLUquadricObj * qobj, GLdouble radius, GLint slices, GLint stacks) +{ + GLfloat rho, drho, theta, dtheta; + GLfloat x, y, z; + GLfloat s, t, ds, dt; + GLint i, j, imin, imax; + GLboolean normals; + GLfloat nsign; + + if (qobj->Normals == GLU_NONE) { + normals = GL_FALSE; + } + else { + normals = GL_TRUE; + } + if (qobj->Orientation == GLU_INSIDE) { + nsign = -1.0; + } + else { + nsign = 1.0; + } + + drho = M_PI / (GLfloat) stacks; + dtheta = 2.0 * M_PI / (GLfloat) slices; + + /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y axis */ + /* t goes from -1.0/+1.0 at z = -radius/+radius (linear along longitudes) */ + /* cannot use triangle fan on texturing (s coord. at top/bottom tip varies) */ + + if (qobj->DrawStyle == GLU_FILL) { + if (!qobj->TextureFlag) { + /* draw +Z end as a triangle fan */ + glBegin(GL_TRIANGLE_FAN); +/* glNormal3f(0.0, 0.0, 1.0); */ + glVertex3f(0.0, 0.0, nsign * radius); + for (j = 0; j <= slices; j++) { + theta = (j == slices) ? 0.0 : j * dtheta; + x = -sin(theta) * sin(drho); + y = cos(theta) * sin(drho); + z = nsign * cos(drho); + glVertex3f(x * radius, y * radius, z * radius); + } + glEnd(); + } + + ds = 1.0 / slices; + dt = 1.0 / stacks; + t = 1.0; /* because loop now runs from 0 */ + if (qobj->TextureFlag) { + imin = 0; + imax = stacks; + } + else { + imin = 1; + imax = stacks - 1; + } + + /* draw intermediate stacks as quad strips */ + for (i = imin; i < imax; i++) { + rho = i * drho; + glBegin(GL_QUAD_STRIP); + s = 0.0; + for (j = 0; j <= slices; j++) { + theta = (j == slices) ? 0.0 : j * dtheta; + x = -sin(theta) * sin(rho); + y = cos(theta) * sin(rho); + z = nsign * cos(rho); + TXTR_COORD(s, t); + glVertex3f(x * radius, y * radius, z * radius); + x = -sin(theta) * sin(rho + drho); + y = cos(theta) * sin(rho + drho); + z = nsign * cos(rho + drho); + TXTR_COORD(s, t - dt); + s += ds; + glVertex3f(x * radius, y * radius, z * radius); + } + glEnd(); + t -= dt; + } + + if (!qobj->TextureFlag) { + /* draw -Z end as a triangle fan */ + glBegin(GL_TRIANGLE_FAN); + glVertex3f(0.0, 0.0, -radius * nsign); + rho = M_PI - drho; + s = 1.0; + t = dt; + for (j = slices; j >= 0; j--) { + theta = (j == slices) ? 0.0 : j * dtheta; + x = -sin(theta) * sin(rho); + y = cos(theta) * sin(rho); + z = nsign * cos(rho); + s -= ds; + glVertex3f(x * radius, y * radius, z * radius); + } + glEnd(); + } + } + else if (qobj->DrawStyle == GLU_LINE || qobj->DrawStyle == GLU_SILHOUETTE) { + /* draw stack lines */ + for (i = 1; i < stacks; i++) { /* stack line at i==stacks-1 was missing here */ + rho = i * drho; + glBegin(GL_LINE_LOOP); + for (j = 0; j < slices; j++) { + theta = j * dtheta; + x = cos(theta) * sin(rho); + y = sin(theta) * sin(rho); + z = cos(rho); + glVertex3f(x * radius, y * radius, z * radius); + } + glEnd(); + } + /* draw slice lines */ + for (j = 0; j < slices; j++) { + theta = j * dtheta; + glBegin(GL_LINE_STRIP); + for (i = 0; i <= stacks; i++) { + rho = i * drho; + x = cos(theta) * sin(rho); + y = sin(theta) * sin(rho); + z = cos(rho); + glVertex3f(x * radius, y * radius, z * radius); + } + glEnd(); + } + } + else if (qobj->DrawStyle == GLU_POINT) { + /* top and bottom-most points */ + glBegin(GL_POINTS); + glVertex3f(0.0, 0.0, radius); + glVertex3f(0.0, 0.0, -radius); + + /* loop over stacks */ + for (i = 1; i < stacks - 1; i++) { + rho = i * drho; + for (j = 0; j < slices; j++) { + theta = j * dtheta; + x = cos(theta) * sin(rho); + y = sin(theta) * sin(rho); + z = cos(rho); + glVertex3f(x * radius, y * radius, z * radius); + } + } + glEnd(); + } + +} + + + +void GLAPIENTRY +gluDisk(GLUquadricObj * qobj, + GLdouble innerRadius, GLdouble outerRadius, GLint slices, GLint loops) +{ + GLfloat da, dr; +#if 0 + GLdouble a, da; + GLfloat r, dr; + GLfloat x, y; + GLfloat r1, r2, dtc; + GLint s, l; +#endif + + + da = 2.0 * M_PI / slices; + dr = (outerRadius - innerRadius) / (GLfloat) loops; + + switch (qobj->DrawStyle) { + case GLU_FILL: + { + /* texture of a gluDisk is a cut out of the texture unit square + * x, y in [-outerRadius, +outerRadius]; s, t in [0, 1] + * (linear mapping) + */ + GLfloat dtc = 2.0f * outerRadius; + GLfloat sa, ca; + GLfloat r1 = innerRadius; + GLint l; + for (l = 0; l < loops; l++) { + GLfloat r2 = r1 + dr; + if (qobj->Orientation == GLU_OUTSIDE) { + GLint s; + glBegin(GL_QUAD_STRIP); + for (s = 0; s <= slices; s++) { + GLfloat a; + if (s == slices) + a = 0.0; + else + a = s * da; + sa = sin(a); + ca = cos(a); + TXTR_COORD(0.5 + sa * r2 / dtc, 0.5 + ca * r2 / dtc); + glVertex2f(r2 * sa, r2 * ca); + TXTR_COORD(0.5 + sa * r1 / dtc, 0.5 + ca * r1 / dtc); + glVertex2f(r1 * sa, r1 * ca); + } + glEnd(); + } + else { + GLint s; + glBegin(GL_QUAD_STRIP); + for (s = slices; s >= 0; s--) { + GLfloat a; + if (s == slices) + a = 0.0; + else + a = s * da; + sa = sin(a); + ca = cos(a); + TXTR_COORD(0.5 - sa * r2 / dtc, 0.5 + ca * r2 / dtc); + glVertex2f(r2 * sa, r2 * ca); + TXTR_COORD(0.5 - sa * r1 / dtc, 0.5 + ca * r1 / dtc); + glVertex2f(r1 * sa, r1 * ca); + } + glEnd(); + } + r1 = r2; + } + break; + } + case GLU_LINE: + { + GLint l, s; + /* draw loops */ + for (l = 0; l <= loops; l++) { + GLfloat r = innerRadius + l * dr; + glBegin(GL_LINE_LOOP); + for (s = 0; s < slices; s++) { + GLfloat a = s * da; + glVertex2f(r * sin(a), r * cos(a)); + } + glEnd(); + } + /* draw spokes */ + for (s = 0; s < slices; s++) { + GLfloat a = s * da; + GLfloat x = sin(a); + GLfloat y = cos(a); + glBegin(GL_LINE_STRIP); + for (l = 0; l <= loops; l++) { + GLfloat r = innerRadius + l * dr; + glVertex2f(r * x, r * y); + } + glEnd(); + } + break; + } + case GLU_POINT: + { + GLint s; + glBegin(GL_POINTS); + for (s = 0; s < slices; s++) { + GLfloat a = s * da; + GLfloat x = sin(a); + GLfloat y = cos(a); + GLint l; + for (l = 0; l <= loops; l++) { + GLfloat r = innerRadius * l * dr; + glVertex2f(r * x, r * y); + } + } + glEnd(); + break; + } + case GLU_SILHOUETTE: + { + if (innerRadius != 0.0) { + GLfloat a; + glBegin(GL_LINE_LOOP); + for (a = 0.0; a < 2.0 * M_PI; a += da) { + GLfloat x = innerRadius * sin(a); + GLfloat y = innerRadius * cos(a); + glVertex2f(x, y); + } + glEnd(); + } + { + GLfloat a; + glBegin(GL_LINE_LOOP); + for (a = 0; a < 2.0 * M_PI; a += da) { + GLfloat x = outerRadius * sin(a); + GLfloat y = outerRadius * cos(a); + glVertex2f(x, y); + } + glEnd(); + } + break; + } + default: + abort(); + } +} + + + +void GLAPIENTRY +gluPartialDisk(GLUquadricObj * qobj, GLdouble innerRadius, + GLdouble outerRadius, GLint slices, GLint loops, + GLdouble startAngle, GLdouble sweepAngle) +{ + if (qobj->DrawStyle == GLU_POINT) { + GLint loop, slice; + GLdouble radius, delta_radius; + GLdouble angle, delta_angle; + delta_radius = (outerRadius - innerRadius) / (loops - 1); + delta_angle = DEG_TO_RAD((sweepAngle) / (slices - 1)); + glBegin(GL_POINTS); + radius = innerRadius; + for (loop = 0; loop < loops; loop++) { + angle = DEG_TO_RAD(startAngle); + for (slice = 0; slice < slices; slice++) { + glVertex2f(radius * sin(angle), radius * cos(angle)); + angle += delta_angle; + } + radius += delta_radius; + } + glEnd(); + } + else if (qobj->DrawStyle == GLU_LINE) { + GLint loop, slice; + GLdouble radius, delta_radius; + GLdouble angle, delta_angle; + delta_radius = (outerRadius - innerRadius) / loops; + delta_angle = DEG_TO_RAD(sweepAngle / slices); + /* draw rings */ + radius = innerRadius; + for (loop = 0; loop < loops; loop++) { + angle = DEG_TO_RAD(startAngle); + glBegin(GL_LINE_STRIP); + for (slice = 0; slice <= slices; slice++) { + glVertex2f(radius * sin(angle), radius * cos(angle)); + angle += delta_angle; + } + glEnd(); + radius += delta_radius; + } + /* draw spokes */ + angle = DEG_TO_RAD(startAngle); + for (slice = 0; slice <= slices; slice++) { + radius = innerRadius; + glBegin(GL_LINE_STRIP); + for (loop = 0; loop < loops; loop++) { + glVertex2f(radius * sin(angle), radius * cos(angle)); + radius += delta_radius; + } + glEnd(); + angle += delta_angle; + } + } + else if (qobj->DrawStyle == GLU_SILHOUETTE) { + GLint slice; + GLdouble angle, delta_angle; + delta_angle = DEG_TO_RAD(sweepAngle / slices); + /* draw outer ring */ + glBegin(GL_LINE_STRIP); + angle = DEG_TO_RAD(startAngle); + for (slice = 0; slice <= slices; slice++) { + glVertex2f(outerRadius * sin(angle), outerRadius * cos(angle)); + angle += delta_angle; + } + glEnd(); + /* draw inner ring */ + if (innerRadius > 0.0) { + glBegin(GL_LINE_STRIP); + angle = DEG_TO_RAD(startAngle); + for (slice = 0; slice < slices; slice++) { + glVertex2f(innerRadius * sin(angle), innerRadius * cos(angle)); + angle += delta_angle; + } + glEnd(); + } + /* draw spokes */ + if (sweepAngle < 360.0) { + GLdouble stopAngle = startAngle + sweepAngle; + glBegin(GL_LINES); + glVertex2f(innerRadius * SIND(startAngle), + innerRadius * COSD(startAngle)); + glVertex2f(outerRadius * SIND(startAngle), + outerRadius * COSD(startAngle)); + glVertex2f(innerRadius * SIND(stopAngle), + innerRadius * COSD(stopAngle)); + glVertex2f(outerRadius * SIND(stopAngle), + outerRadius * COSD(stopAngle)); + glEnd(); + } + } + else if (qobj->DrawStyle == GLU_FILL) { + GLint loop, slice; + GLdouble radius, delta_radius; + GLdouble angle, delta_angle; + delta_radius = (outerRadius - innerRadius) / loops; + delta_angle = DEG_TO_RAD(sweepAngle / slices); + radius = innerRadius; + for (loop = 0; loop < loops; loop++) { + glBegin(GL_QUAD_STRIP); + angle = DEG_TO_RAD(startAngle); + for (slice = 0; slice <= slices; slice++) { + if (qobj->Orientation == GLU_OUTSIDE) { + glVertex2f((radius + delta_radius) * sin(angle), + (radius + delta_radius) * cos(angle)); + glVertex2f(radius * sin(angle), radius * cos(angle)); + } + else { + glVertex2f(radius * sin(angle), radius * cos(angle)); + glVertex2f((radius + delta_radius) * sin(angle), + (radius + delta_radius) * cos(angle)); + } + angle += delta_angle; + } + glEnd(); + radius += delta_radius; + } + } +} diff --git a/src/glu/mini/tess.c b/src/glu/mini/tess.c new file mode 100644 index 00000000000..1a384239be0 --- /dev/null +++ b/src/glu/mini/tess.c @@ -0,0 +1,328 @@ +/* $Id: tess.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * This file is part of the polygon tesselation code contributed by + * Bogdan Sikorski + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <math.h> +#include <stdlib.h> +#include "tess.h" +#endif + + +/* + * This is ugly, but seems the easiest way to do things to make the + * code work under YellowBox for Windows + */ +#if defined(OPENSTEP) && defined(CALLBACK) +#undef CALLBACK +#define CALLBACK +#endif + + +static void delete_contours(GLUtriangulatorObj *); + +#ifdef __CYGWIN32__ +#define _CALLBACK +#else +#define _CALLBACK GLCALLBACK +#endif + + +static void +init_callbacks(tess_callbacks * callbacks) +{ + callbacks->begin = (void (_CALLBACK *) (GLenum)) 0; + callbacks->edgeFlag = (void (_CALLBACK *) (GLboolean)) 0; + callbacks->vertex = (void (_CALLBACK *) (void *)) 0; + callbacks->end = (void (_CALLBACK *) (void)) 0; + callbacks->error = (void (_CALLBACK *) (GLenum)) 0; +} + +void +tess_call_user_error(GLUtriangulatorObj * tobj, GLenum gluerr) +{ + if (tobj->error == GLU_NO_ERROR) + tobj->error = gluerr; + if (tobj->callbacks.error != NULL) + (tobj->callbacks.error) (gluerr); +} + +GLUtriangulatorObj *GLAPIENTRY +gluNewTess(void) +{ + GLUtriangulatorObj *tobj; + + if ((tobj = (GLUtriangulatorObj *) + malloc(sizeof(struct GLUtesselator))) == NULL) + return NULL; + tobj->contours = tobj->last_contour = NULL; + init_callbacks(&tobj->callbacks); + tobj->error = GLU_NO_ERROR; + tobj->current_polygon = NULL; + tobj->contour_cnt = 0; + return tobj; +} + + +void GLAPIENTRY +gluTessCallback(GLUtriangulatorObj * tobj, GLenum which, + void (GLCALLBACK * fn) ()) +{ + switch (which) { + case GLU_BEGIN: + tobj->callbacks.begin = (void (_CALLBACK *) (GLenum)) fn; + break; + case GLU_EDGE_FLAG: + tobj->callbacks.edgeFlag = (void (_CALLBACK *) (GLboolean)) fn; + break; + case GLU_VERTEX: + tobj->callbacks.vertex = (void (_CALLBACK *) (void *)) fn; + break; + case GLU_END: + tobj->callbacks.end = (void (_CALLBACK *) (void)) fn; + break; + case GLU_ERROR: + tobj->callbacks.error = (void (_CALLBACK *) (GLenum)) fn; + break; + default: + tobj->error = GLU_INVALID_ENUM; + break; + } +} + + + +void GLAPIENTRY +gluDeleteTess(GLUtriangulatorObj * tobj) +{ + if (tobj->error == GLU_NO_ERROR && tobj->contour_cnt) + /* was gluEndPolygon called? */ + tess_call_user_error(tobj, GLU_TESS_ERROR1); + /* delete all internal structures */ + delete_contours(tobj); + free(tobj); +} + + +void GLAPIENTRY +gluBeginPolygon(GLUtriangulatorObj * tobj) +{ +/* + if(tobj->error!=GLU_NO_ERROR) + return; +*/ + tobj->error = GLU_NO_ERROR; + if (tobj->current_polygon != NULL) { + /* gluEndPolygon was not called */ + tess_call_user_error(tobj, GLU_TESS_ERROR1); + /* delete all internal structures */ + delete_contours(tobj); + } + else { + if ((tobj->current_polygon = + (tess_polygon *) malloc(sizeof(tess_polygon))) == NULL) { + tess_call_user_error(tobj, GLU_OUT_OF_MEMORY); + return; + } + tobj->current_polygon->vertex_cnt = 0; + tobj->current_polygon->vertices = + tobj->current_polygon->last_vertex = NULL; + } +} + + +void GLAPIENTRY +gluEndPolygon(GLUtriangulatorObj * tobj) +{ + /*tess_contour *contour_ptr; */ + + /* there was an error */ + if (tobj->error != GLU_NO_ERROR) + goto end; + + /* check if gluBeginPolygon was called */ + if (tobj->current_polygon == NULL) { + tess_call_user_error(tobj, GLU_TESS_ERROR2); + return; + } + tess_test_polygon(tobj); + /* there was an error */ + if (tobj->error != GLU_NO_ERROR) + goto end; + + /* any real contours? */ + if (tobj->contour_cnt == 0) { + /* delete all internal structures */ + delete_contours(tobj); + return; + } + tess_find_contour_hierarchies(tobj); + /* there was an error */ + if (tobj->error != GLU_NO_ERROR) + goto end; + + tess_handle_holes(tobj); + /* there was an error */ + if (tobj->error != GLU_NO_ERROR) + goto end; + + /* if no callbacks, nothing to do */ + if (tobj->callbacks.begin != NULL && tobj->callbacks.vertex != NULL && + tobj->callbacks.end != NULL) { + if (tobj->callbacks.edgeFlag == NULL) + tess_tesselate(tobj); + else + tess_tesselate_with_edge_flag(tobj); + } + + end: + /* delete all internal structures */ + delete_contours(tobj); +} + + +void GLAPIENTRY +gluNextContour(GLUtriangulatorObj * tobj, GLenum type) +{ + if (tobj->error != GLU_NO_ERROR) + return; + if (tobj->current_polygon == NULL) { + tess_call_user_error(tobj, GLU_TESS_ERROR2); + return; + } + /* first contour? */ + if (tobj->current_polygon->vertex_cnt) + tess_test_polygon(tobj); +} + + +void GLAPIENTRY +gluTessVertex(GLUtriangulatorObj * tobj, GLdouble v[3], void *data) +{ + tess_polygon *polygon = tobj->current_polygon; + tess_vertex *last_vertex_ptr; + + if (tobj->error != GLU_NO_ERROR) + return; + if (polygon == NULL) { + tess_call_user_error(tobj, GLU_TESS_ERROR2); + return; + } + last_vertex_ptr = polygon->last_vertex; + if (last_vertex_ptr == NULL) { + if ((last_vertex_ptr = (tess_vertex *) + malloc(sizeof(tess_vertex))) == NULL) { + tess_call_user_error(tobj, GLU_OUT_OF_MEMORY); + return; + } + polygon->vertices = last_vertex_ptr; + polygon->last_vertex = last_vertex_ptr; + last_vertex_ptr->data = data; + last_vertex_ptr->location[0] = v[0]; + last_vertex_ptr->location[1] = v[1]; + last_vertex_ptr->location[2] = v[2]; + last_vertex_ptr->next = NULL; + last_vertex_ptr->previous = NULL; + ++(polygon->vertex_cnt); + } + else { + tess_vertex *vertex_ptr; + + /* same point twice? */ + if (fabs(last_vertex_ptr->location[0] - v[0]) < EPSILON && + fabs(last_vertex_ptr->location[1] - v[1]) < EPSILON && + fabs(last_vertex_ptr->location[2] - v[2]) < EPSILON) { + tess_call_user_error(tobj, GLU_TESS_ERROR6); + return; + } + if ((vertex_ptr = (tess_vertex *) + malloc(sizeof(tess_vertex))) == NULL) { + tess_call_user_error(tobj, GLU_OUT_OF_MEMORY); + return; + } + vertex_ptr->data = data; + vertex_ptr->location[0] = v[0]; + vertex_ptr->location[1] = v[1]; + vertex_ptr->location[2] = v[2]; + vertex_ptr->next = NULL; + vertex_ptr->previous = last_vertex_ptr; + ++(polygon->vertex_cnt); + last_vertex_ptr->next = vertex_ptr; + polygon->last_vertex = vertex_ptr; + } +} + + +static void +delete_contours(GLUtriangulatorObj * tobj) +{ + tess_polygon *polygon = tobj->current_polygon; + tess_contour *contour, *contour_tmp; + tess_vertex *vertex, *vertex_tmp; + + /* remove current_polygon list - if exists due to detected error */ + if (polygon != NULL) { + if (polygon->vertices) { + for (vertex = polygon->vertices; vertex != polygon->last_vertex;) { + vertex_tmp = vertex->next; + free(vertex); + vertex = vertex_tmp; + } + free(vertex); + } + free(polygon); + tobj->current_polygon = NULL; + } + /* remove all contour data */ + for (contour = tobj->contours; contour != NULL;) { + for (vertex = contour->vertices; vertex != contour->last_vertex;) { + vertex_tmp = vertex->next; + free(vertex); + vertex = vertex_tmp; + } + free(vertex); + contour_tmp = contour->next; + free(contour); + contour = contour_tmp; + } + tobj->contours = tobj->last_contour = NULL; + tobj->contour_cnt = 0; +} + + +void GLAPIENTRY +gluTessNormal(GLUtesselator *tess, GLdouble valueX, GLdouble valueY, GLdouble valueZ) +{ + /* dummy function */ + (void) tess; + (void) valueX; + (void) valueY; + (void) valueZ; +} diff --git a/src/glu/mini/tess.h b/src/glu/mini/tess.h new file mode 100644 index 00000000000..908e20972cf --- /dev/null +++ b/src/glu/mini/tess.h @@ -0,0 +1,108 @@ +/* $Id: tess.h,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * This file is part of the polygon tesselation code contributed by + * Bogdan Sikorski + */ + + +#ifndef TESS_H +#define TESS_H + + +#include "gluP.h" + +#define EPSILON 1e-06 /* epsilon for double precision compares */ + +typedef enum +{ + OXY, + OYZ, + OXZ +} +projection_type; + +typedef struct callbacks_str +{ + void (GLCALLBACK * begin) (GLenum mode); + void (GLCALLBACK * edgeFlag) (GLboolean flag); + void (GLCALLBACK * vertex) (GLvoid * v); + void (GLCALLBACK * end) (void); + void (GLCALLBACK * error) (GLenum err); +} +tess_callbacks; + +typedef struct vertex_str +{ + void *data; + GLdouble location[3]; + GLdouble x, y; + GLboolean edge_flag; + struct vertex_str *shadow_vertex; + struct vertex_str *next, *previous; +} +tess_vertex; + +typedef struct contour_str +{ + GLenum type; + GLuint vertex_cnt; + GLdouble area; + GLenum orientation; + struct vertex_str *vertices, *last_vertex; + struct contour_str *next, *previous; +} +tess_contour; + +typedef struct polygon_str +{ + GLuint vertex_cnt; + GLdouble A, B, C, D; + GLdouble area; + GLenum orientation; + struct vertex_str *vertices, *last_vertex; +} +tess_polygon; + +struct GLUtesselator +{ + tess_contour *contours, *last_contour; + GLuint contour_cnt; + tess_callbacks callbacks; + tess_polygon *current_polygon; + GLenum error; + GLdouble A, B, C, D; + projection_type projection; +}; + + +extern void tess_call_user_error(GLUtriangulatorObj *, GLenum); +extern void tess_test_polygon(GLUtriangulatorObj *); +extern void tess_find_contour_hierarchies(GLUtriangulatorObj *); +extern void tess_handle_holes(GLUtriangulatorObj *); +extern void tess_tesselate(GLUtriangulatorObj *); +extern void tess_tesselate_with_edge_flag(GLUtriangulatorObj *); + + +#endif diff --git a/src/glu/mini/tesselat.c b/src/glu/mini/tesselat.c new file mode 100644 index 00000000000..a1102e6e5a6 --- /dev/null +++ b/src/glu/mini/tesselat.c @@ -0,0 +1,407 @@ +/* $Id: tesselat.c,v 1.2 2003/08/22 20:11:43 brianp Exp $ */ + +/* + * Mesa 3-D graphics library + * Version: 3.3 + * Copyright (C) 1995-2000 Brian Paul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +/* + * This file is part of the polygon tesselation code contributed by + * Bogdan Sikorski + */ + + +#ifdef PC_HEADER +#include "all.h" +#else +#include <stdlib.h> +#include <math.h> +#include "tess.h" +#endif + + + +static GLboolean edge_flag; + +static void emit_triangle(GLUtriangulatorObj *, tess_vertex *, + tess_vertex *, tess_vertex *); + +static void emit_triangle_with_edge_flag(GLUtriangulatorObj *, + tess_vertex *, GLboolean, + tess_vertex *, GLboolean, + tess_vertex *, GLboolean); + +static GLdouble +twice_the_triangle_area(tess_vertex * va, tess_vertex * vb, tess_vertex * vc) +{ + return (vb->x - va->x) * (vc->y - va->y) - (vb->y - va->y) * (vc->x - + va->x); +} + +static GLboolean +left(GLdouble A, GLdouble B, GLdouble C, GLdouble x, GLdouble y) +{ + if (A * x + B * y + C > -EPSILON) + return GL_TRUE; + else + return GL_FALSE; +} + +static GLboolean +right(GLdouble A, GLdouble B, GLdouble C, GLdouble x, GLdouble y) +{ + if (A * x + B * y + C < EPSILON) + return GL_TRUE; + else + return GL_FALSE; +} + +static GLint +convex_ccw(tess_vertex * va, + tess_vertex * vb, tess_vertex * vc, GLUtriangulatorObj * tobj) +{ + GLdouble d; + + d = twice_the_triangle_area(va, vb, vc); + + if (d > EPSILON) { + return 1; + } + else if (d < -EPSILON) { + return 0; + } + else { + return -1; + } +} + +static GLint +convex_cw(tess_vertex * va, + tess_vertex * vb, tess_vertex * vc, GLUtriangulatorObj * tobj) +{ + GLdouble d; + + d = twice_the_triangle_area(va, vb, vc); + + if (d < -EPSILON) { + return 1; + } + else if (d > EPSILON) { + return 0; + } + else { + return -1; + } +} + +static GLboolean +diagonal_ccw(tess_vertex * va, + tess_vertex * vb, + GLUtriangulatorObj * tobj, tess_contour * contour) +{ + tess_vertex *vc = va->next, *vertex, *shadow_vertex; + struct + { + GLdouble A, B, C; + } + ac, cb, ba; + GLdouble x, y; + + GLint res = convex_ccw(va, vc, vb, tobj); + if (res == 0) + return GL_FALSE; + if (res == -1) + return GL_TRUE; + + ba.A = vb->y - va->y; + ba.B = va->x - vb->x; + ba.C = -ba.A * va->x - ba.B * va->y; + ac.A = va->y - vc->y; + ac.B = vc->x - va->x; + ac.C = -ac.A * vc->x - ac.B * vc->y; + cb.A = vc->y - vb->y; + cb.B = vb->x - vc->x; + cb.C = -cb.A * vb->x - cb.B * vb->y; + for (vertex = vb->next; vertex != va; vertex = vertex->next) { + shadow_vertex = vertex->shadow_vertex; + if (shadow_vertex != NULL && + (shadow_vertex == va || shadow_vertex == vb || shadow_vertex == vc)) + continue; + x = vertex->x; + y = vertex->y; + if (left(ba.A, ba.B, ba.C, x, y) && + left(ac.A, ac.B, ac.C, x, y) && left(cb.A, cb.B, cb.C, x, y)) + return GL_FALSE; + } + return GL_TRUE; +} + +static GLboolean +diagonal_cw(tess_vertex * va, + tess_vertex * vb, + GLUtriangulatorObj * tobj, tess_contour * contour) +{ + tess_vertex *vc = va->next, *vertex, *shadow_vertex; + struct + { + GLdouble A, B, C; + } + ac, cb, ba; + GLdouble x, y; + + GLint res = convex_cw(va, vc, vb, tobj); + if (res == 0) + return GL_FALSE; + if (res == -1) + return GL_TRUE; + + ba.A = vb->y - va->y; + ba.B = va->x - vb->x; + ba.C = -ba.A * va->x - ba.B * va->y; + ac.A = va->y - vc->y; + ac.B = vc->x - va->x; + ac.C = -ac.A * vc->x - ac.B * vc->y; + cb.A = vc->y - vb->y; + cb.B = vb->x - vc->x; + cb.C = -cb.A * vb->x - cb.B * vb->y; + for (vertex = vb->next; vertex != va; vertex = vertex->next) { + shadow_vertex = vertex->shadow_vertex; + if (shadow_vertex != NULL && + (shadow_vertex == va || shadow_vertex == vb || shadow_vertex == vc)) + continue; + x = vertex->x; + y = vertex->y; + if (right(ba.A, ba.B, ba.C, x, y) && + right(ac.A, ac.B, ac.C, x, y) && right(cb.A, cb.B, cb.C, x, y)) + return GL_FALSE; + } + return GL_TRUE; +} + +static void +clip_ear(GLUtriangulatorObj * tobj, tess_vertex * v, tess_contour * contour) +{ + emit_triangle(tobj, v->previous, v, v->next); + /* the first in the list */ + if (contour->vertices == v) { + contour->vertices = v->next; + contour->last_vertex->next = v->next; + v->next->previous = contour->last_vertex; + } + else + /* the last ? */ + if (contour->last_vertex == v) { + contour->vertices->previous = v->previous; + v->previous->next = v->next; + contour->last_vertex = v->previous; + } + else { + v->next->previous = v->previous; + v->previous->next = v->next; + } + free(v); + --(contour->vertex_cnt); +} + +static void +clip_ear_with_edge_flag(GLUtriangulatorObj * tobj, + tess_vertex * v, tess_contour * contour) +{ + emit_triangle_with_edge_flag(tobj, v->previous, v->previous->edge_flag, + v, v->edge_flag, v->next, GL_FALSE); + v->previous->edge_flag = GL_FALSE; + /* the first in the list */ + if (contour->vertices == v) { + contour->vertices = v->next; + contour->last_vertex->next = v->next; + v->next->previous = contour->last_vertex; + } + else + /* the last ? */ + if (contour->last_vertex == v) { + contour->vertices->previous = v->previous; + v->previous->next = v->next; + contour->last_vertex = v->previous; + } + else { + v->next->previous = v->previous; + v->previous->next = v->next; + } + free(v); + --(contour->vertex_cnt); +} + +static void +triangulate_ccw(GLUtriangulatorObj * tobj, tess_contour * contour) +{ + tess_vertex *vertex; + GLuint vertex_cnt = contour->vertex_cnt; + + while (vertex_cnt > 3) { + vertex = contour->vertices; + while (diagonal_ccw(vertex, vertex->next->next, tobj, contour) == + GL_FALSE && tobj->error == GLU_NO_ERROR) + vertex = vertex->next; + if (tobj->error != GLU_NO_ERROR) + return; + clip_ear(tobj, vertex->next, contour); + --vertex_cnt; + } +} + +static void +triangulate_cw(GLUtriangulatorObj * tobj, tess_contour * contour) +{ + tess_vertex *vertex; + GLuint vertex_cnt = contour->vertex_cnt; + + while (vertex_cnt > 3) { + vertex = contour->vertices; + while (diagonal_cw(vertex, vertex->next->next, tobj, contour) == + GL_FALSE && tobj->error == GLU_NO_ERROR) + vertex = vertex->next; + if (tobj->error != GLU_NO_ERROR) + return; + clip_ear(tobj, vertex->next, contour); + --vertex_cnt; + } +} + +static void +triangulate_ccw_with_edge_flag(GLUtriangulatorObj * tobj, + tess_contour * contour) +{ + tess_vertex *vertex; + GLuint vertex_cnt = contour->vertex_cnt; + + while (vertex_cnt > 3) { + vertex = contour->vertices; + while (diagonal_ccw(vertex, vertex->next->next, tobj, contour) == + GL_FALSE && tobj->error == GLU_NO_ERROR) + vertex = vertex->next; + if (tobj->error != GLU_NO_ERROR) + return; + clip_ear_with_edge_flag(tobj, vertex->next, contour); + --vertex_cnt; + } +} + +static void +triangulate_cw_with_edge_flag(GLUtriangulatorObj * tobj, + tess_contour * contour) +{ + tess_vertex *vertex; + GLuint vertex_cnt = contour->vertex_cnt; + + while (vertex_cnt > 3) { + vertex = contour->vertices; + while (diagonal_cw(vertex, vertex->next->next, tobj, contour) == + GL_FALSE && tobj->error == GLU_NO_ERROR) + vertex = vertex->next; + if (tobj->error != GLU_NO_ERROR) + return; + clip_ear_with_edge_flag(tobj, vertex->next, contour); + --vertex_cnt; + } +} + +void +tess_tesselate(GLUtriangulatorObj * tobj) +{ + tess_contour *contour; + + for (contour = tobj->contours; contour != NULL; contour = contour->next) { + if (contour->orientation == GLU_CCW) { + triangulate_ccw(tobj, contour); + } + else { + triangulate_cw(tobj, contour); + } + if (tobj->error != GLU_NO_ERROR) + return; + + /* emit the last triangle */ + emit_triangle(tobj, contour->vertices, contour->vertices->next, + contour->vertices->next->next); + } +} + +void +tess_tesselate_with_edge_flag(GLUtriangulatorObj * tobj) +{ + tess_contour *contour; + + edge_flag = GL_TRUE; + /* first callback with edgeFlag set to GL_TRUE */ + (tobj->callbacks.edgeFlag) (GL_TRUE); + + for (contour = tobj->contours; contour != NULL; contour = contour->next) { + if (contour->orientation == GLU_CCW) + triangulate_ccw_with_edge_flag(tobj, contour); + else + triangulate_cw_with_edge_flag(tobj, contour); + if (tobj->error != GLU_NO_ERROR) + return; + /* emit the last triangle */ + emit_triangle_with_edge_flag(tobj, contour->vertices, + contour->vertices->edge_flag, + contour->vertices->next, + contour->vertices->next->edge_flag, + contour->vertices->next->next, + contour->vertices->next->next->edge_flag); + } +} + +static void +emit_triangle(GLUtriangulatorObj * tobj, + tess_vertex * v1, tess_vertex * v2, tess_vertex * v3) +{ + (tobj->callbacks.begin) (GL_TRIANGLES); + (tobj->callbacks.vertex) (v1->data); + (tobj->callbacks.vertex) (v2->data); + (tobj->callbacks.vertex) (v3->data); + (tobj->callbacks.end) (); +} + +static void +emit_triangle_with_edge_flag(GLUtriangulatorObj * tobj, + tess_vertex * v1, + GLboolean edge_flag1, + tess_vertex * v2, + GLboolean edge_flag2, + tess_vertex * v3, GLboolean edge_flag3) +{ + (tobj->callbacks.begin) (GL_TRIANGLES); + if (edge_flag1 != edge_flag) { + edge_flag = (edge_flag == GL_TRUE ? GL_FALSE : GL_TRUE); + (tobj->callbacks.edgeFlag) (edge_flag); + } + (tobj->callbacks.vertex) (v1->data); + if (edge_flag2 != edge_flag) { + edge_flag = (edge_flag == GL_TRUE ? GL_FALSE : GL_TRUE); + (tobj->callbacks.edgeFlag) (edge_flag); + } + (tobj->callbacks.vertex) (v2->data); + if (edge_flag3 != edge_flag) { + edge_flag = (edge_flag == GL_TRUE ? GL_FALSE : GL_TRUE); + (tobj->callbacks.edgeFlag) (edge_flag); + } + (tobj->callbacks.vertex) (v3->data); + (tobj->callbacks.end) (); +} |