下载源码和工程
3D图形引擎的代码以创建游戏内核中编写的代码为基础进行开发。
使用单一的网格模型虽然可以同时渲染整个层次,但却意味着那些没有被看到的部分在通过渲染管道时会被裁剪掉,也就是说这样做会浪费时间。不要沮丧,因为使用一个单独的网格模型进行层次的渲染仍然有一些非常好的方法。比如说,在游戏世界中包括了单独的地牢,每个地牢包含不同的房间,所有的房间通过走廊连接到一起。其实,每个房间和走廊就是一个单独的网格模型,所要做的就是在游戏的处理过程中加载以及释放那些代表地牢房间的网格模型。
我们首先定义一些成员变量来存储数据。
class APP : public APPLICATION
{
private:
GRAPHICS _graphics;
CAMERA _camera;
INPUT _input;
INPUT_DEVICE _keyboard;
INPUT_DEVICE _mouse;
MESH _room_meshes[2];
OBJECT _room_objects[8];
float _x_pos, _z_pos;
}
初始化窗口:
APP()
{
_width = 640;
_height = 480;
_style = WS_BORDER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
strcpy(_class_name, "mesh level class");
strcpy(_caption, "mesh level demo");
}
加载网格模型:
BOOL init()
{
// initialize graphics and set display mode
_graphics.init();
_graphics.set_mode(get_hwnd(), TRUE, TRUE);
_graphics.set_perspective(D3DX_PI / 4, 1.3333f, 1.0f, 10000.0f);
// initialize input and input device
_input.init(get_hwnd(), get_inst());
_keyboard.create(&_input, KEYBOARD);
_mouse.create(&_input, MOUSE, TRUE);
// load the room meshes
_room_meshes[0].load(&_graphics, "..\\Data\\Corridor.x", "..\\Data\\");
_room_meshes[1].load(&_graphics, "..\\Data\\Room.x", "..\\Data\\");
// setup the room objects
_room_objects[0].create(&_graphics, &_room_meshes[1]);
_room_objects[1].create(&_graphics, &_room_meshes[0]);
_room_objects[2].create(&_graphics, &_room_meshes[1]);
_room_objects[3].create(&_graphics, &_room_meshes[0]);
_room_objects[4].create(&_graphics, &_room_meshes[0]);
_room_objects[5].create(&_graphics, &_room_meshes[1]);
_room_objects[6].create(&_graphics, &_room_meshes[0]);
_room_objects[7].create(&_graphics, &_room_meshes[1]);
// place room object in specified position
_room_objects[0].move(-2000.0f, 0.0f, 2000.0f);
_room_objects[1].move( 0.0f, 0.0f, 2000.0f);
_room_objects[2].move( 2000.0f, 0.0f, 2000.0f);
_room_objects[3].move(-2000.0f, 0.0f, 0.0f);
_room_objects[4].move( 2000.0f, 0.0f, 0.0f);
_room_objects[5].move(-2000.0f, 0.0f, -2000.0f);
_room_objects[6].move( 0.0f, 0.0f, -2000.0f);
_room_objects[7].move( 2000.0f, 0.0f, -2000.0f);
// rotate room object into proper orientation
_room_objects[1].rotate(0.0f, 1.57f, 0.0f);
_room_objects[2].rotate(0.0f, 1.57f, 0.0f);
_room_objects[5].rotate(0.0f, -1.57f, 0.0f);
_room_objects[6].rotate(0.0f, -1.57f, 0.0f);
_room_objects[7].rotate(0.0f, 3.14f, 0.0f);
// position view in a room
_x_pos = -2000.0f;
_z_pos = -2000.0f;
return TRUE;
}
这里加载了两个网格模型corridor.x 和room.x,一个是走廊,一个是房间。
走廊:
房间:
游戏中的每个走廊和房间都使用了一个OBJECT对象,其与网格模型依次相关联。
我们接着来看看如何渲染这些网格模型。
BOOL frame()
{
static DWORD time_now = timeGetTime();
// calculate elapsed time (plus speed boost)
ulong time_elapsed = (timeGetTime() - time_now) * 2;
time_now = timeGetTime();
// read input device data
_keyboard.read();
_mouse.read();
// process input and update everything, ESC quits program.
if(_keyboard.get_key_state(KEY_ESC))
return FALSE;
float x_move = 0.0f;
float z_move = 0.0f;
// ok, now let me explain next codes.
//
// _camera.get_y_rotation():
// get camera vector in xoz plane.
//
// sin(_camera.get_y_rotation()):
// get x-coordinate of camera vector.
//
// cos(_camera.get_y_rotation()):
// get z-coordinate of camera vector.
// move viewpoint forward
if(_keyboard.get_key_state(KEY_UP))
{
x_move = (float) sin(_camera.get_y_rotation()) * time_elapsed;
z_move = (float) cos(_camera.get_y_rotation()) * time_elapsed;
}
// move viewpoint backward
if(_keyboard.get_key_state(KEY_DOWN))
{
x_move = -(float) sin(_camera.get_y_rotation()) * time_elapsed;
z_move = -(float) cos(_camera.get_y_rotation()) * time_elapsed;
}
// translate camera left-right movement into up-down movement.
//
// _camera.get_y_rotation() - 1.57f:
// rotate camera 90 degree anticlockwise.
//
// sin(_camera.get_y_rotation() - 1.57f):
// get x-coordinate of camera vector after camera rotation.
//
// cos(_camera.get_y_rotation() - 1.57f):
// get z-coordinate of camera vector after camera rotation.
// move viewpoint left
if(_keyboard.get_key_state(KEY_LEFT))
{
x_move = (float) sin(_camera.get_y_rotation() - 1.57f) * time_elapsed;
z_move = (float) cos(_camera.get_y_rotation() - 1.57f) * time_elapsed;
}
// _camera.get_y_rotation() + 1.57f:
// rotate camera 90 degree clockwise.
//
// sin(_camera.get_y_rotation() + 1.57f):
// get x-coordinate of camera vector after camera rotation.
//
// cos(_camera.get_y_rotation() + 1.57f):
// get z-coordinate of camera vector after camera rotation.
// move viewpoint right
if(_keyboard.get_key_state(KEY_RIGHT))
{
x_move = (float) sin(_camera.get_y_rotation() + 1.57f) * time_elapsed;
z_move = (float) cos(_camera.get_y_rotation() + 1.57f) * time_elapsed;
}
// update view coordinates
_x_pos += x_move;
_z_pos += z_move;
// position camera and rotate based on mouse position
_camera.move(_x_pos, 400.0f, _z_pos);
// _mouse.get_y_delta():
// get mouse's relative x movement coordinate.
//
// _mouse.get_x_delta():
// get mouse's relative y movement coordinate.
_camera.rotate_rel(_mouse.get_y_delta() / 200.0f, _mouse.get_x_delta() / 200.0f, 0.0f);
// set camera
_graphics.set_camera(&_camera);
// render everything
_graphics.clear(D3DCOLOR_RGBA(0, 64, 128, 255));
// begin render now
if(_graphics.begin_scene())
{
// render each room
for(short i = 0; i < 8; i++)
_room_objects[i].render();
_graphics.end_scene();
}
_graphics.display();
return TRUE;
}
用上下左右键控制相机的位置,用鼠标控制相机的朝向。
截图: