5 import javax.media.opengl.*;
6 import javax.media.nativewindow.*;
8 import com.jogamp.opengl.util.*;
9 import com.jogamp.opengl.util.glsl.*;
11 import com.jogamp.newt.*;
12 import com.jogamp.newt.event.*;
13 import com.jogamp.newt.opengl.*;
15 public class RedSquare extends Thread implements WindowListener, KeyListener, MouseListener, GLEventListener {
17 public Window nWindow = null;
18 public GLWindow window;
19 private GLProfile glp;
20 private boolean quit = false;
21 private String glprofile;
28 public RedSquare(String glprofile, int type) {
30 this.glprofile=glprofile;
34 public void windowRepaint(WindowUpdateEvent e) { }
35 public void windowResized(WindowEvent e) { }
36 public void windowMoved(WindowEvent e) { }
37 public void windowGainedFocus(WindowEvent e) { }
38 public void windowLostFocus(WindowEvent e) { }
39 public void windowDestroyNotify(WindowEvent e) {
40 System.out.println("WINDOW-DESTROY NOTIFY "+Thread.currentThread()+" QUIT "+e);
43 public void windowDestroyed(WindowEvent e) {
44 System.out.println("WINDOW-DESTROYED "+Thread.currentThread());
47 public void keyPressed(KeyEvent e) {
48 System.out.println("KEY-PRESSED "+Thread.currentThread()+" UNHANDLED "+e);
50 public void keyReleased(KeyEvent e) {
51 System.out.println("KEY-RELEASED "+Thread.currentThread()+" UNHANDLED "+e);
53 public void keyTyped(KeyEvent e) {
54 if(e.getKeyChar()=='f') {
55 System.out.println("KEY-TYPED "+Thread.currentThread()+" FULLSCREEN "+e);
56 window.setFullscreen(!window.isFullscreen());
57 } else if(e.getKeyChar()=='q') {
58 System.out.println("KEY-TYPED "+Thread.currentThread()+" QUIT "+e);
61 System.out.println("KEY-TYPED "+Thread.currentThread()+" UNHANDLED "+e);
65 public void mouseClicked(MouseEvent e) {
66 System.out.println("MOUSE-CLICKED "+Thread.currentThread()+" UNHANDLED "+e);
67 switch(e.getClickCount()) {
69 if(e.getButton()>MouseEvent.BUTTON1) {
70 window.setFullscreen(!window.isFullscreen());
78 public void mouseEntered(MouseEvent e) {
80 public void mouseExited(MouseEvent e) {
82 public void mousePressed(MouseEvent e) {
84 public void mouseReleased(MouseEvent e) {
86 public void mouseMoved(MouseEvent e) {
88 public void mouseDragged(MouseEvent e) {
90 public void mouseWheelMoved(MouseEvent e) {
93 public boolean shouldQuit() { return quit; }
98 glp = GLProfile.get(glprofile);
99 System.out.println("RUN "+Thread.currentThread()+" "+glp);
101 GLCapabilities caps = new GLCapabilities(glp);
103 if(0!=(type&USE_AWT)) {
104 Display nDisplay = NewtFactory.createDisplay(NativeWindowFactory.TYPE_AWT, null); // local display
105 Screen nScreen = NewtFactory.createScreen(nDisplay, 0); // screen 0
106 nWindow = NewtFactory.createWindow(nScreen, caps);
107 window = GLWindow.create(nWindow);
109 window = GLWindow.create(caps);
112 window.addWindowListener(this);
113 window.addMouseListener(this);
114 window.addKeyListener(this);
115 window.addGLEventListener(this);
117 // Size OpenGL to Video Surface
118 window.setSize(width, height);
119 // window.setFullscreen(true);
120 window.setVisible(true);
121 window.setUpdateFPSFrames(FPSCounter.DEFAULT_FRAMES_PER_INTERVAL, System.err);
126 } while (!quit && window.getTotalFPSDuration() < 20000) ;
131 } catch (Throwable t) {
136 public void display() {
139 } catch (Throwable t) {
144 public void shutdown() {
146 // Shut things down cooperatively
153 System.out.println("SHUTDOWN "+Thread.currentThread()+" cleanly");
154 } catch (Throwable t) {
163 private void initShader(GL2ES2 gl) {
164 // Create & Compile the shader objects
165 ShaderCode rsVp = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, RedSquare.class,
166 "shader", "shader/bin", "redsquare", false);
167 ShaderCode rsFp = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, RedSquare.class,
168 "shader", "shader/bin", "redsquare", false);
170 // Create & Link the shader program
171 ShaderProgram sp = new ShaderProgram();
174 if(!sp.link(gl, System.err)) {
175 throw new GLException("Couldn't link program: "+sp);
178 // Let's manage all our states using ShaderState.
179 st = new ShaderState();
180 st.attachShaderProgram(gl, sp, false);
183 public void init(GLAutoDrawable drawable) {
184 GL2ES2 gl = drawable.getGL().getGL2ES2();
185 if(swapInterval>=0) {
186 gl.setSwapInterval(swapInterval);
189 System.err.println(Thread.currentThread()+" Entering initialization");
190 System.err.println(Thread.currentThread()+" GL Profile: "+gl.getGLProfile());
191 System.err.println(Thread.currentThread()+" GL:" + gl);
192 System.err.println(Thread.currentThread()+" GL_VERSION=" + gl.glGetString(gl.GL_VERSION));
193 System.err.println(Thread.currentThread()+" GL_EXTENSIONS:");
194 System.err.println(Thread.currentThread()+" " + gl.glGetString(gl.GL_EXTENSIONS));
195 System.err.println(Thread.currentThread()+" swapInterval: " + swapInterval + " (GL: "+gl.getSwapInterval()+")");
196 System.err.println(Thread.currentThread()+" isShaderCompilerAvailable: " + ShaderUtil.isShaderCompilerAvailable(gl));
201 gl = (GL2ES2) gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Debug", GL2ES2.class, gl, null) );
204 gl = (GL2ES2) gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Trace", GL2ES2.class, gl, new Object[] { System.err } ) );
205 } catch (Exception e) {e.printStackTrace();}
208 pmvMatrix = new PMVMatrix();
212 // Push the 1st uniform down the path
213 st.useProgram(gl, true);
215 pmvMatrix.glMatrixMode(pmvMatrix.GL_PROJECTION);
216 pmvMatrix.glLoadIdentity();
217 pmvMatrix.glMatrixMode(pmvMatrix.GL_MODELVIEW);
218 pmvMatrix.glLoadIdentity();
220 if(!st.uniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()))) {
221 throw new GLException("Error setting PMVMatrix in shader: "+st);
223 // Allocate vertex arrays
224 GLArrayDataClient vertices = GLArrayDataClient.createGLSL("mgl_Vertex", 3, gl.GL_FLOAT, false, 4);
227 FloatBuffer verticeb = (FloatBuffer)vertices.getBuffer();
228 verticeb.put(-2); verticeb.put( 2); verticeb.put( 0);
229 verticeb.put( 2); verticeb.put( 2); verticeb.put( 0);
230 verticeb.put(-2); verticeb.put( -2); verticeb.put( 0);
231 verticeb.put( 2); verticeb.put( -2); verticeb.put( 0);
233 vertices.seal(gl, true);
235 GLArrayDataClient colors = GLArrayDataClient.createGLSL("mgl_Color", 4, gl.GL_FLOAT, false, 4);
238 FloatBuffer colorb = (FloatBuffer)colors.getBuffer();
239 colorb.put( 1); colorb.put( 0); colorb.put( 0); colorb.put( 1);
240 colorb.put( 0); colorb.put( 0); colorb.put( 1); colorb.put( 1);
241 colorb.put( 1); colorb.put( 0); colorb.put( 0); colorb.put( 1);
242 colorb.put( 1); colorb.put( 0); colorb.put( 0); colorb.put( 1);
244 colors.seal(gl, true);
246 // OpenGL Render Settings
247 gl.glClearColor(0, 0, 0, 1);
248 gl.glEnable(GL2ES2.GL_DEPTH_TEST);
250 st.useProgram(gl, false);
252 // Let's show the completed shader state ..
253 System.out.println(Thread.currentThread()+" "+st);
256 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
259 GL2ES2 gl = drawable.getGL().getGL2ES2();
261 st.useProgram(gl, true);
263 // Set location in front of camera
264 pmvMatrix.glMatrixMode(pmvMatrix.GL_PROJECTION);
265 pmvMatrix.glLoadIdentity();
266 pmvMatrix.gluPerspective(45.0f, (float)width / (float)height, 1.0f, 100.0f);
267 //pmvMatrix.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f);
269 GLUniformData ud = st.getUniform("mgl_PMVMatrix");
275 st.useProgram(gl, false);
278 public void dispose(GLAutoDrawable drawable) {
281 GL2ES2 gl = drawable.getGL().getGL2ES2();
282 System.out.println(Thread.currentThread()+" RedSquare.dispose: "+gl.getContext());
288 System.out.println(Thread.currentThread()+" RedSquare.dispose: FIN");
291 public void display(GLAutoDrawable drawable) {
294 GL2ES2 gl = drawable.getGL().getGL2ES2();
296 st.useProgram(gl, true);
298 gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
300 // One rotation every four seconds
301 pmvMatrix.glMatrixMode(pmvMatrix.GL_MODELVIEW);
302 pmvMatrix.glLoadIdentity();
303 pmvMatrix.glTranslatef(0, 0, -10);
304 float ang = ((float) window.getTotalFPSDuration() * 360.0f) / 4000.0f;
305 pmvMatrix.glRotatef(ang, 0, 0, 1);
306 pmvMatrix.glRotatef(ang, 0, 1, 0);
308 GLUniformData ud = st.getUniform("mgl_PMVMatrix");
315 gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);
317 st.useProgram(gl, false);
320 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
323 public static int USE_NEWT = 0;
324 public static int USE_AWT = 1 << 0;
326 public static boolean oneThread = false;
327 public static int swapInterval = -1;
328 public static boolean debuggl = false;
330 public static void main(String[] args) {
331 NewtFactory.setUseEDT(true); // should be the default
332 int type = USE_NEWT ;
333 List threads = new ArrayList();
334 for(int i=0; i<args.length; i++) {
335 if(args[i].equals("-swapi")) {
338 swapInterval = Integer.parseInt(args[i]);
339 } catch (Exception ex) { ex.printStackTrace(); }
340 } else if(args[i].equals("-debug")) {
342 } else if(args[i].equals("-1thread")) {
344 } else if(args[i].equals("-awt")) {
346 } else if(args[i].startsWith("-GL")) {
347 threads.add(new RedSquare(args[i].substring(1), type));
350 if(threads.size()==0) {
351 threads.add(new RedSquare(GLProfile.GL2ES2, type));
355 for(Iterator i = threads.iterator(); i.hasNext(); ) {
356 ((Thread)i.next()).start();
359 boolean done = false;
363 for(Iterator i = threads.iterator(); i.hasNext(); ) {
364 if ( ((Thread)i.next()).isAlive() ) {
367 } catch (InterruptedException ie) {}
371 done = 0==aliveCount ;
375 for(Iterator i = threads.iterator(); i.hasNext(); ) {
376 ((Thread)i.next()).run();
378 while (threads.size()>0) {
379 for(Iterator i = threads.iterator(); i.hasNext(); ) {
380 RedSquare app = (RedSquare) i.next();
381 if(app.shouldQuit()) {