Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / JOGL Development / scrolling or something like that on: 2007-05-24 13:29:22
Hello,

I have a program that displays comething in 2D on the screen. I have also an independent zoom by the X and Y axis (I can zoom with the mouse wheel in Y and by selection a region from the screen in X).
When the program starts, there are some nombers that are displayed on the screen along the two axis, they represent the coordinates . At the beginning I had a for(i=0; i <END_OF_X_AXIS; i++) to display all the numbers. But this kind of instruction slow down the computer , I have a 100% CPU usage. So I had an idea to draw these numbers only in the region where I'm seeing something (in the projection region) and to draw the rest of the numers when I'm scrolling. I'm using Java SWING JScrollBar, when I'm scrolling, I'm changing X ot Y and I'm using the glTranslate(X,Y,0) function to go to another place over the drawn scene i.e. if I scroll to the right I'm getting the scroll value and placing in the glTranslate(). I'm trying to use this same value to draw the next number that will appear ot the righ side over the X axis. For example on the screen I'm seeing the numbers between 5 and 10. So when I'm scrolling to the right I want to see 11 and the 5 must disapear. Finally I want to see 6 7 ... 11.
But ... it doesn't work Sad . Sometimes the numbers are over the axis but in a different place or they are not drawn at all.
So, can someone give me an advice how to do this ?

Regards,

Anton

PS. I had also another idea before, it was not to use a scrollbar but to "catch" the sceen and to move it with the mouse in the direction I want, but I didn't know how something like this can be done with OpenGL.
2  Discussions / General Discussions / Multiple GLJPanels and GLJCanvases on: 2007-04-12 18:15:54
Hello,

I found an example on the net where someone show how to create an application with two canvases with sharing the context.
The example , you can see it here:
http://www.cs.plu.edu/~dwolff/talks/jogl-ccsc/src/h_MultipleGLCanvases/MultipleGLCanvasesExample.java

It works fine and the code is clear Smiley
Now on my problem, I'm developping a program and I would like to use a multiple GLJPanels and not GLCanvases. That because I'm using an JInternalFrame and GLCanvas can't work with the internal frame.
So I tried to make the same example but with GJJPanel ... but it doesn't work good. When I'm trying to put the second GLJPanel, there is an exception...
This is my code:

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  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.opengl.DebugGL;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.media.opengl.glu.GLU;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


import com.sun.opengl.util.FPSAnimator;



public class MultipleGLJPanelExample extends JFrame implements ActionListener {
   
   private static final long serialVersionUID = 0L;

   // The animator object
  private FPSAnimator anim1, anim2;
   
   // Frames per second
  private int fps = 30;
   
   // The rotation angle
  private double angle = 0.0;
   
   // Start and stop button
  private JButton startStopButton;
   
   // The ID for the shared display list
  private int cubeList;
   
   /*
    * Start or stop both animators.
    */

   public void actionPerformed( ActionEvent e )
   {
      if( startStopButton.getText() == "Start")
      {
         anim1.start();
         //anim2.start();
        startStopButton.setText("Stop");
      }
      else if( startStopButton.getText() == "Stop") {
         anim1.stop();
         //anim2.stop();
        startStopButton.setText("Start");
      }
     
   }
   
   public MultipleGLJPanelExample()
   {
     
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   
     
      GLJPanel mainPanel = new GLJPanel(); // this is like in the original code : mainPanel contains the 2 others panels 1 and 2
                                                                                          //when I'm using this panel here, the panel1 appears too small, and when I don't it appears fine ... I don't know why :(
                                                                                          //this is another one of my problems with this application
     
      GLJPanel panel1 = new GLJPanel();
      panel1.setSize(200, 200);
      panel1.addGLEventListener(new Panel1Listener());
      mainPanel.add(panel1);
      panel1.requestFocus();
      anim1 = new FPSAnimator(panel1,fps);
      anim1.start();

     
      //here I'm trying to put the second JPanel
     /*
      GLJPanel panel2 = new GLJPanel(null,null,panel1.getContext()); // and here I have the exception, compile the code and you'll see it
      panel2.setSize(200,200);
      panel2.addGLEventListener(new Panel2Listener());
      mainPanel.add(panel2);
      panel1.requestFocus();
      anim2 = new FPSAnimator(panel2,fps);
      anim2.start();
      */

     
      JPanel buttonPanel = new JPanel();
      buttonPanel.add( startStopButton = new JButton("Stop"));
      startStopButton.addActionListener(this);
      getContentPane().add( buttonPanel, BorderLayout.SOUTH );
     
      getContentPane().add( mainPanel, BorderLayout.CENTER );
     
     
      setSize(600,600);
      setVisible( true );
   }
   
