Texture rendering appears Dull (openGL ES 2.0 iOS)
Ive just started with openGLES2.0 on iOS and im trying to render a texture onto screen.
The problem is that the texture appears different (dull and almost monochromatic) as compared to the real image it is supposed to render. In a sense the true colors of the texture image isnt reflected while rendering using openGL
The shader program is quite simple:
Fragment Shader
varying lowp vec4 DestinationColor;
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;
void main(void) {
gl_FragColor = texture2D(Texture, TexCoordOut);
}
Vertex Shader
attribute vec4 Position;
uniform mat4 Projection;
uniform mat4 Modelview;
attribute vec2 TexCoordIn;
varying vec2 TexCoordOut;
void main(void) {
gl_Position = Projection * Modelview * Position;
TexCoordOut = TexCoordIn;
}
The same texturing worked fine on openGL ES 1.0, with the colors showing up correctly!
EDIT:
OpenGL context setup Code:
- (void)setupContext {
EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES2;
_context = [[EAGLContext alloc] initWithAPI:api];
if (!_context) {
DLog(@"Failed to initialize OpenGLES 2.0 context");
exit(1);
}
if (![EAGLContext setCurrentContext:_context]) {
DLog(@"Failed to set current OpenGL context");
exit(1);
}
}
Texture Loading Code:
- (GLuint)setupTexture:(UIImage *)image {
glGenTextures(1, &Texture1);
glBindTexture(GL_TEXTURE_2D, Texture1);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef imgContext = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGContextTranslateCTM (imgContext, 0, height);
CGContextScaleCTM (imgContext, 1.0, -1.0);
CGContextSetBlendMode(imgContext, kCGBlendModeCopy);
CGColorSpaceRelease( colorSpace );
CGContextClearRect( imgContext, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( imgContext, 0, height - height );
CGContextDrawImage( imgContext, CGRectMake( 0, 0, width, height ), image.CGImage );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
CGContextRelease(imgContext);
free(imageData);
return Texture1;
}
SECOND EDIT: Layer Setup
- (void)setupLayer {
_eaglLayer = (CAEAGLLayer*) self.layer;
_eaglLayer.opaque = YES;
_eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
}
Ive added this piece code to the layer setup. Still no luck!
To preserve colors you should initialize OpenGL with 24 or 32 bit color (request config with 888 RGB instead of 565 configs). Also you should use 8-bit per channel textures.
Does resulting image has some green tint? This is the most noticeable color distortion in 16-bit color.
链接地址: http://www.djcxy.com/p/33608.html