gpt4 book ai didi

ios - OpenGL ES 2.0-纹理四边形

转载 作者:行者123 更新时间:2023-12-01 17:43:00 26 4
gpt4 key购买 nike

每当我尝试渲染带纹理的四边形时,最终都会导致纹理的三角形截面变形:see this image


纹理是在GIMP中创建的PNG,我尝试了两种单独的纹理加载方法(均来自Apple的示例代码)。每种加载纹理的方法都会产生不同的结果(我不知道它是否是不同的默认设置,或者纹理是否存在问题),但是我无法正确渲染。

我尝试通过Fastest way to draw quads in OpenGL ES?中发布的建议以多种不同的方式设置索引/顶点/顶点,但还是没有运气。

我可能会缺少什么?

加载纹理的代码

- (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;
}

纹理坐标和顶点
const GLfloat texices[] =
{ 0,1,
1,1,
0,0,
1,0 };

glActiveTexture(GL_TEXTURE0);
glUniform1i(_texturedTextureUniformSlot, 0);
glVertexAttribPointer(_texturedTextureSlot, 2, GL_FLOAT, GL_FALSE, 0, texices);

GLfloat vertices[] = {-1, -1, 0, //bottom left corner
-1, 1, 0, //top left corner
1, 1, 0, //top right corner
1, -1, 0}; // bottom right rocner

GLubyte indices[] = {0,1,2, // first triangle (bottom left - top left - top right)
0,2,3}; // second triangle (bottom left - top right - bottom right)

glVertexAttribPointer(3, GL_FLOAT, 0, vertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

最佳答案

看来您的纹理坐标可能不正确(请注意,纹理似乎包裹在左侧)。

这是我过去使用的代码片段:

const float quadPositions[] = {  1.0,  1.0, 0.0, 
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
1.0, 1.0, 0.0 };
const float quadTexcoords[] = { 1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0 };

// stop using VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);

// setup buffer offsets
glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), quadPositions);
glVertexAttribPointer(ATTRIB_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), quadTexcoords);

// ensure the proper arrays are enabled
glEnableVertexAttribArray(ATTRIB_VERTEX);
glEnableVertexAttribArray(ATTRIB_TEXCOORD0);

// draw
glDrawArrays(GL_TRIANGLES, 0, 2*3);

那应该在z = 0处画两个三角形。您需要将投影的宽度和高度从-1设置为1。

编辑:

这是您的代码的有效版本:
const GLfloat texices[] = { 0, 0,
0, 1,
1, 1,
1, 0 };


const GLfloat vertices[] = { -1, -1, 0, // bottom left corner
-1, 1, 0, // top left corner
1, 1, 0, // top right corner
1, -1, 0}; // bottom right corner

const GLubyte indices[] = { 0, 2, 1, // first triangle (bottom left - top left - top right)
0, 3, 2 };


// ensure no VBOs or IBOs are bound
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), vertices);
glVertexAttribPointer(ATTRIB_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), texices);

// ensure the proper arrays are enabled
glEnableVertexAttribArray(ATTRIB_VERTEX);
glEnableVertexAttribArray(ATTRIB_TEXCOORD0);

glDisable(GL_CULL_FACE);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

关于ios - OpenGL ES 2.0-纹理四边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13455590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com