void DrawString(int x, int y, DWORD color, const char *fmt, ...) { RECT FontPos = { x, y, x + 120, y + 16 }; char buf[1024] = {'\0'}; va_list va_alist; va_start(va_alist, fmt); vsprintf(buf, fmt, va_alist); va_end(va_alist); g_pFont->DrawText(NULL, buf, -1, &FontPos, DT_NOCLIP, color); }
void Direct3DRender(HWND hwnd) { gPD3DDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); gPD3DDevice->BeginScene(); InputUpdate(); MatrixSet(); RECT formatRect; GetClientRect(hwnd, &formatRect); for (int i = 0; i < gDwNumMtrl; i++) { gPD3DDevice->SetMaterial(&gPMaterial[i]); gPD3DDevice->SetTexture(0, gPTexture[i]); gPCharacter->DrawSubset(i); } int strLen = swprintf_s(gStrFPS, _T("FPS: %f"), 123.45f); gPTextFPSFont->DrawTextW(nullptr, gStrFPS, strLen, &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_XRGB(0, 239, 136)); strLen = sizeof(gStrAdapterDesc); gPTextAdapterFont->DrawTextW(nullptr, gStrAdapterDesc, -1, &formatRect, DT_TOP | DT_LEFT, D3DCOLOR_XRGB(23, 23, 236)); formatRect.top = 30; static wchar_t strInfo[256] = { 0 }; swprintf_s(strInfo, -1, L"模型坐标: (%.2f, %.2f, %.2f)", gMatWorld._41, gMatWorld._42, gMatWorld._43); gPTextHelperFont->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135, 239, 136, 255)); formatRect.top = WINDOW_HEIGHT * 2 / 3; gPTextInfoFont->DrawTextW(nullptr, _T("控制说明:"), -1, &formatRect, DT_NOCLIP|DT_LEFT | DT_SINGLELINE, D3DCOLOR_XRGB(23, 25, 111)); formatRect.top += 35; formatRect.left += 50; gPTextHelperFont->DrawText(NULL, L"按住鼠标左键并拖动:平移模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; gPTextHelperFont->DrawText(NULL, L"按住鼠标右键并拖动:旋转模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; gPTextHelperFont->DrawText(NULL, L"滑动鼠标滚轮:拉伸模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; gPTextHelperFont->DrawText(NULL, L"W、S、A、D键:平移模型 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; gPTextHelperFont->DrawText(NULL, L"上、下、左、右方向键:旋转模型 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; gPTextHelperFont->DrawText(NULL, L"ESC键 : 退出程序", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); gPD3DDevice->EndScene(); gPD3DDevice->Present(nullptr, nullptr, nullptr, nullptr); }
void cGraphicsLayer::DrawTextString( int x, int y, DWORD color, const char * str ) { HRESULT r = 0; if( !m_pBackSurf ) return; // Get a handle for the font to use HFONT hFont = (HFONT)GetStockObject( SYSTEM_FONT ); LPD3DXFONT pFont = 0; // Create the D3DX Font r = D3DXCreateFont( m_pDevice, hFont, &pFont ); if( FAILED( r ) ) return; // Rectangle where the text will be located RECT TextRect = { x, y, 0, 0 }; // Inform font it is about to be used pFont->Begin(); // Calculate the rectangle the text will occupy pFont->DrawText( str, -1, &TextRect, DT_CALCRECT, 0 ); // Output the text, left aligned pFont->DrawText( str, -1, &TextRect, DT_LEFT, color ); // Finish up drawing pFont->End(); // Release the font pFont->Release(); }
//----------------------------------------------------------------------------- // Name: render_0() // Desc: //----------------------------------------------------------------------------- void render_0( void ) { D3DXMATRIX matView; D3DXMATRIX matWorld; D3DXMATRIX matRotation; D3DXMATRIX matTranslation; static int nFrameCount = 0; static double nTimeOfLastFPSUpdate = 0.0; static char fpsString[50] = "Frames Per Second = "; RECT destRect; // Now we can clear just view-port's portion of the buffer to red... g_pd3dDevice_0->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE( 1.0f, 0.0f, 0.0f, 1.0f ), 1.0f, 0 ); g_pd3dDevice_0->BeginScene(); // For the left view-port, leave the view at the origin... D3DXMatrixIdentity( &matView ); g_pd3dDevice_0->SetTransform( D3DTS_VIEW, &matView ); // ... and use the world matrix to spin and translate the teapot // out where we can see it... D3DXMatrixRotationYawPitchRoll( &matRotation, D3DXToRadian(g_fSpinX), D3DXToRadian(g_fSpinY), 0.0f ); D3DXMatrixTranslation( &matTranslation, 0.0f, 0.0f, 5.0f ); matWorld = matRotation * matTranslation; g_pd3dDevice_0->SetTransform( D3DTS_WORLD, &matWorld ); g_pd3dDevice_0->SetMaterial( &g_teapotMtrl ); g_pTeapotMesh_0->DrawSubset(0); g_pd3dDevice_0->EndScene(); // Report frames per second and the number of objects culled... ++nFrameCount; if( g_dElpasedAppTime - nTimeOfLastFPSUpdate > 1000 ) // Update once a second { sprintf( fpsString, "Frames Per Second = %4.2f", nFrameCount*1000.0/(g_dElpasedAppTime - nTimeOfLastFPSUpdate) ); nTimeOfLastFPSUpdate = g_dElpasedAppTime; nFrameCount = 0; } SetRect( &destRect, 5, 5, 0, 0 ); g_pd3dxFont->DrawText( NULL, fpsString, -1, &destRect, DT_NOCLIP, D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) ); g_pd3dDevice_0->Present( NULL, NULL, NULL, NULL ); }
void DrawString(std::string text, int x, int y) { RECT FontPosition; FontPosition.left = x; FontPosition.top = y; FontPosition.right = x + 20 * text.length(); FontPosition.bottom = y + 30; font->DrawText(NULL, text.c_str(), -1, &FontPosition, DT_LEFT, 0xffffffff); }
int DirectXHook::getTextWidth(const char *szText, LPD3DXFONT pFont) { RECT rcRect = { 0, 0, 0, 0 }; if (pFont) { pFont->DrawText(NULL, szText, strlen(szText), &rcRect, DT_CALCRECT, D3DCOLOR_XRGB(0, 0, 0)); } int width = rcRect.right - rcRect.left; std::string text(szText); std::reverse(text.begin(), text.end()); text = text.substr(0, text.find_first_not_of(' ') != std::string::npos ? text.find_first_not_of(' ') : 0); for(char c : text) { width += getSpaceCharacterWidth(pFont); } return width; }
void Game_Run(HWND window) { //make sure the Direct3D device is valid if (!d3ddev) return; //update input devices DirectInput_Update(); d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,100), 1.0f, 0); //start rendering if (d3ddev->BeginScene()) { spriteobj->Begin(D3DXSPRITE_ALPHABLEND); //demonstrate font output FontPrint(fontArial24, 60, 50, "This is the Arial 24 font printed with ID3DXFont"); FontPrint(fontGaramond36, 60, 100, "The text can be printed in any color like this magenta!", D3DCOLOR_XRGB(255,0,255)); FontPrint(fontTimesNewRoman40, 60, 150, "Or how about bright green instead?", D3DCOLOR_XRGB(0,255,0)); //demonstrate text wrapping inside a rectangular region RECT rect = { 60, 250, 350, 700 }; D3DCOLOR white = D3DCOLOR_XRGB(255,255,255); string text = "This is a long string that will be "; text += "wrapped inside a rectangle."; fontTimesNewRoman40->DrawText( spriteobj, text.c_str(), text.length(), &rect, DT_WORDBREAK, white); spriteobj->End(); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); } if (KEY_DOWN(VK_ESCAPE)) gameover = true; if (controllers[0].wButtons & XINPUT_GAMEPAD_BACK) gameover = true; }
VOID Graphics::DrawScore(LPCWSTR TextString, int x, int y, int x1, int y1, D3DCOLOR MyColor) { LPD3DXFONT pFont = NULL; RECT Rec; HFONT hFont; hFont = CreateFont(30,10,0,0,FW_NORMAL,FALSE,FALSE,0,1,0,0,0,DEFAULT_PITCH|FF_MODERN,TEXT("Arial")); Rec.left = x; Rec.top = y; Rec.right = x1; Rec.bottom = y1; D3DXCreateFont(this->GetDevice(),30,10,FW_NORMAL,0,FALSE,1,0,0,DEFAULT_PITCH|FF_MODERN,TEXT("Times New Roman"),&pFont); pFont->DrawText(NULL,TextString,-1,&Rec,DT_WORDBREAK,MyColor); DeleteObject(hFont); if (pFont != NULL) pFont->Release(); }
void render() { static RECT rc = {0, 0, 320, 100}; // rectangular region.. used for text drawing static DWORD frameCount = 0; static DWORD startTime = clock(); char str[16]; doMath(); // do the math.. :-P frameCount++; // increment frame count // Clear the back buffer to a black... values r g b are 0-256 lpD3DDevice9->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER ,D3DCOLOR_XRGB(256, 256, 256), 1.0f, 0); // render the cubes myRect1->render(clock()); myRect2->render(clock()); myRect3->render(clock()); myRect4->render(clock()); myRect5->render(clock()); // this function writex a formatted string to a character string // in this case.. it will write "Avg fps" followed by the // frames per second.. with 2 decimal places sprintf(str, "Avg fps %.2f", (float) frameCount / ((clock() - startTime) / 1000.0f)); // draw the text string.. // lpD3DXFont->Begin(); lpD3DXFont->DrawText(NULL, str, -1, &rc, DT_LEFT, 0xFFFFFFFF); // lpD3DXFont->End(); // present the back buffer.. or "flip" the page lpD3DDevice9->Present( NULL, NULL, NULL, NULL ); // these options are for using rectangular // regions for rendering/drawing... // 3rd is which target window.. NULL makes it use the currently set one (default) // last one is NEVER used.. that happens with DirectX often }
//----------------------------------------【ProcessGUI()函数】--------------------------------------------- // Desc:进行GUI的渲染 //--------------------------------------------------------------------------------------------------------- BOOL ProcessGUI(GUIClass * GUI, BOOL LMBDown, WORD MouseX, WORD MouseY, void(CALLBACK *GUIProc)(int Id, int State)) { if (!GUI) return FALSE; LPDIRECT3DDEVICE9 pd3dDevice9 = GUI->GetDevice(); if (!pd3dDevice9) return FALSE; if (!(GUI->IsBKBufferUsed() && GUI->GetBkBuffer() && GUI->GetBkTexture())) return FALSE; //渲染GUI背景 //pd3dDevice9->SetRenderState(D3DRS_LIGHTING, false); pd3dDevice9->SetTexture(0, GUI->GetBkTexture()); pd3dDevice9->SetStreamSource(0, GUI->GetBkBuffer(), 0, sizeof(GUIVERTEX)); pd3dDevice9->SetFVF(D3DFVF_GUI); pd3dDevice9->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //用来显示文本的对象 LPD3DXFONT pFont = NULL; RECT rect = { 0, 0, GUI->GetBackgroundWidth(), GUI->GetBackgroundHeight() }; // 创建一个顶点缓存对象用于按钮的渲染 LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL; int iState = GUI_LBUTTON_UP; //渲染控件 GUICONTROL *pGUIControl = NULL; for (WORD i = 0; i < GUI->GetControlNums(); i++) { iState = GUI_LBUTTON_UP; pGUIControl = GUI->GetControl(i); if (!pGUIControl) continue; switch (pGUIControl->type) { case UGP_GUI_STATIC_TEXT: pFont = GUI->GetFont(pGUIControl->index); if (!pFont) continue; //设置起始位置并输出文本 rect.left = pGUIControl->x; rect.top = pGUIControl->y; pFont->DrawText(NULL, pGUIControl->text, -1, &rect, DT_LEFT, pGUIControl->color); break; case UGP_GUI_BUTTON: pVertexBuffer = GUI->GetCtrlBuffer(pGUIControl->index); if (!pVertexBuffer) continue; //开启Alpha混合 pd3dDevice9->SetRenderState(D3DRS_ALPHABLENDENABLE, true); pd3dDevice9->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); pd3dDevice9->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); if (MouseX >= pGUIControl->x && MouseX <= (pGUIControl->x + pGUIControl->width) && MouseY >= pGUIControl->y && MouseY <= (pGUIControl->y + pGUIControl->height)) { if (LMBDown) iState = GUI_LBUTTON_DOWN; else iState = GUI_MOUSE_OVER; } //开始渲染按钮 if (iState == GUI_LBUTTON_DOWN) pd3dDevice9->SetTexture(0, pGUIControl->textureDown); if (iState == GUI_LBUTTON_UP) pd3dDevice9->SetTexture(0, pGUIControl->textureUP); if (iState == GUI_MOUSE_OVER) pd3dDevice9->SetTexture(0, pGUIControl->textureOver); pd3dDevice9->SetStreamSource(0, pVertexBuffer, 0, sizeof(GUIVERTEX)); pd3dDevice9->SetFVF(D3DFVF_GUI); pd3dDevice9->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //关闭Alpha混合 pd3dDevice9->SetRenderState(D3DRS_ALPHABLENDENABLE, false); } if (GUIProc) GUIProc(pGUIControl->controlID, iState); } return TRUE; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { dInstance = hInstance; // Windows structure WNDCLASSEX wcex; ZeroMemory(&wcex, sizeof(wcex)); wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = dInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = "gameDemoWindow"; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); // register a new type of window if (!RegisterClassEx(&wcex)) { MessageBox( NULL, "Window Error", "Could not create window", NULL); } static TCHAR szWindowClass[] = "gameDemoWindow"; static TCHAR szTitle[] = "Game Demo"; hWnd = CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1920, 1080, NULL, NULL, dInstance, NULL); // Display the window ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); int width; int height; bool windowed; //create engine, using MainCore MainCore* main_core = new MainCore(); main_core->Startup(hWnd); //create pointer to access each core through MainCore AISystem* ai_core = main_core->GetAIManager(); Sound* sound_core = main_core->GetAudioCoreSound(); SoundEffect* sfx_core = main_core->GetAudioCoreSoundEffect(); PhysicsInterface* physics_core = main_core->GetPhysicsManager(); Input* input_core = main_core->GetInputManager(); UI* ui_core = main_core->GetUIManager(); ScriptingCore* script_core = main_core->GetScriptManager(); EntityManager* entity_core = main_core->GetEntityManager(); Clock* clock_manager = main_core->GetClock(); width = script_core->width; height = script_core->height; windowed = true; Init(width, height); /* TO DO IN CASE 2 setup UI and Graphics for game logic */ //load music stream sound_core->Load("Song1.ogg"); //load sound effect sfx_core = new SoundEffect("Jump.wav"); //#5 to play //variables used for entity setups LPCSTR fileName; float pos[3]; float rot[3]; float scale[3]; for(int i = 0; i < 3; i++) { pos[i] = 0; rot[i] = 0; scale[i] = .1; } rot[0] = 180; //setup for player //DO SOMETHING HERE Entity* player; player = new Entity; player->SetEntity("player", "player"); //REPLACE 0'S WITH POSITION VALUES pos[0] = -3, pos[1] = 2.5, pos[2] = 20; scale[0] = .1, scale[1] = .1, scale[2] = .1; player->agentData.setPosition(pos); player->agentData.setRotation(rot); player->agentData.setScale(scale); physics_core->setAABB(D3DXVECTOR3(-3, 2.5, 20), D3DXVECTOR3(-3.01, 2.51, 20.01), true, "player"); ai_core->regeisterPlayer(&player->agentData); //setup for enemy //DO SOMETHING HERE Entity* enemy; enemy = new Entity; enemy->SetEntity("enemy", "basic"); //REPLACE 0'S WITH POSITION VALUES pos[0] = -3, pos[1] = 2.5, pos[2] = 20; scale[0] = .1, scale[1] = .1, scale[2] = .1; enemy->agentData.setPosition(pos); enemy->agentData.setRotation(rot); enemy->agentData.setScale(scale); physics_core->setAABB(D3DXVECTOR3(-3, 2.5, 20), D3DXVECTOR3(-3.01, 2.51, 20.01), true, "enemy"); ai_core->registerAgent(&enemy->agentData); //look at removing the 0 here for a behavior isnt needed as AI sits, it automatically cycles through all states of an enemy already //setup for platforms //DO SOMETHING HERE Entity* platform; platform = new Entity; platform->SetEntity("platform", "platform"); //REPLACE 0'S WITH POSITION VALUES pos[0] = 0, pos[1] = 0, pos[2] = 20; //THIS WILL MOST LIKELY BE REMOVED AS MULTIPLE PLATFORMS NEED DIFFERENT POSITION AND ROTATION VALUES, DO BEFORE RENDERING WHEN CREATEING MULTIPLES OF THIS MESH scale[0] = 100, scale[1] = .01, scale[2] = .1; platform->agentData.setPosition(pos); platform->agentData.setRotation(rot); platform->agentData.setScale(scale); physics_core->setAABB(D3DXVECTOR3(0, 0, 20), D3DXVECTOR3(0.01, 0.01, 20.01), true, "platform"); //load the mesh model to use Meshes cube; cube.load_meshes("cube.X", pD3DDevice); //cube.set_meshes(pos, rot, scale); //camera setup //DO SOMETHING HERE Camera camera; camera.Init(); camera.SetAR(width, height); //tex to use to write Text gText; D3DXCreateFont(pD3DDevice, 30, 0, FW_BOLD, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Times New Roman"), &pFont); gText.Init(22, pD3DDevice); //light setup //DO SOMETHING HERE //graphics_core->PointLight( //D3DXVECTOR3(/*FILL IN*/), //position //D3DXCOLOR(1, 1, 1, 1), //diffuse //D3DXCOLOR(1, 1, 1, 1)); //ambient /* TEST THIS CHANGE WHERE NEEDED IF NEEDED Z AXIS LOCKED AT 50 FOR PLAYER AND GAME OBJECTS Z AXIS LOCKED AT 10 FOR CAMERA X AXIS AT 100 FOR BASE LINE OF DEMO X AXIS MATCHING PLAYER FOR CAMERA Y AXIS AT 100 FOR BASE LINE OF DEMO Y AXIS + 25 ABOVE PLAYER */ //variables for game loop bool jump; jump = false; float time; int gameState = 1; /* gamestate to tell the engine what state the game is in. Menu, Game Logic, Credtis, etc. swtich cases to switch between the different states running the appropiate functions state 1 - Menu state 2 - Game Logic state 3 - Credits state 4 - shutdown application */ MSG msg; msg.message = WM_NULL; char str[128]; RECT rect; sound_core->Play(false); if(clock_manager->GetFPS() < 60) { while(msg.message != WM_QUIT) { // If there are Window messages then process them. if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) { TranslateMessage( &msg ); DispatchMessage( &msg ); } // Otherwise, do animation/game stuff. else { switch(gameState) { case (1)://menu state /* TO DO IN CASE 1 setup menu with UI and Graphics update render loop till user input to change gamestate */ //DO SOMETHING HERE pD3DDevice->BeginScene(); pD3DDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0) , 1.0f, 0); GetClientRect(hWnd, &rect); sprintf(str, "Engine Demo"); pFont->DrawText(NULL, str, -1, &rect, DT_TOP | DT_CENTER | DT_NOCLIP , D3DCOLOR_ARGB(255, 255, 255, 255 )); gText.DisplayText("Play Demo - (1)", width / 2, height * 0.25f, 50, 25, WHITE); gText.DisplayText("Credits - (2)", width / 2, height * 0.5f, 50, 25, WHITE); gText.DisplayText("Exit Demo - (3)", width / 2, height * 0.75f, 50, 25, WHITE); pD3DDevice->EndScene(); pD3DDevice->Present(0, 0, 0, 0); main_core->Update(enemy, gameState); if(input_core->OnePressed()) { gameState = 2; } if(input_core->TwoPressed()) { gameState = 3; } if(input_core->ThreePressed()) { gameState = 4; } break; case (2)://game logic state float temp[3]; float temp2[3]; player->agentData.getPosition(temp); if(temp[1] <= 101) jump = false; if(input_core->EscPressed()) gameState = 1; if(input_core->APressed()) { player->agentData.getPosition(temp); player->agentData.getAcceleration(temp2); temp[0] -= .2f; player->agentData.setPosition(temp); sfx_core->Play(); /* temp2[0] -= .1f; physics_core->setAccel(temp2, "player"); D3DXVECTOR3 vec; vec = physics_core->getVel("player"); player->agentData.setAcceleration(temp2); temp[0] += vec.x; temp[1] += vec.y; temp[2] += vec.z; player->agentData.setPosition(temp); */ } if(input_core->DPressed()) { player->agentData.getPosition(temp); player->agentData.getAcceleration(temp2); temp[0] += .2f; player->agentData.setPosition(temp); sfx_core->Play(); /* temp2[0] += .1f; physics_core->setAccel(temp2, "player"); D3DXVECTOR3 vec; vec = physics_core->getVel("player"); player->agentData.setAcceleration(temp2); temp[0] += vec.x; temp[1] += vec.y; temp[2] += vec.z; player->agentData.setPosition(temp); */ } if(!jump) { if(input_core->SpaceBar()) { jump = true; player->agentData.getPosition(temp); player->agentData.getAcceleration(temp2); /* temp2[1] += 1.0f; physics_core->setAccel(temp2, "player"); D3DXVECTOR3 vec; vec = physics_core->getVel("player"); player->agentData.setAcceleration(temp2); temp[0] += vec.x; temp[1] += vec.y; temp[2] += vec.z; player->agentData.setPosition(temp); */ } } //update main_core->Update(enemy, gameState); for(int i = 0; i < physics_core->Core._collisions->length();i++) { //Pop the next collision off the stack: physics_core->Core._collisions->getNext(); msgbuffer << physics_core->Core._collisions->currentCollision.boxA_ID; msgbuffer << " is colliding with "; msgbuffer << physics_core->Core._collisions->currentCollision.boxB_ID; msgbuffer << "!\n"; char * tempString = new char[msgbuffer.str().length() + 1]; strcpy(tempString, msgbuffer.str().c_str()); //Sample collision handling (this just prints which box ID's are colliding in the output window in visual studio) OutputDebugString(tempString); } if (Wireframe) { pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); } else { pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); } pD3DDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0) , 1.0f, 0); camera.dxSetProjection(pD3DDevice); camera.dxSetView(pD3DDevice); pD3DDevice->BeginScene(); enemy->agentData.getPosition(pos); enemy->agentData.getScale(scale); enemy->agentData.getRotation(rot); cube.set_meshes(pos, rot, scale); cube.draw_meshes(pD3DDevice); player->agentData.getPosition(pos); player->agentData.getScale(scale); player->agentData.getRotation(rot); cube.set_meshes(pos, rot, scale); cube.draw_meshes(pD3DDevice); platform->agentData.getPosition(pos); platform->agentData.getScale(scale); platform->agentData.getRotation(rot); cube.set_meshes(pos, rot, scale); cube.draw_meshes(pD3DDevice); GetClientRect(hWnd, &rect); sprintf(str, "GSP 420 Running Demo"); pFont->DrawText(NULL, str, -1, &rect, DT_TOP | DT_LEFT | DT_NOCLIP , D3DCOLOR_ARGB(255, 255, 255, 255 )); pD3DDevice->EndScene(); pD3DDevice->Present(0, 0, 0, 0); break; case (3)://credits state //setup credits with UI and Graphics //loop till gameState changes //update pD3DDevice->BeginScene(); pD3DDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(122, 0, 0) , 1.0f, 0); GetClientRect(hWnd, &rect); pFont->DrawText(NULL, str, -1, &rect, DT_TOP | DT_CENTER | DT_NOCLIP , D3DCOLOR_ARGB(255, 255, 255, 255 )); gText.DisplayText("CREDITS:", width / 2, 35, 50, 25, WHITE); for (int i = 0; i < AUTHORS; i++) { gText.DisplayText(authors[i], width / 2, i * 25 + 60, 100, 10, WHITE); } pD3DDevice->EndScene(); pD3DDevice->Present(0, 0, 0, 0); main_core->Update(enemy, gameState); if(input_core->EscPressed()) gameState = 1; break; case (4): //shutdown application //shutdown main_core->Shutdown(); pD3DDevice->Release(); pD3DObject->Release(); //destroy application msg.message = WM_QUIT; break; } } } return (int)msg.wParam; } }
void CALLBACK RenderFunc(void) { /** * Set camera * */ D3DMATRIX* pMatView = nullptr; GD::CMatrix44<float> viewMat(gEnv.m_pCamera->GetViewMatrixDX()); pMatView = (D3DMATRIX*)(&viewMat); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_VIEW, pMatView); //应用取景变换矩阵 D3DMATRIX* pProjMatrix; pProjMatrix = (D3DMATRIX*)(&gEnv.m_pCamera->GetProjMatrix().GetFliped()); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_PROJECTION, pProjMatrix); //设置投影变换矩阵 D3DVIEWPORT9* pViewPort; pViewPort = (D3DVIEWPORT9*)(&gEnv.m_pCamera->GetViewPort()); gEnv.m_pDXDevice->GetD3DDevice()->SetViewport(pViewPort); //视口的设置 WCHAR TempName[60] = L"当前显卡型号:"; //定义一个临时字符串,且方便了把"当前显卡型号:"字符串引入我们的目的字符串中 WCHAR adapterName[30]{0}; D3DADAPTER_IDENTIFIER9 Adapter; //定义一个D3DADAPTER_IDENTIFIER9结构体,用于存储显卡信息 LPDIRECT3D9 pD3D = NULL; //Direct3D接口对象的创建 if (NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) //初始化Direct3D接口对象,并进行DirectX版本协商 { return; } pD3D->GetAdapterIdentifier(0, 0, &Adapter);//调用GetAdapterIdentifier,获取显卡信息 int len = ::MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, NULL, 0);//显卡名称现在已经在Adapter.Description中了,但是其为char类型,我们要将其转为wchar_t类型 ::MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, adapterName, len);//这步操作完成后,g_strAdapterName中就为当前我们的显卡类型名的wchar_t型字符串了 wcscat_s(TempName, adapterName);//把当前我们的显卡名加到“当前显卡型号:”字符串后面,结果存在TempName中 pD3D->Release(); gPFont->DrawText(nullptr, TempName, -1, nullptr, DT_TOP | DT_LEFT, D3DCOLOR_XRGB(124, 24, 222)); D3DXMATRIX W; D3DXMatrixIdentity(&W); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &W); gEnv.m_pDXDevice->GetD3DDevice()->SetFVF(Vertex::FVF); gEnv.m_pDXDevice->GetD3DDevice()->SetStreamSource(0, gPVertexBuffer, 0, sizeof(Vertex)); /** * Draw floor * */ gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gFloorMtrl); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, gPFloorTex); gEnv.m_pDXDevice->GetD3DDevice()->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); /** * Draw wall * */ gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gWallMtrl); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, gPWallTex); gEnv.m_pDXDevice->GetD3DDevice()->DrawPrimitive(D3DPT_TRIANGLELIST, 6, 4); /** * Draw mirror * */ gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gMirrorMtrl); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, gPMirrorTex); gEnv.m_pDXDevice->GetD3DDevice()->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2); // Set teapot position static D3DXMATRIX T; static float x = 0.0f; static float y = 3.0f; static float z = -7.5f; const float delta = 0.01f; // increase/decrease alpha via keyboard input if (gEnv.m_pKeyBoard->IsKeyDown(DIK_UPARROW)) { y += delta; } if (gEnv.m_pKeyBoard->IsKeyDown(DIK_DOWNARROW)) { y -= delta; } if (gEnv.m_pKeyBoard->IsKeyDown(DIK_LEFTARROW)) { x += delta; } if (gEnv.m_pKeyBoard->IsKeyDown(DIK_RIGHTARROW)) { x -= delta; } //Draw teapot D3DXMatrixTranslation(&T, x, y, z); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &T); gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gTeapotMtrl); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, 0); gPTeapot->DrawSubset(0); /** * Enable stencil, disable write to z-buffer and back-buffer * */ //Enable stencil, so that the mirror fragment which passed z-test's stencil are set to 0x01 gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILENABLE, true); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILREF, 0x01); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILMASK, 0xffffffff); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); //Disable write to z-buffer, use blend to disable write to color-buffer gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_ZWRITEENABLE, false); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_ALPHABLENDENABLE, true); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); //Finished setting, draw mirror to stencil-buffer; gEnv.m_pDXDevice->GetD3DDevice()->SetStreamSource(0, gPVertexBuffer, 0, sizeof(Vertex)); gEnv.m_pDXDevice->GetD3DDevice()->SetFVF(Vertex::FVF); gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gMirrorMtrl); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, gPMirrorTex); D3DXMATRIX I; D3DXMatrixIdentity(&I); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &I); gEnv.m_pDXDevice->GetD3DDevice()->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_ZWRITEENABLE, true); /** * Then we need to use the modified stencil, disable z-test(why not disable z-test), and use blend to draw mirrored teapot * */ gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP); gEnv.m_pDXDevice->GetD3DDevice()->Clear(0, 0, D3DCLEAR_ZBUFFER, 0, 1.0f, 0); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO); D3DXMATRIX R; D3DXPLANE plane(0.0f, 0.0f, 1.0f, 0.0f); D3DXMatrixReflect(&R, &plane); T = T*R; gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &T); gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gTeapotMtrl); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, 0); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); gPTeapot->DrawSubset(0); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_ALPHABLENDENABLE, false); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_STENCILENABLE, false); gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); }
void CALLBACK RenderFunc(void) { /** * Set camera * */ D3DMATRIX* pMatView = nullptr; GD::CMatrix44<float> viewMat(gEnv.m_pCamera->GetViewMatrixDX()); pMatView = (D3DMATRIX*)(&viewMat); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_VIEW, pMatView); //应用取景变换矩阵 D3DMATRIX* pProjMatrix; pProjMatrix = (D3DMATRIX*)(&gEnv.m_pCamera->GetProjMatrix().GetFliped()); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_PROJECTION, pProjMatrix); //设置投影变换矩阵 D3DVIEWPORT9* pViewPort; pViewPort = (D3DVIEWPORT9*)(&gEnv.m_pCamera->GetViewPort()); gEnv.m_pDXDevice->GetD3DDevice()->SetViewport(pViewPort); //视口的设置 WCHAR TempName[60] = L"当前显卡型号:"; //定义一个临时字符串,且方便了把"当前显卡型号:"字符串引入我们的目的字符串中 WCHAR adapterName[30]{0}; D3DADAPTER_IDENTIFIER9 Adapter; //定义一个D3DADAPTER_IDENTIFIER9结构体,用于存储显卡信息 LPDIRECT3D9 pD3D = NULL; //Direct3D接口对象的创建 if (NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) //初始化Direct3D接口对象,并进行DirectX版本协商 { return; } pD3D->GetAdapterIdentifier(0, 0, &Adapter);//调用GetAdapterIdentifier,获取显卡信息 int len = ::MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, NULL, 0);//显卡名称现在已经在Adapter.Description中了,但是其为char类型,我们要将其转为wchar_t类型 ::MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, adapterName, len);//这步操作完成后,g_strAdapterName中就为当前我们的显卡类型名的wchar_t型字符串了 wcscat_s(TempName, adapterName);//把当前我们的显卡名加到“当前显卡型号:”字符串后面,结果存在TempName中 pD3D->Release(); gPFont->DrawText(nullptr, TempName, -1, nullptr, DT_TOP | DT_LEFT, D3DCOLOR_XRGB(124, 24, 222)); // Set teapot position static D3DXMATRIX T; static float x = 0.0f; static float y = 3.0f; static float z = -7.5f; const float delta = 0.01f; // increase/decrease alpha via keyboard input if (gEnv.m_pKeyBoard->IsKeyDown(DIK_UPARROW)) { y += delta; } if (gEnv.m_pKeyBoard->IsKeyDown(DIK_DOWNARROW)) { y -= delta; } if (gEnv.m_pKeyBoard->IsKeyDown(DIK_LEFTARROW)) { x += delta; } if (gEnv.m_pKeyBoard->IsKeyDown(DIK_RIGHTARROW)) { x -= delta; } //Draw teapot D3DXMatrixTranslation(&T, x, y, z); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &T); gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gTeapotMtrl); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, 0); gPTeapot->DrawSubset(0); D3DXMatrixIdentity(&T); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &T); for (int i = 0; i < gAirplaneMtrl.size(); i++) { gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gAirplaneMtrl[i]); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, gPAirplaneTex[i]); gPAirplane->DrawSubset(i); } /** * Change numFaces by pressing key Q E; * */ int numFaces = gPPAirplane->GetNumFaces(); if (gEnv.m_pKeyBoard->IsKeyDown(DIK_Q)) { gPPAirplane->SetNumFaces(numFaces + 1); //Need to add more than one face to invert an edge collapse transformation if (gPPAirplane->GetNumFaces() == numFaces) { gPPAirplane->SetNumFaces(numFaces + 2); } } if (gEnv.m_pKeyBoard->IsKeyDown(DIK_E)) { gPPAirplane->SetNumFaces(numFaces - 1); //Need to add more than one face to invert an edge collapse transformation if (gPPAirplane->GetNumFaces() == numFaces) { gPPAirplane->SetNumFaces(numFaces - 2); } } /** * Use key R to change fill mode: * When pressed, will change between WIREFRAME/SOLID. * */ static bool bFillMode = true; if (bFillMode == true) { if (gEnv.m_pKeyBoard->IsKeyDown(DIK_R)) { bFillMode = false; gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); } else { gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); } } else { if (gEnv.m_pKeyBoard->IsKeyDown(DIK_R)) { bFillMode = true; gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); } else { gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); } } D3DXMatrixTranslation(&T, x, y, z); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &T); for (int i = 0; i < gAirplaneMtrl.size(); i++) { gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&gAirplaneMtrl[i]); gEnv.m_pDXDevice->GetD3DDevice()->SetTexture(0, gPAirplaneTex[i]); gPPAirplane->DrawSubset(i); } gEnv.m_pDXDevice->GetD3DDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); }
// this is the function used to render a single frame void render_frame(void) { static const int NBUKKIT = 200; static ULARGE_INTEGER bukkits[NBUKKIT]; static int bukidx = 0; int nbuk = (bukidx + 1) % NBUKKIT; int pbuk = (bukidx + NBUKKIT - 1) % NBUKKIT; int ntick = 0; FILETIME curFrame; GetSystemTimeAsFileTime(&curFrame); bukkits[bukidx].HighPart = curFrame.dwHighDateTime; bukkits[bukidx].LowPart = curFrame.dwLowDateTime; if (bukkits[pbuk].QuadPart != 0) { ntick = bukkits[bukidx].LowPart / 10000 - bukkits[pbuk].LowPart / 10000; } d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0); d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); CUSTOMVERTEX* Vertices; v_buffer->Lock(0, 0, (void**)&Vertices, 0); for (unsigned i = 0; i < g_Sim.numPoints(); i++) { const Cloth::Point& p = g_Sim.point(i); Vertices[i].X = p.x(); Vertices[i].Y = p.y(); Vertices[i].Z = p.z(); Vertices[i].COLOR = D3DCOLOR_XRGB(0, 0, 0); } v_buffer->Unlock(); d3ddev->BeginScene(); // select which vertex format we are using d3ddev->SetFVF(CUSTOMFVF); RECT r; r.left = 0; r.top = 0; r.bottom = 0; r.right = 0; char buff[1024]; if (bukkits[nbuk].QuadPart != 0) { double fps = (float)NBUKKIT * (10000000.0 / (double)(bukkits[bukidx].QuadPart - bukkits[nbuk].QuadPart)); sprintf_s(buff, 1024, "FPS: %0.1f", fps); } else { strcpy_s(buff, 1024, "FPS: ??"); } font->DrawText(NULL, buff, -1, &r, DT_CALCRECT, D3DCOLOR_XRGB(1, 1, 1)); font->DrawText(NULL, buff, -1, &r, 0, D3DCOLOR_XRGB(0, 255, 0)); // SET UP THE PIPELINE D3DXMATRIX matView; // the view transform matrix D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (0.0f, 0.0f, -15.0f), // the camera position &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView D3DXMATRIX matProjection; // the projection transform matrix D3DXMatrixPerspectiveFovLH(&matProjection, D3DXToRadian(45), // the horizontal field of view (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio 1.0f, // the near view-plane 100.0f); // the far view-plane d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection D3DXMATRIX matIdentity; D3DXMatrixIdentity(&matIdentity); d3ddev->SetTransform(D3DTS_WORLD, &matIdentity); d3ddev->SetStreamSource(0, v_bordbuffer, 0, sizeof(CUSTOMVERTEX)); d3ddev->DrawPrimitive(D3DPT_LINESTRIP, 0, 4); d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX)); d3ddev->SetIndices(i_buffer); unsigned remaining = g_Sim.numConstraints(); unsigned off = 0; while (remaining) { unsigned toDraw = (remaining > 128) ? 128 : remaining; d3ddev->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, g_Sim.numPoints(), off, toDraw); off += toDraw * 2; remaining -= toDraw; } //d3ddev->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, g_Sim.numConstraints() * 2, 0, g_Sim.numConstraints()); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); bukidx++; bukidx %= NBUKKIT; }
void render() { static RECT rc = {0, 0, 320, 100}; // rectangular region.. used for text drawing static DWORD frameCount = 0; static DWORD startTime = clock(); char str[16]; DWORD val; doMath(); // do the math.. :-P frameCount++; // increment frame count // Clear the back buffer to a black... values r g b are 0-256 lpD3DDevice8->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(256, 256, 256), 1.0f, 0); lpD3DDevice8->GetRenderState(D3DRS_LIGHTING, &val); if (val) { D3DLIGHT8 light; // structure for light data ZeroMemory( &light, sizeof(D3DLIGHT8) ); // zero the mem light.Type = D3DLIGHT_POINT; // set type, either SPOT, DIRECTIONAL, or POINT // light being put onto objects.. light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b = 1.0f; // ambient light of the light source.. only on when light is on light.Ambient.r = 0.25f; light.Ambient.g = 0.25f; light.Ambient.b = 0.25f; // attenuation = Atten0 + Atten1 * d + Atten2 * d * d; // where d is distance // attenuation is decreasing brightness as // a vertex is further away from the light source light.Attenuation0 = .5f; light.Attenuation1 = 0.10f; light.Attenuation2 = 0.01f; // spot lights and directional lights also have a direction // but a POINT light shines in all directions.. kinda like a light bulb light.Position = D3DXVECTOR3(0, 0, -2); light.Range = 15; // after this range the light doesn't affect vertices lpD3DDevice8->SetLight( 0, &light ); // set the light as index 0 lpD3DDevice8->LightEnable( 0, true ); // enable light at index 0 // ambient light.. for everything.. not attached to // this light, just lighting in general lpD3DDevice8->SetRenderState( D3DRS_AMBIENT, 0x00202020 ); } // render the cubes myRect1->render(clock()); myRect2->render(clock()); myRect3->render(clock()); myRect4->render(clock()); myRect5->render(clock()); // this function writes a formatted string to a character string // in this case.. it will write "Avg fps" followed by the // frames per second.. with 2 decimal places sprintf(str, "Avg fps %.2f", (float) frameCount / ((clock() - startTime) / 1000.0f)); // draw the text string.. lpD3DXFont->Begin(); lpD3DXFont->DrawText(str, -1, &rc, DT_LEFT, 0xFFFFFFFF); lpD3DXFont->End(); // present the back buffer.. or "flip" the page lpD3DDevice8->Present( NULL, NULL, NULL, NULL ); // these options are for using rectangular // regions for rendering/drawing... // 3rd is which target window.. NULL makes it use the currently set one (default) // last one is NEVER used.. that happens with DirectX often }
//Add your objects here. void CALLBACK RenderFunc(void) { /** * Show GPU info on TOP|LEFT . */ WCHAR TempName[60] = L"GPU Info:"; WCHAR adapterName[30]{0}; D3DADAPTER_IDENTIFIER9 Adapter; LPDIRECT3D9 pD3D = NULL; if (NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) { return; } pD3D->GetAdapterIdentifier(0, 0, &Adapter); int len = ::MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, NULL, 0); ::MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, adapterName, len); wcscat_s(TempName, adapterName); pD3D->Release(); gPFont->DrawText(nullptr, TempName, -1, nullptr, DT_TOP | DT_LEFT, D3DCOLOR_XRGB(124, 24, 222)); /** * Set camera and view here. */ D3DMATRIX* pMatView = nullptr; GD::CMatrix44<float> viewMat(gEnv.m_pCamera->GetViewMatrixDX()); pMatView = (D3DMATRIX*)(&viewMat); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_VIEW, pMatView); //应用取景变换矩阵 D3DMATRIX* pProjMatrix; pProjMatrix = (D3DMATRIX*)(&gEnv.m_pCamera->GetProjMatrix().GetFliped()); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_PROJECTION, pProjMatrix); //设置投影变换矩阵 D3DVIEWPORT9* pViewPort; pViewPort = (D3DVIEWPORT9*)(&gEnv.m_pCamera->GetViewPort()); gEnv.m_pDXDevice->GetD3DDevice()->SetViewport(pViewPort); //视口的设置 /** * Draw terrain here. */ /** * Draw objects in scene here. */ D3DXMATRIX W; D3DXMatrixIdentity(&W); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &W); D3DMATERIAL9 mtrl; mtrl.Ambient = D3DCOLOR_WHITE; mtrl.Diffuse = D3DCOLOR_WHITE; mtrl.Emissive = D3DCOLOR_RED; mtrl.Specular = D3DCOLOR_PUREGREEN; mtrl.Power = 2.0f; gEnv.m_pDXDevice->GetD3DDevice()->SetMaterial(&mtrl); gPMesh->DrawSubset(0); DrawBasicScene(gEnv.m_pDXDevice->GetD3DDevice(), 1.0f); D3DXMatrixIdentity(&W); gEnv.m_pDXDevice->GetD3DDevice()->SetTransform(D3DTS_WORLD, &W); gEnv.m_snowParticle->Render(); }
//-----------------------------------【Direct3D_Render( )函数】------------------------------- // 描述:使用Direct3D进行渲染 //-------------------------------------------------------------------------------------------------- void Direct3D_Render(HWND hwnd) { //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之一】:清屏操作 //-------------------------------------------------------------------------------------- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0); //定义一个矩形,用于获取主窗口矩形 RECT formatRect; GetClientRect(hwnd, &formatRect); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之二】:开始绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->BeginScene(); // 开始绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之三】:正式绘制,利用顶点缓存绘制图形 //-------------------------------------------------------------------------------------- g_pd3dDevice->SetStreamSource(0, g_pVertexBuffer, 0, sizeof(CUSTOMVERTEX));//把包含的几何体信息的顶点缓存和渲染流水线相关联 g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);//指定我们使用的灵活顶点格式的宏名称 g_pd3dDevice->SetIndices(g_pIndexBuffer);//设置索引缓存 //-------------------------------------------------------------------------------------- // 【纹理绘制四步曲之四】:纹理的启用 //-------------------------------------------------------------------------------------- g_pd3dDevice->SetTexture(0, g_pTexture); //启用纹理 g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);//利用索引缓存配合顶点缓存绘制图形 //在窗口右上角处,显示每秒帧数 int charCount = swprintf_s(g_strFPS, 20, _T("FPS:%0.3f"), Get_FPS()); g_pTextFPS->DrawText(NULL, g_strFPS, charCount, &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_RGBA(0, 239, 136, 255)); //显示显卡类型名 g_pTextAdaperName->DrawText(NULL, g_strAdapterName, -1, &formatRect, DT_TOP | DT_LEFT, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f)); // 输出绘制信息 formatRect.top = 30; static wchar_t strInfo[256] = { 0 }; swprintf_s(strInfo, -1, L"模型坐标: (%.2f, %.2f, %.2f)", g_matWorld._41, g_matWorld._42, g_matWorld._43); g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135, 239, 136, 255)); // 输出帮助信息 formatRect.left = 0, formatRect.top = 380; g_pTextInfor->DrawText(NULL, L"控制说明:", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(235, 123, 230, 255)); formatRect.top += 35; g_pTextHelper->DrawText(NULL, L" 按住鼠标左键并拖动:平移模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 按住鼠标右键并拖动:旋转模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 滑动鼠标滚轮:拉伸模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" W、S、A、D键:平移模型 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 上、下、左、右方向键:旋转模型 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 键盘上1,2数字键:在两种填充模式之间切换 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" ESC键 : 退出程序", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255, 200, 0, 255)); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之四】:结束绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->EndScene(); // 结束绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之五】:显示翻转 //-------------------------------------------------------------------------------------- g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转与显示 }
//-----------------------------------【Direct3D_Render( )函数】------------------------------- // 描述:使用Direct3D进行渲染 //-------------------------------------------------------------------------------------------------- void Direct3D_Render(HWND hwnd) { //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之一】:清屏操作 //-------------------------------------------------------------------------------------- g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0); //定义一个矩形,用于获取主窗口矩形 RECT formatRect; GetClientRect(hwnd, &formatRect); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之二】:开始绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->BeginScene(); // 开始绘制 // 绘制网格 for (DWORD i = 0; i < g_dwNumMtrls; i++) { g_pd3dDevice->SetMaterial(&g_pMaterials[i]); g_pd3dDevice->SetTexture(0, g_pTextures[i]); g_pMesh->DrawSubset(i); } //在窗口右上角处,显示每秒帧数 int charCount = swprintf_s(g_strFPS, 20, _T("FPS:%0.3f"), Get_FPS() ); g_pTextFPS->DrawText(NULL, g_strFPS, charCount , &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_RGBA(0,239,136,255)); //显示显卡类型名 g_pTextAdaperName->DrawText(NULL,g_strAdapterName, -1, &formatRect, DT_TOP | DT_LEFT, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f)); // 输出绘制信息 formatRect.top = 30; static wchar_t strInfo[256] = {0}; swprintf_s(strInfo,-1, L"模型坐标: (%.2f, %.2f, %.2f)", g_matWorld._41, g_matWorld._42, g_matWorld._43); g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135,239,136,255)); // 输出帮助信息 formatRect.left = 0,formatRect.top = 380; g_pTextInfor->DrawText(NULL, L"控制说明:", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(235,123,230,255)); formatRect.top += 35; g_pTextHelper->DrawText(NULL, L" 按住鼠标左键并拖动:平移模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 按住鼠标右键并拖动:旋转模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 滑动鼠标滚轮:拉伸模型", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" W、S、A、D键:平移模型 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" 上、下、左、右方向键:旋转模型 ", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); formatRect.top += 25; g_pTextHelper->DrawText(NULL, L" ESC键 : 退出程序", -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之四】:结束绘制 //-------------------------------------------------------------------------------------- g_pd3dDevice->EndScene(); // 结束绘制 //-------------------------------------------------------------------------------------- // 【Direct3D渲染五步曲之五】:显示翻转 //-------------------------------------------------------------------------------------- g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转与显示 }