JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
GearsObjectES2.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.test.junit.jogl.demos.es2;
22
23import java.nio.FloatBuffer;
24
25import com.jogamp.opengl.GL;
26import com.jogamp.opengl.GL2ES2;
27import com.jogamp.opengl.GLBufferStorage;
28import com.jogamp.opengl.GLException;
29import com.jogamp.opengl.GLUniformData;
30
31import com.jogamp.opengl.test.junit.jogl.demos.GearsObject;
32import com.jogamp.opengl.util.GLArrayDataServer;
33import com.jogamp.opengl.util.PMVMatrix;
34import com.jogamp.opengl.util.glsl.ShaderState;
35
36/**
37 * GearsObjectES2.java <BR>
38 * @author Brian Paul (converted to Java by Ron Cemer and Sven Gothel) <P>
39 */
40public class GearsObjectES2 extends GearsObject {
41 final PMVMatrix pmvMatrix;
42 final GLUniformData pmvMatrixUniform;
43 final GLUniformData colorUniform;
44 final ShaderState st;
45
46 public GearsObjectES2(final GL gl, final boolean useMappedBuffers, final ShaderState st, final FloatBuffer gearColor,
47 final float inner_radius, final float outer_radius,
48 final float width,
49 final int teeth,
50 final float tooth_depth, final PMVMatrix pmvMatrix, final GLUniformData pmvMatrixUniform, final GLUniformData colorUniform, final boolean validateBuffers)
51 {
52 super(gl, useMappedBuffers, gearColor, inner_radius, outer_radius, width, teeth, tooth_depth, validateBuffers);
53 this.pmvMatrix = pmvMatrix;
54 this.pmvMatrixUniform = pmvMatrixUniform;
55 this.colorUniform = colorUniform;
56 this.st = st;
57 associate(st);
58 }
59
60 public GearsObjectES2(final GearsObjectES2 shared,
61 final ShaderState st,
62 final PMVMatrix pmvMatrix,
63 final GLUniformData pmvMatrixUniform, final GLUniformData colorUniform)
64 {
65 super(shared);
66 this.pmvMatrix = pmvMatrix;
67 this.pmvMatrixUniform = pmvMatrixUniform;
68 this.colorUniform = colorUniform;
69 this.st = st;
70 associate(st);
71 }
72
73 private void associate(final ShaderState st) {
74 frontFace.associate(st, true);
75 frontSide.associate(st, true);
76 backFace.associate(st, true);
77 backSide.associate(st, true);
78 outwardFace.associate(st, true);
79 insideRadiusCyl.associate(st, true);
80 }
81
82 @Override
83 public GLArrayDataServer createInterleaved(final boolean useMappedBuffers, final int comps, final int dataType, final boolean normalized, final int initialSize, final int vboUsage) {
84 if( useMappedBuffers ) {
85 return GLArrayDataServer.createGLSLInterleavedMapped(comps, dataType, normalized, initialSize, vboUsage);
86 } else {
87 return GLArrayDataServer.createGLSLInterleaved(comps, dataType, normalized, initialSize, vboUsage);
88 }
89 }
90
91 @Override
92 public void addInterleavedVertexAndNormalArrays(final GLArrayDataServer array, final int components) {
93 array.addGLSLSubArray("vertices", components, GL.GL_ARRAY_BUFFER);
94 array.addGLSLSubArray("normals", components, GL.GL_ARRAY_BUFFER);
95 }
96
97 private void draw(final GL2ES2 gl, final GLArrayDataServer array, final int mode, final int face) {
98 if( !isShared || gl.glIsBuffer(array.getVBOName()) ) {
99 if( validateBuffers ) {
100 array.bindBuffer(gl, true);
101 final int bufferTarget = array.getVBOTarget();
102 final int bufferName = array.getVBOName();
103 final long bufferSize = array.getByteCount();
104 final int hasBufferName = gl.getBoundBuffer(bufferTarget);
105 final GLBufferStorage hasStorage = gl.getBufferStorage(hasBufferName);
106 final boolean ok = bufferName == hasBufferName &&
107 bufferName == hasStorage.getName() &&
108 bufferSize == hasStorage.getSize();
109 if( !ok ) {
110 throw new GLException("GLBufferStorage Validation Error: Target[exp 0x"+Integer.toHexString(bufferTarget)+", has 0x"+Integer.toHexString(bufferTarget)+
111 ", Name[exp "+bufferName+", has "+hasBufferName+", Size exp "+bufferSize+", Storage "+hasStorage+"]");
112 }
113 }
114 array.enableBuffer(gl, true);
115 // System.err.println("XXX Draw face "+face+" of "+this);
116 gl.glDrawArrays(mode, 0, array.getElemCount());
117 array.enableBuffer(gl, false);
118 }
119 }
120
121 @Override
122 public void draw(final GL _gl, final float x, final float y, final float angle) {
123 final GL2ES2 gl = _gl.getGL2ES2();
124 pmvMatrix.glPushMatrix();
125 pmvMatrix.glTranslatef(x, y, 0f);
126 pmvMatrix.glRotatef(angle, 0f, 0f, 1f);
127 st.uniform(gl, pmvMatrixUniform); // automatic sync + update of Mvi + Mvit
128
129 colorUniform.setData(gearColor);
130 st.uniform(gl, colorUniform);
131
132 draw(gl, frontFace, GL.GL_TRIANGLE_STRIP, 0);
133 draw(gl, frontSide, GL.GL_TRIANGLES, 1);
134 draw(gl, backFace, GL.GL_TRIANGLE_STRIP, 2);
135 draw(gl, backSide, GL.GL_TRIANGLES, 3);
136 draw(gl, outwardFace, GL.GL_TRIANGLE_STRIP, 4);
138
139 pmvMatrix.glPopMatrix();
140 }
141}
OpenGL buffer storage object reflecting it's.
final int getName()
Return the buffer name.
final long getSize()
Return the buffer's storage size.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
GLUniformData setData(final int data)
void addInterleavedVertexAndNormalArrays(final GLArrayDataServer array, final int components)
GearsObjectES2(final GL gl, final boolean useMappedBuffers, final ShaderState st, final FloatBuffer gearColor, final float inner_radius, final float outer_radius, final float width, final int teeth, final float tooth_depth, final PMVMatrix pmvMatrix, final GLUniformData pmvMatrixUniform, final GLUniformData colorUniform, final boolean validateBuffers)
GearsObjectES2(final GearsObjectES2 shared, final ShaderState st, final PMVMatrix pmvMatrix, final GLUniformData pmvMatrixUniform, final GLUniformData colorUniform)
GLArrayDataServer createInterleaved(final boolean useMappedBuffers, final int comps, final int dataType, final boolean normalized, final int initialSize, final int vboUsage)
void draw(final GL _gl, final float x, final float y, final float angle)
void associate(final Object obj, final boolean enable)
Implementation and type dependent object association.
boolean bindBuffer(final GL gl, final boolean bind)
if bind is true and the data uses VBO, the latter will be bound and data written to the GPU if requir...
void enableBuffer(final GL gl, final boolean enable)
Enables the buffer if enable is true, and transfers the data if required.
static GLArrayDataServer createGLSLInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO for GLSL interleaved array data starting with a new created Buffer object with initialEl...
GLArrayDataWrapper addGLSLSubArray(final String name, final int comps, final int vboTarget)
Configure a segment of this GLSL interleaved array (see createGLSLInterleaved(int,...
static GLArrayDataServer createGLSLInterleavedMapped(final int compsPerElement, final int dataType, final boolean normalized, final int mappedElementCount, final int vboUsage)
Create a VBO for GLSL interleaved array data intended for GPU buffer storage mapping,...
final int getByteCount()
Returns the byte position (written elements) if not sealed() or the byte limit (available to read) af...
final int getElemCount()
Returns the element position (written elements) if not sealed() or the element limit (available to re...
final int getVBOName()
The VBO name or 0 if not a VBO.
final int getVBOTarget()
The VBO target or 0 if not a VBO.
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
Definition: PMVMatrix.java:62
final void glTranslatef(final float x, final float y, final float z)
Translate the current matrix.
Definition: PMVMatrix.java:379
final void glPushMatrix()
Push the current matrix to it's stack, while preserving it's values.
Definition: PMVMatrix.java:458
final void glRotatef(final float ang_deg, final float x, final float y, final float z)
Rotate the current matrix.
Definition: PMVMatrix.java:413
final void glPopMatrix()
Pop the current matrix from it's stack.
Definition: PMVMatrix.java:447
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
boolean uniform(final GL2ES2 gl, final GLUniformData data)
Set the uniform data, if it's location is valid, i.e.
int getBoundBuffer(int target)
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
GLBufferStorage getBufferStorage(int bufferName)
void glDrawArrays(int mode, int first, int count)
Entry point to C language function: void {@native glDrawArrays}(GLenum mode, GLint first,...
static final int GL_TRIANGLES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLES" with expre...
Definition: GL.java:145
boolean glIsBuffer(int buffer)
Entry point to C language function: GLboolean {@native glIsBuffer}(GLuint buffer) Part of GL_VERSI...
static final int GL_TRIANGLE_STRIP
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLE_STRIP" with ...
Definition: GL.java:760
static final int GL_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
Definition: GL.java:633