绘制纹理时的不透明度问题

时间:2015-12-17 20:45:12

标签: swift opengl opengl-es

所以我一直试图画一段时间的纹理。我遇到了一个新问题。我想知道是否可以强制每个像素的透明度,所以我更改了通用片段着色器并强制alpha为0.5。不幸的是,这产生了一种非常奇怪的效果。这是编辑过的着色器代码

precision highp float;

varying vec2 v_texcoord;

uniform sampler2D s_texture;

void main()
{
    vec4 color = texture2D(s_texture, v_texcoord);
    float r = color.z;
    float g = color.y;
    float b = color.x;
    gl_FragColor = vec4(r, g, b, 0.5);
}

基本上它呈现的是顶部三角形明亮,低三角形更暗(表明占用率正在工作)

func drawTriangle(texture: GLuint)
    {
        loadBuffers()
        //glViewport(0, 0, width, height)
        //glClearColor(0, 0.0, 0, 1.0)
        //glClear(GLbitfield(GL_COLOR_BUFFER_BIT) | GLbitfield(GL_DEPTH_BUFFER_BIT))


        glEnable(GLenum(GL_TEXTURE_2D))
        glActiveTexture(GLenum(GL_TEXTURE0))

        glUseProgram(texShader)
        let loc1 = glGetUniformLocation(texShader, "s_texture")
        glUniform1i(loc1, 0)


        let loc3 = glGetUniformLocation(texShader, "matrix")
        if (loc3 != -1)
        {
            glUniformMatrix4fv(loc3, 1, GLboolean(GL_FALSE), &matrix)
        }

        glBindTexture(GLenum(GL_TEXTURE_2D), texture)
        glDrawArrays(GLenum(GL_TRIANGLE_STRIP), 0, 6)
        glDisable(GLenum(GL_TEXTURE_2D))

        destroyBuffers()
    }
func destroyBuffers()
    {
        glDeleteBuffers(1, &vb)
        //glDeleteVertexArraysOES(1, &vb)
        glDeleteBuffers(1, &tc)
    }
    func loadBuffers()
    {
        buildArrays()
        //****Load up the vertex data******
        //Make a buffer object for it
        glGenBuffers(1, &vb)
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vb)
        glBufferData(GLenum(GL_ARRAY_BUFFER), sizeof(GLfloat) * n_gVertexData.count, n_gVertexData, GLenum(GL_STATIC_DRAW))


        //Bind the buffer to the shader
        GLKVertexAttribPosition = GLuint(glGetAttribLocation(texShader, "positionCoords"))
        glEnableVertexAttribArray(GLKVertexAttribPosition)
        glVertexAttribPointer(GLKVertexAttribPosition, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, BUFFER_OFFSET(0));



        //******Load up all the texture coords*******
        //Make a buffer for texture coords
        glGenBuffers(1, &tc)
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), tc)
        glBufferData(GLenum(GL_ARRAY_BUFFER), sizeof(GLfloat) * n_cubeTexture.count, n_cubeTexture, GLenum(GL_STATIC_DRAW))

        //Bind the buffer to the shader location
        aTexCoordLoc = GLuint(glGetAttribLocation(texShader, "textureCoords"))
        glEnableVertexAttribArray(aTexCoordLoc);
        glVertexAttribPointer(aTexCoordLoc, 2, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, BUFFER_OFFSET(0))


        //Just the sampler for the texture position
        uSamplerLoc = glGetUniformLocation(texShader, "s_texture")

    }
func buildArrays()
    {
        //print("Building arrays")
        n_gVertexData = []

        var state:Int = 1
        for var i = 0; i < n_cubeTexture.count; i++
        {
            if (state == 1)
            {
                state = 2
                n_gVertexData.append(n_cubeTexture[i] * GLfloat(width * 1))
            }
            else if (state == 2)
            {
                state = 1
                n_gVertexData.append(n_cubeTexture[i] * GLfloat(height * 1))
                n_gVertexData.append(0)
            }
        }

    }
    var n_gVertexData:[GLfloat] = [0.0, 0, 0,    0, 200, 0,    0, 200, 0.0,
        200, 0.0, 0,     200, 200, 0.0,       0.0, 200, 0.0]

    //TR, BR, TL
    var n_cubeTexture:[GLfloat] = [0.0, 0.0,
        0.0, 1.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0,
        1.0, 0.0]

有谁知道可能导致这种情况发生的原因?我每次都使用相同的着色器。 Cool Image

1 个答案:

答案 0 :(得分:1)

你的顶点错了。

var n_gVertexData:[GLfloat] = [0.0, 0, 0,    0, 200, 0,    0, 200, 0.0,
        200, 0.0, 0,     200, 200, 0.0,       0.0, 200, 0.0]

第二个和第三个顶点是相同的。

此外,你有6个顶点,你绘制一个GL_TRIANGLE_STRIP,只需要4个顶点来绘制一个矩形。

enter image description here

这意味着你绘制了4个三角形。我的猜测是,你的矩形的更亮的一半实际上被绘制了两次 - 这解释了为什么它看起来更亮。

更改顶点以匹配三角形条带(以及相应的纹理坐标)或修复当前顶点并将其更改为GL_TRIANGLES。