21package com.jogamp.opengl.demos;
23import java.nio.FloatBuffer;
25import com.jogamp.opengl.GL;
27import com.jogamp.common.nio.Buffers;
28import com.jogamp.opengl.util.GLArrayDataServer;
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;
51 public abstract void draw(
GL gl,
float x,
float y,
float angle);
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);
94 final boolean useMappedBuffers,
96 final float inner_radius,
97 final float outer_radius,
98 final float width,
final int teeth,
final float tooth_depth,
final boolean validateBuffers)
100 final float dz = width * 0.5f;
105 final float s[] =
new float[5];
106 final float c[] =
new float[5];
107 final float normal[] =
new float[3];
111 this.gearColor = Buffers.newDirectFloatBuffer(4);
115 r1 = outer_radius - tooth_depth / 2.0f;
116 r2 = outer_radius + tooth_depth / 2.0f;
118 da = 2.0f * (float) Math.PI / teeth / 4.0f;
138 if( useMappedBuffers ) {
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
229 normal[0] = c[0] * -1.0f;
230 normal[1] = s[0] * -1.0f;
239 vert(
frontFace, r0 * c[4], r0 * s[4], dz, normal);
240 vert(
frontFace, r1 * c[4], r1 * s[4], dz, normal);
245 vert(
backFace, r1 * c[4], r1 * s[4], -dz, normal);
246 vert(
backFace, r0 * c[4], r0 * s[4], -dz, normal);
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);
262 vert(
outwardFace, r1 * c[4], r1 * s[4], dz, normal);
263 vert(
outwardFace, r1 * c[4], r1 * s[4], -dz, normal);
267 normal[0] = c[4] * -1.0f;
268 normal[1] = s[4] * -1.0f;
274 if( useMappedBuffers ) {
298 return "GearsObj[0x"+Integer.toHexString(hashCode())+
", vbo ff "+ffVBO+
", fs "+fsVBO+
", bf "+bfVBO+
", bs "+bsVBO+
"]";
301 static void vert(
final GLArrayDataServer array,
final float x,
final float y,
final float z,
final float n[]) {
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);
static final FloatBuffer green
void destroy(final GL gl)
static final FloatBuffer red
GLArrayDataServer outwardFace
GLArrayDataServer frontFace
GLArrayDataServer backFace
GLArrayDataServer insideRadiusCyl
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)
GLArrayDataServer backSide
final FloatBuffer gearColor
static final FloatBuffer blue
void setColor(final float r, final float g, final float b, final float a)
GLArrayDataServer frontSide
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.
void unmapStorage(final GL gl)
GLBufferStorage mapStorage(final GL gl, final int access)
void destroy(final GL gl)
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...
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',...
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_...