JOGL 2.0不支持GLCanvas,Texture,Animator,但是jogl 1.0呢?

时间:2013-05-01 13:15:04

标签: java opengl jar jogl

我从here下载了jogl 2.0,文件jogl-all.jar,并运行了一个来自here的简单代码示例:

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版。

2 个答案:

答案 0 :(得分:4)

在jogl 1.1和2.0之间,jogl项目的顶级包名称从javax.media.opengl更改为com.jogamp.opengl

对于列出的两个案例,Animator类现在可以在com.jogamp.opengl.util.Animator下找到,而Texture现在位于包com.jogamp.opengl.util.texture.Texture

jogl的javadoc应该有助于找到你缺少的任何其他类。 https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/

答案 1 :(得分:2)

我使用上面的例子作为模板并将其改编为Jogl 2.0。 请参阅以下代码:

import java.awt.*;
import java.awt.event.*;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;

import com.jogamp.opengl.util.Animator;

public class IlluminateGeometry extends Frame implements KeyListener {

  private static float deltaZ = 0.0f;
  private static float deltaX = 0.0f;
  private static float deltaY = 0.0f;

  static GL2 gl;

  static GLCanvas canvas;
  static GLCapabilities capabilities;
  static GLProfile profile;
  static Animator animator;

  public IlluminateGeometry() {

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        animator.stop();
        dispose();
        System.exit(0);
      }
    });

    profile = GLProfile.getDefault();
    capabilities = new GLCapabilities(profile);
    canvas = new GLCanvas(capabilities);
    animator = new Animator(canvas);
    GraphicListener graphiclistener = new GraphicListener();
    canvas.addGLEventListener(graphiclistener);
    canvas.addKeyListener(this);
    add(canvas, BorderLayout.CENTER);

    animator.start();

  }

  public class GraphicListener implements GLEventListener {

    public void display(GLAutoDrawable drawable) {
      gl = drawable.getGL().getGL2();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

      // Enables clearing of the depth buffer
      gl.glClearDepth(1.0f);
      // Z-Buffer algorithm
      gl.glEnable(GL2.GL_DEPTH_TEST);
      // The type of depth test to do
      gl.glDepthFunc(GL2.GL_LEQUAL);
      gl.glCullFace(GL2.GL_FRONT);
      gl.glEnable(GL2.GL_CULL_FACE);
      gl.glFrontFace(GL2.GL_CW);

      gl.glLoadIdentity();
      gl.glTranslatef(deltaX, deltaY, deltaZ);
      gl.glColor3f(1.0f, 1.0f, 1.0f);
      gl.glBegin(GL2.GL_TRIANGLE_STRIP);
      gl.glNormal3f(0.0f, 0.0f, 1.0f);
      gl.glVertex3f(0.0f, 0.0f, 0.0f);
      gl.glVertex3f(0.3f, 0.0f, -0.4f);
      gl.glVertex3f(0.0f, 0.3f, 0.0f);
      gl.glVertex3f(0.3f, 0.3f, 0.4f);
      gl.glVertex3f(0.3f, 0.6f, 0.0f);
      gl.glEnd();
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}

    public void init(GLAutoDrawable drawable) {
      gl = drawable.getGL().getGL2();
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

      gl.glEnable(GL2.GL_LIGHTING);
      float lightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
      gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lightAmbient, 0);

      gl.glEnable(GL2.GL_LIGHT0);
      float lightPosition[] = { -0.4f, 0.5f, 0.7f, 1.0f };
      gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPosition, 0);
      float lightIntensity[] = { 1.0f, 1.0f, 1.0f, 1.0f };
      gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, lightIntensity, 0);

      gl.glEnable(GL2.GL_LIGHT1);
      float lightPosition2[] = { 0.0f, -0.8f, 0.3f, 1.0f };
      gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPosition2, 0);
      float lightIntensity2[] = { 1.0f, 0.0f, 0.0f, 0.0f };
      gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, lightIntensity2, 0);
      float lightSpecularIntensity2[] = { 1.0f, 1.0f, 1.0f, 1.0f };
      gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightSpecularIntensity2, 0);

      gl.glEnable(GL2.GL_COLOR_MATERIAL);
      gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
      float lightSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
      gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, lightSpecularColor, 0);
      gl.glMaterialf(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 80);
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}

    public void dispose(GLAutoDrawable drawable) {}

  }

  public void keyTyped(KeyEvent key) {}

  public void keyPressed(KeyEvent key) {
    switch (key.getKeyCode()) {
      case KeyEvent.VK_A:
        deltaZ += 0.05;
        break;
      case KeyEvent.VK_B:
        deltaZ -= 0.05;
        break;
      case KeyEvent.VK_C:
        deltaX += 0.05;
        break;
      case KeyEvent.VK_D:
        deltaX -= 0.05;
        break;
      case KeyEvent.VK_E:
        deltaY += 0.05;
        break;
      case KeyEvent.VK_F:
        deltaY -= 0.05;
        break;  
      default:
        break;
    }
  }

  public void keyReleased(KeyEvent key) {}

  public static void main(String[] args) {
    IlluminateGeometry frame = new IlluminateGeometry();
    frame.setTitle("Illuminate Geometry");
    frame.setSize(640, 480);
    frame.setVisible(true);
  }

}