Sign in to follow this  
smitty1276

GL + Cg: Cubemap problem...

Recommended Posts

I'm having problems getting a cubemap to work properly in a fragment shader (Cg). Each of the six textures works properly if I load them as individual textures, so I know the images are loading properly. I think my problem has something to do with the application side of things. Does anyone see anything in particular that would cause this to result in solid black in the shader? (BTW, *YES* the shader compiles and executes... if I change the shader to explicitly set the alpha of the lookup to something other than 1.0, it becomes semi-transparant black). Relevent snippets from app source:
    // This is where the cube map is generated...
    glGenTextures(1, &cubeMap);
    glEnable(GL_TEXTURE_CUBE_MAP);
    glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMap);
    //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
    //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
    for (int i = 0; i < 6; i++)
        setupCubemapTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, envTex[i]);

    cgGLSetTextureParameter(cubeMapParam, cubeMap);       
    cgGLSetManageTextureParameters(context, CG_TRUE);   
   
     
    // Here's what is in the function that sets up each face of cube map
    //   where target and srcImg are arguments passed above   
    glTexImage2D(
        target, 0, GL_RGBA, srcImg->getWidth(), srcImg->getHeight(), 
        0, GL_RGBA, GL_UNSIGNED_BYTE, srcImg->getData());			

Is the cgGLSetTextureParameter() call the correct way to setup a cubemap sampler in Cg? The runtime side of things isn't very well documented, but I assumed it was setup like any other texture. The fragment program--at least the part that is broken--is very straightforward...
float4 main(in FRAGDATA input,
            uniform float3      lightPos,
            uniform float3      cameraPos,
            uniform samplerCUBE envMap) : COLOR0
{
    // ... do some stuff until eventually...
    // Refract the ray...
    float refRay = refract(view, normal, val);
    color = texCUBE(envMap, refRay);
    color.a = 1.0;
    return color;
}

If I uncomment the glTexParameter lines in the cubemap setup, I get 2 distinct colors (I am refracting a sphere at the moment), but nothing like the actual texture lookup. Any ideas what might be wrong? Thanks in advance for any help you guys (and gals) can offer.

Share this post


Link to post
Share on other sites

I've found that the cgGLSetTextureParameter function is useless, and that one should just use the regular multitexture functions.

Like this in the shader:



float4 main(in FRAGDATA input,
uniform float3 lightPos,
uniform float3 cameraPos,
uniform samplerCUBE envMap : TEXUNIT0) : COLOR0
{
// ... do some stuff until eventually...
// Refract the ray...
float refRay = refract(view, normal, val);
color = texCUBE(envMap, refRay);
color.a = 1.0;
return color;
}




(Where TEXUNIT0 could be any TEXUNIT#)

Then binding it normally with glBindTexture.

Hope that helps, ~SPH

Share this post


Link to post
Share on other sites
I'll give it a try in a little while. I'm a bit skeptical, though, because I've never had any problems with setting up a sampler using cgGLSetTextureParameter(). I've never used cubemaps with a shader before, though.

Thanks for the idea. I'll let you know how it goes later tonight if you're interested.

Share this post


Link to post
Share on other sites
Well, I actually just got home and I'm about to make the changes and see what happens. Referencing your previous post, though... the following line

glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMap);

...does that not bind the cubemap sufficiently? Or rather, SHOULD that not bind the cubemap sufficiently?

Also, I should note that the very same images that compose the cube map are being used as individual GL_TEXTURE_2D textures to display the environment on the inside of a cube in the scene. Those are working fine using the cgGLSetTextureParameter way of doing things.

This is baffling. I wonder why the Cg runtime library isn't documented more clearly.

Share this post


Link to post
Share on other sites
ShmeeBegek,

Thanks for the help, but the problem turned out to be embarassingly simple and stupid. Notice, in my call to refract() in the shader, I was only storing the return value as a float, when I needed the float3. I'm not sure how I let that get by me for so long, but once I fixed that it worked beautifully.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this