/***************************************************************
* TechniqueApplication.cpp *
* *
* This file contains the implementation of the *
* TechniqueApplication class. *
* To compile correctly, this file must be linked with: *
* kernel32.lib *
* user32.lib *
* d3dx8dt.lib *
* d3d8.lib *
* *
***************************************************************/
#include "TechniqueApplication.h"
#define TEX_DIMENSION 1024
#define D3DFVF_MESHVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE)
#define D3DFVF_TESTVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1)
struct MESH_VERTEX
{
float x, y, z;
float nx, ny, nz;
DWORD color;
};
struct TEST_VERTEX
{
float x, y, z, rhw;
float u, v;
};
CTechniqueApplication::CTechniqueApplication()
{
m_pPlaneVertexBuffer = NULL;
m_pMeshVertexBuffer = NULL;
m_pTestVertexBuffer = NULL;
m_pMeshIndexBuffer = NULL;
m_pMesh = NULL;
m_pMeshMaterials = NULL;
m_pShadowTexture = NULL;
m_pShadowTextureSurface = NULL;
m_pShadowZSurface = NULL;
m_pBackBuffer = NULL;
m_pZBuffer = NULL;
m_NumMaterials = 0;
m_CompareShader = 0;
m_DistanceShader = 0;
}
CTechniqueApplication::~CTechniqueApplication()
{
}
BOOL CTechniqueApplication::PostInitialize()
{
D3DCAPS8 Caps;
m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &Caps);
if (Caps.VertexShaderVersion == D3DVS_VERSION(1,1) &&
Caps.PixelShaderVersion == D3DPS_VERSION(1,1))
{
if (FAILED(EasyCreateWindowed(m_hWnd, D3DDEVTYPE_HAL,
D3DCREATE_HARDWARE_VERTEXPROCESSING)))
return FALSE;
}
else
{
if (FAILED(EasyCreateWindowed(m_hWnd, D3DDEVTYPE_REF,
D3DCREATE_SOFTWARE_VERTEXPROCESSING)))
return FALSE;
}
//Do the basic camera positioning, etc.
SetupDevice();
//Load the mesh object
LoadMesh();
//Create the buffers we're actually going to use
ExtractBuffers();
if (FAILED(CreatePlaneBuffers()))
return FALSE;
//Create the shader
if (FAILED(CreateShaders()))
return FALSE;
return TRUE;
}
void CTechniqueApplication::Render()
{
//The light is moving side to side over the model.
float Time = (float)GetTickCount() / 2000.0f;
//These are reusable matrices
D3DXMATRIX LightViewMatrix;
D3DXMATRIX TextureMatrix;
//Create the Texture Matrix
float Offset = 0.5f + (0.5f / (float)TEX_DIMENSION);
ZeroMemory(&TextureMatrix, sizeof(D3DXMATRIX));
TextureMatrix._11 = 0.5f;
TextureMatrix._22 = -0.5f;
TextureMatrix._33 = 1.0f;
TextureMatrix._41 = Offset;
TextureMatrix._42 = Offset;
TextureMatrix._44 = 1.0f;
//Set the view parameters
D3DXVECTOR4 EyePos(75.0, 75.0f, -75.0f, 0.0f);
D3DXVECTOR4 LightPos(-25.0f * sin(Time) - 50.0f, 90.0f, 0.0f, 1.0f);
//Set things up for the light
D3DXMatrixLookAtLH(&LightViewMatrix, &(D3DXVECTOR3)LightPos,
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
//Assume the light is pointed toward the origin.
D3DXVECTOR4 LightDir = D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f) - LightPos;
D3DXVec4Normalize(&LightDir, &LightDir);
//Set the ambient light value.
D3DXVECTOR4 Ambient (0.1f, 0.1f, 0.1f, 0.0f);
m_pD3DDevice->SetVertexShaderConstant(6, &Ambient, 1);
//For this sample only - there is now world matrix transformation,
//so there is no need to transform to object space.
m_pD3DDevice->SetVertexShaderConstant(5, &LightDir, 1);
//Set things up for the viewer
D3DXMatrixLookAtLH(&m_ViewMatrix, &(D3DXVECTOR3)EyePos,
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
D3DXMATRIX LightShaderMatrix = m_WorldMatrix *
LightViewMatrix *
m_ProjectionMatrix;
D3DXMATRIX EyeShaderMatrix = m_WorldMatrix *
m_ViewMatrix *
m_ProjectionMatrix;
D3DXMATRIX TextureShaderMatrix = m_WorldMatrix * LightViewMatrix *
m_ProjectionMatrix * TextureMatrix;
D3DXMatrixTranspose(&LightShaderMatrix, &LightShaderMatrix);
D3DXMatrixTranspose(&EyeShaderMatrix, &EyeShaderMatrix);
D3DXMatrixTranspose(&TextureShaderMatrix, &TextureShaderMatrix);
//Set the global distance scaling constants
D3DXVECTOR4 DistanceScalers(100.0f, 0.025f, 0.0f, 1.0f);
m_pD3DDevice->SetVertexShaderConstant(4, &DistanceScalers, 1);
//The texture should be null for all distance operations
m_pD3DDevice->SetTexture(0, NULL);
//First, render the light's point of view into the render target.
m_pD3DDevice->SetVertexShader(m_DistanceShader);
D3DVIEWPORT8 NormalViewport;
m_pD3DDevice->GetViewport(&NormalViewport);
//Set up the renderTarget
m_pD3DDevice->SetRenderTarget(m_pShadowTextureSurface, m_pShadowZSurface);
D3DVIEWPORT8 ShadowViewport;
ShadowViewport.X = 0;
ShadowViewport.Y = 0;
ShadowViewport.Width = TEX_DIMENSION;
ShadowViewport.Height = TEX_DIMENSION;
ShadowViewport.MinZ = 0.0f;
ShadowViewport.MaxZ = 1.0f;
m_pD3DDevice->SetViewport(&ShadowViewport);
m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);
//Draw the plane and the mesh using the light view matrix
m_pD3DDevice->SetVertexShaderConstant(0, &LightShaderMatrix, 4);
RenderPlane();
//Draw the mesh
m_pD3DDevice->SetStreamSource(0, m_pMeshVertexBuffer, sizeof(MESH_VERTEX));
m_pD3DDevice->SetIndices(m_pMeshIndexBuffer, 0);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0,
m_pMesh->GetNumVertices(), 0,
m_pMesh->GetNumFaces());
//Set the renderTarget back to the normal back buffer
m_pD3DDevice->SetRenderTarget(m_pBackBuffer, m_pZBuffer);
m_pD3DDevice->SetViewport(&NormalViewport);
//Use the compare shader
m_pD3DDevice->SetVertexShader(m_CompareShader);
//Set the pixel shader
m_pD3DDevice->SetPixelShader(m_PixelShader);
//Set the texture states to clamp the coordinates
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
//Render everything from the eye's point of view - use the first
//rendering as a texture.
m_pD3DDevice->SetTexture(0, m_pShadowTexture);
m_pD3DDevice->SetVertexShaderConstant(0, &EyeShaderMatrix, 4);
m_pD3DDevice->SetVertexShaderConstant(10, &LightShaderMatrix, 4);
m_pD3DDevice->SetVertexShaderConstant(20, &TextureShaderMatrix, 4);
RenderPlane();
m_pD3DDevice->SetStreamSource(0, m_pMeshVertexBuffer, sizeof(MESH_VERTEX));
m_pD3DDevice->SetIndices(m_pMeshIndexBuffer, 0);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0,
m_pMesh->GetNumVertices(), 0,
m_pMesh->GetNumFaces());
//Turn off the pixel shader
m_pD3DDevice->SetPixelShader(NULL);
//Draw a small rectangle showing the light's view (for debugging)..
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
m_pD3DDevice->SetVertexShader(D3DFVF_TESTVERTEX);
m_pD3DDevice->SetStreamSource(0, m_pTestVertexBuffer, sizeof(TEST_VERTEX));
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
}
HRESULT CTechniqueApplication::LoadMesh()
{
LPD3DXBUFFE
没有合适的资源?快使用搜索试试~ 我知道了~
DirectX实时渲染技术详解源码
共673个文件
cpp:158个
h:158个
opt:42个
5星 · 超过95%的资源 需积分: 9 16 下载量 80 浏览量
2012-08-19
11:20:59
上传
评论 2
收藏 16.31MB RAR 举报
温馨提示
DirectX实时渲染技术详解源码 Real-Time Rendering Tricks and Techniques in DirectX 书籍下载地址http://www.verycd.com/topics/2902098/
资源推荐
资源详情
资源评论
收起资源包目录
DirectX实时渲染技术详解源码 (673个子文件)
BikeRace.avi 4.59MB
Front.bmp 48KB
spotlight_Attenuation.bmp 48KB
pointlight_Attenuation.bmp 48KB
spotlight.bmp 48KB
Back.bmp 48KB
spotlight_Attenuation2.bmp 48KB
TileSmudge.bmp 48KB
pointlight.bmp 48KB
crosshair_alpha.bmp 48KB
grain.bmp 48KB
BumpHeight.bmp 48KB
Tile.bmp 48KB
Bump.bmp 48KB
crosshair.bmp 48KB
CubeNegY.bmp 17KB
CubePosY.bmp 17KB
Front_Alpha.bmp 17KB
CubeNegZ.bmp 17KB
CubePosX.bmp 17KB
CubeNegX.bmp 17KB
CubePosZ.bmp 17KB
Light.bmp 12KB
Big_Checker.bmp 3KB
Big_Checker_Test.bmp 3KB
ColorCurve.bmp 824B
testgrade1.bmp 248B
testgrade2.bmp 248B
Toon.bmp 104B
TechniqueApplication.cpp 19KB
TechniqueApplication.cpp 18KB
TechniqueApplication.cpp 16KB
TechniqueApplication.cpp 16KB
TechniqueApplication.cpp 15KB
GeometryApplication.cpp 15KB
TechniqueApplication.cpp 14KB
TechniqueApplication.cpp 14KB
LightingApplication.cpp 14KB
TechniqueApplication.cpp 13KB
TechniqueApplication.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
DirectXFont.cpp 13KB
TechniqueApplication.cpp 13KB
TechniqueApplication.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
Application.cpp 12KB
共 673 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- 88202013-10-31这本书很好, 很难得!
- MrNotLonely2014-12-24这书不错,对学习DirectX很有帮助
Linxyou
- 粉丝: 2
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功