jogl四边形问题

时间:2011-03-06 18:25:44

标签: java opengl jogl

我是使用OpenGL的新手,正在尝试使用jogl。我可以毫无问题地绘制三角形,但是当我尝试绘制四边形(在许多教程中使用)时,eclipse一直告诉我GL.GL_QUADS无法解析。

gl.glBegin(GL.GL_QUADS);

不确定我做错了什么。

谢谢,

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.*;

public class SimpleScene implements GLEventListener {
    public static void main(String[] args) {
        GLProfile glp = GLProfile.getDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        GLCanvas canvas = new GLCanvas(caps);

        final Frame frame = new Frame("AWT Window Test");
        frame.setSize(300, 300);
        frame.add(canvas);
        frame.setVisible(true);

        // by default, an AWT Frame doesn't do anything when you click
        // the close button; this bit of code will terminate the program when
        // the window is asked to close
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                frame.dispose();
                System.exit(0);
            }
        });
    canvas.addGLEventListener(new SimpleScene());
    FPSAnimator animator = new FPSAnimator(canvas, 60);
    animator.add(canvas);
    animator.start();
    }

    @Override
    public void display(GLAutoDrawable arg0) {
        update();
        render(arg0);
    }

    private void update() {
        // TODO Auto-generated method stub

    }
    private void render(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
//      gl.glViewport(0, 0, 300, 300); //Possibly use to move around object
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);

        gl.glPushMatrix();
        gl.glTranslatef(-1.5f,1.5f,0.0f);                  // Move left 1.5 units, up 1.5 units, and back 8 units

        gl.glBegin(GL.GL_TRIANGLES); 
        gl.glColor3f(10, 0, 0);
// Begin drawing triangles
        gl.glVertex3f( 0.0f, 1.0f, 0.0f);                   // Top vertex
        gl.glVertex3f(-1.0f,-1.0f, 0.0f);                   // Bottom left vertex
        gl.glVertex3f( 1.0f,-1.0f, 0.0f);                   // Bottom right vertex
        gl.glEnd();                                         // Finish drawing triangles
        gl.glPopMatrix();


    }

    @Override
    public void dispose(GLAutoDrawable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void init(GLAutoDrawable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
            int arg4) {
        // TODO Auto-generated method stub

    }
}

2 个答案:

答案 0 :(得分:5)

javax.media.opengl.GL接口包含GL 3,GL 2,GL ES 2.0和GL ES 1.x通用的OpenGL子集,而GL_QUADS不属于此子集。

如果您使用javax.media.opengl.GL2,则会获得GL2.GL_QUADS。

答案 1 :(得分:2)

它是GL2类中的静态常量。你需要打电话:

gl.glBegin(GL2.GL_QUADS);

而不是:

gl.glBegin(GL.GL_QUADS);