1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
package demos.es2;
import java.nio.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import javax.media.nativewindow.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.glsl.*;
import com.sun.javafx.newt.*;
import com.sun.javafx.newt.opengl.*;
public class RedSquare extends Thread implements MouseListener, GLEventListener {
private GLWindow window;
private GLProfile glp;
private GLU glu;
private boolean quit = false;
private long startTime;
private long curTime;
public RedSquare() {
super();
}
public void mouseClicked(MouseEvent e) {
System.out.println(glp+" mouseevent: "+e);
switch(e.getClickCount()) {
case 1:
window.setFullscreen(!window.isFullscreen());
break;
default:
quit=true;
break;
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
public void mouseWheelMoved(MouseEvent e) {
}
private void init(String glprofile, int type) {
int width = 800;
int height = 480;
glp = GLProfile.GetProfile(glprofile);
System.err.println(glp+" RedSquare.start()");
try {
GLCapabilities caps = new GLCapabilities(glp);
// For emulation library, use 16 bpp
caps.setRedBits(5);
caps.setGreenBits(6);
caps.setBlueBits(5);
caps.setDepthBits(16);
Window nWindow = null;
if(0!=(type&USE_AWT)) {
Display nDisplay = NewtFactory.createDisplay(NativeWindowFactory.TYPE_AWT, null); // local display
Screen nScreen = NewtFactory.createScreen(NativeWindowFactory.TYPE_AWT, nDisplay, 0); // screen 0
nWindow = NewtFactory.createWindow(NativeWindowFactory.TYPE_AWT, nScreen, caps);
}
window = GLWindow.create(nWindow, caps);
window.addMouseListener(this);
window.addGLEventListener(this);
// window.setEventHandlerMode(GLWindow.EVENT_HANDLER_GL_CURRENT); // default
// window.setEventHandlerMode(GLWindow.EVENT_HANDLER_GL_NONE); // no current ..
// Size OpenGL to Video Surface
window.setSize(width, height);
// window.setFullscreen(true);
window.setVisible(true);
} catch (Throwable t) {
t.printStackTrace();
}
}
private void runInThread(String glprofile, int type) {
init(glprofile, type);
run();
}
private void start(String glprofile, int type) {
init(glprofile, type);
start();
}
public void run() {
System.err.println(glp+" RedSquare.run()");
try {
startTime = System.currentTimeMillis();
while (!quit && ((curTime = System.currentTimeMillis()) - startTime) < 20000) {
window.display();
}
// Shut things down cooperatively
window.destroy();
window.getFactory().shutdown();
System.out.println(glp+" RedSquare shut down cleanly.");
} catch (Throwable t) {
t.printStackTrace();
}
}
ShaderState st;
PMVMatrix pmvMatrix;
private void initShader(GL2ES2 gl) {
int tmpI;
// Create & Compile the shader objects
ShaderCode rsVp = ShaderCode.create(gl, gl.GL_VERTEX_SHADER, 1, RedSquare.class,
"shader", "shader/bin", "redsquare");
ShaderCode rsFp = ShaderCode.create(gl, gl.GL_FRAGMENT_SHADER, 1, RedSquare.class,
"shader", "shader/bin", "redsquare");
// Create & Link the shader program
ShaderProgram sp = new ShaderProgram();
sp.add(rsVp);
sp.add(rsFp);
if(!sp.link(gl, System.err)) {
throw new GLException("Couldn't link program: "+sp);
}
// Let's manage all our states using ShaderState.
st = new ShaderState();
st.attachShaderProgram(gl, sp);
}
public void init(GLAutoDrawable drawable) {
GL2ES2 gl = drawable.getGL().getGL2ES2();
glu = GLU.createGLU();
System.err.println(glp+" Entering initialization");
System.err.println(glp+" GL_VERSION=" + gl.glGetString(gl.GL_VERSION));
System.err.println(glp+" GL_EXTENSIONS:");
System.err.println(glp+" " + gl.glGetString(gl.GL_EXTENSIONS));
pmvMatrix = new PMVMatrix();
initShader(gl);
// Push the 1st uniform down the path
st.glUseProgram(gl, true);
pmvMatrix.glMatrixMode(pmvMatrix.GL_PROJECTION);
pmvMatrix.glLoadIdentity();
pmvMatrix.glMatrixMode(pmvMatrix.GL_MODELVIEW);
pmvMatrix.glLoadIdentity();
if(!st.glUniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()))) {
throw new GLException("Error setting PMVMatrix in shader: "+st);
}
// Allocate vertex arrays
GLArrayDataClient vertices = GLArrayDataClient.createGLSL(gl, "mgl_Vertex", 3, gl.GL_FLOAT, false, 4);
{
// Fill them up
FloatBuffer verticeb = (FloatBuffer)vertices.getBuffer();
verticeb.put(-2); verticeb.put( 2); verticeb.put( 0);
verticeb.put( 2); verticeb.put( 2); verticeb.put( 0);
verticeb.put(-2); verticeb.put( -2); verticeb.put( 0);
verticeb.put( 2); verticeb.put( -2); verticeb.put( 0);
}
vertices.seal(gl, true);
GLArrayDataClient colors = GLArrayDataClient.createGLSL(gl, "mgl_Color", 4, gl.GL_FLOAT, false, 4);
{
// Fill them up
FloatBuffer colorb = (FloatBuffer)colors.getBuffer();
colorb.put( 1); colorb.put( 0); colorb.put( 0); colorb.put( 1);
colorb.put( 0); colorb.put( 0); colorb.put( 1); colorb.put( 1);
colorb.put( 1); colorb.put( 0); colorb.put( 0); colorb.put( 1);
colorb.put( 1); colorb.put( 0); colorb.put( 0); colorb.put( 1);
}
colors.seal(gl, true);
// OpenGL Render Settings
gl.glClearColor(0, 0, 0, 1);
gl.glEnable(GL2ES2.GL_DEPTH_TEST);
st.glUseProgram(gl, false);
// Let's show the completed shader state ..
System.out.println(glp+" "+st);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2ES2 gl = drawable.getGL().getGL2ES2();
st.glUseProgram(gl, true);
// Set location in front of camera
pmvMatrix.glMatrixMode(pmvMatrix.GL_PROJECTION);
pmvMatrix.glLoadIdentity();
pmvMatrix.gluPerspective(45.0f, (float)width / (float)height, 1.0f, 100.0f);
//pmvMatrix.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f);
GLUniformData ud = st.getUniform("mgl_PMVMatrix");
if(null!=ud) {
// same data object
st.glUniform(gl, ud);
}
st.glUseProgram(gl, false);
}
public void dispose(GLAutoDrawable drawable) {
GL2ES2 gl = drawable.getGL().getGL2ES2();
System.out.println(glp+" RedSquare.dispose: "+gl.getContext());
st.destroy(gl);
st=null;
pmvMatrix.destroy();
pmvMatrix=null;
quit=true;
System.out.println(glp+" RedSquare.dispose: fin");
}
public void display(GLAutoDrawable drawable) {
GL2ES2 gl = drawable.getGL().getGL2ES2();
st.glUseProgram(gl, true);
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
// One rotation every four seconds
pmvMatrix.glMatrixMode(pmvMatrix.GL_MODELVIEW);
pmvMatrix.glLoadIdentity();
pmvMatrix.glTranslatef(0, 0, -10);
float ang = ((float) (curTime - startTime) * 360.0f) / 4000.0f;
pmvMatrix.glRotatef(ang, 0, 0, 1);
pmvMatrix.glRotatef(ang, 0, 1, 0);
GLUniformData ud = st.getUniform("mgl_PMVMatrix");
if(null!=ud) {
// same data object
st.glUniform(gl, ud);
}
// Draw a square
gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);
st.glUseProgram(gl, false);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
public static int USE_NEWT = 0;
public static int USE_AWT = 1 << 0;
public static void main(String[] args) {
String glprofile = null;
int type = USE_NEWT ;
for(int i=args.length-1; i>=0; i--) {
if(args[i].equals("-awt")) {
type |= USE_AWT;
}
if(args[i].startsWith("-GL")) {
if(null!=glprofile) {
new RedSquare().start(glprofile, type);
}
glprofile=args[i].substring(1);
}
}
new RedSquare().runInThread(glprofile, type);
}
}
|