1
\$\begingroup\$

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);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

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

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

  free(spriteData);
  CGContextRelease(spriteContext);
}

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

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
-2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ why people put minuses, i think that problem is only in image resolution. so I am +1 \$\endgroup\$
    – Yevhen
    Jul 25, 2012 at 11:13
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – user744
    Jul 26, 2012 at 11:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .