📄 graphics.cpp
字号:
Direct3DDevice->SetStreamSource(0, GroundVertexBuffer, 0, sizeof(GROUND_VERTEX));
Direct3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
}
//+-----------------------------------------------------------------------------
//| Renders a texture
//+-----------------------------------------------------------------------------
VOID GRAPHICS::RenderTexture(TEXTURE* Texture, RECT* SourceRect, INT X, INT Y, D3DCOLOR Color)
{
D3DXVECTOR3 Position;
if(Texture == NULL) return;
Position.x = static_cast<FLOAT>(X);
Position.y = static_cast<FLOAT>(Y);
Position.z = 0.0f;
D3DXMatrixIdentity(&WorldMatrix);
Direct3DDevice->SetTransform(D3DTS_WORLD, &WorldMatrix);
Sprite->SetTransform(&WorldMatrix);
Sprite->Begin(D3DXSPRITE_ALPHABLEND);
Sprite->Draw(Texture->GetTexture(), SourceRect, NULL, &Position, Color);
Sprite->End();
}
//+-----------------------------------------------------------------------------
//| Renders a texture
//+-----------------------------------------------------------------------------
VOID GRAPHICS::RenderTexture(TEXTURE* Texture, RECT* SourceRect, RECT* TargetRect, D3DCOLOR Color)
{
FLOAT Width;
FLOAT Height;
FLOAT ScaleX;
FLOAT ScaleY;
RECT TempRect;
D3DXVECTOR3 Position;
if(Texture == NULL) return;
if(TargetRect == NULL)
{
TempRect.left = 0;
TempRect.top = 0;
TempRect.right = ScreenWidth;
TempRect.bottom = ScreenHeight;
TargetRect = &TempRect;
}
Position.x = static_cast<FLOAT>(TargetRect->left);
Position.y = static_cast<FLOAT>(TargetRect->top);
Position.z = 0.0f;
Width = static_cast<FLOAT>(TargetRect->right - TargetRect->left);
Height = static_cast<FLOAT>(TargetRect->bottom - TargetRect->top);
ScaleX = (Width / Texture->GetWidth());
ScaleY = (Height / Texture->GetHeight());
D3DXMatrixScaling(&WorldMatrix, ScaleX, ScaleY, 1.0f);
Direct3DDevice->SetTransform(D3DTS_WORLD, &WorldMatrix);
Sprite->SetTransform(&WorldMatrix);
Sprite->Begin(D3DXSPRITE_ALPHABLEND);
Sprite->Draw(Texture->GetTexture(), SourceRect, NULL, &Position, Color);
Sprite->End();
}
//+-----------------------------------------------------------------------------
//| Renders some text
//+-----------------------------------------------------------------------------
VOID GRAPHICS::RenderText(CONST std::string& Text, INT X, INT Y, D3DCOLOR Color)
{
RECT Rect;
Rect.left = X;
Rect.top = Y;
Rect.right = ScreenWidth;
Rect.bottom = ScreenHeight;
D3DXMatrixIdentity(&WorldMatrix);
Direct3DDevice->SetTransform(D3DTS_WORLD, &WorldMatrix);
Sprite->SetTransform(&WorldMatrix);
Sprite->Begin(D3DXSPRITE_ALPHABLEND);
Font.GetFont()->DrawText(Sprite, Text.c_str(), static_cast<INT>(Text.size()), &Rect, DT_LEFT | DT_WORDBREAK, Color);
Sprite->End();
}
//+-----------------------------------------------------------------------------
//| Renders some text
//+-----------------------------------------------------------------------------
VOID GRAPHICS::RenderText(CONST std::string& Text, RECT* Rect, D3DCOLOR Color)
{
D3DXMatrixIdentity(&WorldMatrix);
Direct3DDevice->SetTransform(D3DTS_WORLD, &WorldMatrix);
Sprite->SetTransform(&WorldMatrix);
Sprite->Begin(D3DXSPRITE_ALPHABLEND);
Font.GetFont()->DrawText(Sprite, Text.c_str(), static_cast<INT>(Text.size()), Rect, DT_LEFT | DT_WORDBREAK, Color);
Sprite->End();
}
//+-----------------------------------------------------------------------------
//| Sets the shaders
//+-----------------------------------------------------------------------------
VOID GRAPHICS::SetShader()
{
BOOL EnableLights;
EnableLights = (UseShading && Properties().UseLighting);
Direct3DDevice->SetRenderState(D3DRS_CULLMODE, (UseCulling ? (Properties().ClockwiseCulling ? D3DCULL_CW : D3DCULL_CCW) : D3DCULL_NONE));
Direct3DDevice->SetRenderState(D3DRS_FILLMODE, Properties().FillMode);
if(Properties().UseShaders)
{
VertexShader.Use();
if(EnableLights) PixelShaderShaded.Use();
else PixelShaderUnshaded.Use();
}
else
{
Direct3DDevice->SetVertexShader(NULL);
Direct3DDevice->SetPixelShader(NULL);
Light.Ambient = D3DXCOLOR(Properties().Ambient);
Light.Diffuse = D3DXCOLOR(Properties().Diffuse);
Light.Specular = D3DXCOLOR(Properties().Specular);
Material.Power = Properties().Power;
Direct3DDevice->SetLight(0, &Light);
Direct3DDevice->SetMaterial(&Material);
Direct3DDevice->SetRenderState(D3DRS_SPECULARENABLE, EnableLights);
Direct3DDevice->SetRenderState(D3DRS_LIGHTING, EnableLights);
Direct3DDevice->LightEnable(0, EnableLights);
}
}
//+-----------------------------------------------------------------------------
//| Sets the shader constants
//+-----------------------------------------------------------------------------
VOID GRAPHICS::SetShaderConstants()
{
D3DXMATRIX ViewProjectionMatrix;
if(Properties().UseShaders)
{
D3DXMatrixMultiply(&ViewProjectionMatrix, &ViewMatrix, &ProjectionMatrix);
D3DXMatrixMultiply(&WorldViewProjectionMatrix, &WorldMatrix, &ViewProjectionMatrix);
SetConstantTableConstants(VertexShader.GetConstantTable());
SetConstantTableConstants(PixelShaderShaded.GetConstantTable());
SetConstantTableConstants(PixelShaderUnshaded.GetConstantTable());
}
}
//+-----------------------------------------------------------------------------
//| Prepares for line drawing
//+-----------------------------------------------------------------------------
VOID GRAPHICS::PrepareForLines()
{
Direct3DDevice->SetVertexShader(NULL);
Direct3DDevice->SetPixelShader(NULL);
Direct3DDevice->SetTexture(0, NULL);
Direct3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
Direct3DDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
Direct3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
Direct3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
Direct3DDevice->SetFVF(LINE_VERTEX::FORMAT);
}
//+-----------------------------------------------------------------------------
//| Prepares for view drawing
//+-----------------------------------------------------------------------------
VOID GRAPHICS::PrepareForViews()
{
Direct3DDevice->SetVertexShader(NULL);
Direct3DDevice->SetPixelShader(NULL);
Direct3DDevice->SetTexture(0, NULL);
Direct3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
Direct3DDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
Direct3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
Direct3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
Direct3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_BLENDFACTOR);
Direct3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
Direct3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
Direct3DDevice->SetRenderState(D3DRS_CLIPPING, FALSE);
}
//+-----------------------------------------------------------------------------
//| Sets a new world matrix
//+-----------------------------------------------------------------------------
VOID GRAPHICS::SetWorldMatrix(CONST D3DXMATRIX& NewWorldMatrix)
{
WorldMatrix = NewWorldMatrix;
Direct3DDevice->SetTransform(D3DTS_WORLD, &WorldMatrix);
}
//+-----------------------------------------------------------------------------
//| Sets a new view matrix
//+-----------------------------------------------------------------------------
VOID GRAPHICS::SetViewMatrix(CONST D3DXMATRIX& NewViewMatrix)
{
ViewMatrix = NewViewMatrix;
Direct3DDevice->SetTransform(D3DTS_VIEW, &ViewMatrix);
}
//+-----------------------------------------------------------------------------
//| Sets a new projection matrix
//+-----------------------------------------------------------------------------
VOID GRAPHICS::SetProjectionMatrix(CONST D3DXMATRIX& NewProjectionMatrix)
{
ProjectionMatrix = NewProjectionMatrix;
Direct3DDevice->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);
}
//+-----------------------------------------------------------------------------
//| Returns the world matrix
//+-----------------------------------------------------------------------------
CONST D3DXMATRIX& GRAPHICS::GetWorldMatrix() CONST
{
return WorldMatrix;
}
//+-----------------------------------------------------------------------------
//| Returns the view matrix
//+-----------------------------------------------------------------------------
CONST D3DXMATRIX& GRAPHICS::GetViewMatrix() CONST
{
return ViewMatrix;
}
//+-----------------------------------------------------------------------------
//| Returns the projection matrix
//+-----------------------------------------------------------------------------
CONST D3DXMATRIX& GRAPHICS::GetProjectionMatrix() CONST
{
return ProjectionMatrix;
}
//+-----------------------------------------------------------------------------
//| Builds a ray from a screen position
//+-----------------------------------------------------------------------------
VOID GRAPHICS::BuildRay(CONST POINT& ScreenPosition, INT Width, INT Height, D3DXVECTOR3& RayPosition, D3DXVECTOR3& RayDirection)
{
FLOAT TempScreenAspect;
D3DXVECTOR3 RayTarget;
D3DVIEWPORT9 ViewPort;
D3DXMATRIX IdentityMatrix;
D3DXMATRIX TempProjectionMatrix;
D3DXVECTOR3 TempScreenPosition;
TempScreenPosition.x = static_cast<FLOAT>(ScreenPosition.x);
TempScreenPosition.y = static_cast<FLOAT>(ScreenPosition.y);
D3DXMatrixIdentity(&IdentityMatrix);
TempScreenAspect = (Height == 0) ? 0.0f : (static_cast<FLOAT>(Width) / static_cast<FLOAT>(Height));
D3DXMatrixPerspectiveFovRH(&TempProjectionMatrix, FieldOfView, TempScreenAspect, NearDistance, FarDistance);
ViewPort.X = 0;
ViewPort.Y = 0;
ViewPort.Width = Width;
ViewPort.Height = Height;
ViewPort.MinZ = 0.0f;
ViewPort.MaxZ = 1.0f;
TempScreenPosition.z = 0.0f;
D3DXVec3Unproject(&RayPosition, &TempScreenPosition, &ViewPort, &TempProjectionMatrix, &ViewMatrix, &IdentityMatrix);
TempScreenPosition.z = 1.0f;
D3DXVec3Unproject(&RayTarget, &TempScreenPosition, &ViewPort, &TempProjectionMatrix, &ViewMatrix, &IdentityMatrix);
RayDirection = RayTarget - RayPosition;
D3DXVec3Normalize(&RayDirection, &RayDirection);
}
//+-----------------------------------------------------------------------------
//| Sets the constant table constants
//+-----------------------------------------------------------------------------
VOID GRAPHICS::SetConstantTableConstants(CONSTANT_TABLE& ConstantTable)
{
D3DXCOLOR TempColor;
TempColor = Properties().Ambient;
ConstantTable.SetVector4(SHADER_AMBIENT_COLOR, D3DXVECTOR4(TempColor.r, TempColor.g, TempColor.b, TempColor.a));
TempColor = Properties().Diffuse;
ConstantTable.SetVector4(SHADER_DIFFUSE_COLOR, D3DXVECTOR4(TempColor.r, TempColor.g, TempColor.b, TempColor.a));
TempColor = Properties().Specular;
ConstantTable.SetVector4(SHADER_SPECULAR_COLOR, D3DXVECTOR4(TempColor.r, TempColor.g, TempColor.b, TempColor.a));
ConstantTable.SetFloat(SHADER_SPECULAR_POWER, Properties().Power);
ConstantTable.SetMatrix(SHADER_WORLD_MATRIX, WorldMatrix);
ConstantTable.SetMatrix(SHADER_VIEW_MATRIX, ViewMatrix);
ConstantTable.SetMatrix(SHADER_PROJECTION_MATRIX, ProjectionMatrix);
ConstantTable.SetMatrix(SHADER_WORLD_VIEW_PROJECTION_MATRIX, WorldViewProjectionMatrix);
ConstantTable.SetVector3(SHADER_CAMERA_POSITION, CameraPosition);
ConstantTable.SetVector3(SHADER_CAMERA_DIRECTION, CameraDirection);
}
//+-----------------------------------------------------------------------------
//| Actions performed when the device is shut down
//+-----------------------------------------------------------------------------
BOOL GRAPHICS::OnShutdown()
{
REFERENCE<GRAPHICS_WINDOW*, GRAPHICS*>* CurrentReference;
REFERENCE<GRAPHICS_WINDOW*, GRAPHICS*>* NextReference;
REFERENCE<TEXTURE*, GRAPHICS*>* CurrentTexture;
REFERENCE<TEXTURE*, GRAPHICS*>* NextTexture;
CurrentReference = GraphicsWindowReferenceObject.GetFirstReference();
while(CurrentReference != NULL)
{
NextReference = GraphicsWindowReferenceObject.GetNextReference(CurrentReference);
CurrentReference->GetData()->Destroy();
CurrentReference = NextReference;
}
GraphicsWindowReferenceObject.Clear();
CurrentTexture = TextureReferenceObject.GetFirstReference();
while(CurrentTexture != NULL)
{
NextTexture = TextureReferenceObject.GetNextReference(CurrentTexture);
CurrentTexture->GetData()->Clear();
CurrentTexture = NextTexture;
}
TextureReferenceObject.Clear();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -