在 openGL ES 中将对象旋转 360° Android
Rotate an object 360° in openGL ES Android
我在 openGL 中有一个对象,我可以使用
旋转它
mRenderer.setAngle(mAngle);
但这只会水平旋转形状。应该怎么做才能让我也可以垂直旋转形状?
mRenderer
是 KubeRenderer 的一个实例
class KubeRenderer implements GLSurfaceView.Renderer {
public interface AnimationCallback {
void animate();
}
public KubeRenderer(GLWorld world, AnimationCallback callback) {
mWorld = world;
mCallback = callback;
}
public void onDrawFrame(GL10 gl) {
if (mCallback != null) {
mCallback.animate();
}
gl.glClearColor(0.5f,0.5f,0.5f,1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -3.0f);
gl.glScalef(0.5f, 0.5f, 0.5f);
gl.glRotatef(mAngle, 0, 1, 0);
gl.glRotatef(mAngle*0.25f, 1, 0, 0);
gl.glColor4f(0.7f, 0.7f, 0.7f, 1.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
mWorld.draw(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float)width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 2, 12);
gl.glDisable(GL10.GL_DITHER);
gl.glActiveTexture(GL10.GL_TEXTURE0);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void setAngle(float angle) {
mAngle = angle;
}
public float getAngle() {
return mAngle;
}
private GLWorld mWorld;
private AnimationCallback mCallback;
private float mAngle;
}
在 class 的底部添加两个变量:
私有浮动 mangle;
在绘图中更改这两行:
gl.glRotatef(mAngle, 0, 1, 0);
gl.glRotatef(mAngle*0.25f, 1, 0, 0);
成由每个变量在不同轴上旋转的线
(https://www.opengl.org/sdk/docs/man2/xhtml/glRotate.xml)
并根据需要更改这三个变量的 setter 和 getter 函数(SetAngle 和 GetAngle)
我在 openGL 中有一个对象,我可以使用
旋转它mRenderer.setAngle(mAngle);
但这只会水平旋转形状。应该怎么做才能让我也可以垂直旋转形状?
mRenderer
是 KubeRenderer 的一个实例
class KubeRenderer implements GLSurfaceView.Renderer {
public interface AnimationCallback {
void animate();
}
public KubeRenderer(GLWorld world, AnimationCallback callback) {
mWorld = world;
mCallback = callback;
}
public void onDrawFrame(GL10 gl) {
if (mCallback != null) {
mCallback.animate();
}
gl.glClearColor(0.5f,0.5f,0.5f,1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -3.0f);
gl.glScalef(0.5f, 0.5f, 0.5f);
gl.glRotatef(mAngle, 0, 1, 0);
gl.glRotatef(mAngle*0.25f, 1, 0, 0);
gl.glColor4f(0.7f, 0.7f, 0.7f, 1.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
mWorld.draw(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float)width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 2, 12);
gl.glDisable(GL10.GL_DITHER);
gl.glActiveTexture(GL10.GL_TEXTURE0);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void setAngle(float angle) {
mAngle = angle;
}
public float getAngle() {
return mAngle;
}
private GLWorld mWorld;
private AnimationCallback mCallback;
private float mAngle;
}
在 class 的底部添加两个变量: 私有浮动 mangle;
在绘图中更改这两行: gl.glRotatef(mAngle, 0, 1, 0); gl.glRotatef(mAngle*0.25f, 1, 0, 0); 成由每个变量在不同轴上旋转的线 (https://www.opengl.org/sdk/docs/man2/xhtml/glRotate.xml)
并根据需要更改这三个变量的 setter 和 getter 函数(SetAngle 和 GetAngle)