1

我已经阅读了这篇文章:https ://code.google.com/p/libgdx-users/wiki/GenericTextureBinding 并尝试使用它。我正在使用复杂的 .obj(airplane) 和 10 个纹理。在我看来,一切都按预期进行,但我在模型上看不到任何纹理。模型是灰色的。我究竟做错了什么?

int listPosition; 
List<Texture> modelTextures = new ArrayList<Texture>();
StillModel m_mesh;

public onCreate(){
String texturePath;
     FileHandle texturePathToLoad;
     boolean engageTextureLookupStrategy2 = false;        

     m_mesh = ModelLoaderRegistry.loadStillModel(Gdx.files.internal("obj/airplane.obj"));

     SubMesh[] subMeshes = m_mesh.getSubMeshes();

     for(SubMesh subMesh : subMeshes)
         Gdx.app.log("create", subMesh.name);
    // strategy #1: texture files share
     //              the object names as basename
     for(SubMesh subMesh : subMeshes)
     {
            // looking for texture file if sub mesh
            // has uv coordinates attached, only!
            if(m_mesh.getSubMesh(subMesh.name).mesh.getVertexAttribute(Usage.TextureCoordinates)!=null)
            {
                    texturePath = "obj/" + subMesh.name;
                    texturePathToLoad = guessTextureFile(texturePath);
                    if(texturePathToLoad!=null)
                    {
                            Gdx.app.log("create","... found Texture! Adding " + texturePathToLoad.path() + " to list of model textures.");

                            Texture temp_texture = new Texture(texturePathToLoad);                             
                            modelTextures.add(temp_texture);
                    }
                    else
                    {
                            Gdx.app.log("create","... not found. Object " + subMesh.name + " will not be textured.");
                            engageTextureLookupStrategy2 = true;
                    }
            }
     }

public void render(float delta) {
    Gdx.graphics.getGL10().glClearColor( 0.5f, 0, 0, 1 );
    Gdx.graphics.getGL10().glClear( GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT );

     listPosition = 0;      

     SubMesh[] subMeshes = m_mesh.getSubMeshes();

     for(SubMesh subMesh : subMeshes){      
     if( (modelTextures!=null)
     &&  (modelTextures.size()>0)       
     && (m_mesh.getSubMesh(subMesh.name).mesh.getVertexAttribute(Usage.TextureCoordinates)!=null)
     )
     {
            if(listPosition<modelTextures.size())
            {
                    // we don't want the texture to be tinted,
                    // thus setting white color.
                Gdx.gl11.glColor4f(1,1,1,1);

                    Gdx.app.log("render","binding texture #" + listPosition + " to texture unit #" + listPosition + " for submesh " + subMesh.name);
                    Gdx.gl.glActiveTexture(listPosition);                      
                    Gdx.gl.glEnable(GL11.GL_TEXTURE_2D);
                    modelTextures.get(listPosition).bind();                    
            }
            else
            {
                Gdx.gl.glActiveTexture(listPosition);
                Gdx.gl.glDisable(GL11.GL_TEXTURE_2D);
            }
            listPosition++;
     }
     }             
        m_mesh.render();           
}

日志猫:

create: ... found Texture! Adding obj/left_wing_1.tga to list of model textures.
create: ... found Texture! Adding obj/rear.tga to list of model textures.
create: ... found Texture! Adding obj/wheel_support.tga to list of model textures.
create: ... found Texture! Adding obj/right_wing_1.tga to list of model textures.
create: ... found Texture! Adding obj/landing_light.tga to list of model textures.
create: ... found Texture! Adding obj/cockpit_1.tga to list of model textures.
create: ... found Texture! Adding obj/glass_1.tga to list of model textures.
create: ... found Texture! Adding obj/body_front.tga to list of model textures.
create: ... found Texture! Adding obj/pilot.tga to list of model textures.
create: ... found Texture! Adding obj/reflect.tga to list of model textures.
render: binding texture #0 to texture unit #0 for submesh left_wing_1
render: binding texture #1 to texture unit #1 for submesh rear
render: binding texture #2 to texture unit #2 for submesh wheel_support
render: binding texture #3 to texture unit #3 for submesh right_wing_1
render: binding texture #4 to texture unit #4 for submesh landing_light
render: binding texture #5 to texture unit #5 for submesh cockpit_1
render: binding texture #6 to texture unit #6 for submesh glass_1
render: binding texture #7 to texture unit #7 for submesh body_front
render: binding texture #8 to texture unit #8 for submesh pilot
render: binding texture #9 to texture unit #9 for submesh reflect

结果:

在此处输入图像描述

4

0 回答 0