Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: cube map problem, please help

  1. #1
    Member Contributor Bluebomber357's Avatar
    Join Date
    Oct 2008
    Posts
    92

    cube map problem, please help

    I know this is a GLSL question, but the GLSL board seems very dead.

    Anyways, I have just recently implemented a whole slew of cool GLSL shaders, like bump-mapping, cel-shading, per-pixel fog, spherical environmental mapping, and multi-texturing

    I got to cube-mapping, and everything has gone south. There are no projects with source that I can find (please link me if there are any). I have only found two tutorials, one in my GLSL orange book, which was a sham, because the reflection is tied to the camera. The other is from oZone3D.Net. I believe I implemented it correctly, but I just get a solid blue for my reflection

    Can anybody help, I am dying to get this last effect working
    The Blue Guy Always Wins

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: cube map problem, please help

    Be sure to take in account the view vector in the direction argument of textureCube.

    something like :

    textureCube(cubetex, reflect(normalize(worldPosition - CameraPosition), worldNormal);

  3. #3
    Member Contributor Bluebomber357's Avatar
    Join Date
    Oct 2008
    Posts
    92

    Re: cube map problem, please help

    Here is my frag and vertex code. It is straight from ozone3D.net. I am supplying my ModelWorld4x4 with this app code to an object at 0,0,0:

    float mat16[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
    glUniformMatrix4fv(getUniLoc(cubeMapProgShader, "ModelWorld4x4"), 15, false, mat16);

    My CameraPos uniform var is supplied eye coordinates, the same values from the first three arguements in gluLookAt.


    Vert Code:

    uniform mat4 ModelWorld4x4;
    uniform vec3 CameraPos;

    mat3 GetLinearPart( mat4 m )
    {
    mat3 result;

    result[0][0] = m[0][0];
    result[0][1] = m[0][1];
    result[0][2] = m[0][2];

    result[1][0] = m[1][0];
    result[1][1] = m[1][1];
    result[1][2] = m[1][2];

    result[2][0] = m[2][0];
    result[2][1] = m[2][1];
    result[2][2] = m[2][2];

    return result;
    }

    void main()
    {
    gl_Position = ftransform();

    // Color map texture coordinates.
    // Increase a little bit the tiling by 2.
    gl_TexCoord[0] = gl_MultiTexCoord0 * 2.0;

    mat3 ModelWorld3x3 = GetLinearPart( ModelWorld4x4 );

    // find world space position.
    vec4 WorldPos = ModelWorld4x4 * gl_Vertex;

    // find world space normal.
    vec3 N = normalize( ModelWorld3x3 * gl_Normal );

    // find world space eye vector.
    vec3 E = normalize( WorldPos.xyz - CameraPos.xyz );

    // calculate the reflection vector in world space.
    gl_TexCoord[1].xyz = reflect( E, N );
    }

    Frag Code:

    uniform samplerCube cubeMap;
    uniform sampler2D colorMap;

    const float reflect_factor = 0.9;

    void main (void)
    {
    vec4 output_color;

    // Perform a simple 2D texture look up.
    vec3 base_color = texture2D(colorMap, gl_TexCoord[0].xy).rgb;

    // Perform a cube map look up.
    vec3 cube_color = textureCube(cubeMap, gl_TexCoord[1].xyz).rgb;

    // Write the final pixel.
    gl_FragColor = vec4( mix(base_color, cube_color, reflect_factor), 1.0);
    }
    The Blue Guy Always Wins

  4. #4
    Member Contributor Bluebomber357's Avatar
    Join Date
    Oct 2008
    Posts
    92

    Re: cube map problem, please help

    I have been trying to get cube mapping to work forever now. Can anybody help? I would kill for some sample programs...

    No matter what I try, the camera is always locked with the reflection
    The Blue Guy Always Wins

  5. #5
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: cube map problem, please help

    Bluebomber357, a very similar problem was mentionned solved here : http://www.ogre3d.org/forums/viewtop...509e6f4e2718c6

  6. #6
    Member Contributor Bluebomber357's Avatar
    Join Date
    Oct 2008
    Posts
    92

    Re: cube map problem, please help

    ZBufferR, I've seen that thread before. It seems to use Ogre3D specific code for the solution, am I right?

    I am still stuck on this problem, more help please
    The Blue Guy Always Wins

  7. #7
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: cube map problem, please help

    You mean the "param_named_auto worldMatrix world_matrix" ? It seems he just passes the world matrix to the program.

    You should start again with the orange book one.
    Then add the camera world matrix position as well as object world matrix, and use that in the reflection vector calculation:

    vec4 ecCamPos = worldMatrix * camPos;
    vec3 ecCamPos3 = ecCamPos.xyz / ecCamPos.w;
    vec3 i = normalize(ecPosition3 - ecCamPos3);
    refvector = reflect(i, n);


    Sorry I can't help more.

  8. #8
    Member Contributor Bluebomber357's Avatar
    Join Date
    Oct 2008
    Posts
    92

    Re: cube map problem, please help

    I am still confused on how to implement it correctly, has nobody done cube-mapping in GLSL before? I feel like it is one of those effects that should have a hundred examples on the web
    The Blue Guy Always Wins

  9. #9
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: cube map problem, please help

    From DXSDK, HDRCubeMap sample:
    Code :
    void HDRVertEnvMap( float4 Pos : POSITION,
                        float3 Normal : NORMAL,
                        out float4 oPos : POSITION,
                        out float3 EnvTex : TEXCOORD0 )
    {
        oPos = mul( Pos, g_mWorldView );
     
        //
        // Compute normal in camera space
        //
        float3 vN = mul( Normal, g_mWorldView );
        vN = normalize( vN );
     
        //
        // Obtain the reverse eye vector
        //
        float3 vEyeR = -normalize( oPos );
     
        //
        // Compute the reflection vector
        //
        float3 vRef = 2 * dot( vEyeR, vN ) * vN - vEyeR;
     
        //
        // Store the reflection vector in texcoord0
        //
        EnvTex = vRef;
     
        //
        // Apply the projection
        //
        oPos = mul( oPos, g_mProj );
    }
     
     
    //-----------------------------------------------------------------------------
    // Pixel Shader: HDRPixEnvMap
    // Desc: Process pixel for HDR environment mapped object
    //-----------------------------------------------------------------------------
    float4 HDRPixEnvMap( float3 Tex : TEXCOORD0 ) : COLOR
    {
        return g_fReflectivity * texCUBE( g_samCubeMap, Tex );
    }

    I usually study shader techniques from DX samples. It's just that matrix multiplication order is reverse, and texel centrinos...

  10. #10
    Member Contributor Bluebomber357's Avatar
    Join Date
    Oct 2008
    Posts
    92

    Re: cube map problem, please help

    I am not an expert at GLSL yet, so converting DX code to GLSL isn't going to help much for me, thanks though. I am still searching the internet for good source code to study from.

    Here is the only friggin GLSL cubemapping code with full source (includes application code) on the entire internet that I have found in 5 hours of searching. Amazing, I have only found one! http://tfc.duke.free.fr/screens/envmap.png

    This code in Linux, and I couldn't get it to run in visual studio, but I copied the relevant code into my "shader testing program", which is just the brick shader program from 3Dshaders.com. I still have the same problem though of the camera following the reflection.
    The Blue Guy Always Wins

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem drawing a cube
    By craterz in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-04-2012, 06:41 PM
  2. coding problem with cube
    By coda in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-08-2003, 06:22 PM
  3. Problem with Rotating cube....
    By rehseboy in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-03-2003, 02:37 PM
  4. Cube map sampling problem
    By h2 in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 05-25-2002, 11:02 AM
  5. Cube texturing problem
    By in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-09-2001, 11:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean