void GetUserInput(void) { g_fFrame_Time = g_Timer.Stop(); g_Timer.Restart(); GutReadKeyboard(); g_Control.Update(g_fFrame_Time, CGutUserControl::CONTROLLER_ROTATEOBJECT); }
void GetUserInput(void) { // 讀取滑鼠 GutMouseInfo mouse; if ( GutReadMouse(&mouse) ) { // 取得畫完前一個畫面到現在所經歷的時間 g_fFrame_Time = g_Timer.Stop(); g_Timer.Restart(); float moving_speed = 2.0f * g_fFrame_Time; float rotation_speed = 1.0 * g_fFrame_Time; // 如果按下滑鼠左鍵,就旋轉鏡頭 if ( mouse.button[0] ) { Matrix4x4 rotate_matrix; rotate_matrix.RotateY_Replace(mouse.x * rotation_speed); rotate_matrix.RotateX(mouse.y * rotation_speed); g_world_matrix = g_world_matrix * rotate_matrix; } } }
void GetUserInput(void) { // `讀取滑鼠` GutMouseInfo mouse; GutReadMouse(&mouse); // `讀取鍵盤` char keyboard_state[256]; GutReadKeyboard(keyboard_state); // `取得畫完前一個畫面到現在所經歷的時間` float time_diff = g_Timer.Stop(); g_Timer.Restart(); float moving_speed = 2.0f * time_diff; float rotation_speed = 1.0 * time_diff; // `極座標系統` static float theta = -MATH_PI * 0.5f; static float phi = 0.0f; // `如果按下滑鼠左鍵, 就旋轉鏡頭.` if ( mouse.button[0] ) { theta += mouse.x * rotation_speed; phi -= mouse.y * rotation_speed; } float sin_phi, cos_phi; float sin_theta, cos_theta; FastMath::SinCos(phi, sin_phi, cos_phi); FastMath::SinCos(theta, sin_theta, cos_theta); // `計算鏡頭的面向` Vector4 camera_facing; camera_facing[0] = cos_phi * cos_theta; camera_facing[1] = sin_phi; camera_facing[2] = cos_phi * sin_theta; // `計算鏡頭正上方的軸向` Vector4 camera_up; FastMath::SinCos(phi + MATH_PI*0.5f, sin_phi, cos_phi); camera_up[0] = cos_phi * cos_theta; camera_up[1] = sin_phi; camera_up[2] = cos_phi * sin_theta; // `取得鏡面右方的方向` Vector4 camera_right = Vector3CrossProduct(camera_facing, camera_up); // `按下W或方向鍵向上` if ( keyboard_state[GUTKEY_W] || keyboard_state[GUTKEY_UP] ) { g_eye += camera_facing * moving_speed; } // `按下S或方向鍵向下` if ( keyboard_state[GUTKEY_S] || keyboard_state[GUTKEY_DOWN] ) { g_eye -= camera_facing * moving_speed; } // `按下A或方向鍵向左` if ( keyboard_state[GUTKEY_A] || keyboard_state[GUTKEY_LEFT] ) { g_eye -= camera_right * moving_speed; } // `按下D或方向鍵向右` if ( keyboard_state[GUTKEY_D] || keyboard_state[GUTKEY_RIGHT] ) { g_eye += camera_right * moving_speed; } // `計算出鏡頭對準的點, 產生鏡頭轉換矩陣時會用到.` g_lookat = g_eye + camera_facing; // `因為是對2個軸轉動, 需要更新鏡頭朝上的軸.` g_up = camera_up; }
void main(void) { // 內定使用DirectX 9來繪圖 char *device = "dx9"; void (*render)(void) = RenderFrameDX9; bool (*init_resource)(void) = InitResourceDX9; bool (*release_resource)(void) = ReleaseResourceDX9; void (*resize_func)(int width, int height) = ResizeWindowDX9; #ifdef _ENABLE_DX10_ printf("Press\n(1) for Direct3D9\n(2) for OpenGL\n(3) for Direct3D10\n"); #else printf("Press\n(1) for Direct3D9\n(2) for OpenGL\n"); #endif int c = getche(); switch(c) { default: case '1': render = RenderFrameDX9; init_resource = InitResourceDX9; release_resource = ReleaseResourceDX9; resize_func = ResizeWindowDX9; break; case '2': device = "opengl"; init_resource = InitResourceOpenGL; release_resource = ReleaseResourceOpenGL; render = RenderFrameOpenGL; resize_func = ResizeWindowOpenGL; break; #ifdef _ENABLE_DX10_ case '3': device = "dx10"; init_resource = InitResourceDX10; release_resource = ReleaseResourceDX10; render = RenderFrameDX10; resize_func = ResizeWindowDX10; break; #endif } printf("\nSelected %s device for rendering.\n", device); GutResizeFunc( resize_func ); // 在(100,100)的位置開啟一個大小為(512x512)的視窗 GutCreateWindow(100, 100, 512, 512, device); // 做OpenGL或DirectX初始化 if ( !GutInitGraphicsDevice(device) ) { printf("Failed to initialize %s device\n", device); exit(0); } // GutInputInit(); GutRegisterKeyDown(GUTKEY_1, KeyDown_1); GutRegisterKeyDown(GUTKEY_2, KeyDown_2); GutRegisterKeyDown(GUTKEY_3, KeyDown_3); GutRegisterKeyDown(GUTKEY_4, KeyDown_4); GutRegisterKeyDown(GUTKEY_5, KeyDown_5); GutRegisterKeyDown(GUTKEY_6, KeyDown_6); GutRegisterKeyPressed(GUTKEY_EQUALS, KeyPressed_ADD); GutRegisterKeyPressed(GUTKEY_MINUS, KeyPressed_MINUS); GutRegisterKeyPressed(GUTKEY_ADD, KeyPressed_ADD); GutRegisterKeyPressed(GUTKEY_SUBTRACT, KeyPressed_MINUS); g_Control.SetCamera(Vector4(0.0f, 3.0f, 1.0f), Vector4(0.0f, 0.0f, 1.0f), Vector4(0.0f, 0.0f, 1.0f) ); g_Control.SetMoveSpeed(5.0f); // g_emitter.m_fEmitRate = 300; g_emitter.m_fEmitTheta = FastMath::DegToRad(15.0f); g_emitter.m_fParticleInitSpeed[0] = 1.0f; g_emitter.m_fParticleInitSpeed[1] = 2.0f; g_emitter.m_fParticleLife[0] = 1.5f; g_emitter.m_fParticleLife[1] = 2.0f; g_emitter.m_fParticleSize[0] = 0.05f; g_emitter.m_fParticleSize[1] = 0.10f; g_Particle.SetEmitter(g_emitter); g_Particle.SetForce(Vector4(0.0f, 0.0f, -1.0f)); // 載入shader if ( !init_resource() ) { release_resource(); printf("Failed to load resources\n"); exit(0); } g_Timer.Restart(); // 主回圈 while( GutProcessMessage() ) { GetUserInput(); // framemove(); // 畫出矩形 render(); } // 卸載shader release_resource(); // 關閉OpenGL/DirectX繪圖裝置 GutReleaseGraphicsDevice(); }