因此,我目前正在使用iOS学习OpenGL ES 2.0并致力于迷宫游戏。迷宫是随机生成的(因此不是加载模型),我的工作是对迷宫的墙壁和地板进行纹理处理。我的方法是将迷宫视为一系列多维数据集,并且我有代码分别绘制多维数据集的各个面(因此我可以通过简单地将一些面排除在外而创建路径)。
使用捕获的GPU框架,我已经确认纹理确实正确加载,框架缓冲区中的数据正确,并且没有出现任何错误。我可以看到其他照明效果(因此面部不是完全黑色),但是没有纹理出现。
这是我定义立方体面的方法
GLfloat rightCubeVertexData[] =
{
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
};
GLfloat rightCubeNormalData[] =
{
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
};
GLfloat rightCubeTexCoords[] =
{
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0,
};
其他面的定义基本上是相同的方式(除了它们每个都在一个数组中,将位置,法线和tex坐标分开是我尝试的;我只是想使一个面具有纹理,然后我将扩展到其余部分)。
这是我将数据加载到缓冲区的方法
glGenVertexArraysOES(1, &_rightVertexArray);
glBindVertexArrayOES(_rightVertexArray);
glGenBuffers(3, _rightVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _rightVertexBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(rightCubeVertexData), rightCubeVertexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0));
glBindBuffer(GL_ARRAY_BUFFER, _rightVertexBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(rightCubeNormalData), rightCubeNormalData, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0));
glBindBuffer(GL_ARRAY_BUFFER, _rightVertexBuffer[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(rightCubeTexCoords), rightCubeTexCoords, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0));
同样,使用三个缓冲区是一个实验,其余的都在一个带有偏移量的缓冲区中定义。
这是我加载纹理的方式
crateTexture = [self setupTexture:@"crate.jpg"];
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, crateTexture);
glUniform1i(uniforms[UNIFORM_TEXTURE], 0);
// Load in and set up texture image (adapted from Ray Wenderlich)
- (GLuint)setupTexture:(NSString *)fileName
{
CGImageRef spriteImage = [UIImage imageNamed:fileName].CGImage;
if (!spriteImage) {
NSLog(@"Failed to load image %@", fileName);
exit(1);
}
size_t width = CGImageGetWidth(spriteImage);
size_t height = CGImageGetHeight(spriteImage);
GLubyte *spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));
CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
CGContextRelease(spriteContext);
GLuint texName;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
free(spriteData);
return texName;
}
然后,在适当的时间,我只需调用glDrawArrays来绘制面。我对此完全感到困惑,这可能是一个非常愚蠢的错误,但是任何人都可以提供的任何帮助将不胜感激。
PS这是我的片段着色器
varying vec3 eyeNormal;
varying vec4 eyePos;
varying vec2 texCoordOut;
uniform sampler2D texture;
uniform vec3 flashlightPosition;
uniform vec3 diffuseLightPosition;
uniform vec4 diffuseComponent;
uniform float shininess;
uniform vec4 specularComponent;
uniform vec4 ambientComponent;
void main()
{
vec4 ambient = ambientComponent;
vec3 N = normalize(eyeNormal);
float nDotVP = max(0.0, dot(N, normalize(diffuseLightPosition)));
vec4 diffuse = diffuseComponent * nDotVP;
vec3 E = normalize(-eyePos.xyz);
vec3 L = normalize(flashlightPosition - eyePos.xyz);
vec3 H = normalize(L+E);
float Ks = pow(max(dot(N, H), 0.0), shininess);
vec4 specular = Ks*specularComponent;
if( dot(L, N) < 0.0 ) {
specular = vec4(0.0, 0.0, 0.0, 1.0);
}
gl_FragColor = (ambient + diffuse + specular) * texture2D(texture, texCoordOut);
//gl_FragColor = ambient + diffuse + specular;
gl_FragColor.a = 1.0;
}
是的,所有统一名称都是正确的,并且与主代码中的某些名称相对应。
编辑:这是顶点着色器
precision mediump float;
attribute vec4 position;
attribute vec3 normal;
attribute vec2 texCoordIn;
varying vec3 eyeNormal;
varying vec4 eyePos;
varying vec2 texCoordOut;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat3 normalMatrix;
void main()
{
eyeNormal = (normalMatrix * normal);
eyePos = modelViewMatrix * position;
texCoordOut = texCoordIn;
gl_Position = modelViewProjectionMatrix * position;
}
总结从注释中完成的过程...
处理纹理时可能会出错,因此最好知道如何找出问题所在。
纹理本身要注意的事项:
检查是否设置了诸如
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glUniform1i(uniformName, 0)
最后一个参数对应于活动纹理而不是纹理ID的位置。除此之外,您的纹理坐标很可能被弄乱了,这似乎是一个非常普遍的问题。要进行调试,最好将片段着色器中从纹理获取的颜色替换为纹理坐标本身。例如,更换texture2D(texture, texCoordOut)
有vec4(texCoordOut.x, texCoordOut.y, .0, 1.0)
。由于纹理坐标应在[0,1]范围内,因此您应该在场景中看到红色和绿色之间的良好渐变。如果看不到它们,则纹理坐标将被弄乱:如果所有为黑色,则坐标均为零,如果大多数为黄色,则坐标很可能太大。
在您的情况下,纹理坐标全为黑色,这意味着您始终从纹理中获取第一个像素,因此场景中的颜色恒定。此时要检查的是:
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 12, BUFFER_OFFSET(0))
(检查所有参数)glEnableVertexAttribArray(GLKVertexAttribTexCoord0)
在您的情况下,您忘记绑定纹理坐标属性,这是很自然会错过的。
根据您提供给我们的信息,不可能找到此错误,但是您应该注意我提供的过程,以查明问题的根源。将来也可能很方便。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句