   private class Panel1Listener implements GLEventListener
   {
      // Cube data
     private float[][] cubeData = { {-0.5f, -0.5f, 0.5f}, {0.5f, -0.5f, 0.5f},
            {0.5f, 0.5f, 0.5f}, {-0.5f, 0.5f, 0.5f}, {-0.5f, -0.5f, -0.5f},
            {0.5f, -0.5f, -0.5f}, {0.5f, 0.5f, -0.5f}, {-0.5f, 0.5f, -0.5f}
      };
     
      private GLU glu = new GLU();
     
      public void init( GLAutoDrawable d ) {
         GL gl = d.getGL();
         gl.glClearColor( 0,0,0,0 );
         
         float[] lightAmbient = {0.4f, 0.4f, 0.4f, 1.0f};
         float[] lightDiffuse = {0.8f, 0.8f, 0.8f, 1.0f};
         float[] lightSpecular = {1.0f, 1.0f, 1.0f, 1.0f};
         float[] lightPosition = {0.0f, 0.0f, 10.0f, 1.0f};
         
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmbient,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightDiffuse,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lightSpecular,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition,0);
         
         gl.glEnable(GL.GL_DEPTH_TEST);
         gl.glEnable(GL.GL_LIGHT0);
         gl.glEnable(GL.GL_LIGHTING);
         
         gl.glCullFace(GL.GL_BACK);
         gl.glEnable(GL.GL_CULL_FACE);
         gl.glMatrixMode( GL.GL_MODELVIEW );
         gl.glLoadIdentity();
         
         // Create the cube display list
        cubeList = gl.glGenLists(1);
         gl.glNewList(cubeList, GL.GL_COMPILE);
         drawCube(gl);
         gl.glEndList();
      }
     
      public void reshape( GLAutoDrawable d, int x, int y, int width, int height ) {
         GL gl = d.getGL();
         gl.glMatrixMode( GL.GL_PROJECTION );
         gl.glLoadIdentity();
         glu.gluPerspective(60.0, (float)width/height, 0.5, 100.0);
         gl.glMatrixMode( GL.GL_MODELVIEW );
      }
     
      /*
       * Draw the scene.
       */

      public void display( GLAutoDrawable d ) {

         angle += 1.0;
         if( angle > 180.0 ) angle -= 360.0;
         
         // Draw the frame
        GL gl = d.getGL();
         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
         gl.glLoadIdentity();
         glu.gluLookAt(0,2.0,2.0,0,1.0,0,0,1,0);
         drawFloor(gl,true);
         gl.glTranslated(0,1,0);
         gl.glRotated(angle,0.5,0.5,-0.5);
         gl.glCallList(cubeList);
      }
     
      private void drawCube(GL gl)
      {
         gl.glBegin(GL.GL_QUADS);
            // Front face
           gl.glNormal3f(0,0,1.0f);
           
            gl.glVertex3fv(cubeData[0],0);
            gl.glVertex3fv(cubeData[1],0);
            gl.glVertex3fv(cubeData[2],0);
            gl.glVertex3fv(cubeData[3],0);
            // Back face  
           gl.glNormal3f(0,0,-1.0f);
            gl.glVertex3fv(cubeData[5],0);
            gl.glVertex3fv(cubeData[4],0);
            gl.glVertex3fv(cubeData[7],0);
            gl.glVertex3fv(cubeData[6],0);
            // Top face
           gl.glNormal3f(0,1.0f,0.0f);
            gl.glVertex3fv(cubeData[3],0);
            gl.glVertex3fv(cubeData[2],0);
            gl.glVertex3fv(cubeData[6],0);
            gl.glVertex3fv(cubeData[7],0);
         gl.glEnd();
         
         gl.glBegin(GL.GL_QUADS);
            // Left face
           gl.glNormal3f(-1.0f, 0.0f,0.0f);
            gl.glVertex3fv(cubeData[4],0);
            gl.glVertex3fv(cubeData[0],0);
            gl.glVertex3fv(cubeData[3],0);
            gl.glVertex3fv(cubeData[7],0);
            // Right face
           gl.glNormal3f(1.0f,0.0f,0.0f);
            gl.glVertex3fv(cubeData[1],0);
            gl.glVertex3fv(cubeData[5],0);
            gl.glVertex3fv(cubeData[6],0);
            gl.glVertex3fv(cubeData[2],0);
            // Bottom face
           gl.glNormal3f(0,-1.0f,0.0f);
            gl.glVertex3fv(cubeData[4],0);
            gl.glVertex3fv(cubeData[5],0);
            gl.glVertex3fv(cubeData[1],0);
            gl.glVertex3fv(cubeData[0],0);
         gl.glEnd();
      }

