📄 litpyramid.cpp
字号:
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: litPyramid.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Renders a lit pyramid. Demonstrates how to specify the vertex
// normals, how to create and set a material, and how to create
// and set a directional light.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#include "d3dUtility.h"
//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
//D3DMATERIAL9 mtr1;
//D3DMATERIAL9 mtr2;
IDirect3DVertexBuffer9* Quad = 0;
IDirect3DTexture9* Tex = 0;
//D3DMATERIAL9 mtr3;
//D3DMATERIAL9 mtr4;
IDirect3DVertexBuffer9* Pyramid = 0;
//
// 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; // texture coordinates
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
//
// Framework Functions
//
bool Setup()
{
//
// Turn on lighting.
//
Device->SetRenderState(D3DRS_LIGHTING, true);
//
// Create the vertex buffer for the pyramid.
//
Device->CreateVertexBuffer(
12* sizeof(Vertex),
D3DUSAGE_WRITEONLY,
Vertex::FVF,
D3DPOOL_MANAGED,
&Quad,
0);
Vertex* v;
Quad->Lock(0, 0, (void**)&v, 0);
// front face
v[0] = Vertex(-1.0f, 0.0f, -1.0f, 0.0f, 0.707f, -0.707f,0.0f,1.0f);
v[1] = Vertex( 0.0f, 1.0f, 0.0f, 0.0f, 0.707f, -0.707f,0.0f,0.0f);
v[2] = Vertex( 1.0f, 0.0f, -1.0f, 0.0f, 0.707f, -0.707f,1.0f,0.0f);
// left face
v[3] = Vertex(-1.0f, 0.0f, 1.0f, -0.707f, 0.707f, 0.0f,0.0f,1.0f);
v[4] = Vertex( 0.0f, 1.0f, 0.0f, -0.707f, 0.707f, 0.0f,1.0f,0.0f);
v[5] = Vertex(-1.0f, 0.0f, -1.0f, -0.707f, 0.707f, 0.0f,1.0f,1.0f);
// right face
v[6] = Vertex( 1.0f, 0.0f, -1.0f, 0.707f, 0.707f, 0.0f,0.0f,1.0f);
v[7] = Vertex( 0.0f, 1.0f, 0.0f, 0.707f, 0.707f, 0.0f,0.0f,0.0f);
v[8] = Vertex( 1.0f, 0.0f, 1.0f, 0.707f, 0.707f, 0.0f,1.0f,0.0f);
// back face
v[9] = Vertex( 1.0f, 0.0f, 1.0f, 0.0f, 0.707f, 0.707f,0.0f,1.0f);
v[10] = Vertex( 0.0f, 1.0f, 0.0f, 0.0f, 0.707f, 0.707f,1.0f,0.0f);
v[11] = Vertex(-1.0f, 0.0f, 1.0f, 0.0f, 0.707f, 0.707f,1.0f,1.0f);
/* v[0] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[1] = Vertex(-1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[2] = Vertex( 1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[3] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[4] = Vertex( 1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[5] = Vertex( 1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);*/
Quad->Unlock();
//
// Create the texture and set filters.
//
D3DXCreateTextureFromFile(
Device,
"dx5_logo.bmp",
&Tex);
/* Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
// Device->SetTexture(0, Tex);
//
// Create and set the material.
//
/* D3DMATERIAL9 mtrl;
mtrl.Ambient = d3d::RED;
mtrl.Diffuse = d3d::WHITE;
mtrl.Specular = d3d::RED;
mtrl.Emissive = d3d::WHITE;
mtrl.Power = 5.0f;
Device->SetMaterial(&mtrl);
mtr1.Ambient = d3d::BLUE;
mtr1.Diffuse = d3d::BLUE;
mtr1.Specular = d3d::WHITE;
mtr1.Emissive = d3d::BLACK;
mtr1.Power = 6.0f;
Device->SetMaterial(&mtr1);
mtr2.Ambient = d3d::WHITE;
mtr2.Diffuse = d3d::BLUE;
mtr2.Specular = d3d::WHITE;
mtr2.Emissive = d3d::BLACK;
mtr2.Power = 5.0f;
Device->SetMaterial(&mtr2);*/
//
// Setup a directional light.
//
/* D3DLIGHT9 dir;
::ZeroMemory(&dir, sizeof(dir));
dir.Type = D3DLIGHT_DIRECTIONAL;
dir.Diffuse = d3d::WHITE;
dir.Specular = d3d::WHITE * 0.3f;
dir.Ambient = d3d::WHITE * 0.6f;
dir.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f);*/
//
// Set and Enable the light.
//
// Device->SetLight(0, &dir);
// Device->LightEnable(0, true);
/* D3DLIGHT9 dir1;
::ZeroMemory(&dir1, sizeof(dir1));
dir1.Type = D3DLIGHT_DIRECTIONAL;
dir1.Diffuse = d3d::RED;
dir1.Specular = d3d::RED * 0.8f;
dir1.Ambient = d3d::RED * 0.9f;
dir1.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f);*/
//
// Set and Enable the light.
//
// Device->SetLight(1, &dir1);
// Device->LightEnable(0, true);
//
// Turn on specular lighting and instruct Direct3D
// to renormalize normals.
//
// Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
// Device->SetRenderState(D3DRS_SPECULARENABLE, true);
//
// Position and aim the camera.
//
D3DXVECTOR3 pos(0.0f, 1.0f, -3.0f);
D3DXVECTOR3 target(0.0f, 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 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);
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(Pyramid);
}
bool Display(float timeDelta)
{
if( Device )
{
//
// Update the scene: Rotate the pyramid.
//
D3DXMATRIX yRot;
static float y = 0.0f;
D3DXMatrixRotationY(&yRot, y);
y += timeDelta;
if( y >= 6.28f )
y = 0.0f;
Device->SetTransform(D3DTS_WORLD, &yRot);
//
// Draw the scene:
//
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
Device->BeginScene();
Device->SetStreamSource(0, Pyramid, 0, sizeof(Vertex));
Device->SetFVF(Vertex::FVF);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 6);
// Device->SetMaterial(&mtr1);
// Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// Device->SetMaterial(&mtr2);
// Device->DrawPrimitive(D3DPT_TRIANGLELIST, 6, 2);
// Device->SetMaterial(&mtr3);
// Device->DrawPrimitive(D3DPT_TRIANGLELIST, 6, 3);
// Device->SetMaterial(&mtr4);
// Device->DrawPrimitive(D3DPT_TRIANGLELIST, 9, 4);
// Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
::PostQuitMessage(0);
break;
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd);
break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE prevInstance,
PSTR cmdLine,
int showCmd)
{
if(!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop( Display );
Cleanup();
Device->Release();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -