Search code examples
iosopengl-estexturesuv-mapping

Texture no longer renders after UV change


I'm going to write a texture atlas manager, but I've run into a problem. When I modified my original UV coordinates, my program no longer rendered anything?? This is my draw code:

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

However, it works fine like this:

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

Help?


Solution

  • Just a guess, but OpenGL considers (0, 0) to be the lower left corner of an image. Is it possible your graphic is in the top left, so that the area you're displaying is actually from a transparent part of the texture?

    If so then you can either adjust your texture coordinates or adjust the texture matrix stack so that (0, 0) is in the top left.