2016-02-28 8 views
1

使用下面的代碼,如果我關閉燈,藍色框將變黑。 enter image description here無法在libgdx中關閉燈

但是對實體似乎沒有影響,它依然色彩繽紛。代碼有什麼問題?請幫忙謝謝。 enter image description here

package com.louxiu.game; 

/** 
* Created by louxiu on 2/22/16. 
*/ 

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL30; 
import com.badlogic.gdx.graphics.PerspectiveCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.VertexAttributes.Usage; 
import com.badlogic.gdx.graphics.g3d.*; 
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; 
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; 
import com.badlogic.gdx.graphics.g3d.loader.ObjLoader; 
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; 
import com.badlogic.gdx.math.Vector3; 

public class TestApp implements ApplicationListener { 
    private PerspectiveCamera camera; 
    private ModelBatch modelBatch; 
    private Model box; 
    private ModelInstance boxInstance; 
    public Model entityModel; 
    public ModelInstance entityInstance; 
    private Environment environment; 

    @Override 
    public void create() { 
     // Create camera sized to screens width/height with Field of View of 75 degrees 
     camera = new PerspectiveCamera(
       75, 
       Gdx.graphics.getWidth(), 
       Gdx.graphics.getHeight()); 

     // Move the camera 3 units back along the z-axis and look at the origin 
     camera.position.set(0f, 0f, 3f); 
     camera.lookAt(0f, 0f, 0f); 

     // Near and Far (plane) repesent the minimum and maximum ranges of the camera in, um, units 
     camera.near = 0.1f; 
     camera.far = 300.0f; 

     // A ModelBatch is like a SpriteBatch, just for models. Use it to batch up geometry for OpenGL 
     modelBatch = new ModelBatch(); 

     // A ModelBuilder can be used to build meshes by hand 
     ModelBuilder modelBuilder = new ModelBuilder(); 

     // It also has the handy ability to make certain premade shapes, like a Cube 
     // We pass in a ColorAttribute, making our cubes diffuse (aka, color) red. 
     // And let openGL know we are interested in the Position and Normal channels 
     box = modelBuilder.createBox(2f, 2f, 2f, 
       new Material(ColorAttribute.createDiffuse(Color.BLUE)), 
       Usage.Position | Usage.Normal 
     ); 

     // A entityModel holds all of the information about an, um, entityModel, such as vertex data and texture info 
     // However, you need an entityInstance to actually render it. The entityInstance contains all the 
     // positioning information (and more). Remember Model==heavy ModelInstance==Light 
     boxInstance = new ModelInstance(box, 0, 0, 0); 

     String entity = "creeper/creeper"; 
     ObjLoader loader = new ObjLoader(); 
     entityModel = loader.loadModel(Gdx.files.internal(entity + ".obj"), new ObjLoader.ObjLoaderParameters(true)); 
     entityInstance = new ModelInstance(entityModel, 0, 0, 0); 
     Texture texture = new Texture(Gdx.files.internal(entity + ".png")); 
     entityInstance.materials.get(0).set(TextureAttribute.createDiffuse(texture)); 

     // Finally we want some light, or we wont see our color. The environment gets passed in during 
     // the rendering process. Create one, then create an Ambient (non-positioned, non-directional) light. 
     environment = new Environment(); 
     // environment.add(new DirectionalLight().set(1f, 1f, 1f, -1f, -0.8f, -0.2f)); 
     // environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.8f, 0.8f, 0.8f, 1.0f)); 
    } 

    @Override 
    public void dispose() { 
     modelBatch.dispose(); 
     box.dispose(); 
    } 

    @Override 
    public void render() { 
     // You've seen all this before, just be sure to clear the GL_DEPTH_BUFFER_BIT when working in 3D 
     Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT); 

     // For some flavor, lets spin our camera around the Y axis by 1 degree each time render is called 
     camera.rotateAround(Vector3.Zero, new Vector3(0, 1, 0), 1f); 
     // When you change the camera details, you need to call update(); 
     // Also note, you need to call update() at least once. 
     camera.update(); 

     // Like spriteBatch, just with models! pass in the box Instance and the environment 
     modelBatch.begin(camera); 
     modelBatch.render(boxInstance, environment); 
     // modelBatch.render(entityInstance, environment); 
     modelBatch.end(); 
    } 

    @Override 
    public void resize(int width, int height) { 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

回答

3

你眼波模式可能不具有法線,這對於照明工作是必需的。

檢查日誌,應該會顯示一條錯誤消息,告訴你完全不應該使用ObjLoader。請使用G3dModelLoader或更好:使用AssetManagerg3djg3db文件格式。

將您的模型從建模應用程序導出到FBX文件格式並使用fbx-conv進行轉換。不要使用fbx-conv將你的.obj文件轉換成.g3dx文件,這是行不通的。

順便說一句,雖然沒有相關的你可能要考慮到:

相機far/near比例是非常高的,你通常不應該使用下面1一個near值。

與你的評論說的不同,ModelBatch is not used to batch geometry and not that comparable to SpriteBatch

ObjLoaderloadModel方法,它接受一個boolean,所以你不必創建ObjLoaderParameters爲(儘管,如說,你不應該使用ObjLoader完全)。

您正在創建一個Texture,但在不再需要時不會正確處理它。這會導致潛在的資源泄漏。

每幀創建一個新的Vector3會對GC施加壓力,並且會導致小心。只需使用Vector3.Y而不是new Vector3(0, 1, 0)即可解決該問題。