Understanding Jogl Window Frame

时间:2018-02-03 11:03:15

标签: opengl jogl

I just ran my first java program running JOGL. I have worked on c++ using the Glut.h library. I have a question as I am trying to understand the coordinate system on the graphic window displayed using JOGL. Now, in c++, the window coordinate system begins at the top left corner of the window (that is where the origin is). However, when I run the below JOGL program, the origin is not at the top left corner. The below program will draw 3 lines, all of which start at the origin:

import com.sun.opengl.util.Animator;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

public class Lab2 implements GLEventListener {
    public static void main(String[] args) {
        Frame frame = new Frame("My First JOGL Project");
        GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(new Lab2());
        frame.add(canvas);
        frame.setSize(640, 480);
        final Animator animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
            // Run this on another thread than the AWT event queue to
            // make sure the call to Animator.stop() completes before
            // exiting
                new Thread(new Runnable() {
                    public void run() {
                    animator.stop();
                    System.exit(0);
                    }
                }).start();
            }
        });
        // Center frame
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        animator.start();
    }
    public void init(GLAutoDrawable drawable) {
        // Use debug pipeline
        // drawable.setGL(new DebugGL(drawable.getGL()));
        GL gl = drawable.getGL();
        System.err.println("INIT GL IS: " + gl.getClass().getName());
        // Enable VSync
        gl.setSwapInterval(1);
        // Setup the drawing area and shading mode
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL.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) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();
        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(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        //glu.gluPerspective(45.0f, h, 1.0, 20.0);
        glu.gluPerspective(45.0f, h, 2.0, 20.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        // 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();
        // Move the "drawing cursor" around
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
        // Drawing Lines
        gl.glBegin(GL.GL_LINES);
        gl.glColor3f(0.0f, 1.0f, 0.0f); // Set the current drawing color to green
        gl.glVertex3f(0.0f, 0.0f, 0.0f); //Set first vertex of first line
        gl.glVertex3f(1.0f, 0.0f, 0.0f); //Set second vertex of first line

        gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the current drawing color to blue
        gl.glVertex3f(0.0f, 0.0f, 0.0f); //Set first vertex of second line
        gl.glVertex3f(0.0f, 1.0f, 0.0f); //Set second vertex of second line

        gl.glColor3f(1.0f, 0.0f, 0.0f); // Set the current drawing color to red
        gl.glVertex3f(0.0f, 0.0f, 0.0f); //Set first vertex of third line
        gl.glVertex3f(0.0f, 0.0f, 1.0f); //Set second vertex of third line
        gl.glEnd();
        // Flush all drawing operations to the graphics card
        gl.glFlush();
    }
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean
        deviceChanged) {}
}

Can someone epxlain?

0 个答案:

没有答案