Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to pass UIImage to GLSL shader. The fragment shader is:

varying highp vec2 textureCoordinate;

uniform sampler2D inputImageTexture;
uniform sampler2D inputImageTexture2;

void main()
  highp vec4 color = texture2D(inputImageTexture, textureCoordinate); 
  highp vec4 color2 = texture2D(inputImageTexture2, textureCoordinate);

  gl_FragColor = color * color2;


What I want to do is send images from camera and do multiply blend with texture. When I just send data from camera, everything is fine. So problem should be with sending another texture to shader. I am doing it this way:

- (void)setTexture:(UIImage*)image forUniform:(NSString*)uniform {

  CGSize sizeOfImage = [image size];
  CGFloat scaleOfImage = [image scale];
  CGSize pixelSizeOfImage = CGSizeMake(scaleOfImage * sizeOfImage.width, scaleOfImage * sizeOfImage.height);

  //create context
  GLubyte * spriteData = (GLubyte *)malloc(pixelSizeOfImage.width * pixelSizeOfImage.height * 4 * sizeof(GLubyte));
  CGContextRef spriteContext = CGBitmapContextCreate(spriteData, pixelSizeOfImage.width, pixelSizeOfImage.height, 8, pixelSizeOfImage.width * 4, CGImageGetColorSpace(image.CGImage), kCGImageAlphaPremultipliedLast);

  //draw image into context
  CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, pixelSizeOfImage.width, pixelSizeOfImage.height), image.CGImage);

  //get uniform of texture
  GLuint uniformIndex = glGetUniformLocation(__programPointer, [uniform UTF8String]);

  //generate texture
  GLuint textureIndex;
  glGenTextures(1, &textureIndex);
  glBindTexture(GL_TEXTURE_2D, textureIndex);


  //create texture
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixelSizeOfImage.width, pixelSizeOfImage.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

  glBindTexture(GL_TEXTURE_2D, textureIndex);
  //"send" to shader
  glUniform1i(uniformIndex, 1); 


Uniform for texture is fine, glGetUniformLocation function do not returns -1. The texture is PNG file of resolution 2000x2000 pixels.

PROBLEM: When the texture is passed to shader, I have got "black screen". Maybe problem are parameters of the CGContext or parameters of the function glTexImage2D

Thank you

share|improve this question
add comment

3 Answers

You should try this code with texture where resolution is power of 2 for each side (512x512, 256x1024 for example..). And max texture resolution in OpenGL ES for iPhone 3GS is 1024x1024, dont forget it.

share|improve this answer
why people put minuses, i think that problem is only in image resolution. so I am +1 –  bobenko Jul 25 '12 at 11:13
@bobenko: The 3GS supports 2048x2048 textures, and NPOT textures are supported on all iPhone models (though with various restrictions and degraded performance). So, please don't +1 this answer. –  user744 Jul 26 '12 at 11:40
add comment

glGetUniformLocation returns a glint, so it probably is returning a -1, but it get converted to a uint.

share|improve this answer
add comment

You may want to use the GLKit framework. In particular GLKTextureLoader.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.