首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libgdx:不使用PerspectiveCamera显示SpriteBatch

libgdx:不使用PerspectiveCamera显示SpriteBatch
EN

Stack Overflow用户
提问于 2012-03-09 20:20:29
回答 1查看 3K关注 0票数 8

虽然我确实有OpenGL的基础知识,但我只是从libgdx开始。

我的问题是:为什么拥有完全相同的代码,但只从OrthographicCamera切换到PerspectiveCamera会导致不再显示我的任何SpriteBatches?

下面是我使用的代码:

create()方法:

代码语言:javascript
复制
public void create() {
    textureMesh = new Texture(Gdx.files.internal("data/texMeshTest.png"));
    textureSpriteBatch = new Texture(Gdx.files.internal("data/texSpriteBatchTest.png"));

    squareMesh = new Mesh(true, 4, 4, 
            new VertexAttribute(Usage.Position, 3, "a_position")
            ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")

    );

    squareMesh.setVertices(new float[] {
            squareXInitial, squareYInitial, squareZInitial,             0,1,    //lower left
            squareXInitial+squareSize, squareYInitial, squareZInitial,  1,1,    //lower right
            squareXInitial, squareYInitial+squareSize, squareZInitial,  0,0,    //upper left
            squareXInitial+squareSize, squareYInitial+squareSize, squareZInitial,1,0});  //upper right 

    squareMesh.setIndices(new short[] { 0, 1, 2, 3});

    spriteBatch = new SpriteBatch();        
}

和render()方法:

代码语言:javascript
复制
public void render() {
    GLCommon gl = Gdx.gl;

    camera.update();
    camera.apply(Gdx.gl10);
    spriteBatch.setProjectionMatrix(camera.combined);

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glEnable(GL10.GL_DEPTH_TEST);

    gl.glEnable(GL10.GL_TEXTURE_2D);
    textureMesh.bind();
    squareMesh.render(GL10.GL_TRIANGLE_STRIP, 0, 4);

    spriteBatch.begin();
    spriteBatch.draw(textureSpriteBatch, -10, 0);
    spriteBatch.end();        
}

现在,如果在我的resize(int width,int height)方法中,我这样设置相机:

代码语言:javascript
复制
   public void resize(int width, int height) {
        float aspectRatio = (float) width / (float) height;
        camera = new OrthographicCamera(cameraViewHeight * aspectRatio, cameraViewHeight);

我明白了:

但是如果我改变相机类型:

代码语言:javascript
复制
public void resize(int width, int height) {
    float aspectRatio = (float) width / (float) height;
    camera = new PerspectiveCamera(64, cameraViewHeight * aspectRatio, cameraViewHeight);
}       

我明白了:

我这么问的原因是因为我真的喜欢libgdx内置的在OpenGL中绘制文本(字体)的能力。但是在他们的示例中,他们使用了一个SpriteBatch,他们将其路径到Font实例,并且他们也总是使用正交相机。我想知道PerspectiveCamera是否可以使用SpriteBatch和字体绘制功能。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-09 21:08:48

好吧,好吧,我解决了:

简短的回答:

SpriteBatch在内部使用OrthogonalPerspective。如果您使用PerspectiveCamera,则需要向SpriteBatch传递一个自定义视图矩阵。您可以在resize(...)中执行此操作。方法:

代码语言:javascript
复制
@Override
public void resize(int width, int height) {
    float aspectRatio = (float) width / (float) height;
    camera = new PerspectiveCamera(64, cameraViewHeight * aspectRatio, cameraViewHeight);
    viewMatrix = new Matrix4();
    viewMatrix.setToOrtho2D(0, 0,width, height);
    spriteBatch.setProjectionMatrix(viewMatrix);
}

这样就不需要对sprite的投影矩阵做任何其他的事情了(除非你想改变sprite在屏幕上的显示方式):

代码语言:javascript
复制
public void render() {
    GLCommon gl = Gdx.gl;

    camera.update();
    camera.apply(Gdx.gl10);
    //this is no longer needed:
    //spriteBatch.setProjectionMatrix(camera.combined);
    //...

长答案:由于我的最终目标是能够使用SpriteBatch来绘制文本,而通过上面提到的代码修改,我可以做到这一点,在这种意义上,精灵上的文本和带有纹理的网格现在都是可见的,我已经注意到,如果我不为网格的顶点指定颜色,这些顶点将获得我用于文本的颜色。换句话说,纹理网格声明如下:

代码语言:javascript
复制
 squareMesh = new Mesh(true, 4, 4, 
            new VertexAttribute(Usage.Position, 3, "a_position")
            ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")

    );

    squareMesh.setVertices(new float[] {
            squareXInitial, squareYInitial, squareZInitial,             0,1,    //lower left
            squareXInitial+squareSize, squareYInitial, squareZInitial,  1,1,    //lower right
            squareXInitial, squareYInitial+squareSize, squareZInitial,  0,0,    //upper left
            squareXInitial+squareSize, squareYInitial+squareSize, squareZInitial,1,0});  //upper right 

    squareMesh.setIndices(new short[] { 0, 1, 2, 3});

在我的render(...)中也有这段代码方法将网格染成红色:

代码语言:javascript
复制
font.setColor(Color.RED);
spriteBatch.draw(textureSpriteBatch, 0, 0);
font.draw(spriteBatch, (int)fps+" fps", 0, 100);

修复方法是从一开始就在网格的顶点上设置颜色:

代码语言:javascript
复制
squareMesh = new Mesh(true, 4, 4, 
        new VertexAttribute(Usage.Position, 3, "a_position")
        ,new VertexAttribute(Usage.ColorPacked, 4, "a_color")
        ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")

);

squareMesh.setVertices(new float[] {
        squareXInitial, squareYInitial, squareZInitial,                         Color.toFloatBits(255, 255, 255, 255),  0,1,    //lower left
        squareXInitial+squareSize, squareYInitial, squareZInitial,              Color.toFloatBits(255, 255, 255, 255),  1,1,    //lower right
        squareXInitial, squareYInitial+squareSize, squareZInitial,              Color.toFloatBits(255, 255, 255, 255),  0,0,    //upper left
        squareXInitial+squareSize, squareYInitial+squareSize, squareZInitial,   Color.toFloatBits(255, 255, 255, 255),  1,0});  //upper right 

squareMesh.setIndices(new short[] { 0, 1, 2, 3});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9633970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档