UBITest_2: DirectX 3D Morphing
Goal
Create a program that performs morphing of a 3D mesh from a sphere mesh to a cube mesh in real-time while respecting the lighting conditions.
Details
In this program, a 3D mesh object will need to morph from a sphere to a cube using linear interpolation. The mesh object must be created with a large number of faces to respect the lighting conditions. The user will be able to rotate around the cube in order to watch the mesh morph and see the lighting adapt to the mesh. The time of the morphing can be pre-determined but it must be slow enough so we can see the transition.
Methodology
You will need to use DirectX to setup a basic rendering framework that supports vertex shaders. You will need to research on DirectX functionalities, vertex shaders and vertex morphing. A few resources are provided at the end of this document. In addition to the web site, you can consult the DirectX SDK documentation.
The program must be able to generate a textured mesh that has a lot of faces. The shape of the mesh object must start as a sphere and morph to a cube using linear interpolation. From the cube, it will morph back linearly to a sphere. You can use any texture that you like.
The program must light the scene using a directional light and an ambient light. The lighting must affect the 3D object as it morphs between the sphere and the cube.
For the vertex morphing, you will have to use linear interpolation on the vertices. For the lighting, you will have to use linear interpolation on the normals (approximation is acceptable).
The camera should be able to rotate around the 3D mesh to verify the lighting. The rotation can be done with the mouse or with arrows.
Requirements
l Draw the mesh on screen (sphere morphing to cube)
l Using one directional light and one ambient light
l Using vertex shaders
l Morph smoothly from sphere to cube and vice-versa
l Camera can rotate around the mesh by mouse or key-board input
Resources
DirectX resources center
DirectX documentation
Vertex shaders
Vertex morphing
这时ubi的一道面试题,咋一看挺复杂,但是想想还是很容易实现的。
圆和内切正方体有这样的关系: 半径^2 = (1/2边长)^2 * 3; 也就是说,如果圆的半径是1.732,那么长方体的边长是2。
之vertex shader中,对超出正方体的顶点,以该顶点和投影到响应正方体面上的点作插值,就可以了。
比如:
if( vert.pos.y>1 )
{
float h = lerp( 1.0,vert.pos.y,factor);
vert.pos.y = h;
}
{
float h = lerp( 1.0,vert.pos.y,factor);
vert.pos.y = h;
}
-----〉
代码放在http://file.mofile.com/ 提取码5026797624383246,有兴趣可以看一下。