Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Resolved]Problem with GL Context  (Read 1292 times)
0 Members and 1 Guest are viewing this topic.
Offline omlip

Junior Newbie





« Posted 2006-05-23 17:57:43 »

Hello everyone,
I'm new with OpenGL in general and so with JOGL...

My objectives in the excercice i want to realise is simple
"Change the polygon mode when i pressed the 'p'  key".
For that i have:
Part1 class like this :

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  
[size=12pt]
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.JFrame;

import com.sun.opengl.util.Animator;

public class Part1 {

   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setSize(300, 300);

      GLCanvas canvas = new GLCanvas(new GLCapabilities());
      frame.add(canvas);

      Renderer rend = new Renderer();
      canvas.addGLEventListener(rend);
      canvas.addKeyListener(rend);

      final Animator animator = new Animator(canvas);
      frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            animator.stop();
            System.exit(0);
         }
      });
      frame.setVisible(true);
      animator.start();

   }
}
[/size]


My renderer which implements GLEventListener and KeyListener
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  
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.media.opengl.DebugGL;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLEventListener;

public class Renderer implements GLEventListener, KeyListener {
   static int cpt = 0;

   public void keyPressed(KeyEvent arg0) {
      /*GLContext.getCurrent().makeCurrent();
      GL gl = GLContext.getCurrent().getGL();*/

      switch (arg0.getKeyChar()) {
      case 'p':
         //gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
     }
   }

   public void keyReleased(KeyEvent arg0) {
      System.out.println("relacher");

   }

   public void keyTyped(KeyEvent arg0) {
      System.out.println("typer");

   }

   public void init(GLAutoDrawable arg0) {
      GL gl = arg0.getGL();
      arg0.setGL(new DebugGL(arg0.getGL()));
     
      System.out.println("Init GL is " + gl.getClass().getName());
   }

   public void display(GLAutoDrawable arg0) {
      GL gl = arg0.getGL();
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      gl.glPointSize(2.0f);
      gl.glClear(GL.GL_COLOR_BUFFER_BIT);

      gl.glBegin(GL.GL_POLYGON);
      gl.glColor3f(1.0f, 0.0f, 0.0f);
      gl.glVertex2f(-0.5f, -0.5f);
      gl.glColor3f(0.0f, 1.0f, 0.0f);
      gl.glVertex2f(0.5f, -0.5f);
      gl.glColor3f(0.0f, 0.0f, 1.0f);
      gl.glVertex2f(0.5f, 0.5f);
      gl.glColor3f(1.0f, 1.0f, 1.0f);
      gl.glVertex2f(-0.5f, 0.5f);

      gl.glEnd();
      gl.glFlush();

      // System.out.println(cpt++);
  }

   public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
         int arg4) {
      System.out.println("reshape");
   }

   public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
      System.out.println("ghghgh");
   }

}


My problem is in the keyPressed method where i don't arrive to make the current context

So where can i find information about GL context ?

thank
oliver

 
Offline omlip

Junior Newbie





« Reply #1 - Posted 2006-05-23 22:47:51 »

hummmm maybe i didn't explain very well my problem


I will aske another way ...

How can we invoke methode from the GL instance outside the method init, display, reshape etc....


Thanks a lot
omlip
Offline Mithrandir

Senior Member




Cut from being on the bleeding edge too long


« Reply #2 - Posted 2006-05-26 08:32:12 »

Well the first thing is that you really do not want to do your own GL context rendering in the way you've done it. Making something current on the windows event thread will not get you the context that is current for the drawable you're working with. In fact, what you're doing with that GL call in the event listener handler is very dangerous and likely to cause all sorts of weird things to happen. You are far better of having a local state variable that the key listener sets to the appropriate state, and then at the beginning of your rendering loop set the right GL state at that time.

The site for 3D Graphics information http://www.j3d.org/
Aviatrix3D JOGL Scenegraph http://aviatrix3d.j3d.org/
Programming is essentially a markup language surrounding mathematical formulae and thus, should not be patentable.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline omlip

Junior Newbie





« Reply #3 - Posted 2006-05-26 15:03:35 »

firstly , thank for your advise and solution


i change my program like this
add an if condition in the "display method" Smiley

1  
2  
3  
4  
5  
6  
7  
8  
if(this.polygonMode != -1)
{
     switch(polygonMode){
            case 'p' : gl.glPolygonMode(GL.GL_FRONT_AND_BACK,GL.GL_POINT);
            ......

}
}


it works correctly now


but in a great project, won't it be complex to manage all capacities with local variables?

thanks again
omlip
Offline Mithrandir

Senior Member




Cut from being on the bleeding edge too long


« Reply #4 - Posted 2006-05-27 06:25:06 »

No, it will be far easier to do so because you don't have to worry about all sorts of oddball states happening. Collecting everything into a single class, and then executing it all at once at exactly the right time is the correct way to do it, and also the easiest to maintain and modify in the future.

The site for 3D Graphics information http://www.j3d.org/
Aviatrix3D JOGL Scenegraph http://aviatrix3d.j3d.org/
Programming is essentially a markup language surrounding mathematical formulae and thus, should not be patentable.
Offline omlip

Junior Newbie





« Reply #5 - Posted 2006-05-27 12:19:45 »

okay thanks

i will marked this topic as resolved
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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 (75 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (220 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!