      public void displayChanged( GLAutoDrawable d, boolean modeChanged,
         boolean deviceChanged ) { }

   }
   
   private class Panel2Listener implements GLEventListener
   {      
     
      private GLU glu = new GLU();
     
      public void init( GLAutoDrawable d ) {
         d.setGL(new DebugGL(d.getGL()));
         GL gl = d.getGL();
         gl.glClearColor( 0,0,0,0 );
         
         float[] lightAmbient = {0.4f, 0.4f, 0.4f, 1.0f};
         float[] lightDiffuse = {0.8f, 0.8f, 0.8f, 1.0f};
         float[] lightSpecular = {1.0f, 1.0f, 1.0f, 1.0f};
         float[] lightPosition = {0.0f, 0.0f, 10.0f, 1.0f};
         
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmbient,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightDiffuse,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lightSpecular,0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition,0);
         
         gl.glEnable(GL.GL_DEPTH_TEST);
         gl.glEnable(GL.GL_LIGHT0);
         gl.glEnable(GL.GL_LIGHTING);
     
         gl.glCullFace(GL.GL_BACK);
         gl.glEnable(GL.GL_CULL_FACE);
         gl.glMatrixMode( GL.GL_MODELVIEW );
         gl.glLoadIdentity();        
      }
     
      public void reshape( GLAutoDrawable d, int x, int y, int width, int height ) {
         GL gl = d.getGL();
         gl.glMatrixMode( GL.GL_PROJECTION );
         gl.glLoadIdentity();
         glu.gluPerspective(60.0, (float)width/height, 0.5, 100.0);
         gl.glMatrixMode( GL.GL_MODELVIEW );
      }
     
      public void display( GLAutoDrawable d ) {      
         // Draw the frame
        GL gl = d.getGL();
         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
         gl.glLoadIdentity();
         glu.gluLookAt(10.5,10.5,10.5,0,1,0,0,1,0);
         drawFloor(gl,true);
         gl.glTranslated(0,1,0);
         gl.glRotated(angle,0.5,0.5,-0.5);
         gl.glCallList(cubeList);
      }

      public void displayChanged( GLAutoDrawable d, boolean modeChanged,
         boolean deviceChanged ) { }
     
   }
   
   private void drawFloor(GL gl, boolean wireframe) {
      int xdivs = 50;
      int zdivs = 50;
      double xmin = -10.0, xmax = 10.0;
      double zmin = -10.0, zmax = 10.0;
      double x1, x2, y = 0.0, z;
      double xsize = (xmax - xmin) / xdivs;
      double zsize = (zmax - zmin) / zdivs;
     
      if( wireframe ) {
         gl.glPushAttrib(GL.GL_LIGHTING_BIT | GL.GL_POLYGON_BIT
               | GL.GL_COLOR_BUFFER_BIT | GL.GL_TEXTURE_BIT);
         gl.glDisable(GL.GL_LIGHTING);
         gl.glDisable(GL.GL_TEXTURE_2D);
         gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
         gl.glColor3f(1.0f,0.2f,0.2f);
      }
     
      for( int i = 0; i < xdivs ; i++ ) {
         x1 = (i * xsize) + xmin;
         x2 = ((i+1) * xsize) + xmin;
         
         gl.glBegin(GL.GL_QUAD_STRIP);
         for(int j = 0; j <= zdivs; j++) {
            z = (j * zsize) + zmin;
            gl.glVertex3d(x2,y,z);
            gl.glVertex3d(x1,y,z);
         }
         gl.glEnd();
      }
     
      if(wireframe) {
         gl.glPopAttrib();
      }
   }
   
   public static void main( String[] args ) {

      new MultipleGLJPanelExample();
   }

}


Any ideas ?


Regards,

Anton
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!