//*************************************************************** // 描画 // 引数 // LPDIRECT3DDEVICE9 pDevice:デバイスポインタ // 戻り値 // なし //*************************************************************** void CCoin::Draw(LPDIRECT3DDEVICE9 pDevice) { //マトリックス変数宣言 D3DXMATRIX mTrans,mScale,mRotate; //左から座標、大きさ、回転 //テクスチャ取得変数 LPDIRECT3DTEXTURE9 Texture = CTexture::GetTexture(m_nTex); //ライト無効化 CLight::SetOffLight(pDevice); //描画タイプの変更 CRenderer::SetRenderType(CRenderer::NOMAL); //マトリックス初期化 D3DXMatrixIdentity(&m_mtxWorld); //大きさ設定 D3DXMatrixScaling(&mScale, 1.0f,1.0f,1.0f); //合成 D3DXMatrixMultiply(&m_mtxWorld,&m_mtxWorld,&mScale); //角度設定 D3DXMatrixRotationYawPitchRoll(&mRotate, m_Vertex_3d.Rot_vtx.y,0.0f,0.0f); //合成 D3DXMatrixMultiply(&m_mtxWorld,&m_mtxWorld,&mRotate); //座標設定 D3DXMatrixTranslation(&mTrans, m_Vertex_3d.vtx.x,CScene3d::GetHeight(m_Vertex_3d.vtx) + 1.0f + m_Vertex_3d.vtx.y,m_Vertex_3d.vtx.z); //合成 D3DXMatrixMultiply(&m_mtxWorld,&m_mtxWorld,&mTrans); //セッティング pDevice->SetTransform(D3DTS_WORLD,&m_mtxWorld); //メッシュ読み込み LPD3DXMATERIAL pMaterial = (LPD3DXMATERIAL)m_pD3DXMatBuff->GetBufferPointer(); //描画開始 for(unsigned int i = 0 ; i < m_nNumMat ; i++) { //マテリアルセット pDevice->SetMaterial(&pMaterial[i].MatD3D); if(Texture) { //テクスチャ貼り付け pDevice->SetTexture(0, Texture); } //描画 m_pMesh->DrawSubset(i); } //マテリアルを元に戻す pMaterial->MatD3D.Diffuse = D3DXCOLOR(1.0f,1.0f,1.0f,0.0f); pMaterial->MatD3D.Ambient = D3DXCOLOR(0.0f,0.0f,0.0f,0.0f); pMaterial->MatD3D.Emissive = D3DXCOLOR(0.0f,0.0f,0.0f,0.0f); pDevice->SetMaterial(&pMaterial->MatD3D); //ライトを元に戻す CLight::SetOnLight(pDevice); }
bool ZEffectBillboardTexAniList::Draw() { if(!m_pVB) return false; if( size()==0 ) return true; BeginState(); RSetFog(FALSE); HRESULT hr; DWORD dwRemainNum = (DWORD)size(); iterator itr = begin(); while(dwRemainNum) { if(m_dwBase >= EFFECTBASE_DISCARD_COUNT) m_dwBase = 0; DWORD dwThisNum = min( dwRemainNum , BILLBOARD_FLUSH_COUNT ); dwThisNum = min( dwThisNum , EFFECTBASE_DISCARD_COUNT - m_dwBase ); BYTE *pVertices; if( FAILED( hr = m_pVB->Lock( m_dwBase * sizeof(ZEFFECTCUSTOMVERTEX) * 4, dwThisNum * sizeof(ZEFFECTCUSTOMVERTEX) * 4, (VOID**)&pVertices, m_dwBase ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD ) ) ) { return false; } BYTE *pInd; if( FAILED( hr = m_pIB->Lock( m_dwBase * sizeof(WORD) * 6, dwThisNum * sizeof(WORD) * 6, (VOID**)&pInd, m_dwBase ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD ) ) ) { return false; } int nRenderCnt = 0; ZCharacter* pChar = NULL; for(DWORD j=0;j<dwThisNum;j++) { ZEFFECTBILLBOARDTEXANIITEM *p = (ZEFFECTBILLBOARDTEXANIITEM*)*itr; if(p->fElapsedTime < p->fAddTime ) { itr++; continue; } pChar = ZGetCharacterManager()->Find(p->CharUID); if( pChar ) { if( pChar->m_pVMesh ) { if( pChar->m_pVMesh->m_bIsRender==false) {//부모가 안그려졌으면 skip... itr++; continue; } } } nRenderCnt++; // Transform rmatrix matTranslation; rmatrix matScaling; rmatrix matWorld; rvector dir = p->normal; rvector up=p->up; rvector right; if(IS_EQ(dir.z,1.f)) up=rvector(1,0,0); D3DXVec3Cross(&right, &up, &dir); D3DXVec3Normalize(&right, &right); D3DXVec3Cross(&up, &right, &dir); D3DXVec3Normalize(&up, &up); rmatrix mat; D3DXMatrixIdentity(&mat); mat._11=right.x;mat._12=right.y;mat._13=right.z; mat._21=up.x;mat._22=up.y;mat._23=up.z; mat._31=dir.x;mat._32=dir.y;mat._33=dir.z; rvector pos=p->position; // float fScale=p->fStartSize * p->fOpacity + p->fEndSize * (1.f - p->fOpacity); float fInt = min(1,max(0,(p->fLifeTime - p->fElapsedTime)/p->fLifeTime)); float fScale=p->fStartSize * fInt + p->fEndSize * (1.f - fInt); D3DXMatrixScaling(&matScaling,fScale*m_Scale.x,fScale*m_Scale.y,fScale*m_Scale.z); D3DXMatrixTranslation(&matTranslation, pos.x, pos.y, pos.z); D3DXMatrixMultiply(&matWorld, &matScaling, &mat); D3DXMatrixMultiply(&matWorld, &matWorld, &matTranslation); DWORD color = ((DWORD)(p->fOpacity * 255))<<24 | p->dwColor; static ZEFFECTCUSTOMVERTEX v[] = { {-1, -1, 0, 0xFFFFFFFF, 1, 0}, {-1, 1, 0, 0xFFFFFFFF, 1, 1}, { 1, 1, 0, 0xFFFFFFFF, 0, 1}, { 1, -1, 0, 0xFFFFFFFF, 0, 0}, }; static rvector sv[4] = { rvector(-1,-1,0) , rvector(-1,1,0) , rvector(1,1,0) , rvector(1,-1,0) }; GetFrameUV( min( p->frame,m_nMaxFrame-1) ); v[0].tu = m_fUV[0]; v[0].tv = m_fUV[1]; v[1].tu = m_fUV[2]; v[1].tv = m_fUV[3]; v[2].tu = m_fUV[4]; v[2].tv = m_fUV[5]; v[3].tu = m_fUV[6]; v[3].tv = m_fUV[7]; // 좋은코드는 아니지만 버텍스 카피를 줄이기위해 타입캐스팅했다. D3DXVec3TransformCoord((D3DXVECTOR3*)&v[0].x,sv+0,&matWorld); D3DXVec3TransformCoord((D3DXVECTOR3*)&v[1].x,sv+1,&matWorld); D3DXVec3TransformCoord((D3DXVECTOR3*)&v[2].x,sv+2,&matWorld); D3DXVec3TransformCoord((D3DXVECTOR3*)&v[3].x,sv+3,&matWorld); v[0].color=v[1].color=v[2].color=v[3].color=color; memcpy(pVertices,v,sizeof(ZEFFECTCUSTOMVERTEX)*4); pVertices+=sizeof(ZEFFECTCUSTOMVERTEX)*4; WORD inds[] = { 0,1,2,0,2,3 }; for(int k=0;k<6;k++) { inds[k]+=(m_dwBase+j)*4; } memcpy(pInd,inds,sizeof(inds)); pInd+=sizeof(inds); itr++; } m_pVB->Unlock(); m_pIB->Unlock(); if(FAILED( hr = RGetDevice()->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,m_dwBase*4,nRenderCnt*4,m_dwBase*6,nRenderCnt*2) )) return false; m_dwBase+=dwThisNum; dwRemainNum-=dwThisNum; } RGetDevice()->SetStreamSource( 0, NULL , 0,0 ); RGetDevice()->SetIndices(NULL); if(ZGetWorld()) { ZGetWorld()->SetFog(true);// 게임 설정에 따라서 } EndState(); return true; }
void CAXModel::UpdateMatrix(void) { D3DXMATRIX matPosition; D3DXMATRIX matSize; D3DXMATRIX matRotate; D3DXMATRIX matAnchorPoint; D3DXMATRIX matComplate; D3DXMATRIX mT; { AVector3 vLeft(1.0f, 0.0f, 0.0f); AVector3 vUp(0.0f, 1.0f, 0.0f); AVector3 vLook(0.0f, 0.0f, 1.0f); AVector3 vUpNew, vLeftNew, vLookNew; D3DXMATRIX mX, mY, mZ; D3DXMatrixRotationX(&mX, m_fRotX); D3DXMatrixRotationY(&mY, m_fRotY); D3DXMatrixRotationZ(&mZ, -m_fRotZ); D3DXMatrixTranslation(&mT, vUp.x, vUp.y, vUp.z); mT *= mX; D3DXMatrixTranslation(&mT, mT._41, mT._42, mT._43); mT *= mY; D3DXMatrixTranslation(&mT, mT._41, mT._42, mT._43); mT *= mZ; vUpNew = AVector3(mT._41, mT._42, mT._43); D3DXMatrixTranslation(&mT, vLeft.x, vLeft.y, vLeft.z); mT *= mX; D3DXMatrixTranslation(&mT, mT._41, mT._42, mT._43); mT *= mY; D3DXMatrixTranslation(&mT, mT._41, mT._42, mT._43); mT *= mZ; vLeftNew = AVector3(mT._41, mT._42, mT._43); D3DXMatrixTranslation(&mT, vLook.x, vLook.y, vLook.z); mT *= mX; D3DXMatrixTranslation(&mT, mT._41, mT._42, mT._43); mT *= mY; D3DXMatrixTranslation(&mT, mT._41, mT._42, mT._43); mT *= mZ; vLookNew = AVector3(mT._41, mT._42, mT._43); mT._11 = vLeftNew.x; mT._12 = vLeftNew.y; mT._13 = vLeftNew.z; mT._14 = 0.0f; // Left Vector mT._21 = vUpNew.x; mT._22 = vUpNew.y; mT._23 = vUpNew.z; mT._24 = 0.0f; // Up Vector mT._31 = vLookNew.x; mT._32 = vLookNew.y; mT._33 = vLookNew.z; mT._34 = 0.0f; // Front Vector mT._41 = 0.0f; mT._42 = 0.0f; mT._43 = 0.0f; mT._44 = 1.0f; // Translation } D3DXMatrixTranslation(&matAnchorPoint, m_vAnchorPoint.x, m_vAnchorPoint.y, m_vAnchorPoint.z); D3DXMatrixScaling(&matSize, m_vSize.x, m_vSize.y, m_vSize.z); D3DXMatrixTranslation(&matPosition, m_vPosition.x, m_vPosition.y, m_vPosition.z); m_matComplate = matAnchorPoint * matSize * mT * matPosition; m_bUpdateMatrix = false; }
bool DirectGraphicsSprite::RenderSpriteRotated(float x, float y, float Winkel, int Anim, D3DCOLOR Color, bool mirror) { float l, r, o, u; // Vertice Koordinaten float tl, tr, to, tu; // Textur Koordinaten // Ausschnitt einstellen Anim %= 255; itsRect = itsPreCalcedRects [Anim]; // normal // if (mirror == false) { l = x-0.5f; // Links r = x+(itsRect.right-itsRect.left-1)+0.5f; // Rechts Winkel = 360 - Winkel; } // oder gespiegelt // else { r = x+0.5f; // Links l = x+(itsRect.right-itsRect.left-1)-0.5f; // Rechts } o = y-0.5f; // Oben u = y+(itsRect.bottom-itsRect.top-1)+0.5f; // Unten to = itsRect.top /itsYSize; // Oben tu = itsRect.bottom/itsYSize; // Unten tl = itsRect.left /itsXSize; // Links tr = itsRect.right /itsXSize; // Rechts TriangleStrip[0].color = TriangleStrip[1].color = TriangleStrip[2].color = TriangleStrip[3].color = Color; TriangleStrip[0].z = TriangleStrip[1].z = TriangleStrip[2].z = TriangleStrip[3].z = 0.0f; TriangleStrip[0].x = l; // Links oben TriangleStrip[0].y = o; TriangleStrip[0].tu = tl; TriangleStrip[0].tv = to; TriangleStrip[1].x = r; // Rechts oben TriangleStrip[1].y = o; TriangleStrip[1].tu = tr; TriangleStrip[1].tv = to; TriangleStrip[2].x = l; // Links unten TriangleStrip[2].y = u; TriangleStrip[2].tu = tl; TriangleStrip[2].tv = tu; TriangleStrip[3].x = r; // Rechts unten TriangleStrip[3].y = u; TriangleStrip[3].tu = tr; TriangleStrip[3].tv = tu; lpD3DDevice->SetTexture (0, itsTexture); // Textur setzen //----- Sprite rotieren D3DXMATRIX matRot, matTrans, matTrans2; // Winkel angleichen, damit er immer zwischen 0° und 360° bleibt if (Winkel > 360) Winkel -= 360; if (Winkel < 0) Winkel += 360; // Rotationsmatrix D3DXMatrixRotationZ (&matRot, float(PI * Winkel / 180)); // Transformation zum Ursprung D3DXMatrixTranslation(&matTrans,-x-(itsRect.right - itsRect.left)/2.0f, -y-(itsRect.bottom - itsRect.top )/2.0f, 0.0f); // Transformation wieder zurück D3DXMatrixTranslation(&matTrans2,x+(itsRect.right - itsRect.left)/2.0f, y+(itsRect.bottom - itsRect.top )/2.0f, 0.0f); D3DXMatrixMultiply (&matWorld, &matWorld, &matTrans); // Verschieben D3DXMatrixMultiply (&matWorld, &matWorld, &matRot); // rotieren D3DXMatrixMultiply (&matWorld, &matWorld, &matTrans2); // und wieder zurück verschieben lpD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); DirectGraphics.SetFilterMode (true); // Sprite zeichnen DirectGraphics.RendertoBuffer (D3DPT_TRIANGLESTRIP, 2,&TriangleStrip[0]); DirectGraphics.SetFilterMode (false); // Normale Projektions-Matrix wieder herstellen D3DXMatrixRotationZ (&matWorld, 0.0f); lpD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); return true; }
void Rect3D2::render(DWORD curTime) { DWORD elapsedTime; D3DXMATRIX matWorld, matTemp; D3DXMATRIX matRot, matTranslate, matScale; float t, halfTSqrd; // calculate time elapsed and store current time for next cycle if (m_oldTime == 0) { m_oldTime = curTime; elapsedTime = 0; } else { elapsedTime = curTime - m_oldTime; m_oldTime = curTime; } t = elapsedTime * 0.001f; // convert time to seconds and store halfTSqrd = 0.5f * t * t; // store half of time squared // calculate rotations // d = d + v * t + (1/2)at^2 // v = v + at; m_yaw += m_dYaw * t + halfTSqrd * m_ddYaw; m_dYaw += m_ddYaw * t; m_pitch += m_dPitch * t + halfTSqrd * m_ddPitch; m_dPitch += m_ddPitch * t; m_roll += m_dRoll * t + halfTSqrd * m_ddRoll; m_dRoll += m_ddRoll * t; // MATRICES these take effect for drawing primitives until they are reset // so to draw objects with different orientation, just // use the matrices, draw, then reset again // in the case of multiple objects // each object sets the matrices for its use, renders, then the next sets.. // renders.. and so on D3DXMatrixRotationYawPitchRoll( &matRot, m_yaw, m_pitch, m_roll); D3DXMatrixTranslation( &matTranslate, m_posX, m_posY, m_posZ ); D3DXMatrixScaling( &matScale, m_width, m_height, m_depth ); D3DXMatrixMultiply( &matTemp, &matScale, &matRot); D3DXMatrixMultiply( &matWorld, &matTemp, &matTranslate); m_device->SetTransform( D3DTS_WORLD, &matWorld ); // for now all we have to do is set the texture // there are a lot of options to set for textures // when different forms of pixel shading are involved, for now it always // looks the same m_device->SetTexture(0, m_texture); // SCENE RENDERING // Begin the scene m_device->BeginScene(); m_device->SetStreamSource( 0, m_vertBuffer, 0, sizeof(CUSTOMVERTEX) ); // set vertex stream.. m_device->SetFVF( D3DFVF_CUSTOMVERTEX ); // m_device->SetVertexShader( D3DFVF_CUSTOMVERTEX ); // set vertex shader options m_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12); m_device->EndScene(); }
void render (float timeDelta) { //设置取景变换 if (snake->ifDead ()==false) { gametime += timeDelta ;//计算游戏时间 D3DXMATRIX V; Camera.SetViewMatrix(&V); Device->SetTransform (D3DTS_VIEW,&V); KeyboardAndMouse(&Camera ,timeDelta); D3DXMATRIX TerrainWorld; D3DXMatrixTranslation(&TerrainWorld,0.0f,00.0f,0.0f); //*************更新区域********************* props->upDateProps (timeDelta); groundobject->updateBird (timeDelta); snow->update(timeDelta);//掉落蓝色的花 snake->changeWallRange (groundobject); D3DXVECTOR3 position; position.x =0.0f,position.y =0.0f,position.z =0.0f; groundobject->createWall (position); //为啥要把蛇的更新函数放在这里呢?因为里面包含了渲染字体的操作... snake->upDateSanke (timeDelta,DInput); //开启深度缓存 //开启深度缓存 Device->SetRenderState (D3DRS_ZENABLE,true); Device->SetRenderState (D3DRS_ZFUNC,D3DCMP_LESS); Device->SetRenderState (D3DRS_ZWRITEENABLE,true); Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(100,155,255), 1.0f, 0); //开始绘制 Device->BeginScene (); //渲染地形 terrain->draw (&TerrainWorld,false); //渲染天空盒 skybox->RenderSkyBox(); //渲染地面物体 groundobject->renderGround (); //绘制雪花 snow->render(); gameui->RenderUI (); RECT rect; rect.left=120,rect.top=50,rect.right=800,rect.bottom=600; gameui->RenderFont(rect,snake->getScore()); snake->Render(); float i; groundobject->getWallRange(&i); if(snake->isDangerous (i)) gameui2->RenderUI(); //绘制道具 props->RenderProps (); Device->EndScene (); Device->Present(0, 0, 0, 0);//提交后台缓存示翻转) } else { //绘制游戏得分 RECT rect; rect.left=280,rect.top=195,rect.right=800,rect.bottom=600; gameui->RenderFont (rect,snake->getScore()); //绘制时间 rect.left=280,rect.top=140,rect.right=800,rect.bottom=600; gameui->RenderFont (rect,d3d::IntToWchart(gametime)); //绘制道具得分 rect.left=280,rect.top=240,rect.right=800,rect.bottom=600; int propscore =props->getpropscore(); gameui->RenderFont(rect ,d3d::IntToWchart(propscore)); rect.left = 280,rect.top=422,rect.right=800,rect.bottom=600; int finnalscore=snake->getscore()+gametime +propscore*10; gameui->RenderFont (rect,d3d::IntToWchart(finnalscore)); } //关闭深度缓存 Device->SetRenderState (D3DRS_ZENABLE,false); Device->SetRenderState (D3DRS_ZFUNC,D3DCMP_LESS); Device->SetRenderState (D3DRS_ZWRITEENABLE,false); }
bool Display(float timeDelta) { if (Device) { // Update the scene: update camera position. static float angle = (3.0f * D3DX_PI) / 2.0f; static float height = 5.0f; if (::GetAsyncKeyState(VK_LEFT) & 0x8000f) angle -= 0.5f * timeDelta; if (::GetAsyncKeyState(VK_RIGHT) & 0x8000f) angle += 0.5f * timeDelta; if (::GetAsyncKeyState(VK_UP) & 0x8000f) height += 5.0f * timeDelta; if (::GetAsyncKeyState(VK_DOWN) & 0x8000f) height -= 5.0f * timeDelta; D3DXVECTOR3 position(cosf(angle) * 7.0f, height, sinf(angle) * 7.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMATRIX V; D3DXMatrixLookAtLH(&V, &position, &target, &up); Device->SetTransform(D3DTS_VIEW, &V); static float x0 = 0.0f; static float y0 = 2.0f; static float z0 = 0.0f; if (::GetAsyncKeyState('A') & 0x8000f) x0 -= 2.0f * timeDelta; if (::GetAsyncKeyState('D') & 0x8000f) x0 += 2.0f * timeDelta; if (::GetAsyncKeyState('W') & 0x8000f) z0 += 2.0f * timeDelta; if (::GetAsyncKeyState('S') & 0x8000f) z0 -= 2.0f * timeDelta; if (::GetAsyncKeyState(VK_SPACE) & 0x8000f) y0 += 2.0f * timeDelta; if (::GetAsyncKeyState(VK_SHIFT) & 0x8000f) y0 -= 2.0f * timeDelta; D3DXMatrixTranslation(&Worlds[0], x0, y0, z0); // Draw the scene: Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0); Device->BeginScene(); for (int i = 0; i < 4; i++) { // set material and world matrix for ith object, then render // the ith object. Device->SetMaterial(&Mtrls[i]); Device->SetTransform(D3DTS_WORLD, &Worlds[i]); Objects[i]->DrawSubset(0); } Device->EndScene(); Device->Present(0, 0, 0, 0); } return true; }
// // Framework Functions // bool Setup() { // // Create the objects. // D3DXCreateTeapot( Device, &Objects[0], 0); D3DXCreateBox( Device, 2.0f, // width 2.0f, // height 2.0f, // depth &Objects[1], 0); // cylinder is built aligned on z-axis D3DXCreateCylinder( Device, 1.0f, // radius at negative z end 1.0f, // radius at positive z end 3.0f, // length of cylinder 10, // slices 10, // stacks &Objects[2], 0); D3DXCreateTorus( Device, 1.0f, // inner radius 3.0f, // outer radius 10, // sides 10, // rings &Objects[3], 0); D3DXCreateSphere( Device, 1.0f, // radius 10, // slices 10, // stacks &Objects[4], 0); // // Build world matrices - position the objects in world space. // For example, ObjWorldMatrices[1] will position Objects[1] at // (-5, 0, 5). Likewise, ObjWorldMatrices[2] will position // Objects[2] at (5, 0, 5). // D3DXMatrixTranslation(&ObjWorldMatrices[0], 0.0f, 0.0f, 0.0f); D3DXMatrixTranslation(&ObjWorldMatrices[1], -5.0f, 0.0f, 5.0f); D3DXMatrixTranslation(&ObjWorldMatrices[2], 5.0f, 0.0f, 5.0f); D3DXMatrixTranslation(&ObjWorldMatrices[3], -5.0f, 0.0f, -5.0f); D3DXMatrixTranslation(&ObjWorldMatrices[4], 5.0f, 0.0f, -5.0f); // // Set the projection matrix. // D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, // 90 - degree (float)Width / (float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); // // Switch to wireframe mode. // Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); return true; }
void CDirectXFramework::Render() { timeGetTime(); RECT rect; // If the device was not created successfully, return if(!m_pD3DDevice) { return; } //************************************************************************* ////////////////////////////////////////////////////////////////////////// // All draw calls between swap chain's functions, and pre-render and post- // render functions (Clear and Present, BeginScene and EndScene) ////////////////////////////////////////////////////////////////////////// // Clear the back buffer, call BeginScene() m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); m_pD3DDevice->BeginScene(); system->update(); ////////////////////////////////////////////////////////////////////////// // Draw 3D Objects (for future labs - not used in Week #1) ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// // Draw 2D sprites ////////////////////////////////////////////////////////////////////////// // Note: You should only be calling the sprite object's begin and end once, // with all draw calls of sprites between them // Call Sprite's Begin to start rendering 2D sprite objects m_pD3DSprite->Begin(NULL); ////////////////////////////////////////////////////////////////////////// // Matrix Transformations to control sprite position, scale, and rotate // Set these matrices for each object you want to render to the screen ////////////////////////////////////////////////////////////////////////// // Scaling // Rotation on Z axis, value in radians, converting from degrees // Translation // Multiply scale and rotation, store in scale // Multiply scale and translation, store in world // Set Transform D3DXMATRIX transMat, rotMat, scaleMat, worldMat; D3DXMatrixIdentity(&transMat); D3DXMatrixIdentity(&scaleMat); D3DXMatrixIdentity(&rotMat); D3DXMatrixIdentity(&worldMat); D3DXMatrixRotationZ(&rotMat, D3DXToRadian(0)); D3DXMatrixRotationZ(&rotMat, D3DXToRadian(0)); D3DXMatrixTranslation(&transMat, Menu.xp, Menu.yp, 0.0f); D3DXMatrixScaling(&scaleMat, 0.5f, 0.5f, 0.0f); D3DXMatrixMultiply(&scaleMat, &scaleMat, &rotMat); D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat); m_pD3DSprite->SetTransform(&worldMat); if(Menu.onSTART == true) { m_pD3DSprite->Draw(m_StartText, 0, &D3DXVECTOR3(m_StartImage.Width * 0.5f, m_StartImage.Height * 0.5f, 0.0f), 0, D3DCOLOR_ARGB(255, 255, 255, 255)); if(controlDown & ARROW_DOWN) { Menu.onSTART = false; Menu.onCREDITS = true; } if(controlDown & ENTER_KEY) { Menu.onSTART = false; Menu.onMovie = true; if(Menu.onMovie = true) { m_pMediaControl->Run(); long evCode; m_pMediaEvent->WaitForCompletion(INFINITE, &evCode); m_pMediaControl->Stop(); Menu.onMovie = false; Menu.onGAME = true; } } } else if(Menu.onCREDITS == true) { m_pD3DSprite->Draw(m_CreditText, 0, &D3DXVECTOR3(m_CreditImage.Width * 0.5f, m_CreditImage.Height * 0.5f, 0.0f), 0, D3DCOLOR_ARGB(255, 255, 255, 255)); if(controlDown & ARROW_DOWN) { Menu.onEXIT = true; Menu.onCREDITS = false; } if(controlDown & ENTER_KEY) { Menu.onCREDITS = false; Menu.onCREDITS2 = true; } if(controlDown & ARROW_UP) { Menu.onCREDITS = false; Menu.onSTART = true; } } else if(Menu.onEXIT == true) { m_pD3DSprite->Draw(m_ExitText, 0, &D3DXVECTOR3(m_ExitImage.Width * 0.5f, m_ExitImage.Height * 0.5f, 0.0f), 0, D3DCOLOR_ARGB(255, 255, 255, 255)); if(controlDown & ARROW_UP) { Menu.onEXIT = false; Menu.onCREDITS = true; } if(controlDown & ENTER_KEY) { Menu.onCREDITS = false; Menu.onCREDITS2 = true; exit(0); } } if(Menu.onCREDITS2 == true) { m_pD3DSprite->Draw(m_Credit2Text, 0, &D3DXVECTOR3(m_Credit2Image.Width * 0.5f, m_Credit2Image.Height * 0.5f, 0.0f), 0, D3DCOLOR_ARGB(255, 255, 255, 255)); if(controlDown & ARROW_LEFT) { Menu.onCREDITS2 = false; Menu.onCREDITS = true; } } if(Menu.onGAME == true) { //BACKGROUND IMAGE D3DXMatrixRotationZ(&rotMat, D3DXToRadian(0)); D3DXMatrixTranslation(&transMat, Wall.xp, Wall.yp, 0.0f); D3DXMatrixScaling(&scaleMat, 1, 1, 0.0f); D3DXMatrixMultiply(&scaleMat, &scaleMat, &rotMat); D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat); // Set Transform for the object m_pD3DSprite m_pD3DSprite->SetTransform(&worldMat); m_pD3DSprite->Draw(m_wallTexture, 0, &D3DXVECTOR3(m_wallImage.Width * 0.5f, m_wallImage.Height * 0.5f, 0.0f), 0, D3DCOLOR_ARGB(255, 255, 255, 255)); for(int i = 0; i < 2; ++i) { if(i == 0) { if(controlActive & S_DOWN) { Paddle[0].yp = Paddle[0].yp + .1f; } if(controlActive & W_UP) { Paddle[0].yp = Paddle[0].yp - .1f; } //Out of Bounds if(Paddle[i].yp-60 <= 0) { Paddle[0].yp = 60; } if(Paddle[i].yp+60 >= 600) { Paddle[0].yp = 540; } D3DXMatrixTranslation(&transMat, Paddle[i].xp, Paddle[i].yp, 0.0f); } if(i == 1) { if(controlActive & ARROW_DOWN) { Paddle[1].yp = Paddle[1].yp + .1f; } if(controlActive & ARROW_UP) { Paddle[1].yp = Paddle[1].yp - .1f; } //Out of Bounds if(Paddle[i].yp-60 <= 0) { Paddle[1].yp = 60; } if(Paddle[i].yp+60 >= 600) { Paddle[1].yp = 540; } D3DXMatrixTranslation(&transMat, Paddle[i].xp, Paddle[i].yp, 0.0f); } D3DXMatrixScaling(&scaleMat, 1, 1, 0.0f); D3DXMatrixMultiply(&scaleMat, &scaleMat, &rotMat); D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat); // Set Transform for the object m_pD3DSprite m_pD3DSprite->SetTransform(&worldMat); // Draw the texture with the sprite object m_pD3DSprite->Draw(m_pTexture, 0, &D3DXVECTOR3(m_imageInfo.Width * 0.5f, m_imageInfo.Height * 0.5f, 0.0f), 0, D3DCOLOR_ARGB(255, 255, 255, 255)); //WALL COLLISION if(Ball.yp-20 <= 0) { if(Ball.DIR_UP_RIGHT == true) { Ball.DIR_UP_RIGHT =false; Ball.DIR_DOWN_RIGHT =true; Ball.DIR_DOWN_LEFT =false; Ball.DIR_UP_LEFT =false; } else if(Ball.DIR_UP_LEFT == true) { Ball.DIR_UP_RIGHT =false; Ball.DIR_DOWN_RIGHT =false; Ball.DIR_DOWN_LEFT =true; Ball.DIR_UP_LEFT =false; } } if(Ball.yp+20 >= 600) { if(Ball.DIR_DOWN_RIGHT == true) { Ball.DIR_UP_RIGHT =true; Ball.DIR_DOWN_RIGHT =false; Ball.DIR_DOWN_LEFT =false; Ball.DIR_UP_LEFT =false; } else if(Ball.DIR_DOWN_LEFT == true) { Ball.DIR_UP_RIGHT =false; Ball.DIR_DOWN_RIGHT =false; Ball.DIR_DOWN_LEFT =false; Ball.DIR_UP_LEFT =true; } } //BALL OUT OF BOUNDS if(Ball.xp >= 800) { result = system->playSound(mySound2, 0, false, 0); Player1Point++; Ball.xp = 400; Ball.yp = 300; Ball.DIR_UP_RIGHT =true; Ball.DIR_DOWN_RIGHT =false; Ball.DIR_DOWN_LEFT =false; Ball.DIR_UP_LEFT =false; } if(Ball.xp <= 0) { result = system->playSound(mySound2, 0, false, 0); Player2Point++; Ball.xp = 400; Ball.yp = 300; Ball.DIR_UP_RIGHT =false; Ball.DIR_DOWN_RIGHT =false; Ball.DIR_DOWN_LEFT =false; Ball.DIR_UP_LEFT =true; } //PADDLE COLLISION if(Ball.xp >= Paddle[1].xp - 30 && Ball.yp >= Paddle[1].yp - 60 && Ball.yp <= Paddle[1].yp + 60) { if(Ball.DIR_DOWN_RIGHT == true) { result = system->playSound(mySound1, 0, false, 0); Ball.DIR_UP_RIGHT =false; Ball.DIR_DOWN_RIGHT =false; Ball.DIR_DOWN_LEFT =true; Ball.DIR_UP_LEFT =false; } else if(Ball.DIR_UP_RIGHT == true) { result = system->playSound(mySound1, 0, false, 0); Ball.DIR_UP_RIGHT =false; Ball.DIR_DOWN_RIGHT =false; Ball.DIR_DOWN_LEFT =false; Ball.DIR_UP_LEFT =true; } } if(Ball.xp <= Paddle[0].xp + 30 && Ball.yp >= Paddle[0].yp - 60 && Ball.yp <= Paddle[0].yp + 60) { if(Ball.DIR_DOWN_LEFT == true) { result = system->playSound(mySound1, 0, false, 0); Ball.DIR_UP_RIGHT =false; Ball.DIR_DOWN_RIGHT =true; Ball.DIR_DOWN_LEFT =false; Ball.DIR_UP_LEFT =false; } else if(Ball.DIR_UP_LEFT == true) { result = system->playSound(mySound1, 0, false, 0); Ball.DIR_UP_RIGHT =true; Ball.DIR_DOWN_RIGHT =false; Ball.DIR_DOWN_LEFT =false; Ball.DIR_UP_LEFT =false; } } //BALL DIRECTION if(Ball.DIR_UP_RIGHT == true) { Ball.xp = Ball.xp + 0.03f; Ball.yp = Ball.yp - 0.05f; } if(Ball.DIR_DOWN_RIGHT == true) { Ball.xp = Ball.xp + 0.03f; Ball.yp = Ball.yp + 0.05f; } if(Ball.DIR_DOWN_LEFT == true) { Ball.xp = Ball.xp - 0.03f; Ball.yp = Ball.yp + 0.05f; } if(Ball.DIR_UP_LEFT == true) { Ball.xp = Ball.xp - 0.03f; Ball.yp = Ball.yp - 0.05f; } } D3DXMatrixRotationZ(&rotMat, D3DXToRadian(0)); D3DXMatrixTranslation(&transMat, Ball.xp, Ball.yp, 0.0f); D3DXMatrixScaling(&scaleMat, 1, 1, 0.0f); D3DXMatrixMultiply(&scaleMat, &scaleMat, &rotMat); D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat); // Set Transform for the object m_pD3DSprite m_pD3DSprite->SetTransform(&worldMat); m_pD3DSprite->Draw(m_ballTexture, 0, &D3DXVECTOR3(m_ballImage.Width * 0.5f, m_ballImage.Height * 0.5f, 0.0f), 0, D3DCOLOR_ARGB(255, 255, 255, 255)); // End drawing 2D sprites } m_pD3DSprite->End(); ////////////////////////////////////////////////////////////////////////// // Draw Text ////////////////////////////////////////////////////////////////////////// // Calculate RECT structure for text drawing placement, using whole screen if(Menu.onGAME == true) { rect.left = 10; rect.right = 10; rect.bottom = 10; rect.top = 10; wchar_t Player1Text[256]; swprintf(Player1Text, 256, L"Point(s): %i", Player1Point); m_pD3DFont->DrawText(0, Player1Text, -1, &rect, DT_TOP | DT_LEFT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255)); rect.left = 670; rect.right = 10; rect.bottom = 10; rect.top = 10; wchar_t Player2Text[256]; swprintf(Player2Text, 256, L"Point(s): %i", Player2Point); m_pD3DFont->DrawText(0, Player2Text, -1, &rect, DT_TOP | DT_LEFT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255)); // to be drawn outside of the rect specified to draw text in. // EndScene, and Present the back buffer to the display buffer } m_pD3DDevice->EndScene(); m_pD3DDevice->Present(NULL, NULL, NULL, NULL); //************************************************************************* //Sound System Update if(controlDown & SOUND1) { } if(controlDown & SOUND2) { } }
//----------------------------------------------------------------------------- // Name: SetupMatrices() // Desc: Sets up the world, view, and projection transform matrices. //----------------------------------------------------------------------------- VOID SetupMatrices() { // For our world matrix, we will just rotate the object about the y-axis. D3DXMATRIXA16 matWorld1, matWorld2, matWorld3, matWorld ; // Set up the rotation matrix to generate 1 full rotation (2*PI radians) // every 1000 ms. To avoid the loss of precision inherent in very high // floating point numbers, the system time is modulated by the rotation // period before conversion to a radian angle. UINT iTime = timeGetTime() % 1000; FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f; D3DXMatrixRotationX( &matWorld1, fAngle ); D3DXMatrixRotationY( &matWorld2, fAngle ); D3DXMatrixMultiply( &matWorld, &matWorld1, &matWorld2 ); D3DXMatrixTranslation(&matWorld3, iTime * (2.0f * D3DX_PI) / 5000.0f, 0, 0 ); D3DXMatrixMultiply( &matWorld, &matWorld, &matWorld3 ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); // Set up our view matrix. A view matrix can be defined given an eye point, // a point to lookat, and a direction for which way is up. Here, we set the // eye five units back along the z-axis and up three units, look at the // origin, and define "up" to be in the y-direction. D3DXVECTOR3 vEyePt( 0.0f, 0.0f,-3.0f ); D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f ); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); // For the projection matrix, we set up a perspective transform (which // transforms geometry from 3D view space to 2D viewport space, with // a perspective divide making objects smaller in the distance). To build // a perpsective transform, we need the field of view (1/4 pi is common), // the aspect ratio, and the near and far clipping planes (which define at // what distances geometry should be no longer be rendered). D3DXMATRIXA16 matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); }
HRESULT HelloShadowVolume::RestoreDeviceObjects() { HRESULT hr; IDirect3DDevice8* device; hr = m_spD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_dpps, &device); if (FAILED(hr)) { MessageBox(0, L"CreateDevice failed", 0, 0); return E_FAIL; } m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); }); m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); D3DVIEWPORT8 viewport = { 0, 0, m_iWidth, m_iHeight }; m_spDevice->SetViewport(&viewport); D3DXVECTOR3 eye(0.0f, 0.0f, 30.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&m_mtView, &eye, &target, &up); D3DXMatrixPerspectiveFovLH(&m_mtProj, 0.2*D3DX_PI, (float)m_iWidth / (float)m_iHeight, 1.0f, 100.f); m_cPlaneTint = { 0.7f, 0.6f, 0.4f, 1.0f }; ID3DXMesh* plane; //D3DXCreatePolygon(m_spDevice.get(), 2.0f, 4, &plane, NULL); CreatePlane(m_spDevice.get(), 15.0f, 10, &plane); //D3DXCreateSphere(m_spDevice.get(), 1.0f,20,20, &plane, NULL); IDirect3DVertexBuffer8* vb; IDirect3DIndexBuffer8* ib; plane->GetVertexBuffer(&vb); plane->GetIndexBuffer(&ib); m_spPlaneVB.reset(vb, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spPlaneIB.reset(ib, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwPlaneNumVertices = plane->GetNumVertices(); m_dwPlaneNumFaces = plane->GetNumFaces(); plane->Release(); DWORD decl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), D3DVSD_REG(3, D3DVSDT_FLOAT3), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"plane.vso", &m_dwPlaneVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"plane.pso", &m_dwPlanePSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } D3DXMATRIX Rx, Tz; D3DXMatrixRotationX(&Rx, D3DX_PI*0.5f); D3DXMatrixTranslation(&Tz, 0.0f, -3.0f, 0.0f); m_mtPlaneWorld = Rx * Tz; ID3DXMesh* occluder; CreateOccluder(m_spDevice.get(), &occluder); IDirect3DVertexBuffer8* vbOccluder; IDirect3DIndexBuffer8* ibOccluder; occluder->GetVertexBuffer(&vbOccluder); occluder->GetIndexBuffer(&ibOccluder); m_spOccluderVB.reset(vbOccluder, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spOccluderIB.reset(ibOccluder, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwOccluderNumVertices = occluder->GetNumVertices(); m_dwOccluderNumFaces = occluder->GetNumFaces(); occluder->Release(); hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"occluder.vso", &m_dwOccluderVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"occluder.pso", &m_dwOccluderPSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } m_cOccluderTint = { 0.3f, 0.0f, 0.8f, 1.0f }; D3DXMATRIX Rz, T; D3DXMatrixTranslation(&T, 5.1f * cosf(0.5), 0.0f, 5.1f * sinf(0.5)); D3DXMatrixIdentity(&m_mtVolumeWorld); D3DXMatrixRotationZ(&Rz, 0.5f); m_mtOccluderWorld = T * Rz; ID3DXMesh* volume; CreateVolume(m_spDevice.get(), &volume); IDirect3DVertexBuffer8* vbVolume; IDirect3DIndexBuffer8* ibVolume; volume->GetVertexBuffer(&vbVolume); volume->GetIndexBuffer(&ibVolume); m_spVolumeVB.reset(vbVolume, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spVolumeIB.reset(ibVolume, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwVolumeNumVertices = volume->GetNumVertices(); m_dwVolumeNumFaces = volume->GetNumFaces(); volume->Release(); hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"volume.vso", &m_dwVolumeVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"volume.pso", &m_dwVolumePSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } m_cVolumeTint = { 0.7f, 0.0f, 0.0f, 1.0f }; D3DXMATRIX Sx; D3DXMatrixIdentity(&m_mtVolumeWorld); D3DXMatrixScaling(&Sx, 6.0f, 1.0f, 1.0f); D3DXMatrixRotationZ(&Rz, 0.5f); m_mtVolumeWorld = Sx * Rz; return S_OK; }
void Map::LoadMap(char* fileName, ResourceManager& resMan) { ////////////////////////////////////////////////////////////////FILE I/O STUFF std::stringstream ss; std::ifstream file; char lineType; Terrain tempTerrain; D3DVECTOR pos, corner1, corner2, size; float rot; int zone; PrimStruct *tempPrim; PrimObj tempObj; sPoint tempSpawn; tempObj.mat = &mat; tempObj.Tex = resMan.loadTexture("uvtest.png",0,0,0,0,D3DFMT_UNKNOWN,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,D3DCOLOR_XRGB(255,0,255),0); file.open(fileName); if(file.is_open()) { while(!file.eof()) { lineType = ' '; file>>lineType; if(lineType == '#') { //comment line file.ignore(256,'\n'); } else if(lineType == 'f'||lineType == 'F') { //floor //get top left file>>size.x; file>>size.z; file>>size.y; //get bottom right file>>pos.x; file>>pos.y; file>>pos.z; //zone file>>zone; ss<<"Floor"<<size.x<<"x"<<size.y<<"x"<<size.z; tempPrim = resMan.loadPrim(ss.str().c_str(),size.y,size.x,size.z); D3DXMatrixIdentity(&tempObj.matrix); D3DXMatrixTranslation(&tempObj.matrix,pos.x,pos.y,pos.z); tempObj.primInfo = tempPrim; renderInfo.push_back(tempObj); tempTerrain.Init(pos,tempPrim,FLOOR); AddFloor(tempTerrain); floorZone.push_back(zone); file.ignore(); } else if(lineType == 'w' || lineType == 'W') { //walls //get top left file>>size.x; file>>size.z; file>>size.y; //get bottom right file>>pos.x; file>>pos.y; file>>pos.z; //zone file>>zone; ss<<"Wall"<<size.x<<"x"<<size.y<<"x"<<size.z; tempPrim = resMan.loadPrim(ss.str().c_str(),size.y,size.x,size.z); D3DXMatrixIdentity(&tempObj.matrix); D3DXMatrixTranslation(&tempObj.matrix,pos.x,pos.y,pos.z); tempObj.primInfo = tempPrim; renderInfo.push_back(tempObj); tempTerrain.Init(pos,tempPrim,WALL); AddWall(tempTerrain); wallZone.push_back(zone); file.ignore(); } else if(lineType == 's' || lineType == 'S'){
//**************渲染道具函数************************* void Props::RenderProps () { std ::list <PROPS >::iterator i; for(i=_allprops.begin ();i!=_allprops.end ();i++) { if(i->_isalive==true) { //设置世界变换 D3DXMATRIX position,scaling,V; D3DXMatrixScaling(&scaling,0.5f,0.3f,0.5f); D3DXMatrixTranslation(&position,i->_position.x ,i->_position.y ,i->_position.z); position=scaling*position; Camera.SetViewMatrix(&V); if(i->_id==1) { prop1->RanderBorad ( &V,position); } if(i->_id==2) { prop2->RanderBorad (&V,position); } if(i->_id==3) { prop3->RanderBorad(&V,position); } if(i->_id==4) { D3DXMatrixScaling(&scaling,1.0f,1.0f,1.0f); position=scaling*position; prop4->RanderBorad (&V,position); } switch (i->_id) { case 5: { prop5TimeExit +=0.1; if(prop5TimeLeft-(int)prop5TimeExit >0) { //指定时间出现的位置 RECT rect; rect.left=400,rect.top=50,rect.right=800,rect.bottom=600; //渲染时间 RenderTime(prop5TimeLeft-(int) prop5TimeExit,rect ); //渲染动态纹理 RenderDynaminc(prop5tex,position,V); } else //这里表示时间到了 没吃到那个道具 { isaddscore=false; //表示没有吃到那个道具 i->_isalive=false; //道具消失 i->_timeHappen=0; //道具出现时间置零 prop5TimeExit=0; //道具5时间绘制置零 } break; } case 6: { RenderDynaminc(prop6tex,position,V); break; } case 7: { if(prop7TimeLeft-prop7TimeExit>0) { RenderDynaminc (prop7tex,position,V); prop7TimeExit += 0.1; } else { prop7TimeExit =0; i->_isalive=false; i->_timeHappen=0; } break; } } } } }
void node::translate( float x, float y, float z){ D3DXMATRIX tmp; D3DXMatrixTranslation(&tmp, x, y, z); matModelView = tmp * matModelView; }
//----[ buildAnimatedBones ]------------------------------------------------- void AnimatedMeshRenderer::buildAnimatedBones( const D3DXMATRIX* root_transform, AnimatedMeshIndex animated_mesh, AnimatedMeshAnimationTrack* animation_track, D3DXMATRIX* bone_matrices) const { assert(animated_mesh < animated_meshes_.size()); assert(animation_track); assert(bone_matrices); const RenderableAnimatedMesh& renderable_animated_mesh = animated_meshes_.at(animated_mesh); AnimatedMeshAnimationTrackElement* internal_animation_track = reinterpret_cast<AnimatedMeshAnimationTrackElement*>(animation_track); RenderableAnimatedMesh::Frame* frames = renderable_animated_mesh.frames; size_t number_of_frames = renderable_animated_mesh.number_of_frames; RenderableAnimatedMesh::Bone* bones = renderable_animated_mesh.bones; size_t number_of_bones = renderable_animated_mesh.number_of_bones; // Build the frames using the data in the animation track D3DXMATRIXA16 matrix; for (size_t frame_index = 0; frame_index < number_of_frames; ++frame_index) { RenderableAnimatedMesh::Frame* frame = &frames[frame_index]; AnimatedMeshAnimationTrackElement* track_element = &internal_animation_track[frame_index]; AnimatedMeshAnimationTrackElement::FrameTransform* frame_transform = &track_element->frame_transform; // Build the transform matrix from the scale/rotate/translate settings //D3DXMatrixTransformation(&matrix, // NULL, // NULL, // frame_transform->scaling(), // NULL, // frame_transform->rotation(), // frame_transform->translation()); // This method of constructing the transform matrices is necessary because // it allows us to transpose the quaternion's matrix during the build. D3DXMATRIXA16 cumulative, builder; D3DXMatrixScaling(&cumulative, frame_transform->s[0], frame_transform->s[1], frame_transform->s[2]); D3DXQUATERNION quat; D3DXQuaternionNormalize(&quat, frame_transform->rotation()); D3DXMatrixRotationQuaternion(&builder, &quat); D3DXMatrixTranspose(&builder, &builder); D3DXMatrixMultiply(&cumulative, &cumulative, &builder); D3DXMatrixTranslation(&builder, frame_transform->t[0], frame_transform->t[1], frame_transform->t[2]); D3DXMatrixMultiply(&matrix, &cumulative, &builder); // Use the frame hierarchy to construct this frame's final transformation size_t parent_frame_index = frame->parent_frame_index; assert(!frame_index || (parent_frame_index < frame_index)); const D3DXMATRIX* parent_frame_matrix = frame_index == 0 ? root_transform : internal_animation_track[parent_frame_index] .frameMatrix(); D3DXMatrixMultiply(track_element->frameMatrix(), &matrix, parent_frame_matrix); } // Construct the bone transformations for this framte state for (size_t bone_index = 0; bone_index < number_of_bones; ++bone_index) { D3DXMATRIX* bone_matrix = bone_matrices + bone_index; const RenderableAnimatedMesh::Bone* bone = bones + bone_index; const D3DXMATRIXA16* frame_matrix = (internal_animation_track + bone->frame_index)->frameMatrix(); D3DXMatrixMultiply(bone_matrix, &bone->inverse_offset, frame_matrix); } }
// This is the function used to render a single frame void RenderFrame( void ) { CBUFFER cBuffer; cBuffer.LightVector = D3DXVECTOR4( 1.0f, 1.0f, 1.0f, 0.0f ); cBuffer.LightColor = D3DXCOLOR( 0.5f, 0.5f, 0.5f, 1.0f ); cBuffer.AmbientColor = D3DXCOLOR( 0.2f, 0.2f, 0.2f, 1.0f ); D3DXMATRIX matRotateY, matRotateZ, matView, matProjection, matOrtho, matScale, matTranslate[2]; D3DXMATRIX matFinal; static float Time = 0.0f; //Time += 0.0003f; //Time = 9.5f; Time = 0; D3DXMatrixTranslation( &matTranslate[0], moveX, moveY, moveZ ); D3DXMatrixScaling( &matScale, 120.0, 120.0, 0.0f ); // Create a world matrices D3DXMatrixRotationY( &matRotateY, Time ); D3DXMatrixRotationZ( &matRotateZ, -1.58f ); // Create a view matrix D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 3.0f, 5.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 // Create a projection matrix D3DXMatrixPerspectiveFovLH( &matProjection, ( FLOAT )D3DXToRadian( 45 ), // Field of view ( FLOAT )SCREEN_WIDTH / ( FLOAT )SCREEN_HEIGHT, // Aspect ratio 1.0f, // Near view-plane 100.0f ); // Far view-plane // Create an orthographic matrix D3DXMatrixOrthoLH( &matOrtho, ( FLOAT )SCREEN_WIDTH, // Set the width to the window's width ( FLOAT )SCREEN_HEIGHT, // Set the height to the window's height 1.0f, // The closest an object can be on the depth buffer is 0.0 100.0f ); // The farthest an object can be on the depth buffer is 1.0 D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc; // Clear the second depth stencil state before setting the parameters. ZeroMemory(&depthDisabledStencilDesc, sizeof(depthDisabledStencilDesc)); // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. The only difference is // that DepthEnable is set to false, all other parameters are the same as the other depth stencil state. depthDisabledStencilDesc.DepthEnable = false; depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; depthDisabledStencilDesc.StencilEnable = true; depthDisabledStencilDesc.StencilReadMask = 0xFF; depthDisabledStencilDesc.StencilWriteMask = 0xFF; depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Load the matrices into the constant buffer // cBuffer.Final = matScale * matTranslate[0] * matRotateY * matRotateZ * matView * matOrtho;//matProjection; // cBuffer.Rotation = matRotateY; cBuffer.Final = matTranslate[0] * matScale * matRotateY * matRotateZ * matView * matOrtho;//matProjection; cBuffer.Rotation = matRotateY * matRotateZ; // Set the various states devcon->RSSetState( pRS ); devcon->PSSetSamplers( 0, 1, &pSS ); devcon->OMSetBlendState( pBS, 0, 0xffffffff ); // Clear the back buffer to a deep blue devcon->ClearRenderTargetView( backbuffer, D3DXCOLOR( 0.0f,0.0f,0.0f,1.0f));//0.0f, 0.2f, 0.4f, 1.0f )); // Clear the depth buffer devcon->ClearDepthStencilView( zbuffer, D3D11_CLEAR_DEPTH, 1.0f, 0 ); // Select which vertex buffer to display UINT stride = sizeof( VERTEX ); UINT offset = 0; devcon->IASetVertexBuffers( 0, 1, &pVBuffer, &stride, &offset ); devcon->IASetIndexBuffer( pIBuffer, DXGI_FORMAT_R32_UINT, 0 ); // Select which primtive type we are using devcon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); // Draw Our sprites here // Draw the Paddle devcon->UpdateSubresource( pCBuffer, 0, 0, &cBuffer, 0, 0 ); devcon->PSSetShaderResources( 0, 1, &pTexture[0] ); devcon->DrawIndexed( 6, 0, 0 ); devcon->UpdateSubresource( pCBuffer, 0, 0, &cBuffer, 0, 0 ); devcon->PSSetShaderResources( 0, 1, &pTexture[1] ); devcon->DrawIndexed( 6, 0, 0 ); // Switch the back buffer and the front buffer swapchain->Present( 0, 0 ); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { D3DXMATRIX world, view, proj; D3DXMATRIX skyworld, viewproj; D3DXVECTOR3 eye(0, 0, -5); D3DXVECTOR3 look(0, 0, 0); D3DXVECTOR3 up(0, 1, 0); D3DXVECTOR2 orient = cameraangle.smooth(alpha); D3DXMatrixRotationYawPitchRoll(&view, orient.x, orient.y, 0); D3DXVec3TransformCoord(&eye, &eye, &view); D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, (float)screenwidth / (float)screenheight, 0.1f, 50); D3DXMatrixLookAtLH(&view, &eye, &look, &up); D3DXMatrixMultiply(&viewproj, &view, &proj); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); device->SetTransform(D3DTS_VIEW, &view); device->SetTransform(D3DTS_PROJECTION, &proj); for( int i = 0; i < NUM_DWARFS; ++i ) dwarfs[i].Update(elapsedtime, &dwarfmatrices[i]); effect->SetMatrix("matViewProj", &viewproj); if( SUCCEEDED(device->BeginScene()) ) { // render sky device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); D3DXMatrixScaling(&skyworld, 20, 20, 20); skyeffect->SetMatrix("matWorld", &skyworld); D3DXMatrixIdentity(&skyworld); skyeffect->SetMatrix("matWorldSky", &skyworld); skyeffect->SetMatrix("matViewProj", &viewproj); skyeffect->SetVector("eyePos", (D3DXVECTOR4*)&eye); skyeffect->Begin(0, 0); skyeffect->BeginPass(0); { device->SetTexture(0, skytex); skymesh->DrawSubset(0); } skyeffect->EndPass(); skyeffect->End(); device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); // render ground D3DXMatrixScaling(&world, 5, 0.1f, 5); world._42 = -0.05f; device->SetTransform(D3DTS_WORLD, &world); device->SetTexture(0, texture2); mesh->DrawSubset(0); // dwarfs for( int i = 0; i < NUM_DWARFS; ++i ) dwarfs[i].Draw(); // fire D3DXMatrixTranslation(&world, 0, 0.25f, 0); device->SetTransform(D3DTS_WORLD, &world); system1.Draw(world, view); // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
void RobotArmDemo::drawScene() { // Clear the backbuffer and depth buffer. HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0)); HR(gd3dDevice->BeginScene()); HR(mFX->SetValue(mhLight, &mLight, sizeof(DirLight))); HR(mFX->SetTechnique(mhTech)); UINT numPasses = 0; HR(mFX->Begin(&numPasses, 0)); HR(mFX->BeginPass(0)); // Build the world transforms for each bone, then render them. buildBoneWorldTransforms(); D3DXMATRIX T; D3DXMatrixTranslation(&T, -NUM_BONES, 0.0f, 0.0f); for(int i = 0; i < NUM_BONES; ++i) { // Append the transformation with a slight translation to better // center the skeleton at the center of the scene. mWorld = mBones[i].toWorldXForm * T; HR(mFX->SetMatrix(mhWorld, &mWorld)); HR(mFX->SetMatrix(mhWVP, &(mWorld*mView*mProj))); D3DXMATRIX worldInvTrans; //! ÄæÏò D3DXMatrixInverse(&worldInvTrans, 0, &mWorld); //! תÖà D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans); HR(mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans)); for(int j = 0; j < mMtrl.size(); ++j) { HR(mFX->SetValue(mhMtrl, &mMtrl[j], sizeof(Mtrl))); // If there is a texture, then use. if(mTex[j] != 0) { HR(mFX->SetTexture(mhTex, mTex[j])); } // But if not, then set a pure white texture. When the texture color // is multiplied by the color from lighting, it is like multiplying by // 1 and won't change the color from lighting. else { HR(mFX->SetTexture(mhTex, mWhiteTex)); } HR(mFX->CommitChanges()); HR(mBoneMesh->DrawSubset(j)); } } HR(mFX->EndPass()); HR(mFX->End()); mGfxStats->display(); HR(gd3dDevice->EndScene()); // Present the backbuffer. HR(gd3dDevice->Present(0, 0, 0, 0)); }
//============================================================================= // 描画処理 //============================================================================= void DrawEffect(LPDIRECT3DDEVICE9 pDevice) { //LPDIRECT3DDEVICE9 pDevice = GetDevice(); D3DXMATRIX mtxView,mtxScale,mtxTranslate; pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); // αソースカラーの指定 pDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE); // αデスティネーションカラーの指定 pDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); // Z比較なし pDevice->SetRenderState(D3DRS_LIGHTING, FALSE); for(int nCntEffect = 0; nCntEffect < MAX_EFFECT; nCntEffect++) { if(g_aEffect[nCntEffect].bUse) { // ワールドマトリックスの初期化 D3DXMatrixIdentity(&g_mtxWorldEffect); // ビューマトリックスを取得 mtxView = GetMtxView(); g_mtxWorldEffect._11 = mtxView._11; g_mtxWorldEffect._12 = mtxView._21; g_mtxWorldEffect._13 = mtxView._31; g_mtxWorldEffect._21 = mtxView._12; g_mtxWorldEffect._22 = mtxView._22; g_mtxWorldEffect._23 = mtxView._32; g_mtxWorldEffect._31 = mtxView._13; g_mtxWorldEffect._32 = mtxView._23; g_mtxWorldEffect._33 = mtxView._33; // スケールを反映 D3DXMatrixScaling(&mtxScale, g_aEffect[nCntEffect].scale.x, g_aEffect[nCntEffect].scale.y, g_aEffect[nCntEffect].scale.z); D3DXMatrixMultiply(&g_mtxWorldEffect, &g_mtxWorldEffect, &mtxScale); // 移動を反映 D3DXMatrixTranslation(&mtxTranslate, g_aEffect[nCntEffect].pos.x, g_aEffect[nCntEffect].pos.y, g_aEffect[nCntEffect].pos.z); D3DXMatrixMultiply(&g_mtxWorldEffect, &g_mtxWorldEffect, &mtxTranslate); // ワールドマトリックスの設定 pDevice->SetTransform(D3DTS_WORLD, &g_mtxWorldEffect); pDevice->SetRenderState(D3DRS_LIGHTING, FALSE); // 頂点バッファをデバイスのデータストリームにバインド pDevice->SetStreamSource(0, g_pD3DVtxBuffEffect, 0, sizeof(VERTEX_3D)); // 頂点フォーマットの設定 pDevice->SetFVF(FVF_VERTEX_3D); // テクスチャの設定 pDevice->SetTexture(0, g_pD3DTextureEffect); // ポリゴンの描画 pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, (nCntEffect * 4), NUM_POLYGON); } } pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); // αソースカラーの指定 pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); // αデスティネーションカラーの指定 pDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL); pDevice->SetRenderState(D3DRS_LIGHTING, TRUE); // Z比較あり }
void CHasu::Draw(D3DXMATRIX view, D3DXMATRIX proj) { D3DXMatrixTranslation(&m_matWorld, m_data.position.x, m_data.position.y, m_data.position.z); m_matWorld = matWorld2*m_matWorld; m_SkinModel.Draw(m_matWorld, view, proj, m_matRot); }
bool DirectGraphicsSprite::RenderSpriteScaledRotated(float x, float y, float width, float height, float Winkel, D3DCOLOR Color) { float l, r, o, u; // Vertice Koordinaten float tl, tr, to, tu; // Textur Koordinaten l = x; // Links r = x+width; // Rechts o = y; // Oben u = y+height; // Unten l -= 0.5f; r += 0.5f; o -= 0.5f; u += 0.5f; tl = itsRect.left /itsXSize; // Links tr = itsRect.right /itsXSize; // Rechts to = itsRect.top /itsYSize; // Oben tu = itsRect.bottom/itsYSize; // Unten TriangleStrip[0].color = TriangleStrip[1].color = TriangleStrip[2].color = TriangleStrip[3].color = Color; TriangleStrip[0].z = TriangleStrip[1].z = TriangleStrip[2].z = TriangleStrip[3].z = 0.0f; TriangleStrip[0].x = l; // Links oben TriangleStrip[0].y = o; TriangleStrip[0].tu = tl; TriangleStrip[0].tv = to; TriangleStrip[1].x = r; // Rechts oben TriangleStrip[1].y = o; TriangleStrip[1].tu = tr; TriangleStrip[1].tv = to; TriangleStrip[2].x = l; // Links unten TriangleStrip[2].y = u; TriangleStrip[2].tu = tl; TriangleStrip[2].tv = tu; TriangleStrip[3].x = r; // Rechts unten TriangleStrip[3].y = u; TriangleStrip[3].tu = tr; TriangleStrip[3].tv = tu; lpD3DDevice->SetTexture (0, itsTexture); // Textur setzen //----- Sprite rotieren D3DXMATRIX matRot, matTrans, matTrans2; // Winkel angleichen, damit er immer zwischen 0° und 360° bleibt if (Winkel > 360) Winkel -= 360; if (Winkel < 0) Winkel += 360; // Rotationsmatrix D3DXMatrixRotationZ (&matRot, float(PI * Winkel / 180)); // Transformation zum Ursprung D3DXMatrixTranslation(&matTrans,-x-(width) /2, -y-(height)/2, 0.0f); // Transformation wieder zurück D3DXMatrixTranslation(&matTrans2,x+(width) /2, y+(height)/2, 0.0f); // Verschieben und rotieren D3DXMatrixMultiply (&matWorld, &matTrans, &matRot); // und wieder zurück verschieben D3DXMatrixMultiply (&matWorld, &matWorld, &matTrans2); lpD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); DirectGraphics.SetFilterMode (true); // Sprite zeichnen DirectGraphics.RendertoBuffer (D3DPT_TRIANGLESTRIP, 2,&TriangleStrip[0]); DirectGraphics.SetFilterMode (false); // Normale Projektions-Matrix wieder herstellen D3DXMatrixRotationZ (&matWorld, 0.0f); lpD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); return true; }
void Camera2D::Update() { D3DXMatrixTranslation(&m_view, (int)-m_position.X, (int)-m_position.Y, m_position.Z); if (mCurrentlyShaking) { float currentTime = Timing::Instance()->GetTotalTimeSeconds(); float timeDiff = (mShakeStartTime + mCurrentShakeDuration) - currentTime; if (timeDiff <= 0.0f) { mCurrentlyShaking = false; } else { float shakePercentTime = timeDiff / mCurrentShakeDuration; bool minusX = (rand() % 2) == 1; bool minusY = (rand() % 2) == 1; GAME_ASSERT(mTargetObject); if (UpdateBoundsX(mTargetObject)) { if (minusX) { m_position.X += mCurrentShakeIntensity * shakePercentTime * Timing::Instance()->GetTimeModifier(); } else { m_position.X -= mCurrentShakeIntensity * shakePercentTime * Timing::Instance()->GetTimeModifier(); } } if (UpdateBoundsY(mTargetObject)) { if (minusY) { m_position.Y += mCurrentShakeIntensity * shakePercentTime * Timing::Instance()->GetTimeModifier(); } else { m_position.Y -= mCurrentShakeIntensity * shakePercentTime * Timing::Instance()->GetTimeModifier(); } } } } #if _DEBUG if (!UIManager::Instance()->IsObjectEditorDisplaying()) { int movespeed = 40; // test if (GetAsyncKeyState('D') < 0) { m_position.X += movespeed; if (Game::GetInstance()->GetIsLevelEditMode()) { list<shared_ptr<GameObject> > & gameObjects = GameObjectManager::Instance()->GetGameObjectList(); for (auto & obj : gameObjects) { if (obj->GetParallaxMultiplierX() != 1.0f || obj->GetParallaxMultiplierY() != 1.0f) { obj->Update(1.0f); } } } } else if (GetAsyncKeyState('A') < 0) { m_position.X -= movespeed; if (Game::GetInstance()->GetIsLevelEditMode()) { list<shared_ptr<GameObject> > & gameObjects = GameObjectManager::Instance()->GetGameObjectList(); for (auto & obj : gameObjects) { if (obj->GetParallaxMultiplierX() != 1.0f || obj->GetParallaxMultiplierY() != 1.0f) { obj->Update(1.0f); } } } } if (GetAsyncKeyState('W') < 0) { m_position.Y += movespeed; if (Game::GetInstance()->GetIsLevelEditMode()) { list<shared_ptr<GameObject> > & gameObjects = GameObjectManager::Instance()->GetGameObjectList(); for (auto & obj : gameObjects) { if (obj->GetParallaxMultiplierX() != 1.0f || obj->GetParallaxMultiplierY() != 1.0f) { obj->Update(1.0f); } } } } else if (GetAsyncKeyState('S') < 0 && GetAsyncKeyState(VK_CONTROL) >= 0) { m_position.Y -= movespeed; if (Game::GetInstance()->GetIsLevelEditMode()) { list<shared_ptr<GameObject> > & gameObjects = GameObjectManager::Instance()->GetGameObjectList(); for (auto & obj : gameObjects) { if (obj->GetParallaxMultiplierX() != 1.0f || obj->GetParallaxMultiplierY() != 1.0f) { obj->Update(1.0f); } } } } } #endif }
void GameState_FVF::Render() { D3DXMATRIXA16 world, rot, trans; D3DXMatrixIdentity(&world); D3DXMatrixRotationZ(&rot, D3DXToRadian(80.0f)); world *= rot; D3DXMatrixTranslation(&trans, 2, 0, 0); world *= trans; GameManager::GetDevice()->SetTransform(D3DTS_WORLD, &world); GameManager::GetDevice()->SetTexture(0, nullptr); //GameManager::GetDevice()->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE); FVF_PositionColor vertex[3]; vertex[0].pos = D3DXVECTOR3(0, 0, 0); vertex[0].color = D3DCOLOR_XRGB(255, 0, 0); vertex[1].pos = D3DXVECTOR3(0, 1, 0); vertex[1].color = D3DCOLOR_XRGB(0, 255, 0); vertex[2].pos = D3DXVECTOR3(1, 0, 0); vertex[2].color = D3DCOLOR_XRGB(0, 0, 255); GameManager::GetDevice()->SetFVF(FVF_PositionColor::FVF); GameManager::GetDevice()->DrawPrimitiveUP( D3DPT_TRIANGLELIST, 1, vertex, sizeof(FVF_PositionColor)); D3DXMatrixIdentity(&world); GameManager::GetDevice()->SetTransform(D3DTS_WORLD, &world); FVF_PositionColor lineVertex[4]; lineVertex[0].pos = D3DXVECTOR3(10, 0, 0); lineVertex[0].color = D3DCOLOR_XRGB(255, 0, 0); lineVertex[1].pos = D3DXVECTOR3(10, 10, 0); lineVertex[1].color = D3DCOLOR_XRGB(0, 255, 0); lineVertex[2].pos = D3DXVECTOR3(20, 10, 0); lineVertex[2].color = D3DCOLOR_XRGB(0, 0, 255); lineVertex[3].pos = D3DXVECTOR3(20, 0, 0); lineVertex[3].color = D3DCOLOR_XRGB(0, 0, 255); GameManager::GetDevice()->DrawPrimitiveUP( D3DPT_LINELIST, 2, lineVertex, sizeof(FVF_PositionColor)); FVF_PositionColorTexture vertexT[3]; vertexT[0].pos = D3DXVECTOR3(-2, 0, 0); vertexT[0].color = D3DCOLOR_XRGB(255, 0, 0); vertexT[0].tex = D3DXVECTOR2(0, 1); vertexT[1].pos = D3DXVECTOR3(-1, 1, 0); vertexT[1].color = D3DCOLOR_XRGB(0, 255, 0); vertexT[1].tex = D3DXVECTOR2(1, 0); vertexT[2].pos = D3DXVECTOR3(-1, 0, 0); vertexT[2].color = D3DCOLOR_XRGB(0, 0, 255); vertexT[2].tex = D3DXVECTOR2(1, 1); GameManager::GetDevice()->SetTexture(0, texture); GameManager::GetDevice()->SetFVF(FVF_PositionColorTexture::FVF); GameManager::GetDevice()->DrawPrimitiveUP( D3DPT_TRIANGLELIST, 1, vertexT, sizeof(FVF_PositionColorTexture)); }
//----------------------------------------------------------------------------- // Updates the player object. //----------------------------------------------------------------------------- void PlayerObject::Update( float elapsed, bool addVelocity ) { // Allow the base animated object to update. AnimatedObject::Update( elapsed, addVelocity ); // Override the object's forward vector to take the view tilt into account. // This will allow the forward vector to move up and down as well instead // of just remaining horizontal. This is not important for movement since // the player can not fly, but for things like shooting it is. m_forward.x = (float)sin( GetRotation().y ); m_forward.y = (float)-tan( m_viewTilt ); m_forward.z = (float)cos( GetRotation().y ); D3DXVec3Normalize( &m_forward, &m_forward ); // Set the player's view point. This is done every frame because as the // mesh is animated, the reference point in the mesh may move. This // will allow the view point to move with the mesh's animations. m_viewPoint = GetMesh()->GetReferencePoint( "rp_view_point" )->GetTranslation(); // Ensure that the view movement is relative to the rotation. D3DXVec3TransformCoord( &m_viewPoint, &m_viewPoint, GetRotationMatrix() ); // Only calculate the correct view matrix if it is being used. if( m_isViewing == true ) { // Create the x axis rotation matrix. D3DXMATRIX rotationXMatrix; D3DXMatrixRotationX( &rotationXMatrix, m_viewTilt ); // Create the combined rotation matrix (i.e. y axis rotation from the // scene object plus the x axis rotation from the player object). D3DXMATRIX combinedRotation; D3DXMatrixMultiply( &combinedRotation, &rotationXMatrix, GetRotationMatrix() ); // Build a translation matrix that represents the final view point. D3DXMATRIX viewPointTranslationMatrix; D3DXVECTOR3 finalViewPointTranslation = GetTranslation() + m_viewPoint; D3DXMatrixTranslation( &viewPointTranslationMatrix, finalViewPointTranslation.x, finalViewPointTranslation.y, finalViewPointTranslation.z ); // Override the object's view matrix using the combined rotation and // the position of the final view point translation. D3DXMatrixMultiply( &m_viewMatrix, &combinedRotation, &viewPointTranslationMatrix ); D3DXMatrixInverse( &m_viewMatrix, NULL, &m_viewMatrix ); } // Ignore the rest if the player is dying (or dead) if( m_dying == true ) return; // Drive and strafe the player accordingly. if( m_drive != 0.0f ) Drive( m_drive * 8000.0f * elapsed ); if( m_strafe != 0.0f ) Strafe( m_strafe * 4000.0f * elapsed ); // Update the step audio paths. m_leftStepAudioPath->SetPosition( GetTranslation() + GetMesh()->GetReferencePoint( "rp_left_foot" )->GetTranslation() ); m_leftStepAudioPath->SetVelocity( GetVelocity() ); m_rightStepAudioPath->SetPosition( GetTranslation() + GetMesh()->GetReferencePoint( "rp_right_foot" )->GetTranslation() ); m_rightStepAudioPath->SetVelocity( GetVelocity() ); // Check if the player is changing their weapon (local player only). static float move = 0.0f; if( m_changingWeapon > 0.0f ) { m_changingWeapon -= elapsed; if( m_changingWeapon > 1.0f ) { // Lower the old weapon. move -= 100.0f * elapsed; m_weapons[m_oldWeapon]->RaiseLowerWeapon( elapsed, this, move ); } else if( m_changingWeapon < 0.0f ) { // The new weapon is in place. m_changingWeapon = 0.0f; move = 0.0f; // Send a message to inform the other players the weapon is ready. PlayerWeaponChangeMsg pwcm; pwcm.msgid = MSGID_PLAYER_WEAPON_CHANGE; pwcm.dpnid = g_engine->GetNetwork()->GetLocalID(); pwcm.weapon = m_currentWeapon; g_engine->GetNetwork()->Send( &pwcm, sizeof( PlayerWeaponChangeMsg ), DPNID_ALL_PLAYERS_GROUP, DPNSEND_NOLOOPBACK ); } else { // Raise the new weapon. move += 100.0f * elapsed; m_weapons[m_currentWeapon]->RaiseLowerWeapon( elapsed, this, move ); } } else { // Update the player's current weapon, when not changing weapons. if( m_weaponChanging == false ) m_weapons[m_currentWeapon]->Update( elapsed, m_fire, this, m_viewPoint.y ); } }
bool ZEffectBillboardList::Draw() { if(!m_pVB) return false; if( size()==0 ) return true; BeginState(); HRESULT hr; DWORD dwRemainNum = (DWORD)size(); iterator itr=begin(); while(dwRemainNum) { if(m_dwBase >= EFFECTBASE_DISCARD_COUNT) m_dwBase = 0; // 갯수가 BILLBOARD_FLUSH_COUNT 를 넘어가면 BILLBOARD_FLUSH_COUNT 씩 찍는다 DWORD dwThisNum = min( dwRemainNum , BILLBOARD_FLUSH_COUNT ); // 버퍼의 크기를 넘어가면 개수를 줄여서 크기만큼만 찍는다 dwThisNum = min( dwThisNum , EFFECTBASE_DISCARD_COUNT - m_dwBase ); BYTE *pVertices; if( FAILED( hr = m_pVB->Lock( m_dwBase * sizeof(ZEFFECTCUSTOMVERTEX) * 4, dwThisNum * sizeof(ZEFFECTCUSTOMVERTEX) * 4, (VOID**)&pVertices, m_dwBase ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD ) ) ) { return false; } BYTE *pInd; if( FAILED( hr = m_pIB->Lock( m_dwBase * sizeof(WORD) * 6, dwThisNum * sizeof(WORD) * 6, (VOID**)&pInd, m_dwBase ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD ) ) ) { return false; } for(DWORD j=0;j<dwThisNum;j++) { ZEFFECTBILLBOARDITEM *p = (ZEFFECTBILLBOARDITEM*)*itr; // Transform rmatrix matTranslation; rmatrix matScaling; rmatrix matWorld; rvector dir = p->normal; rvector up=p->up; rvector right; if(IS_EQ(dir.z,1.f)) up=rvector(1,0,0); D3DXVec3Cross(&right, &up, &dir); D3DXVec3Normalize(&right, &right); D3DXVec3Cross(&up, &right, &dir); D3DXVec3Normalize(&up, &up); rmatrix mat; D3DXMatrixIdentity(&mat); mat._11=right.x;mat._12=right.y;mat._13=right.z; mat._21=up.x;mat._22=up.y;mat._23=up.z; mat._31=dir.x;mat._32=dir.y;mat._33=dir.z; rvector pos=p->position; // float fScale=p->fStartSize * p->fOpacity + p->fEndSize * (1.f - p->fOpacity); float fInt = min(1,max(0,(p->fLifeTime - p->fElapsedTime)/p->fLifeTime)); float fScale=p->fStartSize * fInt + p->fEndSize * (1.f - fInt); if( p->bUseTrainSmoke ) { float fRatio = GetLifeRatio(p); float fAddScale = (p->fEndSize - p->fStartSize) / p->fLifeTime; if(fRatio < 0.1f ) { fAddScale *= 0.001f; } else if(fRatio < 0.4) { fAddScale *= 0.02f; } else { fAddScale *= 0.05f; } p->fCurScale += fAddScale; if(p->fCurScale > p->fEndSize) p->fCurScale = p->fEndSize; fScale = p->fCurScale; } D3DXMatrixScaling(&matScaling,fScale*m_Scale.x,fScale*m_Scale.y,fScale*m_Scale.z); D3DXMatrixTranslation(&matTranslation, pos.x, pos.y, pos.z); D3DXMatrixMultiply(&matWorld, &matScaling, &mat); D3DXMatrixMultiply(&matWorld, &matWorld, &matTranslation); // D3DXMatrixMultiply(&matWorld, &mat, &matTranslation); DWORD color = ((DWORD)(p->fOpacity * 255))<<24 | p->dwColor; static ZEFFECTCUSTOMVERTEX v[] = { {-1, -1, 0, 0xFFFFFFFF, 1, 0}, {-1, 1, 0, 0xFFFFFFFF, 1, 1}, { 1, 1, 0, 0xFFFFFFFF, 0, 1}, { 1, -1, 0, 0xFFFFFFFF, 0, 0}, }; static rvector sv[4] = { rvector(-1,-1,0) , rvector(-1,1,0) , rvector(1,1,0) , rvector(1,-1,0) }; // 좋은코드는 아니지만 버텍스 카피를 줄이기위해 타입캐스팅했다. D3DXVec3TransformCoord((D3DXVECTOR3*)&v[0].x,sv+0,&matWorld); D3DXVec3TransformCoord((D3DXVECTOR3*)&v[1].x,sv+1,&matWorld); D3DXVec3TransformCoord((D3DXVECTOR3*)&v[2].x,sv+2,&matWorld); D3DXVec3TransformCoord((D3DXVECTOR3*)&v[3].x,sv+3,&matWorld); v[0].color=v[1].color=v[2].color=v[3].color=color; memcpy(pVertices,v,sizeof(ZEFFECTCUSTOMVERTEX)*4); pVertices+=sizeof(ZEFFECTCUSTOMVERTEX)*4; WORD inds[] = { 0,1,2,0,2,3 }; for(int k=0;k<6;k++) { inds[k]+=(m_dwBase+j)*4; } memcpy(pInd,inds,sizeof(inds)); pInd+=sizeof(inds); itr++; } m_pVB->Unlock(); m_pIB->Unlock(); if(FAILED( hr = RGetDevice()->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,m_dwBase*4,dwThisNum*4,m_dwBase*6,dwThisNum*2) )) return false; m_dwBase+=dwThisNum; dwRemainNum-=dwThisNum; } RGetDevice()->SetStreamSource( 0, NULL , 0,0 ); RGetDevice()->SetIndices(NULL); EndState(); return true; }
HRESULT CShadow::RenderShadows( LPDIRECT3DDEVICE8 pd3dDevice, D3DXVECTOR3 m_starPosition, BOOL bDrawOpaqueSubsets, BOOL bDrawAlphaSubsets) { static float nintyDegrees = 0.5*D3DX_PI; if (m_pCageApp->GetShadowMode()==1 || m_pCageApp->GetShadowMode()==2) { m_Position = m_starPosition; // now render any shadows for this star D3DXMATRIX matWorld; D3DXMATRIX trans_to_position,rot; // floor first D3DXMatrixTranslation( &trans_to_position, m_Position.x,m_pCageApp->GetShadowMinPosition()->y,m_Position.z ); D3DXMatrixMultiply(&matWorld,&matWorld1,&trans_to_position); pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); Render(pd3dDevice); if (m_pCageApp->GetShadowMode()==2) { // now left wall D3DXMatrixRotationZ( &rot, -nintyDegrees); D3DXMatrixTranslation( &trans_to_position, m_pCageApp->GetShadowMinPosition()->x,m_Position.y,m_Position.z ); D3DXMatrixMultiply(&matWorld,&matWorld1,&rot); D3DXMatrixMultiply(&matWorld,&matWorld,&trans_to_position); pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); Render(pd3dDevice); // now right wall D3DXMatrixRotationZ( &rot, nintyDegrees); D3DXMatrixTranslation( &trans_to_position, m_pCageApp->GetShadowMaxPosition()->x,m_Position.y,m_Position.z ); D3DXMatrixMultiply(&matWorld,&matWorld1,&rot); D3DXMatrixMultiply(&matWorld,&matWorld,&trans_to_position); pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); Render(pd3dDevice); } } return S_OK; }
void RenderMirror() { // // Draw Mirror quad to stencil buffer ONLY. In this way // only the stencil bits that correspond to the mirror will // be on. Therefore, the reflected teapot can only be rendered // where the stencil bits are turned on, and thus on the mirror // only. // Device->SetRenderState(D3DRS_STENCILENABLE, true); Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); Device->SetRenderState(D3DRS_STENCILREF, 0x1); Device->SetRenderState(D3DRS_STENCILMASK, 0xffffffff); Device->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff); Device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP); Device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP); Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); // disable writes to the depth and back buffers Device->SetRenderState(D3DRS_ZWRITEENABLE, false); Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true); Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); // draw the mirror to the stencil buffer Device->SetStreamSource(0, VB, 0, sizeof(Vertex)); Device->SetFVF(Vertex::FVF); Device->SetMaterial(&MirrorMtrl); Device->SetTexture(0, MirrorTex); D3DXMATRIX I; D3DXMatrixIdentity(&I); Device->SetTransform(D3DTS_WORLD, &I); Device->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2); // re-enable depth writes Device->SetRenderState( D3DRS_ZWRITEENABLE, true ); // only draw reflected teapot to the pixels where the mirror // was drawn to. Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL); Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP); // position reflection D3DXMATRIX W, T, R; D3DXPLANE plane(0.0f, 0.0f, 1.0f, 0.0f); // xy plane D3DXMatrixReflect(&R, &plane); D3DXMatrixTranslation(&T, TeapotPosition.x, TeapotPosition.y, TeapotPosition.z); W = T * R; // clear depth buffer and blend the reflected teapot with the mirror Device->Clear(0, 0, D3DCLEAR_ZBUFFER, 0, 1.0f, 0); Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR); Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO); // Finally, draw the reflected teapot Device->SetTransform(D3DTS_WORLD, &W); Device->SetMaterial(&TeapotMtrl); Device->SetTexture(0, 0); Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); Teapot->DrawSubset(0); // Restore render states. Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false); Device->SetRenderState( D3DRS_STENCILENABLE, false); Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); }
bool GraphicsClass::Render() { D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix, translateMatrix; D3DXMATRIX lightViewMatrix, lightProjectionMatrix; bool result; float posX, posY, posZ; // First render the scene to a texture. result = RenderSceneToTexture(); if(!result) { return false; } // Clear the buffers to begin the scene. m_D3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f); // Generate the view matrix based on the camera's position. m_Camera->Render(); // Generate the light view matrix based on the light's position. m_Light->GenerateViewMatrix(); // Get the world, view, and projection matrices from the camera and d3d objects. m_Camera->GetViewMatrix(viewMatrix); m_D3D->GetWorldMatrix(worldMatrix); m_D3D->GetProjectionMatrix(projectionMatrix); // Get the light's view and projection matrices from the light object. m_Light->GetViewMatrix(lightViewMatrix); m_Light->GetProjectionMatrix(lightProjectionMatrix); // Setup the translation matrix for the cube model. m_CubeModel->GetPosition(posX, posY, posZ); D3DXMatrixTranslation(&worldMatrix, posX, posY, posZ); // Put the cube model vertex and index buffers on the graphics pipeline to prepare them for drawing. m_CubeModel->Render(m_D3D->GetDevice()); // Render the model using the shadow shader. m_ShadowShader->Render(m_D3D->GetDevice(), m_CubeModel->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, m_CubeModel->GetTexture(), m_RenderTexture->GetShaderResourceView(), m_Light->GetPosition(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor()); // Reset the world matrix. m_D3D->GetWorldMatrix(worldMatrix); // Setup the translation matrix for the sphere model. m_SphereModel->GetPosition(posX, posY, posZ); D3DXMatrixTranslation(&worldMatrix, posX, posY, posZ); // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing. m_SphereModel->Render(m_D3D->GetDevice()); m_ShadowShader->Render(m_D3D->GetDevice(), m_SphereModel->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, m_SphereModel->GetTexture(), m_RenderTexture->GetShaderResourceView(), m_Light->GetPosition(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor()); // Reset the world matrix. m_D3D->GetWorldMatrix(worldMatrix); // Setup the translation matrix for the ground model. m_GroundModel->GetPosition(posX, posY, posZ); D3DXMatrixTranslation(&worldMatrix, posX, posY, posZ); // Render the ground model using the shadow shader. m_GroundModel->Render(m_D3D->GetDevice()); m_ShadowShader->Render(m_D3D->GetDevice(), m_GroundModel->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, m_GroundModel->GetTexture(), m_RenderTexture->GetShaderResourceView(), m_Light->GetPosition(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor()); // Present the rendered scene to the screen. m_D3D->EndScene(); return true; }
void GraphicCore::WorldMove(const D3DXVECTOR3& coor){ D3DXMATRIX matrix; D3DXMatrixTranslation(&matrix, coor.x, coor.y, coor.z); World = World * matrix; }
void AM_Model::translate(float x, float y, float z) { D3DXMatrixTranslation(&matTranslate, x, y, z); }