Skip to main content
gamedev.net
🔒 Locked

Basic cubic environment mapping help

Started by a1studmuffin 0 replies 850+ views
Original Post
a1studmuffin
a1studmuffin
Hi everyone, Does anyone have or know of sample code that shows how to do plain vanilla static cubic environment maps? (ie. not the DX sample, which generates realtime maps) I''ve gone over the DX8.1 SDK cubic environment sample numerous times (the teapot with the toy plane) trying to get my head around it, but I''m under the impression it''s a lot more complicated than I need, due to the fact they''re rendering a new environment map every time they render to include scene object reflections. I''m also not following the vertex buffer stuff they''re doing in there, but am under the impression I don''t need to dive that deep into the rabbithole if I just want static reflections - am I right in this assumption or am I going to have to get my hands dirty? So close, but so far... Cheers - Michael -------------------------- www.4bitterguys.com
--------------------------www.4bitterguys.com
S1CA
S1CA
You''re right, the sample is overcomplicated if you just want a static cube environment map. I don''t have any complete source code available, but do have a few relevent DX7 snippets (from the old DX7 version of our engine) - the principles and majority of the states haven''t changed much though.

1. First create a cube-map texture (CreateCubeTexture() in DX8).

2. Each face of the cube is actually a separate surface within the texture, fill in the texture for each face you''re interested in.

[I think there''s a D3DX function to do stages 1 & 2 for you]

3. When you want to render something with an envmap, set texture stage 0''s states to whatever you need for the objects base texture. Also set the base texture for stage 0.

4. For stage 1, set the states to something like:
TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_ADD ));
TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE ));
TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT ));
TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ));
TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ));
TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2, D3DTA_CURRENT ));

TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR|1 ));
TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 ));
TRYDX(pDevice->SetTexture( 1, cubeMapTexture ));

TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ));
TRYDX(pDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ));

The interesting part of the above is the TEXCOORDINDEX and texture transform flags:

a. The texcoordindex automatically generates the texture coordinates for stage 1 based on the reflection vector in camera space (same idea as specular lighting reflection vector).

b. The |1 specifies that the generated coordinates are for coordinate set 1.

c. If you want to move the texture coordinates (and thus the view of the cubemap) into a different space (which we needed to do for the game the above code is from), specifying count3 to the texture transform flags means that we need to transform 3D coordinates:

D3DXMATRIX mView, mInvView;
float det = 0.0f;

pDevice->GetTransform( D3DTRANSFORMSTATE_VIEW, (D3DMATRIX*)&mView );

// strip off translation from matrix
mView.m30 = 0;
mView.m31 = 0;
mView.m32 = 0;
mView.m33 = 1;

// go from camera space texture coords to world space texture coords
D3DXMatrixInverse(&mInvView, &det, &mView)
pDevice->SetTransform( D3DTRANSFORMSTATE_TEXTURE1, (D3DMATRIX*)&mInvView );



--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.