It's free to join Gamasutra!|Have a question? Want to know who runs this site? Here you go.|Targeting the game development market with your product or service? Get info on advertising here.||For altering your contact information or changing email subscription preferences.
Registered members can log in here.Back to the home page.

Search articles, jobs, buyers guide, and more.

by Rob Wyatt
Gamasutra
August 11, 2000

Printer Friendly Version
   
Discuss this Article

Letters to the Editor:
Write a letter
View all letters


Features

A Close Look: Direct 3D Texture Matrix

Contents

The Spheremap Demo

A Closer Look: Vertex Buffers

A Close Look: Direct 3D Texture Matrix

Fortunately there is a better solution, Direct3D can be told to use the camera-space normal directly as a texture coordinate by setting the texture coordinate index for the required texture stage to include the D3DTSS_TSI_CAMERASPACENORMAL flag. With this 'Tex-Gen' mode set any texture coordinates within the vertex buffer that are bound to the specified texture stage are ignored and instead the camera-space normal [n'x,n'y,n'z,1] is used, this normal vector automatic texture coordinate includes any skinning operations that may have been performed. Contrary to popular belief, this flag is not just for use in cubic environment mapping, it can be used anytime you want the camera space normal to be used as a texture co-ordinate.

Referring back to the texture matrix, it is easy to create a texture matrix to take advantage of this new flag, as shown below:

where [n'x n'y n'z 1] is the transformed and skinned normal vector.

As you can see, all that needs to be done is multiply the transformed automatic normal-vector texture coordinate by a constant matrix. The first advantage of this method is that you do not have to update the texture matrix when the local-to-camera matrix changes (since this is already taken care of with the transformed normal). Another advantage is that you do not need to keep a second copy of the normal vectors within the mesh - the normal seen by the texture matrix is the same one seen by the geometry. That means that automatic normal normalization and skinning can be utilized. When automatic normalization is enabled, you can even set non-uniform scale factors in the world matrix and everything works fine.

The other big gain is that it is possible to perform reflective spherical environment mapping, since Direct3D can pass the camera-space reflection vector in place of the normal by setting the texture index state to include D3DTSS_TSI_CAMERASPACEREFLECTIONVECTOR. This provides environment mapping similar to that produced by GL_SPHERE_MAP, which looks significantly better than normal-based spherical environment mapping. (Unfortunately, the reflection method is more sensitive to distortion due to bad normal vectors, non-unit normal vectors and low tessellation.)

There are two methods that Direct3D can use to calculate the eye-to-vertex reflection vector. The method used depends on the local-viewer render state (D3DRENDERSTATE_LOCALVIEWER). When this render state is set to true, the per-vertex refection is calculated using this formula:

R = 2(E.N)N-E
(Where N is the vertex normal and E is the camera to vertex unit vector.)

When the local-viewer render state is set to false, the reflection vector is orthogonal, and is calculated from an infinite viewpoint using this more simple formula:

R=2NzN-I
(Where Nz is the world space Z component of the vertex normal and I is the vector [0,0,1].)

Better reflections are obtained with the local viewer model, but it is a more complex calculation and performance can be affected by the level of hardware acceleration; try using both reflection models and see which one works the best for you. By default within Direct3D, local viewer reflections (and specular calculations) are enabled, and they should be disabled if the orthogonal non-local version is required. The screen shot below shows the test application associated with this article performing spherical reflection mapping on a torus:

Finally, depending on the quality of your sphere maps, you might want to pull the boundary in a little (but not too much, as it creates other distortions). By not using the last few pixels around the edge of the sphere map, you can significantly change the look of an object. Another way to look at the texture matrix is as follows:

Where both Scale and Offset are both in u,v coordinates, in the range 0.0 to 1.0. By carefully adjusting this matrix you can not only trim off the outer edge of a sphere map, you can also pack more than one sphere map into a larger texture or even use elliptical sphere maps.

The code associated with this article is available as a Visual C++ 6.0 project call TGReflect, included in the archive is a ready to use pre-built release mode executable. This project was built using the Shrike 7.00 DirectX framework, this framework is only required if you wish to rebuild the project and is available from this link, see Shrike7.doc within the archive for more information.

Spherical environment mapping is useful in a lot of circumstances and if it is going to be used it may as well be hardware accelerated, the less graphics work that is done by the CPU the lesser the chance the rendering hardware will be stalled. By carefully utilizing the resources of the standard DirectX graphics pipeline a lot can be achieved and as a developer trying to obtain maximum performance for your game you should carefully examine what the hardware is capable off before delegating an operation to software, current video cards such as the GeForce 2 have significantly more computational power and bandwidth available to them than even the fastest CPUs so take advantage of it whenever you can. Applications of spherical environment mapping are all over the Internet, start by trying the nVidia (http://www.nvidia.com) or OpenGL (http://www.opengl.org) sites.

Rob Wyatt has been involved in games and graphics for more than a decade and was one of the architects of the X-Box game console, he recently left Microsoft and headed to Southern California where he can be found flying around the skies of Los Angeles in his plane. He is currently looking at various technologies for the Internet.

 

Discuss this article in Gamasutra's discussion forums

________________________________________________________

[Back To] The Spheremap Demo


join | contact us | advertise | write | my profile
news | features | companies | jobs | resumes | education | product guide | projects | store



Copyright © 2003 CMP Media LLC

privacy policy
| terms of service