所以我一直试图画一段时间的纹理。我遇到了一个新问题。我想知道是否可以强制每个像素的透明度,所以我更改了通用片段着色器并强制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]
答案 0 :(得分:1)