简体   繁体   English

网格上的纹理不渲染,仅显示黑色libgdx gl20

[英]Texture on mesh doesnt render, just shows black libgdx gl20

edit-- I've updated my code after TenFour04s answer but still just shows black. 编辑-我在TenFour04s回答后更新了我的代码,但仍然只显示黑色。

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. 我已经更新了我的libgdx,它要求我使用gl20,这使我进行了一些更改,除了尝试对网格进行纹理处理外,其他大多数都可以正常工作。 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. 我一直在尝试绑定和surTexture.bind和grdTexture.bind的顺序,并使用小于16的数字进行渲染,并且我已经得到了使用表面纹理作为除表面和地面以外的所有纹理的信息。

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() 在draw()内部调用drawWorld

    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 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 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" . 在顶点着色器中,使用a_texCoord ,但在网格构造器中,通过使用ShaderProgram.TEXCOORD_ATTRIBUTE+"16"ShaderProgram.TEXCOORD_ATTRIBUTE+"17"有效地命名了属性a_texCoord16a_texCoord17

Since you are not multi-texturing, I would just replace those with "a_texCoord" . 由于您没有多重纹理,所以我只用"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. 您可能想在texCoords中添加数字后缀的原因是,如果您的网格的每个顶点具有多个UV,因为它是多重变形的。 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. 您可能要绑定到除0以外的单元的原因是,如果要在单个网格上进行多重纹理化,则需要一次绑定多个纹理。 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. 因此,如果您实际上是多纹理的,则在尝试将UV与片段着色器中的纹理进行匹配时,使用与纹理单元编号匹配的属性后缀可能有助于避免混淆。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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