这是动画与修改器的最后一个例子,实现一个旋转的3D效果。如果只以学习引擎来看,这个只是一个RotationModifier修改器的使用DEMO;如果要弄懂他是如何运作的,就变成一个3D图形问题了。
先说一下裁剪平面(ClippingPlan)的概念,这是根据“视口”定义的一块梯形空间,在这个空间中的图形将会按照透视关系被渲染,区域以外的部分会被裁剪掉,所以得名裁剪平面。这里打开传送门,感谢博友翻译的质料:OpenGL ES 入门 (一),OpenGL ES 入门 (二) OpenGL ES编程模型:模拟,OpenGL ES 入门 (三) 投影。这样我们就理解DEMO中的camera.setZClippingPlanes(-100, 100); 设置。
至于旋转的实现,是通过模式视图矩阵(ModelView Matrix)的运算完成的,相关学习资料请下载《3D游戏与计算机图形学中的数学方法.pdf》。下面上代码:
package org.andengine.examples;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.ui.activity.SimpleBaseGameActivity;
public class Rotation3DExample extends SimpleBaseGameActivity {
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
// ===========================================================
// Fields
// ===========================================================
private BitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion mFaceTextureRegion;
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, Rotation3DExample.CAMERA_WIDTH, Rotation3DExample.CAMERA_HEIGHT);
//设置Z轴的剪切平面(超出平面外的部分不予渲染)
camera.setZClippingPlanes(-100, 100);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(Rotation3DExample.CAMERA_WIDTH, Rotation3DExample.CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
this.mBitmapTextureAtlas.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
/* Calculate the coordinates for the face, so its centered on the camera. */
final float centerX = (Rotation3DExample.CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final float centerY = (Rotation3DExample.CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
/* Create the face and add it to the scene. */
final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
@Override
protected void applyRotation(final GLState pGLState) {
final float rotation = this.mRotation;
if(rotation != 0) {
final float rotationCenterX = this.mRotationCenterX;
final float rotationCenterY = this.mRotationCenterY;
//平移矩形到中间
pGLState.translateModelViewGLMatrixf(rotationCenterX, rotationCenterY, 0);
/* Note we are applying rotation around the y-axis and not the z-axis anymore! */
//以Y轴为旋转轴
pGLState.rotateModelViewGLMatrixf(rotation, 0, 1, 0);
//平移矩形回到原来的位置
pGLState.translateModelViewGLMatrixf(-rotationCenterX, -rotationCenterY, 0);
}
}
};
face.registerEntityModifier(new LoopEntityModifier(new RotationModifier(6, 0, 360)));
scene.attachChild(face);
return scene;
}
}