2017-04-21 18 views
10

Tôi đang cố gắng libgdx để hiển thị một số mô hình fbx và tôi đã thực hiện thành công điều đó, nhưng tôi bị kẹt tại một điểm.Vẽ văn bản trên hình cầu trong libgdx

Tôi đang hiển thị tệp bóng rổ fbx với sự trợ giúp của ModelInstance và giờ tôi muốn vẽ một văn bản trên bóng rổ đó. Tôi có thể vẽ văn bản trên bóng rổ nhưng tuyến tính của nó không phải là một phần của bóng rổ đó. Tôi muốn văn bản theo cách đường cong giống như quả bóng đó.

Đây là cách tôi vẽ text--

batch = new SpriteBatch(); 
batch.setProjectionMatrix(camera.combined); 

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font/Lato-Bold.ttf")); 
    FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); 
    parameter.size = 30; 
    parameter.color = com.badlogic.gdx.graphics.Color.WHITE; 
    parameter.magFilter = Texture.TextureFilter.Linear; // used for resizing quality 
    parameter.minFilter = Texture.TextureFilter.Linear; 
    generator.scaleForPixelHeight(3); 

    BitmapFont aFont = generator.generateFont(parameter); 
    aFont.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); 

aFont.draw(batch, options.get(i), aBound.getCenterX(), aBound.getCenterY() + textHeight/2); 

đâu rất nhiều -> BoundingBox của Model (ví dụ: bóng rổ)

+0

Bạn đang vẽ văn bản dưới dạng thực thể riêng biệt trên cùng một ma trận.Một cách bạn có thể đạt được điều này để vẽ văn bản trên bitmap kết cấu mà bạn đang sử dụng cho basketBall vv Nó sẽ có hình dạng hình cầu. – Qamar

Trả lời

1

gì bạn đang cố gắng để đạt được ở đây là hơi phức tạp trong libGdx. Cách tiếp cận của bạn, tuy nhiên là không chính xác bởi vì bạn đang vẽ các văn bản như là một đối tượng sprite riêng biệt trên đầu trang của quả bóng của bạn.

Thủ tục chính xác để đạt được điều này là vẽ văn bản lên một kết cấu và sau đó ánh xạ và ràng buộc kết cấu vào bóng.

Thủ tục có thể được chia thành 4 phần -

  1. Thiết lập hệ phông chữ và các đối tượng bộ đệm khung
  2. Thiết lập hàng loạt sprite
  3. Vẽ văn bản, lập bản đồ kết cấu cho mô hình
  4. Hiển thị mô hình

Lưu ý, tôi có thể sử dụng bất kỳ kết cấu nào khác và nếu bạn không muốn cập nhật văn bản trong thời gian thực, bạn có thể dễ dàng sử dụng kết cấu được tạo sẵn với bất kỳ văn bản nào bạn muốn.

Mã này là như sau -

Thiết lập hệ phông chữ và bộ đệm khung đối tượng

//Create a new SpriteBatch object 
batch = new SpriteBatch(); 

//Generate font 
FreeTypeFontGenerator generator = new 
FreeTypeFontGenerator(Gdx.files.internal("font/Lato-Bold.ttf")); 
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new 
FreeTypeFontGenerator.FreeTypeFontParameter(); 
parameter.size = 30; 
parameter.color = com.badlogic.gdx.graphics.Color.WHITE; 
parameter.magFilter = Texture.TextureFilter.Linear; // used for resizing quality 
parameter.minFilter = Texture.TextureFilter.Linear; 
generator.scaleForPixelHeight(10); 

//Get bitmap font 
BitmapFont aFont = generator.generateFont(parameter); 
aFont.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, 
Texture.TextureFilter.Linear); 

//Generate new framebuffer object of 128x128 px. This will be our texture 
FrameBuffer lFb = new FrameBuffer(Pixmap.Format.RGBA4444,128,128,false); 

Thiết lập hàng loạt sprite

//Set the correct resolution for drawing to the framebuffer 
lFb.begin(); 
Gdx.gl.glViewport(0,0,128,128); 
Gdx.gl.glClearColor(1f, 1f, 1f, 1); 

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
Matrix4 lm = new Matrix4(); 
//Set the correct projection for the sprite batch 
lm.setToOrtho2D(0,0,128,128); 
batch.setProjectionMatrix(lm); 

Cuối cùng rút ra những văn bản vào kết cấu

batch.begin(); 
aFont.draw(batch,"Goal!",64,64); 
batch.end(); 
lFb.end(); 

Ánh xạ các kết cấu cho mô hình

//Generate the material to be applied to the ball 
//Notice here how we use the FBO's texture as the material base 
Material lMaterial = new Material(TextureAttribute.createDiffuse(lFb.getColorBufferTexture())); 

//Since I do not have a sphere model, I'll just create one with the newly 
//generated material   
ballModel = mb.createSphere(1.0f,1.0f,1.0f,8,8,lMaterial, 
VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates); 

//Finally instantiate an object of the model 
ballInstance = new ModelInstance(ballModel); 

Render các mô hình

mBatch.begin(mCamera); 
mBatch.render(ballInstance); 
mBatch.end(); 
//Rotate the ball along the y-axis 
ballInstance.transform.rotate(0.0f,1.0f,0.0f,0.5f); 

quả -

Render text to sphere libGDX

Các vấn đề liên quan