opengl - Java JOGL2 and Mouse Listeners -
i've been using jogl2 java , i'm having issue mouse event listeners. can mouse listener update x , y position if use public void mousedragged(mouseevent e), not redraw on public void display(glautodrawable gldrawable)
here's how code works: main method window , listeners constructed
import java.awt.event.windowadapter; import java.awt.event.windowevent; import javax.media.opengl.gl2; import javax.media.opengl.glcapabilities; import javax.media.opengl.gleventlistener; import javax.media.opengl.glprofile; import javax.media.opengl.awt.glcanvas; import javax.swing.jframe; import javax.media.opengl.glautodrawable; public class helloworld { public static void main(string[] args) { // setup opengl version 2 glprofile profile = glprofile.get(glprofile.gl2); glcapabilities capabilities = new glcapabilities(profile); renderer render = new renderer(); // canvas widget that's drawn in jframe glcanvas glcanvas = new glcanvas(capabilities); glcanvas.addgleventlistener(render); glcanvas.addmouselistener(render); glcanvas.addmousemotionlistener(render); glcanvas.setsize( 800, 600 ); jframe frame = new jframe( "graphics demo" ); frame.getcontentpane().add( glcanvas); // shutdown program on windows close event frame.addwindowlistener(new windowadapter() { public void windowclosing(windowevent ev) { system.exit(0); } }); frame.setsize( frame.getcontentpane().getpreferredsize() ); frame.setvisible( true ); } }
here opengl rendering class, renders 1 frame , not re-render unless adjust window size reason. if decide use mouse listener, update values not update rendering. why this?
import java.io.*; import javax.imageio.imageio; import javax.media.opengl.gl2; import javax.media.opengl.glautodrawable; import javax.media.opengl.gleventlistener; import javax.media.opengl.glu.glu; import java.awt.dimension; import java.awt.frame; import java.awt.event.mouseevent; import java.awt.event.windowadapter; import java.awt.event.windowevent; import javax.media.opengl.glautodrawable; import javax.media.opengl.glcapabilities; import javax.media.opengl.gleventlistener; import javax.media.opengl.glprofile; import javax.media.opengl.awt.glcanvas; import com.jogamp.opengl.util.fpsanimator; import javax.media.opengl.glu.gluquadric; import java.awt.graphics2d; import java.awt.color.colorspace; import java.awt.image.bufferedimage; import java.awt.image.componentcolormodel; import java.awt.image.databuffer; import java.awt.image.databufferbyte; import java.awt.image.raster; import java.awt.image.writableraster; import java.io.ioexception; import java.nio.bytebuffer; import java.awt.event.*; class renderer implements gleventlistener, mouselistener, mousemotionlistener { private glu glu = new glu(); float x = 1; float y = 1; float z = 1; private gluquadric quadric; glcanvas canvas; public void init(glautodrawable gldrawable) { system.out.println("init called"); } public void display(glautodrawable gldrawable) { final gl2 gl = gldrawable.getgl().getgl2(); gl.glclear(gl2.gl_color_buffer_bit | gl2.gl_depth_buffer_bit); /*models loaded here*/ gl.glloadidentity(); gl.gltranslatef(0.0f, -9.0f, -150.0f); //-1.5f 0.0f -6.0f gl.glrotatef(15f+x, 45f+x, 0f+y,0f); //15 45 0 0 objloader playingfield = new objloader("playingfield.obj", "crema_timber.jpg"); objloader coin = new objloader("coin.obj","early_shilling.jpg"); objloader tower = new objloader("tower1.obj","stone.jpg"); objloader tower2 = new objloader("tower2.obj","stone.jpg"); /* 3d rendering computed here */ playingfield.drawmodel(gl); coin.drawmodel(gl); tower.drawmodel(gl); tower2.drawmodel(gl); } public void displaychanged(glautodrawable gldrawable, boolean modechanged, boolean devicechanged) { system.out.println("displaychanged called"); } public void reshape(glautodrawable gldrawable, int x, int y, int width, int height) { system.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height); final gl2 gl = gldrawable.getgl().getgl2(); if (height <= 0) { height = 1; } final float h = (float) width / (float) height; gl.glviewport(0, 0, width, height); gl.glmatrixmode(gl2.gl_projection); gl.glloadidentity(); glu.gluperspective(45.0f, h, 1.0, 200.0); gl.glmatrixmode(gl2.gl_modelview); gl.glloadidentity(); } public void dispose(glautodrawable arg0) { system.out.println("dispose() called"); } @override public void mouseclicked(mouseevent arg0) { // todo auto-generated method stub system.out.println("mouse clicked"); } @override public void mouseentered(mouseevent arg0) { // todo auto-generated method stub system.out.println("mouse entered frame"); } @override public void mouseexited(mouseevent arg0) { // todo auto-generated method stub system.out.println("mouse exited frame"); } @override public void mousepressed(mouseevent arg0) { // todo auto-generated method stub system.out.println("mouse pressed."); } @override public void mousereleased(mouseevent arg0) { // todo auto-generated method stub system.out.println("mouse has been released"); } //here problem, display not update input @override public void mousedragged(mouseevent e) { // todo auto-generated method stub system.out.println("mouse being dragged"); x = e.getx(); y = e.gety(); system.out.println("x = "+x); system.out.println("y = "+y); } @override public void mousemoved(mouseevent arg0) { // todo auto-generated method stub system.out.println("mouse has moved."); } }
you may notice there functions say: objloader = new objloader("something.obj", "something.jpg");
i have class loads obj files , textures them, drawing function simple, reads in data in arrays , calls gldrawarrays (also note imports, used later).
the last thing need figure out way mouse update
in helloworld class should start animator:
fpsanimator animator = new fpsanimator(canvas, 60); animator.start();
Comments
Post a Comment