UV变化后纹理不再渲染

时间:2011-09-01 23:53:54

标签: ios opengl-es textures uv-mapping

我要写一个纹理图集管理器,但我遇到了一个问题。当我修改我的原始UV坐标时,我的程序不再呈现任何东西?这是我的抽奖代码:

- (void)drawFrame {
[(EAGLView *)self.view setFramebuffer];

//GLfloat aspectRatio = self.view.bounds.size.height/self.view.bounds.size.width; 
float m[16] = {2/self.view.bounds.size.width, 0, 0, 0, 0, 2/self.view.bounds.size.height, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1 };

CGSize imageSize = CGSizeMake(512, 512);
CGRect drawBounds = CGRectMake(0, 0, 106, 126);

GLfloat x = (1/imageSize.width) * drawBounds.size.width;
GLfloat y = (1/imageSize.height) * drawBounds.size.height;

static const GLfloat vertices[] = {
    -53.0f, -63.0f,
    53.0f, -63.0f,
    -53.0f,  63.0f,
    53.0f,  63.0f,
};

GLfloat texCoords[] = {
    0.0, 0.0,
    x, 0.0,
    0.0, y,
    x, y
};

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(program);

[[TextureLibrary sharedTextureLibrary] bindTexture:@"bounce" toSlot:GL_TEXTURE0];

glUniform1f(uniforms[UNIFORM_TEXTURE], 0);
glUniformMatrix4fv(UNIFORM_MVP_MATRIX, 1, GL_FALSE, m);

glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, vertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_TEXTURE_POSITION, 2, GL_FLOAT, 0, 0, texCoords);
glEnableVertexAttribArray(ATTRIB_TEXTURE_POSITION);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


[(EAGLView *)self.view presentFramebuffer];
}

然而,它的工作原理如下:

GLfloat texCoords[] = {
    0.0, 0.0,
    1.0, 0.0,
    0.0, 1.0,
    1.0, 1.0
};

帮助?

1 个答案:

答案 0 :(得分:1)

只是一个猜测,但OpenGL认为(0,0)是图像的左下角。您的图形是否可能位于左上方,因此您显示的区域实际上来自纹理的透明部分?

如果是这样,那么您可以调整纹理坐标或调整纹理矩阵堆栈,使(0,0)位于左上角。