从网上找了NEHE JOGL的旋转例子加以修改,感谢NEHE 的贡献。
代码已经上传,直接复制粘贴到src下即可(eclipse)
运行效果:哈哈~前面的2D图象是不是不爽了,来,我们来用前面学到的4个例子的知识做3D的效果,其实也没有太多改动,你会发现和前面例子里面的代码几乎没什么两样,对,,就是增加使用了一些方法。
glu.gluPerspective(45.0f, h, 1.0, 30.0); //是设置3D摄像机,重点还有个重要方法glu.gluLookAt()没有用上
package com.jogl.redbook.first; //要是想使用默认包,请去掉这行
import javax.media.opengl.GL;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;
class Renderer implements GLEventListener {
private float rquad = 0.0f;
private float rtri = 0.0f;
public void display(GLAutoDrawable gLDrawable) {
double x,y;
final GL gl = gLDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(-1.5f, 0.0f, -6.0f); //移动画图点到指定坐标X,Y,Z
gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
gl.glBegin(GL.GL_TRIANGLES); //画三角形
gl.glColor3f(1.0f, 0.0f, 0.0f); //设置当前画刷颜色红
gl.glVertex3f(0.0f, 1.0f, 0.0f); // 上
gl.glColor3f(0.0f, 1.0f, 0.0f); //设置当前画刷颜色绿
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // 下左
gl.glColor3f(0.0f, 0.0f, 1.0f); //设置当前画刷颜色蓝
gl.glVertex3f(1.0f, -1.0f, 0.0f); // 下右
gl.glEnd();
gl.glLoadIdentity();
gl.glTranslatef(1.5f, 0.0f, -6.0f); //移动画图点到指定坐标X,Y,Z
gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);
// gl.glBegin(GL.GL_QUADS); // Draw A Quad 画正方形 原NEHE JOGL的内容,修改了。
// gl.glColor3f(0.5f, 0.5f, 1.0f); // 设置为光绿色
// gl.glVertex3f(-1.0f, 1.0f, 0.0f); // 左上顶点
// gl.glVertex3f(1.0f, 1.0f, 0.0f); // 右上顶点
// gl.glVertex3f(1.0f, -1.0f, 0.0f); // 右下顶点
// gl.glVertex3f(-1.0f, -1.0f, 0.0f); //左下顶点
// gl.glEnd(); // glVertex3f必须被包含在glBegin()glEnd()里
gl.glLineWidth(5.0f); //设置线宽为5
gl.glBegin(GL.GL_LINE_STRIP);
for (double i = 0; i < 60; i += 0.1) { //这个for语句里画螺旋,由glBegin开始,glEnd
float red = (float) ((Math.random())*(1.0f)); //随机红
float green = (float) ((Math.random())*(1.0f)); //随机绿
float blue = (float) ((Math.random())*(1.0f)); //随机蓝
gl.glColor3f(red, green, blue); //设置画图颜色,第二章的内容
x = Math.sin(i) * i /10 ;
y = Math.cos(i) * i/10 ;
gl.glVertex2d(x, y);
}
gl.glEnd();
gl.glFlush();
rtri += 0.2f;
rquad += 0.15f;
}
public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {
}
public void init(GLAutoDrawable gLDrawable) {
GL gl = gLDrawable.getGL();
gl.glShadeModel(GL.GL_SMOOTH); // 开启平滑画图
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // 背景颜色
gl.glClearDepth(1.0f); // 深度缓冲
gl.glEnable(GL.GL_DEPTH_TEST); //开启深度测试
gl.glDepthFunc(GL.GL_LEQUAL); // 深度测试的类型
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
// GL_PERSPECTIVE_CORRECTION_HINT 指定颜色和纹理坐标的插值质量.
// GL_NICEST为使用质量最好的模式.
}
public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {
final GL gl = gLDrawable.getGL();
final GLU glu = new GLU();
if (height <= 0)
height = 1;
final float h = (float) width / (float) height;
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 30.0); //设置摄像机可视范围的 近上下宽度,远上下宽度,近可视点,远可视点
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
package com.jogl.redbook.first; //要是想使用默认包,请去掉这行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import com.jogl.second.LineGraphApp;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
public class RotateThing extends JFrame {
Renderer listener=new Renderer();
static FPSAnimator animator=null;
public RotateThing() throws HeadlessException {
super("旋转的物体");
setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GLCapabilities glcaps=new GLCapabilities();
GLCanvas canvas=new GLCanvas(glcaps);
canvas.addGLEventListener(listener);
//canvas.addMouseListener(listener);
getContentPane().add(canvas, BorderLayout.CENTER);
animator=new FPSAnimator(canvas,60,true);
centerWindow(this);
}
private void centerWindow(Component frame) { // 居中窗体
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
frame.setLocation((screenSize.width - frameSize.width) >> 1,
(screenSize.height - frameSize.height) >> 1);
}
public static void main(String[] args) {
final RotateThing app = new RotateThing();
// 显示窗体
SwingUtilities.invokeLater(new Runnable() {
public void run() {
app.setVisible(true);
}
});
// 动画线程开始
SwingUtilities.invokeLater(new Runnable() {
public void run() {
animator.start();
}
});
}
}