gpt4 book ai didi

java - libgdx:SpriteBatch 不与 PerspectiveCamera 一起显示

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:22:40 25 4
gpt4 key购买 nike

虽然我确实具备 OpenGL 的基本知识,但我只是从 libgdx 开始。

我的问题是:为什么使用完全相同的代码但只是从 OrthographicCamera 切换到 PerspectiveCamera 会产生不再显示任何 SpriteBatches 的效果?

这是我使用的代码:

创建()方法:

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() 方法:

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) 方法中我像这样设置相机:

   public void resize(int width, int height) {
float aspectRatio = (float) width / (float) height;
camera = new OrthographicCamera(cameraViewHeight * aspectRatio, cameraViewHeight);

我明白了:

enter image description here

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

public void resize(int width, int height) {
float aspectRatio = (float) width / (float) height;
camera = new PerspectiveCamera(64, cameraViewHeight * aspectRatio, cameraViewHeight);
}

我明白了:

enter image description here

我问的原因是因为我真的很喜欢 libgdx 内置的在 OpenGL 中绘制文本(字体)的能力。但是在他们的示例中,他们使用了指向 Font 实例的 SpriteBatch,并且他们也始终使用 Ortho Camera。我想知道 SpriteBatch 和字体绘图功能是否适用于 PerspectiveCamera。

最佳答案

好吧,我解决了:

简答:

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

@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 在屏幕上的显示方式):

public void render() {
GLCommon gl = Gdx.gl;

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

长答案:由于我的最终目标是能够使用 SpriteBatch 来绘制文本,而通过对我的代码进行上述修改,我可以做到这一点,因为 Sprite 上的文本和网格都具有纹理现在可见,我注意到如果我没有为网格的顶点指定颜色,那么顶点将获得我用于文本的颜色。换句话说,使用如下声明的纹理网格:

 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(...) 方法中也有这段代码将使网格变成红色:

font.setColor(Color.RED);
spriteBatch.draw(textureSpriteBatch, 0, 0);
font.draw(spriteBatch, (int)fps+" fps", 0, 100);

解决这个问题的方法是从一开始就在网格的顶点上设置颜色:

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});

关于java - libgdx:SpriteBatch 不与 PerspectiveCamera 一起显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9633970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com