JOGL v2.6.0-rc-20250822
JOGL, High-Performance Graphics Binding for Java™ (public API).
GearsObject.java
Go to the documentation of this file.
1/**
2 * Copyright (C) 2011 JogAmp Community. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21package com.jogamp.opengl.demos;
22
23import java.nio.FloatBuffer;
24
25import com.jogamp.opengl.GL;
26
27import com.jogamp.common.nio.Buffers;
28import com.jogamp.opengl.util.GLArrayDataServer;
29
30/**
31 * GearsObject.java <BR>
32 * @author Brian Paul (converted to Java by Ron Cemer and Sven Gothel) <P>
33 */
34public abstract class GearsObject {
35 public static final FloatBuffer red = Buffers.newDirectFloatBuffer( new float[] { 0.8f, 0.1f, 0.0f, 0.7f } );
36 public static final FloatBuffer green = Buffers.newDirectFloatBuffer( new float[] { 0.0f, 0.8f, 0.2f, 0.7f } );
37 public static final FloatBuffer blue = Buffers.newDirectFloatBuffer( new float[] { 0.2f, 0.2f, 1.0f, 0.7f } );
38 public static final float M_PI = (float)Math.PI;
39
40 public final FloatBuffer gearColor;
47 protected boolean validateBuffers = false;
48
49 public abstract GLArrayDataServer createInterleaved(boolean useMappedBuffers, int comps, int dataType, boolean normalized, int initialSize, int vboUsage);
50 public abstract void addInterleavedVertexAndNormalArrays(GLArrayDataServer array, int components);
51 public abstract void draw(GL gl, float x, float y, float angle);
52
53 public void setColor(final float r, final float g, final float b, final float a) {
54 gearColor.put(0, r).put(1, g).put(2, b).put(3, a);
55 }
56
57 private void init(final GL gl, final GLArrayDataServer array) {
58 array.enableBuffer(gl, true);
59 array.enableBuffer(gl, false);
60 }
61
62 public void destroy(final GL gl) {
63 {
64 // could be already destroyed by shared configuration
65 if(null != frontFace) {
67 }
68 if(null != frontSide) {
70 }
71 if(null != backFace) {
72 backFace.destroy(gl);
73 }
74 if(null != backSide) {
75 backSide.destroy(gl);
76 }
77 if(null != outwardFace) {
79 }
80 if(null != insideRadiusCyl) {
82 }
83 }
84 frontFace=null;
85 frontSide=null;
86 backFace=null;
87 backSide=null;
88 outwardFace=null;
89 insideRadiusCyl=null;
90 }
91
92 public GearsObject (
93 final GL gl,
94 final boolean useMappedBuffers,
95 final FloatBuffer gearColor,
96 final float inner_radius,
97 final float outer_radius,
98 final float width, final int teeth, final float tooth_depth, final boolean validateBuffers)
99 {
100 final float dz = width * 0.5f;
101 int i;
102 float r0, r1, r2;
103 float angle, da;
104 float u, v, len;
105 final float s[] = new float[5];
106 final float c[] = new float[5];
107 final float normal[] = new float[3];
108 // final int tris_per_tooth = 32;
109
110 this.validateBuffers = validateBuffers;
111 this.gearColor = Buffers.newDirectFloatBuffer(4);
112 this.gearColor.put(gearColor).rewind(); gearColor.rewind();
113
114 r0 = inner_radius;
115 r1 = outer_radius - tooth_depth / 2.0f;
116 r2 = outer_radius + tooth_depth / 2.0f;
117
118 da = 2.0f * (float) Math.PI / teeth / 4.0f;
119
120 s[4] = 0; // sin(0f)
121 c[4] = 1; // cos(0f)
122
123 final int vboUsage = GL.GL_STATIC_DRAW;
124
125 frontFace = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 4*teeth+2, vboUsage);
127 backFace = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 4*teeth+2, vboUsage);
129 frontSide = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 6*teeth, vboUsage);
131 backSide = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 6*teeth, vboUsage);
133 outwardFace = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 4*4*teeth+2, vboUsage);
135 insideRadiusCyl = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 2*teeth+2, vboUsage);
137
138 if( useMappedBuffers ) {
145 }
146
147 for (i = 0; i < teeth; i++) {
148 angle = i * 2.0f * M_PI / teeth;
149 sincos(angle + da * 0f, s, 0, c, 0);
150 sincos(angle + da * 1f, s, 1, c, 1);
151 sincos(angle + da * 2f, s, 2, c, 2);
152 sincos(angle + da * 3f, s, 3, c, 3);
153
154 /* front */
155 normal[0] = 0.0f;
156 normal[1] = 0.0f;
157 normal[2] = 1.0f;
158
159 /* front face - GL.GL_TRIANGLE_STRIP */
160 vert(frontFace, r0 * c[0], r0 * s[0], dz, normal);
161 vert(frontFace, r1 * c[0], r1 * s[0], dz, normal);
162 vert(frontFace, r0 * c[0], r0 * s[0], dz, normal);
163 vert(frontFace, r1 * c[3], r1 * s[3], dz, normal);
164
165 /* front sides of teeth - GL.GL_TRIANGLES */
166 vert(frontSide, r1 * c[0], r1 * s[0], dz, normal);
167 vert(frontSide, r2 * c[1], r2 * s[1], dz, normal);
168 vert(frontSide, r2 * c[2], r2 * s[2], dz, normal);
169 vert(frontSide, r1 * c[0], r1 * s[0], dz, normal);
170 vert(frontSide, r2 * c[2], r2 * s[2], dz, normal);
171 vert(frontSide, r1 * c[3], r1 * s[3], dz, normal);
172
173 /* back */
174 normal[0] = 0.0f;
175 normal[1] = 0.0f;
176 normal[2] = -1.0f;
177
178 /* back face - GL.GL_TRIANGLE_STRIP */
179 vert(backFace, r1 * c[0], r1 * s[0], -dz, normal);
180 vert(backFace, r0 * c[0], r0 * s[0], -dz, normal);
181 vert(backFace, r1 * c[3], r1 * s[3], -dz, normal);
182 vert(backFace, r0 * c[0], r0 * s[0], -dz, normal);
183
184 /* back sides of teeth - GL.GL_TRIANGLES*/
185 vert(backSide, r1 * c[3], r1 * s[3], -dz, normal);
186 vert(backSide, r2 * c[2], r2 * s[2], -dz, normal);
187 vert(backSide, r2 * c[1], r2 * s[1], -dz, normal);
188 vert(backSide, r1 * c[3], r1 * s[3], -dz, normal);
189 vert(backSide, r2 * c[1], r2 * s[1], -dz, normal);
190 vert(backSide, r1 * c[0], r1 * s[0], -dz, normal);
191
192 /* outward faces of teeth */
193 u = r2 * c[1] - r1 * c[0];
194 v = r2 * s[1] - r1 * s[0];
195 len = (float)Math.sqrt(u * u + v * v);
196 u /= len;
197 v /= len;
198 normal[0] = v;
199 normal[1] = -u;
200 normal[2] = 0.0f;
201
202 vert(outwardFace, r1 * c[0], r1 * s[0], dz, normal);
203 vert(outwardFace, r1 * c[0], r1 * s[0], -dz, normal);
204 vert(outwardFace, r2 * c[1], r2 * s[1], dz, normal);
205 vert(outwardFace, r2 * c[1], r2 * s[1], -dz, normal);
206
207 normal[0] = c[0];
208 normal[1] = s[0];
209 vert(outwardFace, r2 * c[1], r2 * s[1], dz, normal);
210 vert(outwardFace, r2 * c[1], r2 * s[1], -dz, normal);
211 vert(outwardFace, r2 * c[2], r2 * s[2], dz, normal);
212 vert(outwardFace, r2 * c[2], r2 * s[2], -dz, normal);
213
214 normal[0] = ( r1 * s[3] - r2 * s[2] );
215 normal[1] = ( r1 * c[3] - r2 * c[2] ) * -1.0f ;
216 vert(outwardFace, r2 * c[2], r2 * s[2], dz, normal);
217 vert(outwardFace, r2 * c[2], r2 * s[2], -dz, normal);
218 vert(outwardFace, r1 * c[3], r1 * s[3], dz, normal);
219 vert(outwardFace, r1 * c[3], r1 * s[3], -dz, normal);
220
221 normal[0] = c[0];
222 normal[1] = s[0];
223 vert(outwardFace, r1 * c[3], r1 * s[3], dz, normal);
224 vert(outwardFace, r1 * c[3], r1 * s[3], -dz, normal);
225 vert(outwardFace, r1 * c[0], r1 * s[0], dz, normal);
226 vert(outwardFace, r1 * c[0], r1 * s[0], -dz, normal);
227
228 /* inside radius cylinder */
229 normal[0] = c[0] * -1.0f;
230 normal[1] = s[0] * -1.0f;
231 normal[2] = 0.0f;
232 vert(insideRadiusCyl, r0 * c[0], r0 * s[0], -dz, normal);
233 vert(insideRadiusCyl, r0 * c[0], r0 * s[0], dz, normal);
234 }
235 /* finish front face */
236 normal[0] = 0.0f;
237 normal[1] = 0.0f;
238 normal[2] = 1.0f;
239 vert(frontFace, r0 * c[4], r0 * s[4], dz, normal);
240 vert(frontFace, r1 * c[4], r1 * s[4], dz, normal);
241 frontFace.seal(true);
242
243 /* finish back face */
244 normal[2] = -1.0f;
245 vert(backFace, r1 * c[4], r1 * s[4], -dz, normal);
246 vert(backFace, r0 * c[4], r0 * s[4], -dz, normal);
247 backFace.seal(true);
248
249 backSide.seal(true);
250 frontSide.seal(true);
251
252 /* finish outward face */
253 sincos(da * 1f, s, 1, c, 1);
254 u = r2 * c[1] - r1 * c[4];
255 v = r2 * s[1] - r1 * s[4];
256 len = (float)Math.sqrt(u * u + v * v);
257 u /= len;
258 v /= len;
259 normal[0] = v;
260 normal[1] = -u;
261 normal[2] = 0.0f;
262 vert(outwardFace, r1 * c[4], r1 * s[4], dz, normal);
263 vert(outwardFace, r1 * c[4], r1 * s[4], -dz, normal);
264 outwardFace.seal(true);
265
266 /* finish inside radius cylinder */
267 normal[0] = c[4] * -1.0f;
268 normal[1] = s[4] * -1.0f;
269 normal[2] = 0.0f;
270 vert(insideRadiusCyl, r0 * c[4], r0 * s[4], -dz, normal);
271 vert(insideRadiusCyl, r0 * c[4], r0 * s[4], dz, normal);
272 insideRadiusCyl.seal(true);
273
274 if( useMappedBuffers ) {
281 } else {
282 /** Init VBO and data .. */
283 init(gl, frontFace);
284 init(gl, frontSide);
285 init(gl, backFace);
286 init(gl, backSide);
287 init(gl, outwardFace);
288 init(gl, insideRadiusCyl);
289 }
290 }
291
292 @Override
293 public String toString() {
294 final int ffVBO = null != frontFace ? frontFace.getVBOName() : 0;
295 final int fsVBO = null != frontSide ? frontSide.getVBOName() : 0;
296 final int bfVBO = null != backFace ? backFace.getVBOName() : 0;
297 final int bsVBO = null != backSide ? backSide.getVBOName() : 0;
298 return "GearsObj[0x"+Integer.toHexString(hashCode())+", vbo ff "+ffVBO+", fs "+fsVBO+", bf "+bfVBO+", bs "+bsVBO+"]";
299 }
300
301 static void vert(final GLArrayDataServer array, final float x, final float y, final float z, final float n[]) {
302 array.putf(x);
303 array.putf(y);
304 array.putf(z);
305 array.putf(n[0]);
306 array.putf(n[1]);
307 array.putf(n[2]);
308 }
309
310 static void sincos(final float x, final float sin[], final int sinIdx, final float cos[], final int cosIdx) {
311 sin[sinIdx] = (float) Math.sin(x);
312 cos[cosIdx] = (float) Math.cos(x);
313 }
314}
static final FloatBuffer green
static final FloatBuffer red
abstract void draw(GL gl, float x, float y, float angle)
abstract GLArrayDataServer createInterleaved(boolean useMappedBuffers, int comps, int dataType, boolean normalized, int initialSize, int vboUsage)
abstract void addInterleavedVertexAndNormalArrays(GLArrayDataServer array, int components)
static final FloatBuffer blue
void setColor(final float r, final float g, final float b, final float a)
GearsObject(final GL gl, final boolean useMappedBuffers, final FloatBuffer gearColor, final float inner_radius, final float outer_radius, final float width, final int teeth, final float tooth_depth, final boolean validateBuffers)
void seal(final GL gl, final boolean seal)
Convenience method calling seal(boolean) and enableBuffer(GL, boolean).
void enableBuffer(final GL gl, final boolean enable)
Enables the buffer if enable is true, and transfers the data if required.
GLBufferStorage mapStorage(final GL gl, final int access)
final int getVBOName()
The VBO name or 0 if not a VBO.
static final int GL_STATIC_DRAW
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_STATI...
Definition: GL.java:673
static final int GL_FLOAT
GL_ES_VERSION_2_0, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FLOAT" with expression '0x1406',...
Definition: GL.java:786
static final int GL_WRITE_ONLY
GL_VERSION_1_5, GL_ES_VERSION_3_1, GL_OES_mapbuffer, GL_ARB_vertex_buffer_object Alias for: GL_WRITE_...
Definition: GL.java:670