java - libgdx: SpriteBatch not displayed with PerspectiveCamera -
while have basic knowledge of opengl i'm starting libgdx.
my question is: why, having exact same code switching orthographiccamera perspectivecamera has effect of no longer displaying of spritebatches ?
here's code 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 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 resize(int width, int height) method setup camera so:
public void resize(int width, int height) { float aspectratio = (float) width / (float) height; camera = new orthographiccamera(cameraviewheight * aspectratio, cameraviewheight);
i this:
but if change camera type:
public void resize(int width, int height) { float aspectratio = (float) width / (float) height; camera = new perspectivecamera(64, cameraviewheight * aspectratio, cameraviewheight); }
i this:
the reason i'm asking because liked libgdx's built in ability draw text (font) in opengl. in examples use spritebatch path font instance, , use ortho camera. i'd know if spritebatch , font drawing functionality work perspectivecamera.
well, ok, solved it:
short answer:
spritebatch uses orthogonalperspective internally. if use perspectivecamera need pass custom view matrix spritebatch. can in 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 there's no need else sprite's projection matrix (unless want change how sprite displayed on screen):
public void render() { glcommon gl = gdx.gl; camera.update(); camera.apply(gdx.gl10); //this no longer needed: //spritebatch.setprojectionmatrix(camera.combined); //...
long answer: since final goal able use spritebatch draw text, while above mentioned modification code can that, in sense both text on sprite , mesh texture visible, i've noticed if don't specify color vertices of mesh, said vertices color use text. in other words, textured mesh declared 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 code in render(...) method make mesh red-tinted:
font.setcolor(color.red); spritebatch.draw(texturespritebatch, 0, 0); font.draw(spritebatch, (int)fps+" fps", 0, 100);
the fix set colors on mesh's vertices right 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});
Comments
Post a Comment