JOGL v2.6.0-rc-20250706
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 public boolean isShared;
48 protected boolean validateBuffers = false;
49
50 public abstract GLArrayDataServer createInterleaved(boolean useMappedBuffers, int comps, int dataType, boolean normalized, int initialSize, int vboUsage);
51 public abstract void addInterleavedVertexAndNormalArrays(GLArrayDataServer array, int components);
52 public abstract void draw(GL gl, float x, float y, float angle);
53
54 private GLArrayDataServer createInterleavedClone(final GLArrayDataServer ads) {
55 final GLArrayDataServer n = new GLArrayDataServer(ads);
57 return n;
58 }
59
60 private void init(final GL gl, final GLArrayDataServer array) {
61 array.enableBuffer(gl, true);
62 array.enableBuffer(gl, false);
63 }
64
65 public void destroy(final GL gl) {
66 if(!isShared) {
67 // could be already destroyed by shared configuration
68 if(null != frontFace) {
70 }
71 if(null != frontSide) {
73 }
74 if(null != backFace) {
75 backFace.destroy(gl);
76 }
77 if(null != backSide) {
78 backSide.destroy(gl);
79 }
80 if(null != outwardFace) {
82 }
83 if(null != insideRadiusCyl) {
85 }
86 }
87 frontFace=null;
88 frontSide=null;
89 backFace=null;
90 backSide=null;
91 outwardFace=null;
92 insideRadiusCyl=null;
93 isShared = false;
94 }
95
96 public GearsObject ( final GearsObject shared ) {
97 isShared = true;
99 frontFace = createInterleavedClone(shared.frontFace);
101 backFace = createInterleavedClone(shared.backFace);
103 frontSide = createInterleavedClone(shared.frontSide);
105 backSide= createInterleavedClone(shared.backSide);
107 outwardFace = createInterleavedClone(shared.outwardFace);
109 insideRadiusCyl = createInterleavedClone(shared.insideRadiusCyl);
111 gearColor = shared.gearColor;
112 }
113
114 public GearsObject (
115 final GL gl,
116 final boolean useMappedBuffers,
117 final FloatBuffer gearColor,
118 final float inner_radius,
119 final float outer_radius,
120 final float width, final int teeth, final float tooth_depth, final boolean validateBuffers)
121 {
122 final float dz = width * 0.5f;
123 int i;
124 float r0, r1, r2;
125 float angle, da;
126 float u, v, len;
127 final float s[] = new float[5];
128 final float c[] = new float[5];
129 final float normal[] = new float[3];
130 // final int tris_per_tooth = 32;
131
132 this.validateBuffers = validateBuffers;
133 this.isShared = false;
134 this.gearColor = gearColor;
135
136 r0 = inner_radius;
137 r1 = outer_radius - tooth_depth / 2.0f;
138 r2 = outer_radius + tooth_depth / 2.0f;
139
140 da = 2.0f * (float) Math.PI / teeth / 4.0f;
141
142 s[4] = 0; // sin(0f)
143 c[4] = 1; // cos(0f)
144
145 final int vboUsage = GL.GL_STATIC_DRAW;
146
147 frontFace = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 4*teeth+2, vboUsage);
149 backFace = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 4*teeth+2, vboUsage);
151 frontSide = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 6*teeth, vboUsage);
153 backSide = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 6*teeth, vboUsage);
155 outwardFace = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 4*4*teeth+2, vboUsage);
157 insideRadiusCyl = createInterleaved(useMappedBuffers, 6, GL.GL_FLOAT, false, 2*teeth+2, vboUsage);
159
160 if( useMappedBuffers ) {
167 }
168
169 for (i = 0; i < teeth; i++) {
170 angle = i * 2.0f * M_PI / teeth;
171 sincos(angle + da * 0f, s, 0, c, 0);
172 sincos(angle + da * 1f, s, 1, c, 1);
173 sincos(angle + da * 2f, s, 2, c, 2);
174 sincos(angle + da * 3f, s, 3, c, 3);
175
176 /* front */
177 normal[0] = 0.0f;
178 normal[1] = 0.0f;
179 normal[2] = 1.0f;
180
181 /* front face - GL.GL_TRIANGLE_STRIP */
182 vert(frontFace, r0 * c[0], r0 * s[0], dz, normal);
183 vert(frontFace, r1 * c[0], r1 * s[0], dz, normal);
184 vert(frontFace, r0 * c[0], r0 * s[0], dz, normal);
185 vert(frontFace, r1 * c[3], r1 * s[3], dz, normal);
186
187 /* front sides of teeth - GL.GL_TRIANGLES */
188 vert(frontSide, r1 * c[0], r1 * s[0], dz, normal);
189 vert(frontSide, r2 * c[1], r2 * s[1], dz, normal);
190 vert(frontSide, r2 * c[2], r2 * s[2], dz, normal);
191 vert(frontSide, r1 * c[0], r1 * s[0], dz, normal);
192 vert(frontSide, r2 * c[2], r2 * s[2], dz, normal);
193 vert(frontSide, r1 * c[3], r1 * s[3], dz, normal);
194
195 /* back */
196 normal[0] = 0.0f;
197 normal[1] = 0.0f;
198 normal[2] = -1.0f;
199
200 /* back face - GL.GL_TRIANGLE_STRIP */
201 vert(backFace, r1 * c[0], r1 * s[0], -dz, normal);
202 vert(backFace, r0 * c[0], r0 * s[0], -dz, normal);
203 vert(backFace, r1 * c[3], r1 * s[3], -dz, normal);
204 vert(backFace, r0 * c[0], r0 * s[0], -dz, normal);
205
206 /* back sides of teeth - GL.GL_TRIANGLES*/
207 vert(backSide, r1 * c[3], r1 * s[3], -dz, normal);
208 vert(backSide, r2 * c[2], r2 * s[2], -dz, normal);
209 vert(backSide, r2 * c[1], r2 * s[1], -dz, normal);
210 vert(backSide, r1 * c[3], r1 * s[3], -dz, normal);
211 vert(backSide, r2 * c[1], r2 * s[1], -dz, normal);
212 vert(backSide, r1 * c[0], r1 * s[0], -dz, normal);
213
214 /* outward faces of teeth */
215 u = r2 * c[1] - r1 * c[0];
216 v = r2 * s[1] - r1 * s[0];
217 len = (float)Math.sqrt(u * u + v * v);
218 u /= len;
219 v /= len;
220 normal[0] = v;
221 normal[1] = -u;
222 normal[2] = 0.0f;
223
224 vert(outwardFace, r1 * c[0], r1 * s[0], dz, normal);
225 vert(outwardFace, r1 * c[0], r1 * s[0], -dz, normal);
226 vert(outwardFace, r2 * c[1], r2 * s[1], dz, normal);
227 vert(outwardFace, r2 * c[1], r2 * s[1], -dz, normal);
228
229 normal[0] = c[0];
230 normal[1] = s[0];
231 vert(outwardFace, r2 * c[1], r2 * s[1], dz, normal);
232 vert(outwardFace, r2 * c[1], r2 * s[1], -dz, normal);
233 vert(outwardFace, r2 * c[2], r2 * s[2], dz, normal);
234 vert(outwardFace, r2 * c[2], r2 * s[2], -dz, normal);
235
236 normal[0] = ( r1 * s[3] - r2 * s[2] );
237 normal[1] = ( r1 * c[3] - r2 * c[2] ) * -1.0f ;
238 vert(outwardFace, r2 * c[2], r2 * s[2], dz, normal);
239 vert(outwardFace, r2 * c[2], r2 * s[2], -dz, normal);
240 vert(outwardFace, r1 * c[3], r1 * s[3], dz, normal);
241 vert(outwardFace, r1 * c[3], r1 * s[3], -dz, normal);
242
243 normal[0] = c[0];
244 normal[1] = s[0];
245 vert(outwardFace, r1 * c[3], r1 * s[3], dz, normal);
246 vert(outwardFace, r1 * c[3], r1 * s[3], -dz, normal);
247 vert(outwardFace, r1 * c[0], r1 * s[0], dz, normal);
248 vert(outwardFace, r1 * c[0], r1 * s[0], -dz, normal);
249
250 /* inside radius cylinder */
251 normal[0] = c[0] * -1.0f;
252 normal[1] = s[0] * -1.0f;
253 normal[2] = 0.0f;
254 vert(insideRadiusCyl, r0 * c[0], r0 * s[0], -dz, normal);
255 vert(insideRadiusCyl, r0 * c[0], r0 * s[0], dz, normal);
256 }
257 /* finish front face */
258 normal[0] = 0.0f;
259 normal[1] = 0.0f;
260 normal[2] = 1.0f;
261 vert(frontFace, r0 * c[4], r0 * s[4], dz, normal);
262 vert(frontFace, r1 * c[4], r1 * s[4], dz, normal);
263 frontFace.seal(true);
264
265 /* finish back face */
266 normal[2] = -1.0f;
267 vert(backFace, r1 * c[4], r1 * s[4], -dz, normal);
268 vert(backFace, r0 * c[4], r0 * s[4], -dz, normal);
269 backFace.seal(true);
270
271 backSide.seal(true);
272 frontSide.seal(true);
273
274 /* finish outward face */
275 sincos(da * 1f, s, 1, c, 1);
276 u = r2 * c[1] - r1 * c[4];
277 v = r2 * s[1] - r1 * s[4];
278 len = (float)Math.sqrt(u * u + v * v);
279 u /= len;
280 v /= len;
281 normal[0] = v;
282 normal[1] = -u;
283 normal[2] = 0.0f;
284 vert(outwardFace, r1 * c[4], r1 * s[4], dz, normal);
285 vert(outwardFace, r1 * c[4], r1 * s[4], -dz, normal);
286 outwardFace.seal(true);
287
288 /* finish inside radius cylinder */
289 normal[0] = c[4] * -1.0f;
290 normal[1] = s[4] * -1.0f;
291 normal[2] = 0.0f;
292 vert(insideRadiusCyl, r0 * c[4], r0 * s[4], -dz, normal);
293 vert(insideRadiusCyl, r0 * c[4], r0 * s[4], dz, normal);
294 insideRadiusCyl.seal(true);
295
296 if( useMappedBuffers ) {
303 } else {
304 /** Init VBO and data .. */
305 init(gl, frontFace);
306 init(gl, frontSide);
307 init(gl, backFace);
308 init(gl, backSide);
309 init(gl, outwardFace);
310 init(gl, insideRadiusCyl);
311 }
312 }
313
314 @Override
315 public String toString() {
316 final int ffVBO = null != frontFace ? frontFace.getVBOName() : 0;
317 final int fsVBO = null != frontSide ? frontSide.getVBOName() : 0;
318 final int bfVBO = null != backFace ? backFace.getVBOName() : 0;
319 final int bsVBO = null != backSide ? backSide.getVBOName() : 0;
320 return "GearsObj[0x"+Integer.toHexString(hashCode())+", vbo ff "+ffVBO+", fs "+fsVBO+", bf "+bfVBO+", bs "+bsVBO+"]";
321 }
322
323 static void vert(final GLArrayDataServer array, final float x, final float y, final float z, final float n[]) {
324 array.putf(x);
325 array.putf(y);
326 array.putf(z);
327 array.putf(n[0]);
328 array.putf(n[1]);
329 array.putf(n[2]);
330 }
331
332 static void sincos(final float x, final float sin[], final int sinIdx, final float cos[], final int cosIdx) {
333 sin[sinIdx] = (float) Math.sin(x);
334 cos[cosIdx] = (float) Math.cos(x);
335 }
336}
static final FloatBuffer green
GearsObject(final GearsObject shared)
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
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.
final void setInterleavedOffset(final int interleavedOffset)
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_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FLOAT" with expressio...
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