2013-05-21 116 views
0

我刚开始在iOS上使用openGLES2.0,并试图在屏幕上渲染纹理。纹理渲染出现沉闷(openGL ES 2.0 iOS)

问题是,纹理看起来与它应该呈现的真实图像相比显得不同(暗淡,几乎是单色)。

片段着色器

varying lowp vec4 DestinationColor; 
varying lowp vec2 TexCoordOut; 
uniform sampler2D Texture; 

void main(void) { 
    gl_FragColor = texture2D(Texture, TexCoordOut); 
} 

顶点着色器

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

的:在某种意义上纹理图像心不是真实颜色,同时用openGL

的着色器程序是相当简单的渲染反射相同的纹理在openGL ES 1.0上运行良好,颜色正确显示!

编辑:

OpenGL上下文设置代码:

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

纹理加载代码:

- (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编辑: 层设置

- (void)setupLayer { 
    _eaglLayer = (CAEAGLLayer*) self.layer; 
    _eaglLayer.opaque = YES; 
    _eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 

} 

我已将此代码添加到图层设置。仍然没有运气!

+0

您可能想要显示用于加载图像和上传纹理的代码,以及如何设置EAGLContext。 –

回答

1

要保留的颜色应该具有24位或32位彩色初始化的OpenGL(请求的配置与888 RGB代替 CONFIGS)。你也应该使用8位每通道纹理。

产生的图像是否有绿色色调?这是16位颜色中最引人注目的颜色失真。

+0

我在问题中添加了代码以显示我如何设置我的EAGL上下文。我如何在这里请求配置888 RGB? – Sagar

+0

我在设置图层时按照您的建议添加了一段代码。它仍然不起作用!任何想法可能会出错? – Sagar

+0

并没有绿色的色调。只是结束了单色! – Sagar