Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  using GLJPanel on JOGL 2.0  (Read 980 times)
0 Members and 1 Guest are viewing this topic.
Offline lonedrau

JGO n00b
*

Posts: 5



« on: 2009-12-31 09:34:38 »

I recently upgraded to JOGL 2.0, and am having trouble rendering to GLJPanel.  I am trying to do something simple, draw three colored rectangles to a 400x200 frame, but I cannot see anything on the last third of the screen.  If I change to use GLCanvas, or make the frame 400x300, my code works fine, and it worked under JOGL 1.1.1 as well.  Is 2.0 stricter on something I missed?  My code is below.

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  
import java.awt.Dimension;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.swing.JFrame;

/** Draws a white box. */
class Ex1_2 {
   /**
    * Gets the canvas displayed by this exercise.
    * @return GLJPanel
    */

   public static GLJPanel getCanvas() {
      GLCapabilities capabilities = new GLCapabilities(null);
      //* enable double buffering
     capabilities.setDoubleBuffered(true);
      GLJPanel canvas = new GLJPanel(capabilities);
      canvas.addGLEventListener(new Ex1Listener());
      return canvas;
   }
   /** Hidden constructor. */
   private Ex1_2() { }
}
/** The event listener for this class. */
class Ex1Listener implements GLEventListener {
   /** the GL instance. */
   private GL2 gl;
   /**
    * {@inheritDoc}
    */

   public void display(final GLAutoDrawable drawable) {
      // includes all operations to render (or re-render) the scene
     if (gl == null) {
         gl = drawable.getGL().getGL2();
      }
      //** clear all pixels
     gl.glClear(GL.GL_COLOR_BUFFER_BIT);
      //* draw white polygon (rectangle) with corners at
     //* (0.25, O.25 , O.0) and (0.75 , O.75 , O.0)
     gl.glColor3f(1.0f, 1.0f, 1.0f);
      gl.glBegin(GL2.GL_POLYGON);
      {
         gl.glVertex2i(5, 5);
         gl.glVertex2i(195, 5);
         gl.glVertex2i(195, 45);
         gl.glVertex2i(5, 45);
      }
      gl.glEnd();
      gl.glColor3f(0.75f, 1.0f, 0.375f);
      gl.glBegin(GL2.GL_POLYGON);
      {
         gl.glVertex2i(195, 45);
         gl.glVertex2i(267, 45);
         gl.glVertex2i(267, 95);
         gl.glVertex2i(195, 95);
      }
      gl.glEnd();
      gl.glColor3f(0.95f, 0.15f, 0.25f);
      gl.glBegin(GL2.GL_POLYGON);
      {
         gl.glVertex2i(267, 95);
         gl.glVertex2i(395, 95);
         gl.glVertex2i(395, 145);
         gl.glVertex2i(267, 145);
      }
      gl.glEnd();
      //* force the above commands to begin executing.
     //* don't wait! start processing buffered OpenGL routines
     gl.glFlush();
   }
   /**
    * {@inheritDoc}
    */

   public void displayChanged(final GLAutoDrawable drawable,
         final boolean modeChanged, final boolean deviceChanged) { }
   /**
    * {@inheritDoc}
    */

   public void dispose(final GLAutoDrawable drawable) { }
   /**
    * {@inheritDoc}
    */

   public void init(final GLAutoDrawable drawable) {
      if (gl == null) {
         gl = drawable.getGL().getGL2();
      }
      // clear color is a state variable
     //* select clearing (background) color
     gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      //* initialize viewing values
     gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
      gl.glLoadIdentity();
      gl.glOrtho(0.0, // left
           400.0,  // right
           0.0,    // bottom
           200.0,  // top
           -1.0,   // near
           1.0);   // far
  }
   /**
    * {@inheritDoc}
    */

   public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
}
/** Application entry point. */
public final class RedBookMain {
   /**
    * Application entry point.
    * @param args the command line arguments; ignored by this application
    */

   public static void main(final String[] args) {

      JFrame frame = new JFrame();
      frame.addWindowListener(new WindowHandler(frame));
      frame.setPreferredSize(new Dimension(400, 200));
      frame.getContentPane().add(Ex1_2.getCanvas());

      frame.pack();
      frame.setVisible(true);
   }
   /** Hidden constructor. */
   private RedBookMain() { }
}
Offline gouessej

JGO Kernel
*****

Posts: 3560
Medals: 30


TUER


« Reply #1 on: 2010-01-22 13:00:30 »

Does it work now?

Julien Gouesse
Offline broumbroum

Sr. Member
**

Posts: 384



« Reply #2 on: 2010-01-22 18:41:14 »

implement reshape with the correct viewport and projection
1  
2  
3  
4  
5  
6  
7  
8  
/**
    * {@inheritDoc}
    */

   public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
 gl.glViewport(x,width, y, height) ...
gl.gl...
}
}

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.189 seconds with 21 queries.