“你好,JOGL - Java與OpenGl的綁定”是網絡上比較容易找到的關於JOGL的入門文章,其英文版“Hello JOGL, Introducing Java Bindings for OpenGL”最早發佈於2005年2月,隨着JOGL新版本的發佈,其中的一些代碼無法在新的JOGL中正確的編譯,我在學習的過程中對該文中的代碼進行修改,從而在新版JOGL中可以正常編譯和運行。
文中代碼中需要進行的主要變化包括:
- 模塊的打包已改爲javax.media.opengl.*;
- GLEventListener的接口參數drawable類型改爲GLAutoDrawable;
- GLCanvas對象已經可以通過構造函數直接創建;
- GLU對象可以通過構造函數直接創建,不必從GLAutoDrawable中獲取;
- GLU類在javax.media.opengl.glu模塊中定義,因此需要單獨import;
修改後的代碼在JOGL 1.1.1環境下正常編譯與運行。
相關經過修改的代碼如下:
helloJogl.java:
public class helloJogl {
public static void main (String args[]) {
try {
System.loadLibrary("jogl");
System.out.println("Hello World! (The native libraries are installed.)");
}
catch (Exception e) {
System.out.println(e);
}
}
}
SimpleJoglApp.java
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
/** * This is a basic JOGL app. Feel free to
* reuse this code or modify it.
*/
public class SimpleJoglApp extends JFrame {
public static void main(String[] args) {
final SimpleJoglApp app = new SimpleJoglApp();
// show what we've done
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
app.setVisible(true);
}
}
);
}
public SimpleJoglApp() {
//set the JFrame title
super("Simple JOGL Application");
//kill the process when the JFrame is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//only three JOGL lines of code ... and here they are
GLCapabilities glcaps = new GLCapabilities();
GLCanvas glcanvas = new GLCanvas(glcaps);
//GLDrawableFactory.getFactory().createGLCanvas(glcaps);
glcanvas.addGLEventListener(new SimpleGLEventListener());
//add the GLCanvas just like we would any Component
getContentPane().add(glcanvas, BorderLayout.CENTER);
setSize(500, 300);
//center the JFrame on the screen
centerWindow(this);
}
public void centerWindow(Component frame) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.width > screenSize.width )
frameSize.width = screenSize.width;
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
// Frame.setLocation (
// (screenSize.width - frameSize.width ) >> 1,
// (screenSize.height - frameSize.height) >> 1
// );
}
}
SimpleGLEventListener.java
import java.awt.event.*;
import javax.media.opengl.*;
/*** For our purposes only two of the
* GLEventListeners matter. Those would
* be init() and display().
* 爲了達到我們的目的,GLEventListener中只有兩個方法有用。
* 它們是init()和display()。
*/
public class SimpleGLEventListener implements GLEventListener{
/**
* Take care of initialization here.
* 注意這裏的初始化。
*/
public void init(GLAutoDrawable drawable) { }
/**
* Take care of drawing here.
* 注意這裏的繪圖。
*/
public void display(GLAutoDrawable drawable) { }
/**
* Called when the GLDrawable (GLCanvas
* or GLJPanel) has changed in size. We
* won't need this, but you may eventually
* need it -- just not yet.
* 當GLDrawable(GLCanvas或GLJPanel)大小改變時被調用。
* 我們不需要它,但你可能最後會用到——雖然現在並不需要。
*/
public void reshape(
GLAutoDrawable drawable,
int x,
int y,
int width,
int height) {}
/** * If the display depth is changed while the
* program is running this method is called.
* Nowadays this doesn't happen much, unless
* a programmer has his program do it.
* 當程序運行時顯示深度被改變的時候此方法被調用。
* 現在這種事發生得不多,除非程序裏面觸發此事。
*/
public void displayChanged(
GLAutoDrawable drawable,
boolean modeChanged,
boolean deviceChanged) {}
}
SecondJoglApp.java
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
/*
* this is a basic JOGL app, feel free
* to reuse this code or mordify it.
*/
public class SecondJoglApp extends JFrame {
public static void main(String[] args) {
final SecondJoglApp app = new SecondJoglApp();
// show what we've done
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
app.setVisible(true);
}
}
);
}
public SecondJoglApp() {
// set the frame title
super("Second JOGL Application");
// kill the process when the JFrame is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// only three JOGL lines of code ... and here they are
GLCapabilities glaps = new GLCapabilities();
GLCanvas glcanvas = new GLCanvas(glaps);
glcanvas.addGLEventListener(new SecondGLEventListener());
// add the GLCanvas just like we would any component
getContentPane().add(glcanvas, BorderLayout.CENTER);
setSize(500, 300);
// center the JFrame on the screen
centerWindow(this);
}
public void centerWindow(Component Frame) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = Frame.getSize();
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
Frame.setLocation (
(screenSize.width - frameSize.width) >> 1,
(screenSize.height - frameSize.height) >> 1);
}
}
SecondGLEventListener.java
import javax.media.opengl.glu.*;
/*
* for our purposes only tow of the GLEventListeners matter.
* those would be init() and display().
*/
public class SecondGLEventListener implements GLEventListener {
/*
* take care of initialization here.
*/
public void init (GLAutoDrawable gld) {
GL gl = gld.getGL();
GLU glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(0, 0, 500, 300);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0.0, 500.0f, 0.0, 300.0f);
}
/*
* take care of drawing here
*/
public void display(GLAutoDrawable drawable) {
float red = 0.0f;
float green = 0.0f;
float blue = 0.0f;
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glPointSize(5.0f);
for (int i=0; i<50; i++) {
red -= .09f;
green -= .12f;
blue -= .15f;
if (red < 0.15) red = 1.0f;
if (green < 0.15) green = 1.0f;
if (blue < 0.15) blue = 1.0f;
gl.glColor3f(red, green, blue);
gl.glBegin(GL.GL_POINTS);
gl.glVertex2i(i*10, 150);
gl.glEnd();
}
}
public void reshape(
GLAutoDrawable drawable,
int x,
int y,
int width,
int height
) {}
public void displayChanged(
GLAutoDrawable drawable,
boolean modeChanged,
boolean deviceChanged
) {}
}
參考資料:
1.“你好,JOGL - Java與OpenGl的綁定”,
http://java.ccidnet.com/art/297/20060320/483789_1.html;
2.“Hello JOGL, Introducing Java Bindings for OpenGL”來源於
http://www.javaworld.com/javaworld/jw-02-2005/jw-0221-jogl.html
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>