Drawing problem when height > width

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

Drawing problem when height > width

Djak
Hi :)
I've got a bug in an application using JOGL1.1.1 and a GLCanvas. When I reshape the Frame, if height frame is greater than width frame then nothing is drawn in the GLCanvas. Scene is drawn back again when height becomes lower than width.

Anyone may have a clue on this ?

Thanks in advance ^^
Reply | Threaded
Open this post in threaded view
|

Re: Drawing problem when height > width

gouessej
Administrator
Hi!

Switch to JOGL 2.0 as JOGL 1.1.1 is no more maintained.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Drawing problem when height > width

Wade Walker
Administrator
In reply to this post by Djak
Hi Djak,

I tested this in JOGL 1.1.1a and it works with no problems. Here's some example code (save into two separate files). Just run the program and resize the window to various shapes, you can see that it draws fine

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;

public class OneTriangle {
    protected static void setup( GL gl, int width, int height ) {
        gl.glMatrixMode( GL.GL_PROJECTION );
        gl.glLoadIdentity();

        // coordinate system origin at lower left with width and height same as the window
        GLU glu = new GLU();
        glu.gluOrtho2D( 0.0f, width, 0.0f, height );

        gl.glMatrixMode( GL.GL_MODELVIEW );
        gl.glLoadIdentity();

        gl.glViewport( 0, 0, width, height );
    }

    protected static void render( GL gl, int width, int height ) {
        gl.glClear( GL.GL_COLOR_BUFFER_BIT );

        // draw a triangle filling the window
        gl.glLoadIdentity();
        gl.glBegin( GL.GL_TRIANGLES );
        gl.glColor3f( 1, 0, 0 );
        gl.glVertex2f( 0, 0 );
        gl.glColor3f( 0, 1, 0 );
        gl.glVertex2f( width, 0 );
        gl.glColor3f( 0, 0, 1 );
        gl.glVertex2f( width / 2, height );
        gl.glEnd();
    }
}

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLCapabilities;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class OneTriangleAWT {

    public static void main(String [] args) {
        final Frame frame = new Frame( "One Triangle AWT" );
        GLCapabilities glcapabilities = new GLCapabilities();
        final GLCanvas glcanvas = new GLCanvas( glcapabilities );
        glcanvas.addGLEventListener( new GLEventListener() {
           
            @Override
            public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height ) {
                OneTriangle.setup( glautodrawable.getGL(), width, height );
            }
           
            @Override
            public void init( GLAutoDrawable glautodrawable ) {
            }
           
            @Override
            public void display( GLAutoDrawable glautodrawable ) {
                OneTriangle.render( glautodrawable.getGL(), glautodrawable.getWidth(), glautodrawable.getHeight() );
            }

            @Override
            public void displayChanged( GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged ) {
            }
        });

        frame.add( glcanvas );
        frame.addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent windowevent ) {
                frame.remove( glcanvas );
                frame.dispose();
                System.exit( 0 );
            }
        });

        frame.setSize( 640, 480 );
        frame.setVisible( true );
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Drawing problem when height > width

Djak
Thank you very much for your help :)
I tested your test case and it works very fine, so I modified it to adapted from my application and I got the same problem but I don't understand it well. I must make a mistake in the projection matrix configuration but I don't understand why it's linked to the dimensions of the frame. Here is the new code :

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;

public class OneTriangle {
    protected static void setup( GL gl, int width, int height ) {
        gl.glViewport( 0, 0, width, height );
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU glu = new GLU();
        glu.gluPerspective(45, width / height, 1, 50000);
               
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
               
    }

    protected static void render( GL gl, int width, int height ) {
        gl.glClear( GL.GL_COLOR_BUFFER_BIT );
// new triangle
     gl.glLoadIdentity();
        gl.glBegin( GL.GL_TRIANGLES );
        gl.glColor3f( 1, 0, 0 );
        gl.glVertex2f( 0, 1 );
        gl.glColor3f( 0, 1, 0 );
        gl.glVertex2f( -1, 0 );
        gl.glColor3f( 0, 0, 1 );
        gl.glVertex2f( 1, 0);
        gl.glEnd();
    }
}
************************
and added an animator :
*************************

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;

import com.sun.opengl.util.FPSAnimator;

public class OneTriangleAWT {

    public static void main(String [] args) {
        final Frame frame = new Frame( "One Triangle AWT" );
        GLCapabilities glcapabilities = new GLCapabilities();
        final GLCanvas glcanvas = new GLCanvas( glcapabilities );
        glcanvas.addGLEventListener( new GLEventListener() {
           
            @Override
            public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height ) {
                OneTriangle.setup( glautodrawable.getGL(), width, height );
            }
           
            @Override
            public void init( GLAutoDrawable glautodrawable ) {
            }
           
            @Override
            public void display( GLAutoDrawable glautodrawable ) {
                OneTriangle.render( glautodrawable.getGL(), glautodrawable.getWidth(), glautodrawable.getHeight() );
            }

            @Override
            public void displayChanged( GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged ) {
            }
        });

    // Animator
                final FPSAnimator animator = new FPSAnimator(glcanvas, 60);
                animator.start();
       
        frame.add( glcanvas );
        frame.addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent windowevent ) {
                frame.remove( glcanvas );
                frame.dispose();
                System.exit( 0 );
            }
        });

        frame.setSize( 640, 480 );
        frame.setVisible( true );
    }
}


Now, the triangle is drawn but only if height > width. And I made a mistake in my problem description, scene is drawn, but some objects are not drawn (in my other application, background color is grey, so I can see scene is drawn but I can't see objects of the scene). Anyone see something bad in this matrix configuration ?
Thanks in advance

@gouessej : I'll switch to 2.0 a few later. Actually, I got some BSOD from ATI drivers from some pre-releases of 2.0 and needed to rollback to 1.1.1. I'll retry 2.0 with same drivers as soon as possible.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing problem when height > width

Wade Walker
Administrator
Your problem could be due to integer division. In this line

glu.gluPerspective(45, width / height, 1, 50000);

if width = 199 and height = 200, then width / height = 0 by integer division. You should do something like this:

glu.gluPerspective(45, ((double)width) / ((double)height), 1, 50000);

Technically only one double cast is needed, because Java will auto-promote the other one
Reply | Threaded
Open this post in threaded view
|

Re: Drawing problem when height > width

Djak
Thanks you :)
Arf, not the first time I'm own by the float/double conversion in Java ^^
So, I tested and modified the code. There was effectively a problem with the aspect which was always bad (1.0 or 0.0).
Thanks again for showing me this point.
Unfortunately, it's now works badder because nothing is drawn at all even if height > width with the above code and the replacement of :
                glu.gluPerspective(45, ((double)width) / ((double)height), 1, 50000);

The scene is drawn but not the triangle. I'm still searching on it.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing problem when height > width

Djak
My bad, that's OK.
All works great now, the problem was really the float/double cast so the ratio was wrong
Thank you very much :)
Reply | Threaded
Open this post in threaded view
|

Re: Drawing problem when height > width

Wade Walker
Administrator
You're welcome! I've made that mistake a few times myself