Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / gui / figure3d / MassObjectRenderer.java
diff --git a/core/src/net/sf/openrocket/gui/figure3d/MassObjectRenderer.java b/core/src/net/sf/openrocket/gui/figure3d/MassObjectRenderer.java
new file mode 100644 (file)
index 0000000..4c1d934
--- /dev/null
@@ -0,0 +1,263 @@
+/*\r
+ ** License Applicability. Except to the extent portions of this file are\r
+ ** made subject to an alternative license as permitted in the SGI Free\r
+ ** Software License B, Version 2.0 (the "License"), the contents of this\r
+ ** file are subject only to the provisions of the License. You may not use\r
+ ** this file except in compliance with the License. You may obtain a copy\r
+ ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600\r
+ ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:\r
+ ** \r
+ ** http://oss.sgi.com/projects/FreeB\r
+ ** \r
+ ** Note that, as provided in the License, the Software is distributed on an\r
+ ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS\r
+ ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND\r
+ ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A\r
+ ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.\r
+ ** \r
+ ** NOTE:  The Original Code (as defined below) has been licensed to Sun\r
+ ** Microsystems, Inc. ("Sun") under the SGI Free Software License B\r
+ ** (Version 1.1), shown above ("SGI License").   Pursuant to Section\r
+ ** 3.2(3) of the SGI License, Sun is distributing the Covered Code to\r
+ ** you under an alternative license ("Alternative License").  This\r
+ ** Alternative License includes all of the provisions of the SGI License\r
+ ** except that Section 2.2 and 11 are omitted.  Any differences between\r
+ ** the Alternative License and the SGI License are offered solely by Sun\r
+ ** and not by SGI.\r
+ **\r
+ ** Original Code. The Original Code is: OpenGL Sample Implementation,\r
+ ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,\r
+ ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.\r
+ ** Copyright in any portions created by third parties is as indicated\r
+ ** elsewhere herein. All Rights Reserved.\r
+ ** \r
+ ** Additional Notice Provisions: The application programming interfaces\r
+ ** established by SGI in conjunction with the Original Code are The\r
+ ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released\r
+ ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version\r
+ ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X\r
+ ** Window System(R) (Version 1.3), released October 19, 1998. This software\r
+ ** was created using the OpenGL(R) version 1.2.1 Sample Implementation\r
+ ** published by SGI, but has not been independently verified as being\r
+ ** compliant with the OpenGL(R) version 1.2.1 Specification.\r
+ **\r
+ ** $Date: 2009-03-04 17:23:34 -0800 (Wed, 04 Mar 2009) $ $Revision: 1856 $\r
+ ** $Header$\r
+ */\r
+\r
+/* \r
+ * Copyright (c) 2002-2004 LWJGL Project\r
+ * All rights reserved.\r
+ * \r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are \r
+ * met:\r
+ * \r
+ * * Redistributions of source code must retain the above copyright \r
+ *   notice, this list of conditions and the following disclaimer.\r
+ *\r
+ * * Redistributions in binary form must reproduce the above copyright\r
+ *   notice, this list of conditions and the following disclaimer in the\r
+ *   documentation and/or other materials provided with the distribution.\r
+ *\r
+ * * Neither the name of 'LWJGL' nor the names of \r
+ *   its contributors may be used to endorse or promote products derived \r
+ *   from this software without specific prior written permission.\r
+ * \r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, \r
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, \r
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR \r
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING \r
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+\r
+/*\r
+ * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.\r
+ * \r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are\r
+ * met:\r
+ * \r
+ * - Redistribution of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ * \r
+ * - Redistribution in binary form must reproduce the above copyright\r
+ *   notice, this list of conditions and the following disclaimer in the\r
+ *   documentation and/or other materials provided with the distribution.\r
+ * \r
+ * Neither the name of Sun Microsystems, Inc. or the names of\r
+ * contributors may be used to endorse or promote products derived from\r
+ * this software without specific prior written permission.\r
+ * \r
+ * This software is provided "AS IS," without a warranty of any kind. ALL\r
+ * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,\r
+ * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A\r
+ * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN\r
+ * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR\r
+ * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR\r
+ * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR\r
+ * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR\r
+ * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE\r
+ * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,\r
+ * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF\r
+ * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\r
+ * \r
+ * You acknowledge that this software is not designed or intended for use\r
+ * in the design, construction, operation or maintenance of any nuclear\r
+ * facility.\r
+ */\r
+package net.sf.openrocket.gui.figure3d;\r
+\r
+import javax.media.opengl.GL;\r
+import javax.media.opengl.GL2;\r
+\r
+import net.sf.openrocket.rocketcomponent.MassObject;\r
+\r
+public final class MassObjectRenderer {\r
+       private static final boolean textureFlag = true;\r
+\r
+       private MassObjectRenderer() {\r
+       }\r
+\r
+       public static final void drawMassObject(final GL2 gl, final MassObject o,\r
+                       final int slices, final int stacks) {\r
+\r
+               double da, r, dz;\r
+               double x, y, z, nz, nsign;\r
+               int i, j;\r
+\r
+               nsign = 1.0f;\r
+\r
+               da = 2.0f * PI / slices;\r
+               dz = o.getLength() / stacks;\r
+\r
+               double ds = 1.0f / slices;\r
+               double dt = 1.0f / stacks;\r
+               double t = 0.0f;\r
+               z = 0.0f;\r
+               for (j = 0; j < stacks; j++) {\r
+                       r = getRadius(o, z);\r
+                       double rNext = getRadius(o, z + dz);\r
+                       if (j == stacks - 1)\r
+                               rNext = 0;\r
+\r
+                       if (j == stacks - 1)\r
+                               rNext = 0;\r
+\r
+                       // Z component of normal vectors\r
+                       nz = -(rNext - r) / dz;\r
+\r
+                       double s = 0.0f;\r
+                       glBegin(gl, GL2.GL_QUAD_STRIP);\r
+                       for (i = 0; i <= slices; i++) {\r
+                               if (i == slices) {\r
+                                       x = sin(0.0f);\r
+                                       y = cos(0.0f);\r
+                               } else {\r
+                                       x = sin((i * da));\r
+                                       y = cos((i * da));\r
+                               }\r
+                               if (nsign == 1.0f) {\r
+                                       normal3d(gl, (x * nsign), (y * nsign), (nz * nsign));\r
+                                       TXTR_COORD(gl, s, t);\r
+                                       glVertex3d(gl, (x * r), (y * r), z);\r
+                                       normal3d(gl, (x * nsign), (y * nsign), (nz * nsign));\r
+                                       TXTR_COORD(gl, s, t + dt);\r
+                                       glVertex3d(gl, (x * rNext), (y * rNext), (z + dz));\r
+                               } else {\r
+                                       normal3d(gl, x * nsign, y * nsign, nz * nsign);\r
+                                       TXTR_COORD(gl, s, t);\r
+                                       glVertex3d(gl, (x * r), (y * r), z);\r
+                                       normal3d(gl, x * nsign, y * nsign, nz * nsign);\r
+                                       TXTR_COORD(gl, s, t + dt);\r
+                                       glVertex3d(gl, (x * rNext), (y * rNext), (z + dz));\r
+                               }\r
+                               s += ds;\r
+                       } // for slices\r
+                       glEnd(gl);\r
+                       // r += dr;\r
+                       t += dt;\r
+                       z += dz;\r
+               } // for stacks\r
+       }\r
+\r
+       private static final double getRadius(MassObject o, double z) {\r
+               double arc = Math.min(o.getLength(), 2 * o.getRadius()) * 0.35f;\r
+               double r = o.getRadius();\r
+               if (z == 0 || z == o.getLength())\r
+                       return 0;\r
+               if (z < arc) {\r
+                       double zz = z - arc;\r
+                       return (r - arc) + Math.sqrt(arc * arc - zz * zz);\r
+               }\r
+               if (z > o.getLength() - arc) {\r
+                       double zz = (z - o.getLength() + arc);\r
+                       return (r - arc) + Math.sqrt(arc * arc - zz * zz);\r
+               }\r
+               return o.getRadius();\r
+       }\r
+\r
+       // ----------------------------------------------------------------------\r
+       // Internals only below this point\r
+       //\r
+\r
+       private static final double PI = Math.PI;\r
+\r
+       private static final void glBegin(GL gl, int mode) {\r
+               gl.getGL2().glBegin(mode);\r
+       }\r
+\r
+       private static final void glEnd(GL gl) {\r
+               gl.getGL2().glEnd();\r
+       }\r
+\r
+       private static final void glVertex3d(GL gl, double x, double y, double z) {\r
+               gl.getGL2().glVertex3d(x, y, z);\r
+       }\r
+\r
+       private static final void glNormal3d(GL gl, double x, double y, double z) {\r
+               gl.getGL2().glNormal3d(x, y, z);\r
+       }\r
+\r
+       private static final void glTexCoord2d(GL gl, double x, double y) {\r
+               gl.getGL2().glTexCoord2d(x, y);\r
+       }\r
+\r
+       /**\r
+        * Call glNormal3f after scaling normal to unit length.\r
+        * \r
+        * @param x\r
+        * @param y\r
+        * @param z\r
+        */\r
+       private static final void normal3d(GL gl, double x, double y, double z) {\r
+               double mag;\r
+\r
+               mag = Math.sqrt(x * x + y * y + z * z);\r
+               if (mag > 0.00001F) {\r
+                       x /= mag;\r
+                       y /= mag;\r
+                       z /= mag;\r
+               }\r
+               glNormal3d(gl, x, y, z);\r
+       }\r
+\r
+       private static final void TXTR_COORD(GL gl, double x, double y) {\r
+               if (textureFlag)\r
+                       glTexCoord2d(gl, x, y);\r
+       }\r
+\r
+       private static final double sin(double r) {\r
+               return Math.sin(r);\r
+       }\r
+\r
+       private static final double cos(double r) {\r
+               return Math.cos(r);\r
+       }\r
+}\r