2012-03-09 29 views
6

Trong khi tôi có kiến ​​thức cơ bản về OpenGL, tôi chỉ mới bắt đầu với libgdx.libgdx: SpriteBatch không hiển thị với PerspectiveCamera

Câu hỏi của tôi là: tại sao, có cùng mã chính xác nhưng chỉ chuyển từ OrthographicCamera sang PerspectiveCamera có hiệu lực không còn hiển thị bất kỳ SpriteBatch nào của tôi nữa không?

Dưới đây là đoạn code tôi sử dụng:

create() phương pháp:

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

và render) phương pháp (:

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

Bây giờ, nếu trong thay đổi kích thước của tôi (int width , int chiều cao) phương pháp Tôi thiết lập máy ảnh như vậy:

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

tôi có được điều này:

enter image description here

Nhưng nếu tôi thay đổi kiểu máy ảnh:

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

tôi có được điều này:

enter image description here

Lý do tôi hỏi là bởi vì tôi thực sự thích khả năng tích hợp sẵn của libgdx để vẽ văn bản (phông chữ) trong OpenGL. Nhưng trong ví dụ của họ, họ sử dụng một SpriteBatch mà họ dẫn đến cá thể Font, và họ cũng luôn sử dụng Ortho Camera. Tôi muốn biết sau đó nếu SpriteBatch và Font vẽ chức năng làm việc với PerspectiveCamera.

+0

Khi sử dụng PerspectiveCamera Tôi nghĩ rằng [Decal] (http://code.google.com/p/libgdx-users/wiki/Decals) và lớp học DecalBatch là những gì các tác giả libgdx dự định sử dụng. – Sundae

+0

Mã có chứa các dòng quá dài (mã cuộn ngang không mát) (cũng có trong câu trả lời) –

+0

bằng cách này, tôi đoán bạn chỉ có thể sử dụng 2 camera - tất cả những gì họ làm là ẩn các phép tính ma trận "đáng sợ" sau "đơn giản" trừu tượng. –

Trả lời

4

Vâng, ok, tôi giải quyết nó:

Câu trả lời ngắn:

SpriteBatch sử dụng một OrthogonalPerspective nội bộ. Nếu bạn sử dụng PerspectiveCamera, bạn cần chuyển ma trận chế độ xem tùy chỉnh cho SpriteBatch. Bạn có thể làm điều đó trong phương pháp thay đổi kích thước (...):

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

Và sau đó có không cần phải làm bất cứ điều gì khác với ma trận chiếu của sprite đó (trừ khi bạn muốn thay đổi cách sprite được hiển thị trên màn hình):

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

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

Long trả lời: vì mục tiêu cuối cùng của tôi là để có thể sử dụng một SpriteBatch để vẽ văn bản, trong khi với việc sửa đổi nêu trên mã của tôi, tôi có thể làm điều đó, theo nghĩa là cả hai văn bản trên sprite và lưới với kết cấu bây giờ có thể nhìn thấy, tôi đã nhận thấy rằng nếu tôi không chỉ định một màu sắc cho các đỉnh của lưới của tôi, cho biết đỉnh sẽ nhận được màu sắc tôi sử dụng cho văn bản. Nói cách khác, với một lưới kết cấu được khai báo như vậy:

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

cũng có mã này trong kết xuất của tôi (...) Phương pháp sẽ làm cho lưới màu đỏ-màu:

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

Việc sửa chữa này là để thiết lập màu sắc trên đỉnh của lưới của bạn ngay từ đầu:

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}); 
Các vấn đề liên quan