我正在尝试在iOS下的OpenGL ES中加载纹理。这是我正在采取的方法:
纹理文件:t1.png:
渲染结果:
显然没有纹理映射到立方体。虽然我从texture2D()中获得了一些值,但是你会看到,在着色器中没有传递或处理光照或颜色。 .. 我的猜测是,我在某种程度上搞砸了我认为我将png转换为原始缓冲区以传递给纹理内存。
这是obj文件。测试立方体:
# Blender v2.63 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube2.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -0.000000 -0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
usemtl Material
s off
f 1/1/1 2/2/1 3/3/1
f 1/1/1 3/3/1 4/4/1
f 5/1/2 8/2/2 7/3/2
f 5/1/2 7/3/2 6/4/2
f 1/1/3 5/2/3 6/3/3
f 1/1/3 6/3/3 2/4/3
f 2/1/4 6/2/4 7/3/4
f 2/1/4 7/3/4 3/4/4
f 3/1/5 7/2/5 8/3/5
f 3/1/5 8/3/5 4/4/5
f 5/1/6 1/2/6 4/3/6
f 5/1/6 4/3/6 8/4/6
然后是解析obj并创建数据结构的代码 - 正常解析法线。然后,相关的代码片段是:
确保链接期间位置和texcoords绑定:
// Attach vertex shader to program.
glAttachShader(_program, vertShader);
// Attach fragment shader to program.
glAttachShader(_program, fragShader);
// Bind attribute locations.
glBindAttribLocation(_program, 0, "position");
glBindAttribLocation(_program, 1, "tCoordinates");
顶点着色器(只是将texcoords传递给片段着色器):
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
attribute vec4 position;
attribute vec2 tCoordinates;
varying highp vec2 tCoordinatesVarying;
void main()
{
tCoordinatesVarying = tCoordinates;
gl_Position = modelViewProjectionMatrix * position;
}
片段着色器,“应该”从加载的纹理中获取vec4并仅将其作为frag颜色传递:
uniform sampler2D s_texture;
varying highp vec2 tCoordinatesVarying;
void main()
{
gl_FragColor = texture2D(s_texture, tCoordinatesVarying);
}
设置代码,预绘图,在应用程序中设置顶点属性以及纹理和纹理单元/采样器绑定:(忽略我暂时不释放CG对象)
NSString* path = [[NSBundle mainBundle] pathForResource:@"t1.png" ofType:nil];
NSData* texData = [NSData dataWithContentsOfFile:path];
UIImage* image = [UIImage imageWithData:texData];
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void* imageData = malloc(height*width*4);
CGContextRef context = CGBitmapContextCreate(imageData, width, height, 8, 4*width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Host);
CGContextClearRect(context, CGRectMake(0, 0, width, height));
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);
CGContextRelease(context);
[EAGLContext setCurrentContext:self.context];
[self loadShaders];
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &_textureId);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
int loc = glGetUniformLocation(_program, "s_texture");
glUniform1i(loc, 0);
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vboData->vertexAttributesSize, vboData->vertexAttributes, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vboData->vertexAttributesStride,vboData->vPositionOffset);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vboData->vertexAttributesStride,vboData->vTCoordinatesOffset);
答案 0 :(得分:0)
从zacaj的建议中,我意识到我没有在着色器中获得任何texcoords。错误是:
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vboData->vertexAttributesStride,vboData->vTCoordinatesOffset);
因此,我启用了通过GLKVertexAttribTexCoord0
引用的属性编号,但绑定了属性1中的属性.Cleary,GLK常量未转换为1.
代替做
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vboData->vertexAttributesStride,vboData->vTCoordinatesOffset);
我开始在着色器中获得texcoords。