create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / figure3d / TransitionRenderer.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.Transition;\r
121 \r
122 public final class TransitionRenderer {\r
123         private static final boolean textureFlag = true;\r
124 \r
125         private TransitionRenderer() {\r
126         }\r
127 \r
128         public static final void drawTransition(final GL2 gl, final Transition tr,\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 = (double) tr.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                 r = (double) tr.getForeRadius();\r
145                 for (j = 0; j < stacks; j++) {\r
146                         r = (double) tr.getRadius(z);\r
147                         double rNext = (double) tr.getRadius(z + dz);\r
148 \r
149                         if (j == stacks - 1)\r
150                                 rNext = (double) tr.getRadius(tr.getLength());\r
151 \r
152                         // Z component of normal vectors\r
153                         nz = -(rNext - r) / dz;\r
154 \r
155                         double s = 0.0f;\r
156                         glBegin(gl, GL2.GL_QUAD_STRIP);\r
157                         for (i = 0; i <= slices; i++) {\r
158                                 if (i == slices) {\r
159                                         x = sin(0.0f);\r
160                                         y = cos(0.0f);\r
161                                 } else {\r
162                                         x = sin((i * da));\r
163                                         y = cos((i * da));\r
164                                 }\r
165                                 if (nsign == 1.0f) {\r
166                                         normal3d(gl, (x * nsign), (y * nsign), (nz * nsign));\r
167                                         TXTR_COORD(gl, s, t);\r
168                                         glVertex3d(gl, (x * r), (y * r), z);\r
169                                         normal3d(gl, (x * nsign), (y * nsign), (nz * nsign));\r
170                                         TXTR_COORD(gl, s, t + dt);\r
171                                         glVertex3d(gl, (x * rNext), (y * rNext), (z + dz));\r
172                                 } else {\r
173                                         normal3d(gl, x * nsign, y * nsign, nz * nsign);\r
174                                         TXTR_COORD(gl, s, t);\r
175                                         glVertex3d(gl, (x * r), (y * r), z);\r
176                                         normal3d(gl, x * nsign, y * nsign, nz * nsign);\r
177                                         TXTR_COORD(gl, s, t + dt);\r
178                                         glVertex3d(gl, (x * rNext), (y * rNext), (z + dz));\r
179                                 }\r
180                                 s += ds;\r
181                         } // for slices\r
182                         glEnd(gl);\r
183                         // r += dr;\r
184                         t += dt;\r
185                         z += dz;\r
186                 } // for stacks\r
187 \r
188         }\r
189 \r
190         // ----------------------------------------------------------------------\r
191         // Internals only below this point\r
192         //\r
193 \r
194         private static final double PI = (double) Math.PI;\r
195 \r
196         private static final void glBegin(GL gl, int mode) {\r
197                 gl.getGL2().glBegin(mode);\r
198         }\r
199 \r
200         private static final void glEnd(GL gl) {\r
201                 gl.getGL2().glEnd();\r
202         }\r
203 \r
204         private static final void glVertex3d(GL gl, double x, double y, double z) {\r
205                 gl.getGL2().glVertex3d(x, y, z);\r
206         }\r
207 \r
208         private static final void glNormal3d(GL gl, double x, double y, double z) {\r
209                 gl.getGL2().glNormal3d(x, y, z);\r
210         }\r
211 \r
212         private static final void glTexCoord2d(GL gl, double x, double y) {\r
213                 gl.getGL2().glTexCoord2d(x, y);\r
214         }\r
215 \r
216         /**\r
217          * Call glNormal3f after scaling normal to unit length.\r
218          * \r
219          * @param x\r
220          * @param y\r
221          * @param z\r
222          */\r
223         private static final void normal3d(GL gl, double x, double y, double z) {\r
224                 double mag;\r
225 \r
226                 mag = (double) Math.sqrt(x * x + y * y + z * z);\r
227                 if (mag > 0.00001F) {\r
228                         x /= mag;\r
229                         y /= mag;\r
230                         z /= mag;\r
231                 }\r
232                 glNormal3d(gl, x, y, z);\r
233         }\r
234 \r
235         private static final void TXTR_COORD(GL gl, double x, double y) {\r
236                 if (textureFlag)\r
237                         glTexCoord2d(gl, x, y);\r
238         }\r
239 \r
240         private static final double sin(double r) {\r
241                 return (double) Math.sin(r);\r
242         }\r
243 \r
244         private static final double cos(double r) {\r
245                 return (double) Math.cos(r);\r
246         }\r
247 }\r