我们的世界是个立体的世界,但并不是所有的媒介都可以把我们看到的立体图像表现出来。
可以表现为正交投影,比如相机拍照片,它展现出来的是一个平面的二维的效果。
我们也可以用透视投影的方式,按照人眼观察事物的原则,例如近处大,远处小的方式,来画透视图。
OrthographicCamera(正交) 和 PerspectiveCamera(透视)。
package com.example.libgdx_sample_spirtebatch; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.FPSLogger; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; public class SampleRender { public static int WIDTH = 320; public static int HEIGHT = 480; OrthographicCamera m_Cam; SpriteBatch batch = new SpriteBatch(5460); FPSLogger fps = new FPSLogger(); ImmediateModeRenderer20 renderer = new ImmediateModeRenderer20(false, true, 0); private Animation m_MoveLeft; private Animation m_MoveRight; private float m_StateTime; Vector2 m_Pos = new Vector2(); Texture m_bkg = null; private TextureRegion m_region; public SampleRender() { this.m_Cam = new OrthographicCamera(WIDTH, HEIGHT); this.m_Cam.position.set(WIDTH/2, HEIGHT/2, 0); m_StateTime = 0; createAnimations(); //m_Pos.x = 0; //m_Pos.y = 0; } private void createAnimations() { m_bkg = new Texture(Gdx.files.internal("data1.png")); m_region=new TextureRegion(m_bkg); Texture TankTexture = new Texture(Gdx.files.internal("data.png")); TextureRegion[] split = new TextureRegion(TankTexture).split(32, 32)[0]; TextureRegion[] mirror = new TextureRegion(TankTexture).split(32,32)[0]; for (TextureRegion region : mirror) region.flip(true, false); m_MoveLeft = new Animation(0.1f, split[0], split[1]); m_MoveRight = new Animation(0.1f, mirror[0], mirror[1]); } Vector3 lerpTarget = new Vector3(); public void render (float deltaTime) { //m_Cam.position.lerp(lerpTarget.set(m_Pos.x, m_Pos.y, 0), 2f * deltaTime); //m_Cam.update(); m_StateTime += deltaTime; renderer.begin(m_Cam.combined, GL20.GL_LINES); renderer.end(); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // batch.setProjectionMatrix(m_Cam.combined); batch.begin(); Animation anim = m_MoveLeft; batch.draw(m_region, 0, 0); batch.draw(anim.getKeyFrame(m_StateTime, true), 0, 0); batch.end(); } public void dispose () { batch.dispose(); } }
在测试结果中,相机的设置为
package com.example.libgdx_sample_spirtebatch; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.FPSLogger; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; public class SampleRender { public static int WIDTH = 320; public static int HEIGHT = 480; OrthographicCamera m_Cam; SpriteBatch batch = new SpriteBatch(5460); FPSLogger fps = new FPSLogger(); ImmediateModeRenderer20 renderer = new ImmediateModeRenderer20(false, true, 0); private Animation m_MoveLeft; private Animation m_MoveRight; private float m_StateTime; Vector2 m_Pos = new Vector2(); Texture m_bkg = null; private TextureRegion m_region; public SampleRender() { this.m_Cam = new OrthographicCamera(WIDTH, HEIGHT); this.m_Cam.position.set(WIDTH/2, HEIGHT/2, 0); m_StateTime = 0; createAnimations(); //m_Pos.x = 0; //m_Pos.y = 0; } private void createAnimations() { m_bkg = new Texture(Gdx.files.internal("data1.png")); m_region=new TextureRegion(m_bkg); Texture TankTexture = new Texture(Gdx.files.internal("data.png")); TextureRegion[] split = new TextureRegion(TankTexture).split(32, 32)[0]; TextureRegion[] mirror = new TextureRegion(TankTexture).split(32,32)[0]; for (TextureRegion region : mirror) region.flip(true, false); m_MoveLeft = new Animation(0.1f, split[0], split[1]); m_MoveRight = new Animation(0.1f, mirror[0], mirror[1]); } Vector3 lerpTarget = new Vector3(); public void render (float deltaTime) { //m_Cam.position.lerp(lerpTarget.set(m_Pos.x, m_Pos.y, 0), 2f * deltaTime); //m_Cam.update(); m_StateTime += deltaTime; renderer.begin(m_Cam.combined, GL20.GL_LINES); renderer.end(); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(m_Cam.combined); batch.begin(); Animation anim = m_MoveLeft; batch.draw(m_region, 0, 0); batch.draw(anim.getKeyFrame(m_StateTime, true), 0, 0); batch.end(); } public void dispose () { batch.dispose(); } }