create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / figure3d / MassObjectRenderer.java
1 /*\r
2  ** License Applicability. Except to the extent portions of this file are\r
3  ** made subject to an alternative license as permitted in the SGI Free\r
4  ** Software License B, Version 2.0 (the "License"), the contents of this\r
5  ** file are subject only to the provisions of the License. You may not use\r
6  ** this file except in compliance with the License. You may obtain a copy\r
7  ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600\r
8  ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:\r
9  ** \r
10  ** http://oss.sgi.com/projects/FreeB\r
11  ** \r
12  ** Note that, as provided in the License, the Software is distributed on an\r
13  ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS\r
14  ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND\r
15  ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A\r
16  ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.\r
17  ** \r
18  ** NOTE:  The Original Code (as defined below) has been licensed to Sun\r
19  ** Microsystems, Inc. ("Sun") under the SGI Free Software License B\r
20  ** (Version 1.1), shown above ("SGI License").   Pursuant to Section\r
21  ** 3.2(3) of the SGI License, Sun is distributing the Covered Code to\r
22  ** you under an alternative license ("Alternative License").  This\r
23  ** Alternative License includes all of the provisions of the SGI License\r
24  ** except that Section 2.2 and 11 are omitted.  Any differences between\r
25  ** the Alternative License and the SGI License are offered solely by Sun\r
26  ** and not by SGI.\r
27  **\r
28  ** Original Code. The Original Code is: OpenGL Sample Implementation,\r
29  ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,\r
30  ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.\r
31  ** Copyright in any portions created by third parties is as indicated\r
32  ** elsewhere herein. All Rights Reserved.\r
33  ** \r
34  ** Additional Notice Provisions: The application programming interfaces\r
35  ** established by SGI in conjunction with the Original Code are The\r
36  ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released\r
37  ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version\r
38  ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X\r
39  ** Window System(R) (Version 1.3), released October 19, 1998. This software\r
40  ** was created using the OpenGL(R) version 1.2.1 Sample Implementation\r
41  ** published by SGI, but has not been independently verified as being\r
42  ** compliant with the OpenGL(R) version 1.2.1 Specification.\r
43  **\r
44  ** $Date: 2009-03-04 17:23:34 -0800 (Wed, 04 Mar 2009) $ $Revision: 1856 $\r
45  ** $Header$\r
46  */\r
47 \r
48 /* \r
49  * Copyright (c) 2002-2004 LWJGL Project\r
50  * All rights reserved.\r
51  * \r
52  * Redistribution and use in source and binary forms, with or without\r
53  * modification, are permitted provided that the following conditions are \r
54  * met:\r
55  * \r
56  * * Redistributions of source code must retain the above copyright \r
57  *   notice, this list of conditions and the following disclaimer.\r
58  *\r
59  * * Redistributions in binary form must reproduce the above copyright\r
60  *   notice, this list of conditions and the following disclaimer in the\r
61  *   documentation and/or other materials provided with the distribution.\r
62  *\r
63  * * Neither the name of 'LWJGL' nor the names of \r
64  *   its contributors may be used to endorse or promote products derived \r
65  *   from this software without specific prior written permission.\r
66  * \r
67  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
68  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
69  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
70  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
71  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, \r
72  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, \r
73  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR \r
74  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
75  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING \r
76  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
77  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
78  */\r
79 \r
80 /*\r
81  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.\r
82  * \r
83  * Redistribution and use in source and binary forms, with or without\r
84  * modification, are permitted provided that the following conditions are\r
85  * met:\r
86  * \r
87  * - Redistribution of source code must retain the above copyright\r
88  *   notice, this list of conditions and the following disclaimer.\r
89  * \r
90  * - Redistribution in binary form must reproduce the above copyright\r
91  *   notice, this list of conditions and the following disclaimer in the\r
92  *   documentation and/or other materials provided with the distribution.\r
93  * \r
94  * Neither the name of Sun Microsystems, Inc. or the names of\r
95  * contributors may be used to endorse or promote products derived from\r
96  * this software without specific prior written permission.\r
97  * \r
98  * This software is provided "AS IS," without a warranty of any kind. ALL\r
99  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,\r
100  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A\r
101  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN\r
102  * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR\r
103  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR\r
104  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR\r
105  * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR\r
106  * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE\r
107  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,\r
108  * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF\r
109  * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\r
110  * \r
111  * You acknowledge that this software is not designed or intended for use\r
112  * in the design, construction, operation or maintenance of any nuclear\r
113  * facility.\r
114  */\r
115 package net.sf.openrocket.gui.figure3d;\r
116 \r
117 import javax.media.opengl.GL;\r
118 import javax.media.opengl.GL2;\r
119 \r
120 import net.sf.openrocket.rocketcomponent.MassObject;\r
121 \r
122 public final class MassObjectRenderer {\r
123         private static final boolean textureFlag = true;\r
124 \r
125         private MassObjectRenderer() {\r
126         }\r
127 \r
128         public static final void drawMassObject(final GL2 gl, final MassObject o,\r
129                         final int slices, final int stacks) {\r
130 \r
131                 double da, r, dz;\r
132                 double x, y, z, nz, nsign;\r
133                 int i, j;\r
134 \r
135                 nsign = 1.0f;\r
136 \r
137                 da = 2.0f * PI / slices;\r
138                 dz = o.getLength() / stacks;\r
139 \r
140                 double ds = 1.0f / slices;\r
141                 double dt = 1.0f / stacks;\r
142                 double t = 0.0f;\r
143                 z = 0.0f;\r
144                 for (j = 0; j < stacks; j++) {\r
145                         r = getRadius(o, z);\r
146                         double rNext = getRadius(o, z + dz);\r
147                         if (j == stacks - 1)\r
148                                 rNext = 0;\r
149 \r
150                         if (j == stacks - 1)\r
151                                 rNext = 0;\r
152 \r
153                         // Z component of normal vectors\r
154                         nz = -(rNext - r) / dz;\r
155 \r
156                         double s = 0.0f;\r
157                         glBegin(gl, GL2.GL_QUAD_STRIP);\r
158                         for (i = 0; i <= slices; i++) {\r
159                                 if (i == slices) {\r
160                                         x = sin(0.0f);\r
161                                         y = cos(0.0f);\r
162                                 } else {\r
163                                         x = sin((i * da));\r
164                                         y = cos((i * da));\r
165                                 }\r
166                                 if (nsign == 1.0f) {\r
167                                         normal3d(gl, (x * nsign), (y * nsign), (nz * nsign));\r
168                                         TXTR_COORD(gl, s, t);\r
169                                         glVertex3d(gl, (x * r), (y * r), z);\r
170                                         normal3d(gl, (x * nsign), (y * nsign), (nz * nsign));\r
171                                         TXTR_COORD(gl, s, t + dt);\r
172                                         glVertex3d(gl, (x * rNext), (y * rNext), (z + dz));\r
173                                 } else {\r
174                                         normal3d(gl, x * nsign, y * nsign, nz * nsign);\r
175                                         TXTR_COORD(gl, s, t);\r
176                                         glVertex3d(gl, (x * r), (y * r), z);\r
177                                         normal3d(gl, x * nsign, y * nsign, nz * nsign);\r
178                                         TXTR_COORD(gl, s, t + dt);\r
179                                         glVertex3d(gl, (x * rNext), (y * rNext), (z + dz));\r
180                                 }\r
181                                 s += ds;\r
182                         } // for slices\r
183                         glEnd(gl);\r
184                         // r += dr;\r
185                         t += dt;\r
186                         z += dz;\r
187                 } // for stacks\r
188         }\r
189 \r
190         private static final double getRadius(MassObject o, double z) {\r
191                 double arc = Math.min(o.getLength(), 2 * o.getRadius()) * 0.35f;\r
192                 double r = o.getRadius();\r
193                 if (z == 0 || z == o.getLength())\r
194                         return 0;\r
195                 if (z < arc) {\r
196                         double zz = z - arc;\r
197                         return (r - arc) + Math.sqrt(arc * arc - zz * zz);\r
198                 }\r
199                 if (z > o.getLength() - arc) {\r
200                         double zz = (z - o.getLength() + arc);\r
201                         return (r - arc) + Math.sqrt(arc * arc - zz * zz);\r
202                 }\r
203                 return o.getRadius();\r
204         }\r
205 \r
206         // ----------------------------------------------------------------------\r
207         // Internals only below this point\r
208         //\r
209 \r
210         private static final double PI = Math.PI;\r
211 \r
212         private static final void glBegin(GL gl, int mode) {\r
213                 gl.getGL2().glBegin(mode);\r
214         }\r
215 \r
216         private static final void glEnd(GL gl) {\r
217                 gl.getGL2().glEnd();\r
218         }\r
219 \r
220         private static final void glVertex3d(GL gl, double x, double y, double z) {\r
221                 gl.getGL2().glVertex3d(x, y, z);\r
222         }\r
223 \r
224         private static final void glNormal3d(GL gl, double x, double y, double z) {\r
225                 gl.getGL2().glNormal3d(x, y, z);\r
226         }\r
227 \r
228         private static final void glTexCoord2d(GL gl, double x, double y) {\r
229                 gl.getGL2().glTexCoord2d(x, y);\r
230         }\r
231 \r
232         /**\r
233          * Call glNormal3f after scaling normal to unit length.\r
234          * \r
235          * @param x\r
236          * @param y\r
237          * @param z\r
238          */\r
239         private static final void normal3d(GL gl, double x, double y, double z) {\r
240                 double mag;\r
241 \r
242                 mag = Math.sqrt(x * x + y * y + z * z);\r
243                 if (mag > 0.00001F) {\r
244                         x /= mag;\r
245                         y /= mag;\r
246                         z /= mag;\r
247                 }\r
248                 glNormal3d(gl, x, y, z);\r
249         }\r
250 \r
251         private static final void TXTR_COORD(GL gl, double x, double y) {\r
252                 if (textureFlag)\r
253                         glTexCoord2d(gl, x, y);\r
254         }\r
255 \r
256         private static final double sin(double r) {\r
257                 return Math.sin(r);\r
258         }\r
259 \r
260         private static final double cos(double r) {\r
261                 return Math.cos(r);\r
262         }\r
263 }\r