Libgdx: SpriteBatch not showing with PerspectiveCamera

Although I have basic OpenGL knowledge, I am just starting with libgdx.

My question is: why, having the exact same code, but only switching from OrthographicCamera to PerspectiveCamera, does any of my SpriteBatches no longer display?

Here is the code I'm using:

create () method:

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

and render () method:

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

Now, if in my resize (int width, int height) method, I configure the camera as follows:

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

I get this:

enter image description here

But if I change the type of camera:

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

I get this:

enter image description here

The reason I ask is because I really liked libgdx, which is built into the ability to draw text (font) in OpenGL. But in their examples, they use SpriteBatch, which they refer to the Font instance, and they also always use Ortho Camera. I would like to know if the SpriteBatch and Font functions will work with PerspectiveCamera.

+6
source share
1 answer

Ok, ok, I solved this:

Short answer:

SpriteBatch uses OrthogonalPerpective internally. If you are using PerspectiveCamera, you need to pass the custom view matrix to SpriteBatch. You can do this in the resize method (...):

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

And then there is no need to do anything else with this matrix of project sprites (if you do not want to change the way the sprite is displayed on the screen):

 public void render() { GLCommon gl = Gdx.gl; camera.update(); camera.apply(Gdx.gl10); //this is no longer needed: //spriteBatch.setProjectionMatrix(camera.combined); //... 

Long answer: since my final goal was to use SpriteBatch to draw text, while with the above modification of my code I can do this in the sense that both the sprite text and the mesh with texture are now visible, I noticed that if I don’t specify a color for the vertices of my grid, the indicated vertices will get the color that I use for the text. In other words, a textured mesh is declared like this:

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

also having this code in my render (...) method will make the grid red tinted:

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

Fix this to set colors on your mesh vertices from the start:

 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
source

Source: https://habr.com/ru/post/910345/


All Articles