• Advertisement
Sign in to follow this  

Shining Meshes

This topic is 1663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I noticed in some modern games that all the meshes are shining.
 
I'm not talking about specular light here, but the entire mesh is shining which make it look much more realistic, how do I get this effect?

Share this post


Link to post
Share on other sites
Advertisement

Like the environment is reflected on polished metal? Maybe you are looking for environmental mapping.

Edited by Tispe

Share this post


Link to post
Share on other sites

Do you mean meshes which seem to be emitting light by themselves instead of reflecting light from light sources?

You can achieve this with emissive materials, which are materials which have an emissive color or texture which gets added to the lighting results of the geometry.

Note that emissive materials in general don't affect surrounding geometry though as they're not actual light sources

Share this post


Link to post
Share on other sites

Yeah, those are done with environment maps, like Tispe said

This technique is achieved by calculating a reflection vector for your surface point and using it to sample relfections from a cube map.

Share this post


Link to post
Share on other sites

At start use pre-existing cube texture and practice with that.

If you must, to match the surrounding scene you must have dynamic cube, which means you must render your scene multiple times, possibly in lower resolution for better performance, something like this (chopped code from my old project):

D3DXCreateRenderToEnvMap(device, CUBEMAP_RESOLUTION, 1, format, TRUE, D3DFMT_D16, &RenderToEnvMap);
D3DXCreateCubeTexture(device, CUBEMAP_RESOLUTION, 1, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &CubeMapTexture);
...
void RenderSceneIntoEnvMap(D3DXVECTOR3 camPosition)
{
    // 90 degree fov to get a seamless environment map texture
    D3DXMATRIX cubeProj;
    D3DXMatrixPerspectiveFovLH( &cubeProj, D3DX_PI * 0.5f, 1.0f, 1.0f, 1000.0f );

    RenderToEnvMap->BeginCube(CubeMapTexture);

    for(UINT i = 0; i < 6; i++)
    {
        RenderToEnvMap->Face((D3DCUBEMAP_FACES)i, 0 );
        D3DXMATRIX cubeFaceView = getCubeMapViewMatrix((D3DCUBEMAP_FACES)i, camPosition);
        D3DDevice->Clear( 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 );
        RenderScene(&cubeFaceView, &cubeProj);
    }

    RenderToEnvMap->End(0);
}
...
 
D3DXMATRIX getCubeMapViewMatrix(DWORD dwFace, D3DXVECTOR3 camPosition)
{
    D3DXVECTOR3 lookDir;
    D3DXVECTOR3 upDir;

    switch( dwFace )
    {
    case D3DCUBEMAP_FACE_POSITIVE_X:
        lookDir = D3DXVECTOR3( 1.0f, 0.0f, 0.0f ) + camPosition;
        upDir   = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
        break;
    case D3DCUBEMAP_FACE_NEGATIVE_X:
        lookDir = D3DXVECTOR3(-1.0f, 0.0f, 0.0f ) + camPosition;
        upDir   = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
        break;
    case D3DCUBEMAP_FACE_POSITIVE_Y:
        lookDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) + camPosition;
        upDir   = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
        break;
    case D3DCUBEMAP_FACE_NEGATIVE_Y:
        lookDir = D3DXVECTOR3( 0.0f,-1.0f, 0.0f ) + camPosition;
        upDir   = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
        break;
    case D3DCUBEMAP_FACE_POSITIVE_Z:
        lookDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f ) + camPosition;
        upDir   = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
        break;
    case D3DCUBEMAP_FACE_NEGATIVE_Z:
        lookDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f ) + camPosition;
        upDir   = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
        break;
    }

    D3DXMATRIX cubeFaceView;
    D3DXMatrixLookAtLH( &cubeFaceView, &camPosition, &lookDir, &upDir );
    return cubeFaceView;
}

Or use some other method to render your environment like "dual paraboloid" but then that is not cube texture.

Edited by belfegor

Share this post


Link to post
Share on other sites

@belfegor: I'm trying to implement the basic idea by starting with a texture that I already have, I'm loading the texture and passing it to the shader.

 

But I'm NOT getting any reflection.

 

Here is how I calculate the reflection:

texture cubeMapTex;
sampler cubeSampler = sampler_state
{
   Texture = <cubeMapTex>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;
   AddressU  = Clamp;
   AddressV  = Clamp;
   MaxAnisotropy = 16;
};

float4 PS( VS_OUTPUT IN ) : COLOR
{
       float3 toCamera = cameraPos - IN.worldPos;
       float3 cubeTexCoord = reflect( normalize(-toCamera), IN.Normal );
       float3 reflectedColor = texCUBE( cubeSampler, cubeTexCoord );
       return float4(tex2D(colorMap, IN.UV).rgb * reflectedColor, 1.0);
}

Share this post


Link to post
Share on other sites

Make sure that you pass cube texture, i am not sure if that would work with 2D texture. If you don't have one download it from here. And use D3DXCreateCubeTextureFromFile to load it.

Try returning just cubemap color and report back if you see it:

//return float4(tex2D(colorMap, IN.UV).rgb * reflectedColor, 1.0);
return float4(reflectedColor, 1.0);

When you got that working, you might want to add instead multiply with diffuse, and also maybe mask it with specular texture:

float4 diffuse = tex2D( ...);
float3 specular = tex2D(...).rgb;
float3 cube = texCUBE(...);
 
diffuse.rgb += specular * cube;
Edited by belfegor

Share this post


Link to post
Share on other sites

@belfegor: I downloaded one of them, but I see 6 JPG files instead of a single one, when I use D3DXCreateCubeTextureFromFile() I get the following output message:

D3DX: File does not contain a cube texture

Share this post


Link to post
Share on other sites

OMG.

You could make one yourself with "DxTex.exe" utility program from DX SDK.

It would be to big to upload it, so i made you a little tutorial:

 

1. Open DxTex.exe and select New texture:

cubemap01.jpg

 

2. then set type and appropriate size:

cubemap02.jpg

 

3. then choose open onto surface (or cubemap face), choose/load your jpg image, and wait it to load it into surface:

cubemap03.jpg

 

4. then switch to other face (note the name of the current surface to match image name!) and repeat step 3 for all faces:

cubemap04.jpg

 

5. Save texture

Edited by belfegor

Share this post


Link to post
Share on other sites

@belfegor: Many thanks! smile.png That works very well.

 

Now, how do I determine how much reflection? I mean I want to control how much reflection appear on the mesh.

 

I need to change this line in my Pixel Shader:

return float4(tex2D(colorMap, IN.TexCoord).rgb * reflectedColor, 1.0);

BTW, Is that technique used in water reflection as well? (e.g: sea water reflection)

Edited by Medo3337

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement