JOGL in Eclipse

何谓JOGL?

Java也许是最流行的真正的面向对象的编程语言。有许多用Java去结合OpenGL的尝试,但是第一个被大家认可并注意的是Java对于OpenGl的绑定(Java Bindings for OpenGL), 或者称为JOGL.理由是它得到Sun(Java的创建者)和SGI(OpenGL的创建者)的支持。

This getting started-tutorial is intended for JOGL users that are mere beginners. It helps to setup a recent JOGL installation in Eclipse on Windows. There is a video-walk-through further down the post, in case you dont feel like reading.

First you need to download JOGL. This is actually more difficult than it sounds because the precompiled binary packages are a bit hidden in the new project site. Fortunately, I figured outwhere you can download recent JOGL binaries.

For this tutorial, I am using

  • jogl-b633-2012-01-23_20-37-13, and
  • gluegen-b480-2012-01-23_16-49-04.

This is how your project setup should look like:

JOGL Project Setup

So one you got the binaries, unpack the ZIP file somewhere. You will notice the jar folder with a number of JAR files. All you need is in that jar folder, because the DLL binaries are included in the JARs.

Create a new eclipse project (name doesnt matter) and add the following JARs on the classpath:

  • gluegen-rt-natives-windows-amd64.jar
  • gluegen-rt.jar
  • jogl-all-natives-windows-amd64.jar
  • gluegen-rt.jar

These files are the native part of the OpenGL binding and are linked via Java Native Interface (JNI). Since the DLLs are precompiled as part of the JAR files, you are almost ready to go.

Once you got that set up, create a package de.schabby.jogl.helloworldand copypaste the following two classes in the newly created package.

package de.schabby.jogl.helloworld;
 
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
 
public class HelloWorld 
{
    public static void main(String[] args) 
    {
        // setup OpenGL Version 2
        GLProfile profile = GLProfile.get(GLProfile.GL2);
        GLCapabilities capabilities = new GLCapabilities(profile);
 
        // The canvas is the widget that's drawn in the JFrame
        GLCanvas glcanvas = new GLCanvas(capabilities);
        glcanvas.addGLEventListener(new Renderer());
        glcanvas.setSize( 300, 300 );
 
        JFrame frame = new JFrame( "Hello World" );
        frame.getContentPane().add( glcanvas);
 
        // shutdown the program on windows close event
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                System.exit(0);
            }
        });
 
        frame.setSize( frame.getContentPane().getPreferredSize() );
        frame.setVisible( true );
    }
}

The Render.java class looks like follows. It implements GLEventListenerwhich is the call-back implementaiton by JOGL to do all OpenGL rendering.

package de.schabby.jogl.helloworld;
 
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
 
class Renderer implements GLEventListener 
{
    private GLU glu = new GLU();
 
    public void display(GLAutoDrawable gLDrawable) 
    {
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
        gl.glBegin(GL2.GL_TRIANGLES);              
        gl.glVertex3f(0.0f, 1.0f, 0.0f); 
        gl.glVertex3f(-1.0f, -1.0f, 0.0f); 
        gl.glVertex3f(1.0f, -1.0f, 0.0f); 
        gl.glEnd();                              
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        gl.glBegin(GL2.GL_QUADS);                  
        gl.glVertex3f(-1.0f, 1.0f, 0.0f); 
        gl.glVertex3f(1.0f, 1.0f, 0.0f); 
        gl.glVertex3f(1.0f, -1.0f, 0.0f); 
        gl.glVertex3f(-1.0f, -1.0f, 0.0f); 
        gl.glEnd();                              
        gl.glFlush();
    }
 
 
    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) 
    {
        System.out.println("displayChanged called");
    }
 
    public void init(GLAutoDrawable gLDrawable) 
    {
        System.out.println("init() called");
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_FLAT);
    }
 
    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) 
    {
        System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height);
        final GL2 gl = gLDrawable.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.gluPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
 
 
        public void dispose(GLAutoDrawable arg0) 
        {
                System.out.println("dispose() called");
        }
}

By right-clicking on the main() method and chosing "run as application", Eclipse will execute the program. You should see something like this:

Jogle Hello World

For the lazy, I provide a video-walkthough.

http://schabby.de/jogl-example-hello-world/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值