• Advertisement

• ### Popular Now

• 11
• 9
• 10
• 9
• 10
• Advertisement
• Advertisement
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# environment mapping with vertex shaders

This topic is 6069 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

hi, i want to use environment mapping in a space-shooter for reflections on spaceships, that would look pretty cool i think. so i must calculate the texcoords for the environment-map for every vertex of the spaceships considering the position of the camera... doing this on every frame with quite a number vertices will be slow i think, but a vertex shader on my geforce3 could do that quite fast, eh? could somebody give me a hint how to do that? im using d3d with nvasm to compile shaders. i have looked at the examples in the nvidia sdk but there´s no example of simple environment mapping, there´s only combined bump/environment mapping and after looking at the shaders i saw that this is very complex. thanks for any help cody

#### Share this post

##### Share on other sites
Advertisement
Take a look at the EnvMapping samples in the DirectX SDK...

In particular look at the cubemap sample.

You can get D3D to calculate the envmap texture coordinates for you if you''re using the fixed function pipeline. There is a TEXCOORDINDEX flag for CAMERASPACEREFLECTIONVECTOR which as it sounds generates the texture coordinate from the reflection vector of the vertex in camera space.

You can also do it in a vertex shader too.

The cubemap sample demonstrates both methods (I''m going off the 8.1 beta SDK, but AFAIK its not changed since 8.0).

The reflection vector is:

R = 2(E dot N)N-E

Where: E is the Eye vector (normalised vector from the vertex position to the eye/viewer/camera)

And: N is the vertex normal.

R is used to look up into the environment map.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

#### Share this post

##### Share on other sites
nice, really great help!

vertex shaders can wait, this way is easier!

one thing i dont understand:
"R is used to look up into the environment map."
mmhh... is R the texture coordinate?...no?...how do i use it?

#### Share this post

##### Share on other sites
  N R ^ E \_ | _/ \_ | _/ \|/----------------------- p o l y g o n

The above ASCII-art diagram is a side on view of a polygon.

N is the normal vector which lets us know which way the polygon points in the world.

E is the vector from the polygon to where the viewers eye is viewing from (where your camera is).

If the polygon was a perfect mirror, R is what would be reflected back to the eye in the mirror.

You can proove this for yourself with a real mirror - put it on a table, look into the mirror at a 45 degree angle (E vector) and place some sort of object at the same position as your head except on the other side of the mirror (R vector).

Back to realtime computer graphics - if you have a cube map which represents your environment (really just 6 textures representing left, right, front, back, above and below the object), the R vector gives you a 3D coordinate to look up into the cube map texture. To see this in practice check out the samples in the SDK.

In D3D people don't usually store face normals for rendering, instead using vertex normals for Gouraud shading, so the above maths still applies, but is done for every vertex instead of every face. This does mean your model must have correct vertex normals to be able to do the maths.

All of the maths to generate R and lookup into the cubemap etc is automatic (assuming you have hardware which supports cube maps) providing you enable the correct states:

// Get D3D to automatically generate the texture coordinates
// for "stage" using the maths for the reflection vector (R)
// which I showed above. On T&L hardware this is hardware
// accelerated.
SetTSS( stage, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR );

// Tell D3D that the texture coordinates for "stage" are
// 3-Dimensional (assuming we're using a cube map for the envmap)
SetTSS( stage, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 );

Assuming the cube map is set up (see examples and docs in DirectX SDK) and the normals are ok, and texture address states are set to something suitable - it'll work fine, nothing else required (unless you need to do more complex stuff like applying Fresnel term effects or look up into a 2D environment map).

--
Simon O'Connor
Creative Asylum Ltd
www.creative-asylum.com

Edited by - s1ca on September 9, 2001 6:04:18 PM

#### Share this post

##### Share on other sites
thanks again;-)

i tested sphere mapping and it looks good, cube mapping will follow.

now you mentioned face-normals and vertex normals, since im loading my spaceships from 3ds files i only have the face normals.
i computed the vertex normals by taking the average of the face normals the vertex belongs to, thats almost perfect on a normal model but on a model with sharp edges it really sucks.
ah theres smoothing information in the 3ds-file but how to use it?? i have taken a look at it and it seems to be just numbers like 1,2,4,8,16,32... what do they mean??
i didnt find an explanation on the web:-(

#### Share this post

##### Share on other sites

• Advertisement