【问题标题】:LibGDX - My Image isn't filling the width and height of the screenLibGDX - 我的图像没有填满屏幕的宽度和高度
【发布时间】:2014-09-02 01:43:52
【问题描述】:

我无法让我的图像填满整个屏幕而不拉伸自身。目前我有一个正交相机设置,但问题是屏幕中间显示的图像的原点(0,0)。我怎样才能使这项工作?这是我的代码。

public class GameScreen implements Screen {
   SlingshotSteve game; 

   public void Menu(SlingshotSteve game){
       this.game = game;
   }

   OrthographicCamera camera;

   SpriteBatch batch;
   TextureRegion backgroundTexture;
   Texture texture;

   GameScreen(final SlingshotSteve gam) {
        this.game = gam; 

    camera = new OrthographicCamera(800, 480);

    batch = new SpriteBatch();

    Texture texture = new Texture(Gdx.files.internal("background.jpg"));
    backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500);

}

public void render(float delta) {   
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      camera.update();
      batch.setProjectionMatrix(camera.combined);

      batch.begin();
      batch.draw(backgroundTexture, 0, 0, SlingshotSteve.WIDTH, SlingshotSteve.HEIGHT); 
      batch.end();

}

@Override
public void dispose() {
       batch.dispose();
       texture.dispose();

}

【问题讨论】:

    标签: android libgdx


    【解决方案1】:

    您的正交相机是如何实现的?听起来您可能对“像素完美”变换感兴趣,它允许屏幕坐标直接与世界坐标相关。您可以使用以下参数来实现此转换。

    final float lOrthoLeft   = 0;
    final float lOrthoRight  = pScreenHeight;
    final float lOrthoTop    = 0;
    final float lOrthoBottom = pScreenWidth;
    final float lOrthoNear   = Float.MIN_VALUE;
    final float lOrthoFar    = Float.MAX_VALUE;
    

    在这方面,您的正交投影的lOrthoLeftlOrthoRightlOrthoToplOrthoBottom 的值可能是(-Width/2)(+Width/2)(-Height/2)(+Height/2) (或一些变体),以便将世界的原点定位在屏幕的中心。

    然而,这也与 libgdx 如何渲染到屏幕有关。 OpenGL ES 是 libgdx 底层的硬件加速图形 API,它使用一个特殊的坐标系统,以屏幕中心为原点。您会发现这些资源有助于了解如何在这些领域内工作。

    libgdx co-ordinate system

    Displaying Graphics with OpenGL ES

    【讨论】:

      猜你喜欢
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 2021-05-26
      • 1970-01-01
      • 2012-04-02
      • 2012-07-23
      • 1970-01-01
      相关资源
      最近更新 更多