我从这里下载了 jogl 2.0文件,并运行了一个从这里jogl-all.jar
获取的简单代码示例 :
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
import com.sun.opengl.util.Animator;
/**
* This is a simple example for the method
*
* glNormal();
*
*
* Keyboard commands:
*
* Key A) Increase the deltaZ value
*
* Key B) Decrease the deltaZ value
*
* Key C) Increase the deltaX value
*
* Key D) Decrease the deltaX value
*
* Key E) Increase the deltaY value
*
* Key F) Decrease the deltaY value
*
* @author Alessandro Martinelli
*/
public class Practice11IlluminateGeometry extends JFrame implements KeyListener{
private static float deltaZ=0;
private static float deltaX=0;
private static float deltaY=0;
public static void main(String[] args) {
Practice11IlluminateGeometry frame=new Practice11IlluminateGeometry();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public Practice11IlluminateGeometry(){
setSize(600,600);
setTitle("Hello Universe");
GraphicListener listener=new GraphicListener();
GLCanvas canvas = new GLCanvas(new GLCapabilities());
canvas.addGLEventListener(listener);
getContentPane().add(canvas);
Animator animator = new Animator(canvas);
animator.start();
addKeyListener(this);
}
public class GraphicListener implements GLEventListener{
public void display(GLAutoDrawable arg0) {
GL gl=arg0.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glCullFace(GL.GL_FRONT);
gl.glEnable(GL.GL_CULL_FACE);
gl.glFrontFace(GL.GL_CW);
gl.glLoadIdentity();
gl.glTranslatef(deltaX, deltaY, deltaZ);
gl.glColor3f(1,1,1);
gl.glBegin(GL.GL_TRIANGLE_STRIP);
gl.glNormal3f(0, 0, 1);
gl.glVertex3f(0,0,0);
gl.glVertex3f(0.3f,0,-0.4f);
gl.glVertex3f(0,0.3f,0);
gl.glVertex3f(0.3f,0.3f,0.4f);
gl.glVertex3f(0.3f,0.6f,0);
gl.glEnd();
}
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
}
public void init(GLAutoDrawable arg0) {
GL gl=arg0.getGL();
gl.glEnable(GL.GL_LIGHTING);
float ambient[]= {0.2f,0.2f,0.2f,1};
gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT , ambient,0);
gl.glEnable(GL.GL_LIGHT0);
float position[]= {-0.4f,0.5f,0.7f,1};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position, 0);
float intensity[]= {1,1,1,1};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, intensity, 0);
gl.glEnable(GL.GL_LIGHT1);
float position2[]= {0,-0.8f,0.3f,1};
gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, position2, 0);
float intensity2[]= {1,0,0,0};
gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, intensity2, 0);
float specIntensity2[]= {1,1,1,1};
gl.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, specIntensity2, 0);
gl.glEnable(GL.GL_COLOR_MATERIAL);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
float specColor[]= {1,1,1,1};
gl.glMaterialfv(GL.GL_FRONT_AND_BACK,GL.GL_SPECULAR, specColor,0);
gl.glMaterialf(GL.GL_FRONT_AND_BACK,GL.GL_SHININESS, 80);
}
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
}
}
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode()==KeyEvent.VK_A){
deltaZ+=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_B){
deltaZ-=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_C){
deltaX+=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_D){
deltaX-=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_E){
deltaY+=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_F){
deltaY-=0.05;
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
static final long serialVersionUID=100;
}
我得到了:
The import javax.media.opengl.GLCanvas cannot be resolved
The import com.sun.opengl cannot be resolved
还有一些(来自其他项目):
Texture cannot be resolved to a type
Animator cannot be resolved to a type
我猜这个 jar 不支持这些类型,这是为什么呢?有什么办法吗?
这很奇怪,因为 2.0 版假设支持 1.0 版支持的所有内容,还是我错了?
此外,这段代码在 jogl 1.0 中效果很好,但我必须使用 2.0 版。