Problem environment mapping using samplerCube

Hi everyone,

I’m having a strange issue with environment mapping by using samplerCube for reflections (on an iPhone 4).

I don’t think the issue is with my shader code because I’ve tested the output without the samplerCube (by using the reflection vector as the gl_FragColor), and it seems to output as I’d expect.

When I try using the samplerCube though, all fragments are just output as black colour.

Here is my shader code, first of all the vertex shader:

//  Shader.vsh

attribute vec4 position;
attribute vec3 normal;

uniform mat4 Projection;
uniform mat4 Modelview;
uniform mat3 Model;
uniform vec3 EyePosition;

varying vec3 ReflectDir;

void main()
    gl_Position = Projection * Modelview * position;
    // Compute eye direction in object space:
    highp vec3 eyeDir = normalize( - EyePosition);

    // Reflect eye direction over normal and transform to world space:
    ReflectDir = Model * reflect(eyeDir, normal);

And here is my fragment shader:

//  Shader.fsh
varying highp vec3 ReflectDir;
uniform samplerCube cubeMap;

void main()
    gl_FragColor = vec4(ReflectDir.x, ReflectDir.y, ReflectDir.z, 1.0);
    //gl_FragColor = textureCube(cubeMap, ReflectDir);

In my fragment shader, you can see I’ve commented out the line causing the issue which tries to use the reflection direction vector to sample the cube map.
Above it, you can see my debug code which is using the reflection direction as a colour (to visually debug).

I get no errors during build or run, just the wrong output.

To load my cube map, I’m using GLKit for convenience. Here is my code for that:

//setup the cube map
        NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderGenerateMipmaps];
        NSString *rightTex = [[NSBundle mainBundle] pathForResource:@"right" ofType:@"png"];
        NSString *leftTex = [[NSBundle mainBundle] pathForResource:@"left" ofType:@"png"];
        NSString *topTex = [[NSBundle mainBundle] pathForResource:@"top" ofType:@"png"];
        NSString *bottomTex = [[NSBundle mainBundle] pathForResource:@"down" ofType:@"png"];
        NSString *frontTex = [[NSBundle mainBundle] pathForResource:@"front" ofType:@"png"];
        NSString *backTex = [[NSBundle mainBundle] pathForResource:@"back" ofType:@"png"];
        NSError *error;
        cubeMapInfo = [GLKTextureLoader cubeMapWithContentsOfFiles:[NSArray arrayWithObjects:rightTex, leftTex, topTex, bottomTex, frontTex, backTex, nil] options:options error:&error];
        if (error!=nil){
            NSLog(@"ERROR: %@", error.localizedDescription);
            NSLog(@"Failure reason: %@", error.description);
            NSLog(@"Error code: %i", error.code);
        } else {
            NSLog(@"No error!");
            [cubeMapInfo retain];
            NSLog(@"Cube map name: %i",;

I’m not sure how I can debug further or what to do next. If anyone has any idea then your help would be much appreciated.


This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.