libgdx : PerspectiveCamera와 함께 표시되지 않는 spritebatch
-
10-12-2019 - |
문제
OpenGL에 대한 기본 지식이있는 동안 나는 LibGDX로 시작합니다.
내 질문은 똑같은 코드를 가지고 있지만 artegraphiccamera에서 perspectiveCamera에서 전환하는 이유는 더 이상 내 스프라이트 배치를 표시하지 않는 효과가 있습니까?
여기에 사용되는 코드가 있습니다.
create () 메소드 :
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();
}
.
이제, 내 크기 조정 (int width, int height) 메소드 메소드 i :
public void resize(int width, int height) {
float aspectRatio = (float) width / (float) height;
camera = new OrthographicCamera(cameraViewHeight * aspectRatio, cameraViewHeight);
.
나는 이것을 얻는다 :
그러나 카메라 유형을 변경하면 :
public void resize(int width, int height) {
float aspectRatio = (float) width / (float) height;
camera = new PerspectiveCamera(64, cameraViewHeight * aspectRatio, cameraViewHeight);
}
.
나는 이것을 얻는다 :
내가 묻는 이유는 LibGDX가 OpenGL에서 텍스트 (글꼴)를 그릴 수있는 능력이 지어 졌기 때문입니다. 그러나 해당 예에서는 글꼴 인스턴스로가는 SpriteBatch를 사용하며 항상 오르토 카메라를 사용합니다. SpriteBatch 및 Font Drawing 기능이 PerspectiveCamera와 함께 작동하는 경우 알고 싶습니다.
해결책
잘, OK, 나는 그것을 해결했다 :
짧은 대답 :
SpriteBatch는 내부적으로 OrthogonalPersive를 사용합니다. PerspectiveCamera를 사용하는 경우 사용자 정의보기 행렬을 SpriteBatch에 전달해야합니다. 크기 조정 (...) 메서드 에서이 작업을 수행 할 수 있습니다.
@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);
}
.
및 그런 다음 스프라이트의 투영 행렬로 다른 일을 할 필요가 없습니다 (스프라이트가 화면에 표시되는 방식을 변경하려는 경우) :
public void render() {
GLCommon gl = Gdx.gl;
camera.update();
camera.apply(Gdx.gl10);
//this is no longer needed:
//spriteBatch.setProjectionMatrix(camera.combined);
//...
.
답변 : 최종 목표는 스프라이트의 수정을 사용하여 텍스트를 그릴 수있는 반면, Sprite의 텍스트와 메쉬가있는 의미에서 이제 텍스처가 표시됩니다. My Mesh의 정점에 대한 색상을 지정하지 않으면, 정점은 텍스트에 사용하는 색을 얻을 수 있습니다. 즉, 질감이있는 메쉬가 그렇게 선언 된 :
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});
.
또한 내 렌더링 (...) 메서드 에이 코드가 메쉬를 붉은 색으로 만듭니다.
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});
.