简体   繁体   中英

Texture on mesh doesnt render, just shows black libgdx gl20

edit-- I've updated my code after TenFour04s answer but still just shows black.

I've updated my libgdx and its required me to use gl20 which has lead me to make a few changes most of it works fine except when trying to do texture the mesh. This currently shows surface mesh as black and doesnt show the ground mesh at all. with some changes I can get it to show both surface and ground meshes as black.

I've played around with binding and the order of surTexture.bind and grdTexture.bind and using numbers less than 16 and render and i've got it to use the surface texture as the texture for everything except the surface and ground.

Can anyone see where I might be going wrong with this?

// creating a mesh with maxVertices set to vertices,size*3
            groundMesh = new Mesh(true, vertices.size*3, vertices.size*3, 
                        new VertexAttribute(Usage.Position,2,ShaderProgram.POSITION_ATTRIBUTE),
                        new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));

            groundMesh.setVertices(temp);

            short[] indices = new short[vertices.size*2];
            for(int i=0;i<vertices.size*2;i++){
                indices[i] = (short)i;
            }
            groundMesh.setIndices(indices);


    surfaceMesh = new Mesh(true, vertices.size*3, vertices.size*3, 
                        new VertexAttribute(Usage.Position,3,ShaderProgram.POSITION_ATTRIBUTE),
                        new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));    

...

         grdTexture = new Texture(Gdx.files.internal("data/img/leveltest/ground.png"));
        // Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE16);
        //says that setWrap and SetFilter bind the texture so I thought I might have to set         
        //activetexture here but does nothing.
         grdTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
         grdTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);


         surTexture = new Texture(Gdx.files.internal("data/img/leveltest/surface.png"));
        // Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE17);
         surTexture.setWrap(TextureWrap.Repeat, TextureWrap.ClampToEdge);
         //TODO change these filters for better quality
         surTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

drawWorld gets called inside render()

    public  void drawWorld(SpriteBatch batch,OrthographicCamera camera) {


    batch.begin();

    batch.setProjectionMatrix(camera.combined);

    layers.drawLayers(batch);




    if ((spatials != null) && (spatials.size > 0)){
         for (int i = 0; i < spatials.size; i++){
            spatials.get(i).render(batch);
         }
     }

    batch.end();
drawGround(camera);

}

    private void drawGround(OrthographicCamera camera){


    shader.begin();
    shader.setUniformMatrix("u_projTrans", camera.combined);

    grdTexture.bind(0);
    shader.setUniformi("u_texture", 0);
    //changed GL_TRIANGLES to GL_TRIANGLE_STRIP to render meshes correctly after changing to GL20
    groundMesh.render(shader, GL20.GL_TRIANGLE_STRIP);

    surTexture.bind(0);
    shader.setUniformi("u_texture", 0);
    surfaceMesh.render(shader, GL20.GL_TRIANGLE_STRIP);
    shader.end();


}

fragment.glsl

     #ifdef GL_ES
     #define LOWP lowp
     precision mediump float;
     #else
     #define LOWP
     #endif

      varying LOWP vec4 v_color;
     varying vec2 v_texCoords;

     uniform sampler2D u_texture;


     void main()
      {
      gl_FragColor = v_color * texture2D(u_texture, v_texCoords);
       }

vertex.glsl

      attribute vec4 a_position;
      attribute vec4 a_color;
      attribute vec2 a_texCoord;

      uniform mat4 u_projTrans;

      varying vec4 v_color;
      varying vec2 v_texCoords;

      void main() {
      v_color = a_color;
      v_color.a = v_color.a * (256.0/255.0);
      v_texCoords = a_texCoord;
      gl_Position = u_projTrans * a_position;
      }

In your vertex shader, you are using a_texCoord , but in your mesh constructor, you have effectively named your attributes a_texCoord16 and a_texCoord17 by using ShaderProgram.TEXCOORD_ATTRIBUTE+"16" and ShaderProgram.TEXCOORD_ATTRIBUTE+"17" .

Since you are not multi-texturing, I would just replace those with "a_texCoord" .

It looks like maybe you are conflating attribute name suffixes with what texture units are, although the two concepts are not necessarily related. The reason you might want to add number suffixes to your texCoords is if your mesh has multiple UV's for each vertex because it is multi-tetxtured. But really you can use any naming scheme you like. The reason you might want to bind to a unit other than 0 is if you're multi-texturing on a single mesh so you need multiple textures bound at once. So if you actually were multi-texturing, using attribute suffixes that match texture unit numbers might help avoid confusion when you are trying to match UV's to textures in the fragment shader.

ok so it turns out the problem was the vertex shader They code from here doesnt work.

here is the working shader

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;

uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;


void main() {         

     v_color = vec4(1, 1, 1, 1);

     v_texCoords = a_texCoord; 
     gl_Position =  u_projTrans * a_position; 
}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM