- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.media.opengl.*;
- import javax.media.opengl.glu.*;
- import com.sun.opengl.util.*;
- public class GLTest implements GLEventListener {
- GL gl;
- GLU glu;
- int width;
- int height;
- float nearClipping = 0.1f;
- float farClipping = 100.0f;
- public static void main(String[] args) {
- // Create a Window
- Frame win = new Frame("GL Test");
- win.setSize(640, 480);
- // JOGL
- GLCapabilities glCaps = new GLCapabilities();
- glCaps.setRedBits(8);
- glCaps.setBlueBits(8);
- glCaps.setGreenBits(8);
- glCaps.setAlphaBits(8);
- GLCanvas canvas = new GLCanvas(glCaps);
- final Animator anim = new Animator(canvas);
- // Window Close Event
- win.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- new Thread(new Runnable() {
- public void run() {
- anim.stop();
- System.exit(0);
- }
- }).start();
- }
- });
- win.add(canvas);
- canvas.addGLEventListener(new GLTest(640, 480));
- win.setVisible(true);
- anim.start();
- }
- public GLTest(int width, int height) {
- this.width = width;
- this.height = height;
- }
- public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
- }
- public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
- this.width = width;
- this.height = height;
- // Reset Viewport and Projection
- gl.glViewport(0, 0, width, height);
- gl.glMatrixMode(GL.GL_PROJECTION_MATRIX);
- gl.glLoadIdentity();
- double ratio = (double)width/(double)height;
- glu.gluPerspective(45.0f, ratio, nearClipping, farClipping);
- }
- public void display(GLAutoDrawable drawable) {
- // Clear
- gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
- // Modelview Matrix
- gl.glMatrixMode(GL.GL_MODELVIEW_MATRIX);
- gl.glLoadIdentity();
- gl.glBegin(GL.GL_QUADS);
- gl.glColor3f(0.5f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 0.0f, 0.0f);
- gl.glColor3f(0.0f, 0.5f, 0.0f); gl.glVertex3f(0.5f, 0.0f, 0.0f);
- gl.glColor3f(0.0f, 0.0f, 0.5f); gl.glVertex3f(0.5f, 0.5f, 0.0f);
- gl.glColor3f(0.5f, 0.5f, 0.0f); gl.glVertex3f(0.0f, 0.5f, 0.0f);
- gl.glEnd();
- gl.glTranslatef(0.0f, 0.0f, -5.0f); // <--- HIER LIEGT DAS PROBLEM Oo
- gl.glBegin(GL.GL_QUADS);
- gl.glColor3f(1.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 0.0f, 0.0f);
- gl.glColor3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(1.0f, 0.0f, 0.0f);
- gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(1.0f, 1.0f, 0.0f);
- gl.glColor3f(1.0f, 1.0f, 0.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f);
- gl.glEnd();
- }
- public void init(GLAutoDrawable drawable) {
- gl = drawable.getGL();
- glu = new GLU();
- // Debug Mode
- //drawable.setGL(new DebugGL(drawable.getGL()));
- // Initialize Viewport and Projection
- gl.glViewport(0, 0, width, height);
- gl.glMatrixMode(GL.GL_PROJECTION_MATRIX);
- gl.glLoadIdentity();
- double ratio = (double)width/(double)height;
- glu.gluPerspective(45.0f, ratio, nearClipping, farClipping);
- // Enable GL Functions
- gl.glEnable(GL.GL_DEPTH_TEST);
- // gl.glEnable(GL.GL_CULL_FACE);
- }
- }