//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: stencilmirrorshadow.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Demonstrates mirrors and shadows with stencils. Use the arrow keys
// and the 'A' and 'S' key to navigate the scene and translate the teapot.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#include "d3dUtility.h"
//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
IDirect3DVertexBuffer9* VB = 0;
IDirect3DTexture9* FloorTex = 0;
IDirect3DTexture9* WallTex = 0;
IDirect3DTexture9* MirrorTex = 0;
D3DMATERIAL9 FloorMtrl = d3d::WHITE_MTRL;
D3DMATERIAL9 WallMtrl = d3d::WHITE_MTRL;
D3DMATERIAL9 MirrorMtrl = d3d::WHITE_MTRL;
ID3DXMesh* Teapot = 0;
D3DXVECTOR3 TeapotPosition(0.0f, 3.0f, -7.5f);
D3DMATERIAL9 TeapotMtrl = d3d::YELLOW_MTRL;
void RenderScene();
void RenderMirror();
void RenderShadow();
//
// Classes and Structures
//
struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z,
float nx, float ny, float nz,
float u, float v)
{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}
float _x, _y, _z;
float _nx, _ny, _nz;
float _u, _v;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
//
// Framework Functions
//
bool Setup()
{
//
// Make walls have low specular reflectance - 20%.
//
WallMtrl.Specular = d3d::WHITE * 0.2f;
//
// Create the teapot.
//
D3DXCreateTeapot(Device, &Teapot, 0);
//
// Create and specify geometry. For this sample we draw a floor
// and a wall with a mirror on it. We put the floor, wall, and
// mirror geometry in one vertex buffer.
//
// |----|----|----|
// |Wall|Mirr|Wall|
// | | or | |
// /--------------/
// / Floor /
// /--------------/
//
Device->CreateVertexBuffer(
24 * sizeof(Vertex),
0, // usage
Vertex::FVF,
D3DPOOL_MANAGED,
&VB,
0);
Vertex* v = 0;
VB->Lock(0, 0, (void**)&v, 0);
// floor
v[0] = Vertex(-7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
v[1] = Vertex(-7.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
v[2] = Vertex( 7.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
v[3] = Vertex(-7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
v[4] = Vertex( 7.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
v[5] = Vertex( 7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
// wall
v[6] = Vertex(-7.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[7] = Vertex(-7.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[8] = Vertex(-2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[9] = Vertex(-7.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[10] = Vertex(-2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[11] = Vertex(-2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
// Note: We leave gap in middle of walls for mirror
v[12] = Vertex(2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[13] = Vertex(2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[14] = Vertex(7.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[15] = Vertex(2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[16] = Vertex(7.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[17] = Vertex(7.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
// mirror
v[18] = Vertex(-2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[19] = Vertex(-2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[20] = Vertex( 2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[21] = Vertex(-2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[22] = Vertex( 2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[23] = Vertex( 2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
VB->Unlock();
//
// Load Textures, set filters.
//
D3DXCreateTextureFromFile(Device, "checker.jpg", &FloorTex);
D3DXCreateTextureFromFile(Device, "brick0.jpg", &WallTex);
D3DXCreateTextureFromFile(Device, "ice.bmp", &MirrorTex);
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
//
// Lights.
//
D3DXVECTOR3 lightDir(0.707f, -0.707f, 0.707f);
D3DXCOLOR color(1.0f, 1.0f, 1.0f, 1.0f);
D3DLIGHT9 light = d3d::InitDirectionalLight(&lightDir, &color);
Device->SetLight(0, &light);
Device->LightEnable(0, true);
Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
Device->SetRenderState(D3DRS_SPECULARENABLE, true);
//
// Set Camera.
//
D3DXVECTOR3 pos(-10.0f, 3.0f, -15.0f);
D3DXVECTOR3 target(0.0, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &pos, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
//
// Set projection matrix.
//
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI / 4.0f, // 45 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(VB);
d3d::Release<IDirect3DTexture9*>(FloorTex);
d3d::Release<IDirect3DTexture9*>(WallTex);
d3d::Release<IDirect3DTexture9*>(MirrorTex);
d3d::Release<ID3DXMesh*>(Teapot);
}
bool Display(float timeDelta)
{
if( Device )
{
//
// Update the scene:
//
static float radius = 20.0f;
if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
TeapotPosition.x -= 3.0f * timeDelta;
if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
TeapotPosition.x += 3.0f * timeDelta;
if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
radius -= 2.0f * timeDelta;
if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
radius += 2.0f * timeDelta;
static float angle = (3.0f * D3DX_PI) / 2.0f;
if( ::GetAsyncKeyState('A') & 0x8000f )
angle -= 0.5f * timeDelta;
if( ::GetAsyncKeyState('S') & 0x8000f )
angle += 0.5f * timeDelta;
D3DXVECTOR3 position( cosf(angle) * radius, 3.0f, sinf(angle) * radius );
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);
//
// Draw the scene:
//
Device->Clear(0, 0,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,
0xff000000, 1.0f, 0L);
Device->BeginScene();
RenderScene();
// draw shadow before mirror because the depth buffer hasn't been cleared
// yet and we need the depth buffer so that the shadow is blended correctly.
// That is, if an object obscures the shadow, we don't want to write the shadow
// pixel. Alternatively, we could redraw the scene to rewrite the depth buffer.
// (RenderMirror clears the depth buffer).
RenderShadow();
RenderMirror();
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
void RenderScene()
{
// draw teapot
Device->SetMaterial(&TeapotMtrl);
Device->SetTexture(0, 0);
D3DXMATRIX W;
D3DXMatrixTranslation(&W,
TeapotPosition.x,
TeapotPosition.y,
TeapotPosition.z);
Device->SetTransform(D3DTS_WORLD, &W);
Teapot->DrawSubset(0);
D3DXMATRIX I;
D3DXMatrixIdentity(&I);
Device->SetTransform(D3DTS_WORLD, &I);
Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
Device->SetFVF(Vertex::FVF);
// draw the floor
Device->SetMaterial(&FloorMtrl);
Device->SetTexture(0, FloorTex);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// draw the walls
Device->SetMaterial(&WallMtrl);
Device->SetTexture(0, WallTex);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 6, 4);
// draw
没有合适的资源?快使用搜索试试~ 我知道了~
Stencil-Mirror-Shadow.rar_Mirror Mirror
共8个文件
jpg:2个
cpp:2个
vcproj:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 185 浏览量
2022-09-14
19:55:15
上传
评论
收藏 328KB RAR 举报
温馨提示
directx9.0利用模板绘制镜子反射,和墙壁不反射
资源推荐
资源详情
资源评论
收起资源包目录
Stencil-Mirror-Shadow.rar (8个子文件)
Stencil Mirror Shadow
stencilmirrorshadow.cpp 13KB
brick0.jpg 5KB
Stencil Mirror Shadow.sln 931B
Stencil Mirror Shadow.vcproj 4KB
checker.jpg 14KB
d3dUtility.cpp 6KB
d3dUtility.h 3KB
ice.bmp 768KB
共 8 条
- 1
资源评论
alvarocfc
- 粉丝: 108
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功