JOGL 3D基本形状
精华
小牛编辑
118浏览
2023-03-14
在3D物体中,z 轴也与 x 轴和 y 轴同时存在。这些轴表示对象的高度、宽度和深度。因此,它代表了对象的真实视图。
JOGL 3D 示例
在这个例子中,我们正在创建一个简单的 3D 对象。
package cn.xnip;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.*;
public class J3DBasic implements GLEventListener {
private GLU glu = new GLU();
@Override
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glTranslatef(0f, 0f, -3f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(-1f, 0f, 0);
gl.glVertex3f(0f, 1f, 0);
gl.glEnd();
//3D
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(-1f, 0f, -2f);
gl.glVertex3f(0f, 1f, -2f);
gl.glEnd();
//top
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(-1f, 0f, 0);
gl.glVertex3f(-1f, 0f, -2f);
gl.glEnd();
//bottom
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(0f, 1f, 0);
gl.glVertex3f(0f, 1f, -2f);
gl.glEnd();
}
@Override
public void dispose(GLAutoDrawable arg0) {
//method body
}
@Override
public void init(GLAutoDrawable arg0) {
// method body
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
if (height <= 0)
height = 1;
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(40.0f, h, 1.5, 18.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
public static void main(String[] args) {
final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);
final GLCanvas gc = new GLCanvas(cap);
J3DBasic b = new J3DBasic();
gc.addGLEventListener(b);
gc.setSize(600, 400);
final JFrame frame = new JFrame("xnip.cn:JOGL 3D");
frame.getContentPane().add(gc);
frame.setSize(frame.getContentPane().getPreferredSize());
frame.setVisible(true);
}
}
输出结果为: