本文概述
- JOGL基本三角形示例
- JOGL基本四边形示例
JOGL基本三角形示例 在此示例中, 我们将看到如何在基本三角形参数(GL_TRIANGLES)的帮助下创建三角形。
Triangle.java
package com.srcmini.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class Triangle implements GLEventListener { @Overridepublic void init(GLAutoDrawable arg0) {}@Overridepublic void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2();
gl.glBegin(GL2.GL_TRIANGLES);
gl.glVertex2d(0, 0.5);
gl.glVertex2d(-0.5, -0.5);
gl.glVertex2d(0.5, -0.5);
gl.glEnd();
}@Overridepublic void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { }@Overridepublic void dispose(GLAutoDrawable arg0) {}public static void main(String[] args) {final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);
final GLCanvas gc = new GLCanvas(cap);
Triangle tr = new Triangle();
gc.addGLEventListener(tr);
gc.setSize(400, 400);
final JFrame frame = new JFrame("JOGL Triangle");
frame.add(gc);
frame.setSize(500, 400);
frame.setVisible(true);
} }
输出:
文章图片
JOGL基本四边形示例 在此示例中, 我们将看到如何在基本四边形参数(GL_QUADS)的帮助下创建四边形。
JQuad.java
package com.srcmini.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class JQuad implements GLEventListener { @Overridepublic void init(GLAutoDrawable arg0) {}@Overridepublic void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2();
gl.glBegin(GL2.GL_QUADS);
gl.glVertex3f( 0.0f, 0.5f, 0);
gl.glVertex3f(-0.5f, 0f, 0);
gl.glVertex3f(0f, -0.5f, 0);
gl.glVertex3f(0.5f, 0f, 0);
gl.glEnd();
}@Overridepublic void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { }@Overridepublic void dispose(GLAutoDrawable arg0) {}public static void main(String[] args) {final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);
final GLCanvas gc = new GLCanvas(cap);
JQuad jq = new JQuad();
gc.addGLEventListener(jq);
gc.setSize(400, 400);
final JFrame frame = new JFrame("JOGL Primitive Quadrilateral");
frame.add(gc);
frame.setSize(500, 400);
frame.setVisible(true);
} }
【JOGL基本形状】输出:
文章图片