我一直在使用Open GL ES 2.0编程指南,并且在关于纹理的第9章之后,我已经设置了相同的像素阵列(2x2-红绿蓝黄),它只显示整个三角形的红色。似乎无法找到问题,我认为我的着色器也很好,请帮助我的代码。
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
[self loadShaders];
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_SRC_COLOR);
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(gRectVertexData), gRectVertexData, GL_STATIC_DRAW);
// 2 x 2 Image, 3 bytes per pixel(R, G, B)
GLubyte pixels[12] =
{
255,0,0, // Red
0,255,0, // Green
0,0,255, // Blue
0,255,255 // Yellow
};
// Use tightly packed data
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Generate a texture object
glGenTextures(1, &texture[0]);
// Bind the texture object
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Load the texture
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, 2,2, 0, GL_RGB,
GL_UNSIGNED_BYTE, pixels);
// Set the filtering mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLint SamplerLoc = glGetUniformLocation(_program, "Texture");
glUniform1i(SamplerLoc, 0);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(24));
glBindVertexArrayOES(0);
}
我在加载着色器部分
中有这个glBindAttribLocation(_program, ATTRIB_VERTEX, "position");
glBindAttribLocation(_program, ATTRIB_NORMAL, "normal");
glBindAttribLocation(_program, ATTRIB_TEX, "TexCoordIn");
然后我的顶点着色器和片段着色器如下
attribute vec4 position;
attribute vec3 normal;
attribute vec2 TexCoordIn;
varying lowp vec4 colorVarying;
varying vec2 TexCoordOut;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
void main()
{
vec3 eyeNormal = normalize(normalMatrix * normal);
vec3 lightPosition = vec3(0.0, 0.0, 1.0);
vec4 diffuseColor = vec4(0.8, 0.4, 1.0, 1.0);
float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));
colorVarying = diffuseColor*nDotVP;
gl_Position = modelViewProjectionMatrix * position;
TexCoordOut = TexCoordIn;
}
片段着色器
varying lowp vec4 colorVarying;
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;
void main()
{
lowp vec4 col = texture2D(Texture, TexCoordOut);
col.x *= colorVarying.x;
col.y *= colorVarying.x;
col.z *= colorVarying.x;
col.a = 1.0;
gl_FragColor = col;
}
答案 0 :(得分:1)
改变你的电话
glBindAttribLocation(_program, ATTRIB_TEX, "TexCoordIn");
使用GLKVertexAttribTexCoord0
代替您的ATTRIB_TEX
枚举。
这是OpenGL Game项目中令人讨厌的陷阱。他们在glBindAttribLocation
调用位置和法线时使用的枚举成员恰好具有与GLKVertexAttribPosition和GLKVertexAttribNormal枚举相同的值,但这只是巧合。
查看headnoodles上的帖子,了解分步说明。该示例使用GLKit但其他所有内容都应该相同。
答案 1 :(得分:0)
如果只输出纹理坐标会怎样?
你看到[0-1]的渐变吗?