jogl 2

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

jogl 2

sindroide
Hi, inside a for statement I need to perform an animator.pause() and then animator.resume() , so I can rotate my object with "i" variable ... but this needs to be achieved from a JButton (that's why the for statement, bc with one click the object has to rotete on one axis). I think animator has to be paused and resumed for that... otherwise i'm seeing that the object rotate with the last value of "i". So finally I see animator.pause() is on jogl 2, but when I delete libraries jogl 1.1 and import jar's for jogl 2, then I found that gl.glLoadIdentity method is missing ... and some other constants too (regarding with GL).

Can someone help me please... all what I need it's a rotation of an object achieved with a JButton. Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: jogl 2

sindroide
sorry I found out that I have to use GL2
Reply | Threaded
Open this post in threaded view
|

Re: jogl 2

sindroide
there's no way I can get the desired rotation effect from a JButton, could someone please help me with this?? thanks a lot

I even tried Thread.sleep(10);  for doing the increment of rotation variable.... but nothing
Reply | Threaded
Open this post in threaded view
|

Re: jogl 2

Sven Gothel
Administrator
On 05/24/2012 10:23 AM, sindroide [via jogamp] wrote:
> there's no way I can get the desired rotation effect from a JButton, could
> someone please help me with this?? thanks a lot
>
> I even tried Thread.sleep(10);  for doing the increment of rotation
> variable.... but nothing
>

If you are able to produce a small & simple example in one file
and post it here, we might be able to help.

Since the dimensional space of making errors is infinitive here
it is almost impossible to give you any advise
based on the information at hand. <smiley />

~Sven
Reply | Threaded
Open this post in threaded view
|

Re: jogl 2

sindroide
public class GUI extends javax.swing.JFrame implements GLEventListener {
private Animator animator;
private double z=10, rot=0;
private javax.swing.JButton jButton1;
private javax.media.opengl.awt.GLJPanel gLJPanel2;
public GUI() {
jButton1 = new javax.swing.JButton();
jButton1.setText("Rotate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                for(int i=1; i<361; i++) {
                     rot+=1;
                }
            }
        });
gLJPanel2 = new javax.media.opengl.awt.GLJPanel();
//adding the gLJPanel2 to the frame
javax.swing.GroupLayout gLJPanel2Layout = new javax.swing.GroupLayout(gLJPanel2);
        gLJPanel2.setLayout(gLJPanel2Layout);
        gLJPanel2Layout.setHorizontalGroup(
            gLJPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 281, Short.MAX_VALUE)
        );
        gLJPanel2Layout.setVerticalGroup(
            gLJPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
//adding the button to the frame
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(gLJPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

gLJPanel2.addGLEventListener(this);
animator = new Animator(gLJPanel2);
animator.start();
} //end of constructor

public void init(GLAutoDrawable drawable) {
        // Use debug pipeline
        // drawable.setGL(new DebugGL(drawable.getGL()));

        GL2 gl = drawable.getGL().getGL2();
        System.err.println("INIT GL IS: " + gl.getClass().getName());

        // Enable VSync
        gl.setSwapInterval(0);

        // Setup the drawing area and shading mode
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL2 gl = drawable.getGL().getGL2();                
        if (height <= 0) { // avoid a divide by zero error!        
            height = 1;
        }
        final float h = (float) width / (float) height;
       
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
   
        GLU glu = new GLU();        
        glu.gluPerspective(45.0f, h, 1.0, 1000.0);        
        glu.gluLookAt(x, y, -10, 0, 0, 0, 0, 1, 0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        // Clear the drawing area
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        // Reset the current matrix to the "identity"
        gl.glLoadIdentity();
        gl.glPushMatrix();
       
        gl.glTranslated(0.0f, 0.0f, z);
        gl.glRotated(rot, 1.0f, 0.0f, 0.0f);
        gl.glPointSize(5);
     
       gl.glBegin(GL.GL_TRIANGLES);
 
        gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f);
        gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
        gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f);
        gl.glPopMatrix();
       
        gl.glEnd();
        // Flush all drawing operations to the graphics card
        gl.glFlush();
        // rot+=.2%360; adding this line I have the desited effect but this has to be done when the user clicks the button
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }
   
    @Override
    public void dispose(GLAutoDrawable drawable) {
                // Hardly used.
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
}

I really hope you got the idea... and really apreciate your help Sven
Reply | Threaded
Open this post in threaded view
|

Re: jogl 2

Xerxes Rånby
2012-05-24 10:56, sindroide [via jogamp] skrev:
> jButton1.setText("Rotate");
>         jButton1.addActionListener(new java.awt.event.ActionListener() {
>             public void actionPerformed(java.awt.event.ActionEvent evt) {
>                 for(int i=1; i<361; i++) {
>                      rot+=1;
>                 }
>
You bug is in your actionPerformed.
This code will increase rot with 360 for each click of the button:
rot=360.0
rot=720.0
rot=1080.0
rot=1440.0
rot=1800.0
... etc

Replace this part with:
public void actionPerformed(java.awt.event.ActionEvent evt) {
    rot+=.2%360;
}

and you will get what you want:
rot=0.2
rot=0.4
rot=0.6000000000000001
rot=0.8
rot=1.0
rot=1.2
rot=1.4
...

Cheers
Xerxes
Reply | Threaded
Open this post in threaded view
|

Re: jogl 2

sindroide
thanks for your help Xerxes, but I need a 360 rotation , little by little (simulating motion) ... and I'm only having one rotation with the last value of rot (not with all of them like I'm trying to achieve) . I think this has to be done with Threads but dont know really how.

each for iteration should stop , and giving the chance the display method to be called, I'm trying lot of combination without success

do you have any other advice? thanks a lot!
Reply | Threaded
Open this post in threaded view
|

Re: jogl 2

Xerxes Rånby
Try rewrite your code to let the jButton simply toggle animation on and off:

Add a boolean to your code that control if you want to animate your object or not:

private boolean animate=false;

then change the button logic to toggle this boolean:

      jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               //toggle start/stop animation
               animate=!animate;
            }
     });

Then inside your display(GLAutoDrawable drawable) {
 add:

        if(animate)
           rot+=.2%360;
Reply | Threaded
Open this post in threaded view
|

Re: jogl 2

sindroide
Thanks a lot man this is just what i've recently done ... but didn't use toggle just count to 360 and stop... anyways thanks you very much... this is kind and awful solution, isn't it? bah... maybe it's the right solution for openGL programming... dont know really