JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
DemoGL2ES1Plain.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.opengl.test.junit.jogl.util;
29
30import java.nio.ByteBuffer;
31import java.nio.FloatBuffer;
32
33import com.jogamp.opengl.GL;
34import com.jogamp.opengl.GL2ES1;
35import com.jogamp.opengl.GLAutoDrawable;
36import com.jogamp.opengl.GLEventListener;
37import com.jogamp.opengl.GLException;
38import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
39import com.jogamp.opengl.fixedfunc.GLPointerFunc;
40import com.jogamp.opengl.glu.GLU;
41import com.jogamp.opengl.glu.gl2es1.GLUgl2es1;
42
43import com.jogamp.common.nio.Buffers;
44import com.jogamp.opengl.util.GLArrayDataWrapper;
45import com.jogamp.opengl.util.GLBuffers;
46
47class DemoGL2ES1Plain implements GLEventListener {
48 final boolean useArrayData;
49 final boolean useVBO;
50 final GLU glu;
51
52 final float[] vertices = new float[] { 0, 0, 0,
53 TestImmModeSinkES1NEWT.iWidth, 0, 0,
54 TestImmModeSinkES1NEWT.iWidth / 2, TestImmModeSinkES1NEWT.iHeight, 0 };
55
56 final float[] colors = new float[] { 1, 0, 0,
57 0, 1, 0,
58 0, 0, 1 };
59
60 final ByteBuffer bufferAll;
61 final int bufferVOffset, bufferCOffset;
62 final int bufferVSize, bufferCSize;
63 final FloatBuffer bufferC, bufferV;
64 final int[] vboName = new int[] { 0 };
65 final GLArrayDataWrapper arrayC, arrayV;
66
67 DemoGL2ES1Plain(final boolean useArrayData, final boolean useVBO) {
68 this.useArrayData = useArrayData;
69 this.useVBO = useVBO;
70 this.glu = new GLUgl2es1();
71
72 bufferAll = Buffers.newDirectByteBuffer( ( colors.length + vertices.length ) * Buffers.SIZEOF_FLOAT );
73
74 bufferVOffset = 0;
75 bufferVSize = 3*3*GLBuffers.sizeOfGLType(GL.GL_FLOAT);
76 bufferCOffset = bufferVSize;
77 bufferCSize = 3*3*GLBuffers.sizeOfGLType(GL.GL_FLOAT);
78
79 bufferV = (FloatBuffer) GLBuffers.sliceGLBuffer(bufferAll, bufferVOffset, bufferVSize, GL.GL_FLOAT);
80 bufferV.put(vertices, 0, vertices.length).rewind();
81 bufferC = (FloatBuffer) GLBuffers.sliceGLBuffer(bufferAll, bufferCOffset, bufferCSize, GL.GL_FLOAT);
82 bufferC.put(colors, 0, colors.length).rewind();
83
84 System.err.println("bufferAll: "+bufferAll+", byteOffset "+Buffers.getDirectBufferByteOffset(bufferAll));
85 System.err.println("bufferV: off "+bufferVOffset+", size "+bufferVSize+": "+bufferV+", byteOffset "+Buffers.getDirectBufferByteOffset(bufferV));
86 System.err.println("bufferC: off "+bufferCOffset+", size "+bufferCSize+": "+bufferC+", byteOffset "+Buffers.getDirectBufferByteOffset(bufferC));
87
88 if(useArrayData) {
89 arrayV = GLArrayDataWrapper.createFixed(GLPointerFunc.GL_VERTEX_ARRAY, 3, GL.GL_FLOAT, false, 0,
90 bufferV, 0, bufferVOffset, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER);
91
92 arrayC = GLArrayDataWrapper.createFixed(GLPointerFunc.GL_COLOR_ARRAY, 3, GL.GL_FLOAT, false, 0,
93 bufferC, 0, bufferCOffset, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER);
94 } else {
95 arrayV = null;
96 arrayC = null;
97 }
98 }
99
100 @Override
101 public void init(final GLAutoDrawable drawable) {
102 final GL gl = drawable.getGL();
103 System.err.println("GL_VENDOR "+gl.glGetString(GL.GL_VENDOR));
104 System.err.println("GL_RENDERER "+gl.glGetString(GL.GL_RENDERER));
105 System.err.println("GL_VERSION "+gl.glGetString(GL.GL_VERSION));
106 if(useVBO) {
107 gl.glGenBuffers(1, vboName, 0);
108 if(0 == vboName[0]) {
109 throw new GLException("glGenBuffers didn't return valid VBO name");
110 }
111 }
112 }
113
114 @Override
115 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
116 final GL2ES1 gl = drawable.getGL().getGL2ES1();
117
118 gl.glMatrixMode( GLMatrixFunc.GL_PROJECTION );
119 gl.glLoadIdentity();
120
121 // coordinate system origin at lower left with width and height same as the window
122 glu.gluOrtho2D( 0.0f, width, 0.0f, height );
123
124 gl.glMatrixMode( GLMatrixFunc.GL_MODELVIEW );
125 gl.glLoadIdentity();
126
127 gl.glViewport( 0, 0, width, height );
128 }
129
130 @Override
131 public void display(final GLAutoDrawable drawable) {
132 final GL2ES1 gl = drawable.getGL().getGL2ES1();
133
134 gl.glClear( GL.GL_COLOR_BUFFER_BIT );
135
136 // draw a triangle filling the window
137 gl.glLoadIdentity();
138
139 if(useVBO) {
140 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName[0]);
141 gl.glBufferData(GL.GL_ARRAY_BUFFER, bufferAll.limit(), bufferAll, GL.GL_STATIC_DRAW);
142 if(useArrayData) {
143 arrayV.setVBOName(vboName[0]);
144 arrayC.setVBOName(vboName[0]);
145 }
146 }
147
148 gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
149 if(useArrayData) {
150 gl.glVertexPointer(arrayV);
151 } else {
152 if(useVBO) {
153 gl.glVertexPointer(3, GL.GL_FLOAT, 0, bufferVOffset);
154 } else {
155 gl.glVertexPointer(3, GL.GL_FLOAT, 0, bufferV);
156 }
157 }
158
159 gl.glEnableClientState(GLPointerFunc.GL_COLOR_ARRAY);
160 if(useArrayData) {
161 gl.glColorPointer(arrayC);
162 } else {
163 if(useVBO) {
164 gl.glColorPointer(3, GL.GL_FLOAT, 0, bufferCOffset);
165 } else {
166 gl.glColorPointer(3, GL.GL_FLOAT, 0, bufferC);
167 }
168 }
169
170 if(useVBO) {
171 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
172 }
173
174 gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3);
175 gl.glFlush();
176
177 gl.glDisableClientState(GLPointerFunc.GL_COLOR_ARRAY);
178 gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
179 }
180
181 @Override
182 public void dispose(final GLAutoDrawable drawable) {
183 final GL gl = drawable.getGL();
184 if(0 != vboName[0]) {
185 gl.glDeleteBuffers(1, vboName, 0);
186 vboName[0] = 0;
187 }
188 }
189}