glBegin/glEnd inside methods of MouseListener

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

glBegin/glEnd inside methods of MouseListener

rhdxmr
public void mousePressed(MouseEvent me) {
            gl.glBegin(GL.GL_LINE_STRIP);
            gl.glVertex3f(0,0,0);
            gl.glVertex3f(100,50,0);
            gl.glVertex3f(200,100,0);
            gl.glVertex3f(100,150,0);
            gl.glEnd();
            gl.glFlush();
}
as you already know, mousePressed(..) method is called when mouse button is pressed.


I tested that calling mousePressed from from display(..) method as follows

public void display(GLAutoDrawable drawable) {
...
mousePressed(new MouseEvent(canvas, 1, 1L, 1, 1,1,1,true));
...
}

it shows line.


But when mousePressed is called by event, it does not.

I wonder why the latter doesn't work and how i can fix this?
Reply | Threaded
Open this post in threaded view
|

Re: glBegin/glEnd inside methods of MouseListener

Xfel
The GLContext has to be current on the thread you are rendering in. It automatically is in display, init, reshape and dispose.

so you would have to code:

public void mousePressed(MouseEvent me) {
            context.makeCurrent();

            gl.glBegin(GL.GL_LINE_STRIP);
            gl.glVertex3f(0,0,0);
            gl.glVertex3f(100,50,0);
            gl.glVertex3f(200,100,0);
            gl.glVertex3f(100,150,0);
            gl.glEnd();
            gl.glFlush();

            context.release();
}

but this is also no good cone in general, because wif your scene is rendered for other reasons, like a system-repaint, your vertices will not be drawn.

it is better if you just handle the information provided by the mouse event (eg. where the mouse points to), save it and call repaint() or display();

in display(..) you render your scene and nowhere else.
This is a part of my trackball implementation:

@Override
        public void mouseWheelMoved(MouseWheelEvent e)
        {
                transZ += e.getWheelRotation() * 6f;
                if(transZ<0) {
                        transZ=0;
                }
                drawable.display();
        }

@Override
        public void display(GLAutoDrawable drawable)
        {
                ...
               
                gl.glTranslatef(-transX, transY, transZ);
               
                gl.glRotatef(rotX, 1, 0, 0);
                gl.glRotatef(rotY, 0, 1, 0);
               
                gl.glScalef(scale, scale, scale);
               
//Now you may render
                ...
        }
Reply | Threaded
Open this post in threaded view
|

Re: glBegin/glEnd inside methods of MouseListener

rhdxmr
thanks for your advice.

But makeCurrent()/release() in mousePressed() doesn't work for me..

I found

- that display(..) and mousePressed(..) runs in the same AWT-EventQueue-0

- and that if release() is not called at the end of codeb AWT-EventQueue-0 can't get out of GLContext
and then no more AWT event can be performed :(

- and that public void display(GLAutoDrawable drawable) {
System.out.println(drawable.getContext().hashCode())
} (called automatically by event)
and public void mousePressed(MouseEvent e) {
System.out.println(this.canvas.getContext().hasCode())
} (called automatically by mouse event)
prints the same hash value.

- and I realized that it's not a good decision to make OpenGL rendering be multi-threaded.
But that only one thread dedicated to OpenGL rendering and the other threads covering the rest part is better.


However I still wonder for curiosity why makeCurrent()/release() doesn't work.

Thank you for reading my ugly English...(Even if you tried to read but failed to understand..)