Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7613519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:10:32+00:00 2026-05-31T02:10:32+00:00

While I do have the basic knowledge of OpenGL I’m just starting with libgdx.

  • 0

While I do have the basic knowledge of OpenGL I’m just starting with libgdx.

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

Here’s the code I use:

the 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 the 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 setup the camera like so:

   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 camera type:

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’m asking is because I really liked libgdx’s built in ability to draw text (font) in OpenGL. But in their examples they use a SpriteBatch which they path to the Font instance, and they also always use Ortho Camera. I’d like to know then if SpriteBatch and Font drawing functionality work with PerspectiveCamera.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:10:33+00:00Added an answer on May 31, 2026 at 2:10 am

    Well, ok, I solved it:

    Short answer:

    SpriteBatch uses an OrthogonalPerspective internally. If you use PerspectiveCamera you need to pass a custom view matrix to the SpriteBatch. You can do that 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’s no need to do anything else with that sprite’s projection matrix (unless you want to change how the sprite is displayed on 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 be able to use a SpriteBatch to draw text, while with the above mentioned modification to my code I can do that, in the sense that both the text on the sprite and the mesh with the texture are now visible, I’ve noticed that if I don’t specify a color for the vertices of my mesh, said vertices will get the color I use for the text. In other words, with a textured mesh declared like so:

     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 mesh red-tinted:

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

    The fix to this is to set colors on your mesh’s vertices right 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});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic question,there are two incidents happended while working with Tomcat. 1.I
I have basic code that looks like this: while(inputfileStream.good()) { for(int i = 0;i<levels;i++)
I have a very basic knowledge regarding XML. But now I want to dig
Its been a while I have ready Mcconnell's Code Complete . Now I read
First off, please accept my apologies if this question is basic, I mainly have
im very new to this and have a very basic knowledge php and SQL
Disclaimer: The author is a newbie in OTP having some basic knowledge of Erlang's
I have been programming Java for around 15 years. While this is a basic
I am a beginner in C programming language but I have knowledge of programming
I have a basic question regarding memory management in TCL. Suppose I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.