[Resolved]Problem with GL Context

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 :



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();

	}
}


My renderer which implements GLEventListener and KeyListener


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

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

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.

firstly , thank for your advise and solution

i change my program like this
add an if condition in the “display method” :slight_smile:


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

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.

okay thanks

i will marked this topic as resolved