使用 gl.glrotatef 旋转对象
Posted
技术标签:
【中文标题】使用 gl.glrotatef 旋转对象【英文标题】:rotating an object using gl.glrotatef 【发布时间】:2012-11-22 15:13:19 【问题描述】:我正在尝试使用 opengl 和命令 gl.glroate 让对象旋转,但似乎没有发生任何事情,谁能告诉我哪里出错了?下面是代码
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
public class Cube2 implements GLEventListener
private float Rotate = 0.0f;
private static final GLU glu = new GLU ();
/**
* @param args
*/
public static void main(String[] args)
JFrame frame = new JFrame("A simple JOGL demo");
GLCanvas canvas = new GLCanvas ();
canvas.addGLEventListener(new Cube2());
frame.add(canvas);
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Animator animator = new FPSAnimator(canvas, 60);
animator.add(canvas);
@Override
public void display (GLAutoDrawable glDrawable)
final GL gl = glDrawable.getGL();
//gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -7.0f);
gl.glRotatef(Rotate++, 1.0f, 0.0f, 0.0f);
gl.glRotatef(Rotate++, 0.0f, 1.0f, 0.0f);
gl.glRotatef(Rotate++, 0.0f, 0.0f, 1.0f);
gl.glBegin(GL.GL_TRIANGLES);
//Counter Clockwise Front Face 1
gl.glColor3f(0.0f, 1.0f, 0.0f); //Green
gl.glVertex3f (1.0f, 1.0f, 0.0f); //Top
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // left to right
gl.glVertex3f (-1.0f, -1.0f, 0.0f); //left
gl.glVertex3f (-1.0f, -1.0f, -0.0f); //left
gl.glVertex3f(1.0f, -1.0f, -0.0f); //left to right
gl.glVertex3f (1.0f, 1.0f, -0.0f); //left
// Right Face 2
gl.glColor3f(1.0f, 0.0f, 1.0f); //Pink
gl.glVertex3f (1.0f, 1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, -0.0f);
gl.glVertex3f (1.0f, 1.0f, 1.0f);
gl.glVertex3f (-1.0f, -1.0f, -0.0f);
gl.glVertex3f(-1.0f, 1.0f, 0.0f);
gl.glVertex3f (-1.0f, -1.0f, -1.0f);
gl.glEnd();
@Override
public void displayChanged (GLAutoDrawable arg0, boolean arg1, boolean arg2)
//TODO Auto-generated method stub
@Override
public void init(GLAutoDrawable glDrawable)
final GL gl = glDrawable.getGL();
int width = 640;
int height = 480;
//Set the state of out new OpenGL context
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f,(float)(width)/(float)(height),1.0f,100.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glShadeModel(GL.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background
gl.glClearDepth(1.0f); //Depth Buffer Setup
gl.glEnable(GL.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL.GL_LEQUAL); //The Type of Depth Testing To Do
gl.glEnable(GL.GL_CULL_FACE); //Start culling back faces
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); //Really Nice Perspective Calculations
@Override
public void reshape (GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4)
//TODO Auto-generated method stub
提前感谢您的帮助
【问题讨论】:
【参考方案1】:你应该先添加
animator.start();
为了开始渲染。
您正在使用不再维护的 JOGL 1。而且它肯定行不通。您应该遵循 Jogamp.org 上的教程。
您正在使用专有 API,例如 import com.sun.opengl.util.Animator;首先。如果您想从无权使用该 api 的人那里获得帮助,这是一个糟糕的选择。正如我所说,您可以访问 jogamp.org 获取该软件包的开源替代品。
【讨论】:
以上是关于使用 gl.glrotatef 旋转对象的主要内容,如果未能解决你的问题,请参考以下文章
Opengl es Android 3D 手指触控旋转object
动画 SVG 组对象 - 使用样式组件进行变换旋转不会围绕圆原点旋转