环境:WIN7+VS2012+Direct3D11
//***************************************************************************************
// CrateDemo.cpp by Frank Luna (C) 2011 All Rights Reserved.
//
// Demonstrates texturing a box.
//
// Controls:
// Hold the left mouse button down and move the mouse to rotate.
// Hold the right mouse button down to zoom in and out.
//
//***************************************************************************************
#include <iostream>
#include "d3dApp.h"
#include "d3dx11Effect.h"
#include "GeometryGenerator.h"
#include "MathHelper.h"
#include "LightHelper.h"
#include "Effects.h"
#include "Vertex.h"
#include "xnamath.h"
class CrateApp : public D3DApp
{
public:
CrateApp(HINSTANCE hInstance);
~CrateApp();
//初始化
bool Init();
void OnResize();
void UpdateScene(float dt);
//绘制场景
void DrawScene();
//鼠标事件
void OnMouseDown(WPARAM btnState, int x, int y);
void OnMouseUp(WPARAM btnState, int x, int y);
void OnMouseMove(WPARAM btnState, int x, int y);
private:
void BuildGeometryBuffers();
private:
ID3D11Buffer* mBoxVB;
ID3D11Buffer* mBoxIB;
ID3D11ShaderResourceView* mDiffuseMapSRV;
//灯光
DirectionalLight mDirLights[3];
//材质
Material mBoxMat[9];
int num_Sphere; //画圆的个数
XMFLOAT4X4 mTexTransform; //贴图变换矩阵
XMFLOAT4X4 mBoxWorld[9]; //立方体世界坐标
XMMATRIX Move[9];
XMFLOAT4X4 mView; //视点矩阵
XMFLOAT4X4 mProj;
int mBoxVertexOffset;
UINT mBoxIndexOffset;
UINT mBoxIndexCount;
XMFLOAT3 mEyePosW; //摄像机位置
float mTheta;
float mPhi;
float mRadius;
float light_Ambient;
float light_Diffuse;
float light_Specular;
POINT mLastMousePos; //鼠标的最后位置坐标
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
PSTR cmdLine, int showCmd)
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
CrateApp theApp(hInstance);
if( !theApp.Init() )
return 0;
return theApp.Run();
}
CrateApp::CrateApp(HINSTANCE hInstance)
: D3DApp(hInstance), mBoxVB(0), mBoxIB(0), mDiffuseMapSRV(0), mEyePosW(0.0f, 0.0f, 0.0f),
mTheta(1.3f*MathHelper::Pi), mPhi(0.4f*MathHelper::Pi), mRadius(2.5f)
{
mMain