0
\$\begingroup\$

I've a MapView class that contains many tiles (MapTile class, a tile can be in the background), vertex array object and vertex buffer object for rendering of the map. I also have a function that update the buffers :

        // FloatBuffer(s) and IntBuffer for indices
        positionsBuffer = MemoryUtil.memAllocFloat( this.tiles.size() * 4 * 3 );
        colorsBuffer = MemoryUtil.memAllocFloat( this.tiles.size() * 4 * 3 );
        texCoordsBuffer = MemoryUtil.memAllocFloat( this.tiles.size() * 4 * 2 );
        indicesBuffer = MemoryUtil.memAllocInt( this.vertexCount = this.tiles.size() * 2 * 3 );

        TextureDefinition textureDefinition = game.getTexturesManager().getTextureDefinition("tiles");

        int i = 0;

        for ( MapTile mapViewTile : this.tiles ) {

            Tile tile = mapViewTile.getTile();
            if ( tile == null ) continue;

            String tileTextureIdentifier = tile.getTextureIdentifier( mapViewTile.getVariant() );

            TextureTilePosition tileTexturePosition = textureDefinition.getTextureTilePosition( tileTextureIdentifier );
            if ( tileTexturePosition == null ) continue;

            // If a tile is in the background, I place the triangles of the tile square 0.1f further back and I also darken the texture
            float z = mapViewTile.isBackground() ? -0.1f : 0f;
            float[] color = mapViewTile.isBackground() ? new float[]{ 0.5f, 0.5f, 0.5f } : new float[]{ 1.0f, 1.0f, 1.0f };

            positionsBuffer.put( mapViewTile.getX() - 0.5f ).put( mapViewTile.getY() + 0.5f ).put( z );
            positionsBuffer.put( mapViewTile.getX() - 0.5f ).put( mapViewTile.getY() - 0.5f ).put( z );
            positionsBuffer.put( mapViewTile.getX() + 0.5f ).put( mapViewTile.getY() - 0.5f ).put( z );
            positionsBuffer.put( mapViewTile.getX() + 0.5f ).put( mapViewTile.getY() + 0.5f ).put( z );

            colorsBuffer.put( color ).put( color ).put( color ).put( color );

            texCoordsBuffer.put( tileTexturePosition.getX() ).put( tileTexturePosition.getY() );
            texCoordsBuffer.put( tileTexturePosition.getX() ).put( tileTexturePosition.getY() + tileTexturePosition.getHeight() );
            texCoordsBuffer.put( tileTexturePosition.getX() + tileTexturePosition.getWidth() ).put( tileTexturePosition.getY() + tileTexturePosition.getHeight() );
            texCoordsBuffer.put( tileTexturePosition.getX() + tileTexturePosition.getWidth() ).put( tileTexturePosition.getY() );

            indicesBuffer.put( i ).put( i + 1 ).put( i + 3 );
            indicesBuffer.put( i + 1 ).put( i + 2 ).put( i + 3 );

            i += 4;

        }

        positionsBuffer.flip();
        colorsBuffer.flip();
        texCoordsBuffer.flip();
        indicesBuffer.flip();

        this.vao.bind();

        this.positionsVbo.bind( GL15.GL_ARRAY_BUFFER );
        VertexBufferObject.uploadData( GL15.GL_ARRAY_BUFFER, positionsBuffer, GL15.GL_STATIC_DRAW );
        ShaderProgram.pointVertexAttribute( 0, 3, 0, 0 );

        this.colorsVbo.bind( GL15.GL_ARRAY_BUFFER );
        VertexBufferObject.uploadData( GL15.GL_ARRAY_BUFFER, colorsBuffer, GL15.GL_STATIC_DRAW );
        ShaderProgram.pointVertexAttribute( 1, 3, 0, 0 );

        this.texCoordsVbo.bind( GL15.GL_ARRAY_BUFFER );
        VertexBufferObject.uploadData( GL15.GL_ARRAY_BUFFER, texCoordsBuffer, GL15.GL_STATIC_DRAW );
        ShaderProgram.pointVertexAttribute( 2, 2, 0, 0 );

        this.indicesVbo.bind( GL15.GL_ELEMENT_ARRAY_BUFFER );
        VertexBufferObject.uploadData( GL15.GL_ELEMENT_ARRAY_BUFFER , indicesBuffer, GL15.GL_STATIC_DRAW );

        VertexBufferObject.unbind( GL15.GL_ARRAY_BUFFER );

        VertexArrayObject.unbind();

But the background tiles are not display behind others squares : Screenshot

For information, i'm using an orthographic projection and the camera of this screenshot is slightly angled. OpenGL version is 4.3.0 and LWJGL version is 3.1.3. I'm using GL_BLEND for texture transparency

I hypothesize that it would be necessary to use two different vertex array objects, is that it or something else?

\$\endgroup\$
  • \$\begingroup\$ Are you using blending or discard? \$\endgroup\$ – Bálint Nov 3 '17 at 16:39
  • \$\begingroup\$ @Bálint Already \$\endgroup\$ – Mindstorm38 Nov 3 '17 at 17:49
0
\$\begingroup\$

Blending happens after the fragment shader, which means that the fragments still get written to the depth buffer. If you try to draw something behind the object, then the fragments in that object will get culled. Use glsl's discard instead of blending.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ How can I use discard ? \$\endgroup\$ – Mindstorm38 Nov 3 '17 at 18:43
  • \$\begingroup\$ @RozierThéo In your fragment shader, after you calculated the color you use if (color.a < 0.5) discard; \$\endgroup\$ – Bálint Nov 3 '17 at 18:47
  • \$\begingroup\$ It's working but : What's really going on ? I've read that ignore the result of the fragment shader in the pipeline. And what are we comparing with "< 0.5" ? \$\endgroup\$ – Mindstorm38 Nov 3 '17 at 19:12
  • \$\begingroup\$ @RozierThéo You check if the ouput color's alpha is less than 0.5 (0.5 is a good middle point), and if it is, then you simply throw the fragment away \$\endgroup\$ – Bálint Nov 4 '17 at 9:22
0
\$\begingroup\$

Seems as though you need to enable blending. Your init function should have something like

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Already : GL11.glEnable( GL11.GL_BLEND ); GL11.glBlendFunc( GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA ); \$\endgroup\$ – Mindstorm38 Nov 3 '17 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.