3D图形引擎【OO改良版】的代码以创建游戏内核【OO改良版】中编写的代码为基础进行开发,关于该引擎的细节说明请参阅创建3D图形引擎(1)。
/************************************************************************************
PURPOSE:
mesh level test.
************************************************************************************/
#include "core_common.h"
#include "core_framework.h"
#include "core_graphics.h"
#include "core_input.h"
class APP : public FRAMEWORK
{
public:
BOOL init()
{
if(! create_display(g_hwnd, get_client_width(g_hwnd), get_client_height(g_hwnd), 16, TRUE, TRUE))
return FALSE;
set_perspective(D3DX_PI / 4, 1.3333f, 1.0f, 10000.0f);
// initialize input and input device
m_input.create(g_hwnd, get_window_inst());
m_keyboard.create(&m_input, KEYBOARD, TRUE);
m_mouse.create(&m_input, MOUSE, TRUE);
// load the room meshes
if(! m_room_meshes[0].load("..\\Data\\Corridor.x", "..\\Data\\"))
return FALSE;
if(! m_room_meshes[1].load("..\\Data\\Room.x", "..\\Data\\"))
return FALSE;
// setup the room objects
m_room_objects[0].create(&m_room_meshes[1]);
m_room_objects[1].create(&m_room_meshes[0]);
m_room_objects[2].create(&m_room_meshes[1]);
m_room_objects[3].create(&m_room_meshes[0]);
m_room_objects[4].create(&m_room_meshes[0]);
m_room_objects[5].create(&m_room_meshes[1]);
m_room_objects[6].create(&m_room_meshes[0]);
m_room_objects[7].create(&m_room_meshes[1]);
// place room object in specified position
m_room_objects[0].move(-2000.0f, 0.0f, 2000.0f);
m_room_objects[1].move( 0.0f, 0.0f, 2000.0f);
m_room_objects[2].move( 2000.0f, 0.0f, 2000.0f);
m_room_objects[3].move(-2000.0f, 0.0f, 0.0f);
m_room_objects[4].move( 2000.0f, 0.0f, 0.0f);
m_room_objects[5].move(-2000.0f, 0.0f, -2000.0f);
m_room_objects[6].move( 0.0f, 0.0f, -2000.0f);
m_room_objects[7].move( 2000.0f, 0.0f, -2000.0f);
// rotate room object into proper orientation
m_room_objects[1].rotate(0.0f, 1.57f, 0.0f);
m_room_objects[2].rotate(0.0f, 1.57f, 0.0f);
m_room_objects[5].rotate(0.0f, -1.57f, 0.0f);
m_room_objects[6].rotate(0.0f, -1.57f, 0.0f);
m_room_objects[7].rotate(0.0f, 3.14f, 0.0f);
// position view in a room
m_x_pos = -2000.0f;
m_z_pos = -2000.0f;
//m_camera.init();
return TRUE;
}
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
m_keyboard.read();
m_mouse.read();
// process input and update everything, ESC quits program.
if(m_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.
//
// m_camera.get_y_rotation():
// get camera vector in xoz plane.
//
// sin(m_camera.get_y_rotation()):
// get x-coordinate of camera vector.
//
// cos(m_camera.get_y_rotation()):
// get z-coordinate of camera vector.
// move viewpoint forward
if(m_keyboard.get_key_state(KEY_UP))
{
x_move = (float) sin(m_camera.get_y_rotation()) * time_elapsed;
z_move = (float) cos(m_camera.get_y_rotation()) * time_elapsed;
}
// move viewpoint backward
if(m_keyboard.get_key_state(KEY_DOWN))
{
x_move = -(float) sin(m_camera.get_y_rotation()) * time_elapsed;
z_move = -(float) cos(m_camera.get_y_rotation()) * time_elapsed;
}
// translate camera left-right movement into up-down movement.
//
// m_camera.get_y_rotation() - 1.57f:
// rotate camera 90 degree anticlockwise.
//
// sin(m_camera.get_y_rotation() - 1.57f):
// get x-coordinate of camera vector after camera rotation.
//
// cos(m_camera.get_y_rotation() - 1.57f):
// get z-coordinate of camera vector after camera rotation.
// move viewpoint left
if(m_keyboard.get_key_state(KEY_LEFT))
{
x_move = (float) sin(m_camera.get_y_rotation() - 1.57f) * time_elapsed;
z_move = (float) cos(m_camera.get_y_rotation() - 1.57f) * time_elapsed;
}
// m_camera.get_y_rotation() + 1.57f:
// rotate camera 90 degree clockwise.
//
// sin(m_camera.get_y_rotation() + 1.57f):
// get x-coordinate of camera vector after camera rotation.
//
// cos(m_camera.get_y_rotation() + 1.57f):
// get z-coordinate of camera vector after camera rotation.
// move viewpoint right
if(m_keyboard.get_key_state(KEY_RIGHT))
{
x_move = (float) sin(m_camera.get_y_rotation() + 1.57f) * time_elapsed;
z_move = (float) cos(m_camera.get_y_rotation() + 1.57f) * time_elapsed;
}
// update view coordinates
m_x_pos += x_move;
m_z_pos += z_move;
// position camera and rotate based on mouse position
m_camera.move(m_x_pos, 400.0f, m_z_pos);
// m_mouse.get_y_delta():
// get mouse's relative x movement coordinate.
//
// m_mouse.get_x_delta():
// get mouse's relative y movement coordinate.
m_camera.rotate_rel(m_mouse.get_y_delta() / 200.0f, m_mouse.get_x_delta() / 200.0f, 0.0f);
// set view transform matrix
g_d3d_device->SetTransform(D3DTS_VIEW, m_camera.get_view_matrix());
// render everything
clear_display(D3DCOLOR_RGBA(0, 64, 128, 255), 1.0f);
// begin render now
if(SUCCEEDED(g_d3d_device->BeginScene()))
{
// render each room
for(short i = 0; i < 8; i++)
m_room_objects[i].render();
g_d3d_device->EndScene();
}
present_display();
return TRUE;
}
BOOL shutdown()
{
return TRUE;
}
private:
CAMERA m_camera;
INPUT m_input;
INPUT_DEVICE m_keyboard;
INPUT_DEVICE m_mouse;
MESH m_room_meshes[2];
OBJECT m_room_objects[8];
float m_x_pos, m_z_pos;
};
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
DWORD _client_width = 640;
DWORD _client_height = 480;
DWORD _x_pos = (get_screen_width() - _client_width) / 2;
DWORD _y_pos = (get_screen_height() - _client_height) / 4;
if(! build_window(inst, "mesh_level_class", "mesh level test",
WS_BORDER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
_x_pos, _y_pos, _client_width, _client_height))
{
return -1;
}
APP app;
app.run();
return 0;
}