Login  Register

Re: Drawing problem when height > width

Posted by Wade Walker on Jan 27, 2011; 3:50pm
URL: https://forum.jogamp.org/Drawing-problem-when-height-width-tp2361172p2363025.html

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