JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
ReadBuffer2Screen.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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 */
28
29package com.jogamp.opengl.test.junit.jogl.offscreen;
30
31import java.nio.*;
32
33import com.jogamp.math.FloatUtil;
34import com.jogamp.opengl.*;
35import com.jogamp.opengl.fixedfunc.*;
36
37import com.jogamp.opengl.util.*;
38
39import com.jogamp.opengl.fixedfunc.GLPointerFunc;
40import com.jogamp.opengl.util.texture.TextureCoords;
41import com.jogamp.opengl.util.GLArrayDataClient;
42import com.jogamp.opengl.util.GLArrayDataServer;
43
44public class ReadBuffer2Screen extends ReadBufferBase {
45 PMVMatrix pmvMatrix;
46 GLArrayDataClient readTextureVertices = null;
47 GLArrayDataClient readTextureCoords = null;
48 boolean enableBufferAlways = false; // FIXME
49 boolean enableBufferVBO = true; // FIXME
50
52 super(externalRead, true);
53 }
54
55 @Override
56 public void init(final GLAutoDrawable drawable) {
57 super.init(drawable);
58
59 final GL gl = drawable.getGL();
60
61 pmvMatrix = new PMVMatrix();
62
63 final float f_edge = 1f;
64 if(null==readTextureVertices) {
65 //readTextureVertices = GLArrayDataClient.createFixed(gl, GLPointerFunc.GL_VERTEX_ARRAY, "mgl_Vertex",
66 // 2, GL.GL_FLOAT, true, 4);
68 GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW);
69 readTextureVertices.setEnableAlways(enableBufferAlways);
70 readTextureVertices.setVBOEnabled(enableBufferVBO);
71 {
72 final FloatBuffer vb = (FloatBuffer)readTextureVertices.getBuffer();
73 vb.put(-f_edge); vb.put(-f_edge);
74 vb.put( f_edge); vb.put(-f_edge);
75 vb.put(-f_edge); vb.put( f_edge);
76 vb.put( f_edge); vb.put( f_edge);
77 }
78 readTextureVertices.seal(gl, true);
79 System.out.println(readTextureVertices);
80 }
81
82 // Clear background to gray
83 gl.glClearColor(0.5f, 0.5f, 0.5f, 0.4f);
84 }
85
86 @Override
87 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
88 super.reshape(drawable, x, y, width, height);
89
90 final GL gl = drawable.getGL();
91
92 gl.glViewport(0, 0, width, height);
93
94 if(gl instanceof GLLightingFunc) {
95 ((GLLightingFunc)gl).glShadeModel(GLLightingFunc.GL_SMOOTH);
96 }
97
98 GLMatrixFunc glM;
99 if(gl instanceof GLMatrixFunc) {
100 glM = (GLMatrixFunc)gl;
101 } else {
102 throw new GLException("ES2 currently unhandled .. ");
103 }
104
105 // Identity ..
107 pmvMatrix.glLoadIdentity();
108 pmvMatrix.glTranslatef(0, 0, -2.5f);
109 if(null!=glM) {
111 glM.glLoadMatrixf(pmvMatrix.getSyncMv().getSyncFloats());
112 }
113
114 // Set location in front of camera
116 pmvMatrix.glLoadIdentity();
117 pmvMatrix.gluPerspective(FloatUtil.QUARTER_PI, (float)width / (float)height, 1.0f, 100.0f);
118 if(null!=glM) {
120 glM.glLoadMatrixf(pmvMatrix.getSyncP().getSyncFloats());
121 }
122 }
123
124 @Override
125 public void dispose(final GLAutoDrawable drawable) {
126 super.dispose(drawable);
127 }
128
129 void renderOffscreenTexture(final GL gl) {
130 if(!readBufferUtil.isValid()) return;
131
132 // Now draw one quad with the texture
133 readBufferUtil.getTexture().enable(gl);
134 readBufferUtil.getTexture().bind(gl);
135
136 if(gl.isGL2ES1()) {
137 // gl.getGL2ES1().glTexEnvi(GL2ES1.GL_TEXTURE_ENV, GL2ES1.GL_TEXTURE_ENV_MODE, GL2ES1.GL_REPLACE);
139 }
140
141 updateTextureCoords(gl, false);
142
143 readTextureVertices.enableBuffer(gl, true);
144 if(null!=readTextureCoords) {
145 readTextureCoords.enableBuffer(gl, true);
146 }
147 gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, readTextureVertices.getElemCount());
148 /**
149 if(null!=readTextureCoords) {
150 readTextureCoords.enableBuffer(gl, false);
151 }
152 readTextureVertices.enableBuffer(gl, false); */
153
154 readBufferUtil.getTexture().disable(gl);
155 }
156
157 void updateTextureCoords(final GL gl, final boolean force) {
158 if(force || null==readTextureCoords) {
160 GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW);
161 readTextureCoords.setEnableAlways(enableBufferAlways);
162 readTextureCoords.setVBOEnabled(enableBufferVBO);
163 {
164 final TextureCoords coords = readBufferUtil.getTexture().getImageTexCoords();
165 final FloatBuffer cb = (FloatBuffer)readTextureCoords.getBuffer();
166 cb.put(coords.left()); cb.put(coords.bottom());
167 cb.put(coords.right()); cb.put(coords.bottom());
168 cb.put(coords.left()); cb.put(coords.top());
169 cb.put(coords.right()); cb.put(coords.top());
170 }
171 readTextureCoords.seal(gl, true);
172 System.out.println(readTextureCoords);
173 }
174 }
175
176 @Override
177 public void display(final GLAutoDrawable drawable) {
178 super.display(drawable);
179
180 final GL gl = drawable.getGL();
181
183 if(gl instanceof GLLightingFunc) {
184 ((GLLightingFunc)gl).glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
185 }
186
187 renderOffscreenTexture(gl);
188 }
189}
190
Basic Float math utility functions.
Definition: FloatUtil.java:83
static final float QUARTER_PI
The value PI/4, i.e.
final SyncMatrix4f getSyncMv()
Returns the SyncMatrix of modelview matrix (Mv).
final SyncMatrix4f getSyncP()
Returns the SyncMatrix of projection matrix (P).
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
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 setEnableAlways(final boolean always)
Affects the behavior of 'enableBuffer'.
static GLArrayDataServer createFixed(final int index, final int compsPerElement, final int dataType, final boolean normalized, final int stride, final Buffer buffer, final int vboUsage)
Create a VBO, using a predefined fixed function array index and starting with a given Buffer object i...
final int getElemCount()
Returns the element position (written elements) if not sealed() or the element limit (available to re...
void setVBOEnabled(final boolean vboEnabled)
Enable or disable use of VBO.
Buffer getBuffer()
The Buffer holding the data, may be null if a GPU buffer without client bound data.
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 glMatrixMode(final int matrixName)
Sets the current matrix mode.
Definition: PMVMatrix.java:218
final void gluPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Multiply the current matrix with the perspective/frustum matrix.
Definition: PMVMatrix.java:499
final void glLoadIdentity()
Load the current matrix with the identity matrix.
Definition: PMVMatrix.java:325
TextureCoords getImageTexCoords()
Returns the set of texture coordinates corresponding to the entire image.
Definition: Texture.java:480
void bind(final GL gl)
Binds this texture to the given GL context.
Definition: Texture.java:377
void disable(final GL gl)
Disables this texture's target (e.g., GL_TEXTURE_2D) in the given GL state.
Definition: Texture.java:357
void enable(final GL gl)
Enables this texture's target (e.g., GL_TEXTURE_2D) in the given GL context's state.
Definition: Texture.java:330
FloatBuffer getSyncFloats()
Return the FloatBuffer after synchronizing it w/ the underlying getMatrix().
static final int GL_MODULATE
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_MODULATE" with expression '0x2100', CType: int
Definition: GL2ES1.java:147
static final int GL_TEXTURE_ENV_MODE
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_TEXTURE_ENV_MODE" with expression '0x2200',...
Definition: GL2ES1.java:202
void glTexEnvi(int target, int pname, int param)
Entry point to C language function: void {@native glTexEnvi}(GLenum target, GLenum pname,...
static final int GL_TEXTURE_ENV
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_TEXTURE_ENV" with expression '0x2300',...
Definition: GL2ES1.java:154
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
boolean isGL2ES1()
Indicates whether this GL object conforms to a GL2ES1 compatible profile.
GL2ES1 getGL2ES1()
Casts this object to the GL2ES1 interface.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
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_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_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
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
void glViewport(int x, int y, int width, int height)
Entry point to C language function: void {@native glViewport}(GLint x, GLint y, GLsizei width,...
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW
Matrix mode modelview.
void glLoadMatrixf(java.nio.FloatBuffer m)
Load the current matrix w/ the provided one.
void glMatrixMode(int mode)
Sets the current matrix mode.