//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: d3dxcreate.cpp
//
// Author: Frank D. Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Renders several D3DX shapes in wireframe mode and has the camera
// fly around the scene. Demonstrates the D3DXCreate* functions, and
// demonstrates more complex transformations used to position the objects
// in the world and move the camera around the world.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#include "d3dUtility.h"
//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
// Store 5 objects.`
ID3DXMesh* Objects[5] = {0, 0, 0, 0, 0};
ID3DXMesh *box=0;
// World matrices for each object. These matrices
// specify the locations of the objects in the world.
D3DXMATRIX ObjWorldMatrices[5];
LPDIRECT3DVERTEXBUFFER9 point=NULL;
//
D3DCAPS9 caps;
// Framework Functions
//
struct points
{
float x,y,z;
DWORD color;
};
bool Setup()
{
PlaySound("星空.wav",0,SND_ASYNC|SND_LOOP);
//
Device->GetDeviceCaps( &caps);
// Create the objects.
//
/*D3DXCreateTeapot(
Device,
&Objects[0],
0);
D3DXCreateBox(
Device,
2.0f, // width
2.0f, // height
2.0f, // depth
&Objects[1],
0);
// cylinder is built aligned on z-axis
D3DXCreateCylinder(
Device,
1.0f, // radius at negative z end
1.0f, // radius at positive z end
3.0f, // length of cylinder
10, // slices
10, // stacks
&Objects[2],
0);
D3DXCreateTorus(
Device,
1.0f, // inner radius
3.0f, // outer radius
10, // sides
10, // rings
&Objects[3],
0);*/
if(FAILED(Device->CreateVertexBuffer(100*sizeof(points),0,D3DFVF_XYZ|D3DFVF_DIFFUSE,D3DPOOL_DEFAULT,&point,NULL)))
return false;
points *ptr;
point->Lock(0,100*sizeof(points),
(void**)&ptr,0);
for(int i=0;i<100;i++)
{
ptr[i].x=i+1;
ptr[i].y=i+2;
ptr[i].z=0.0f;
ptr[i].color=D3DCOLOR_XRGB(255,255,255);
}
point->Unlock();
float ra[5]={2.0f,0.2f,0.4f,0.8f,1.0f};
for(int i=0;i<5;i++)
D3DXCreateSphere(
Device,
ra[i], // radius
10, // slices
10, // stacks
&Objects[i],
0);
D3DXCreateBox(Device,10.0f,10.0f,0.0f,&box,0);
//
// Build world matrices - position the objects in world space.
// For example, ObjWorldMatrices[1] will position Objects[1] at
// (-5, 0, 5). Likewise, ObjWorldMatrices[2] will position
// Objects[2] at (5, 0, 5).
//
D3DMATERIAL9 mtrl;
ZeroMemory(&mtrl,sizeof(mtrl));
mtrl.Diffuse.r=0.7f;
mtrl.Diffuse.g=0.8f;
mtrl.Diffuse.b=0.7f;
mtrl.Diffuse.a=0.5f;
mtrl.Power=5.0f;
mtrl.Specular=D3DXCOLOR(0.9f,0.9f,0.9f,0.5f);
Device->SetMaterial(&mtrl);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
Device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_DIFFUSE);
Device->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
Device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
D3DLIGHT9 light;
ZeroMemory(&light,sizeof(light));
light.Ambient=D3DXCOLOR(0.3f,0.3f,0.3f,1.0f);
light.Diffuse=D3DXCOLOR(1.0f,1.0f,1.0f,0.8f);
light.Specular=D3DXCOLOR(1.0f,1.0f,1.0f,0.8f);
light.Direction=D3DXVECTOR3(0.0f,0.0f,5.0f);
light.Type=D3DLIGHT_DIRECTIONAL;
Device->SetRenderState(D3DRS_LIGHTING,TRUE);
Device->SetLight(0,&light);
Device->LightEnable(0,true);
D3DLIGHT9 light2;
ZeroMemory(&light2,sizeof(light2));
light2.Ambient=D3DXCOLOR(0.3f,0.3f,0.3f,1.0f);
light2.Diffuse=D3DXCOLOR(1.0f,1.0f,1.0f,0.8f);
light2.Specular=D3DXCOLOR(1.0f,1.0f,1.0f,0.8f);
light2.Direction=-D3DXVECTOR3(0.0f,0.0f,5.0f);
light2.Type=D3DLIGHT_DIRECTIONAL;
Device->SetRenderState(D3DRS_LIGHTING,TRUE);
Device->SetLight(1,&light2);
Device->LightEnable(1,true);
D3DXMatrixTranslation(&ObjWorldMatrices[0], 0.0f, 0.0f, 0.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[1], -5.0f, 0.0f, 5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[2], 5.0f, 0.0f, 5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[3], -5.0f, 0.0f, -5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[4], 5.0f, 0.0f, -5.0f);
//
// 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);
//
// Switch to wireframe mode.
//
Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
return true;
}
void Cleanup()
{
for(int i = 0; i < 5; i++)
d3d::Release<ID3DXMesh*>(Objects[i]);
}
bool Display(float timeDelta)
{
srand(time(0));
if( Device )
{
// Animate the camera:
// The camera will circle around the center of the scene. We use the
// sin and cos functions to generate points on the circle, then scale them
// by 10 to further the radius. In addition the camera will move up and down
// as it circles about the scene.
//static float angle = (3.0f * D3DX_PI) / 2.0f;
static float angle=0.0f;
static float cameraHeight = -20.0f;
static float cameraHeightDirection = 5.0f;
D3DXVECTOR3 position( cosf(angle) * 10.0f, cameraHeight, sinf(angle) * 10.0f );
// the camera is targetted at the origin of the world
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
// the worlds up vector
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
// compute the position for the next frame
angle += timeDelta;
if( angle >= 6.28f )
angle = 0.0f;
// compute the height of the camera for the next frame
cameraHeight += cameraHeightDirection * timeDelta;
if( cameraHeight >= 10.0f )
cameraHeightDirection = -5.0f;
if( cameraHeight <= -20.0f )
cameraHeightDirection = 5.0f;
//MODIFY VERTEX
static float angles=0.0f;
points* ptr=NULL;
point->Lock(0,100*sizeof(points),
(void**)&ptr,0);
for(int i=0;i<100;i++)
{
ptr[i].x=cosf(angle) * 10.0f;
ptr[i].y=cameraHeight;
ptr[i].z= sinf(angle) * 10.0f+5.0f;
ptr[i].color=D3DCOLOR_XRGB(255,0,0);
}
angles+=0.1f;
if(angles>=6.28f)
angles=0.0f;
point->Unlock();
// Draw the Scene:
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
Device->BeginScene();
D3DXMATRIX boxWorld;
D3DXMatrixIdentity(&boxWorld);
Device->SetTransform(D3DTS_WORLD,&boxWorld);
Device->SetStreamSource(0,point,0,sizeof(points));
Device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
Device->DrawPrimitive(D3DPT_POINTLIST,0,100);
box->DrawSubset(0);
static float tray=-5.0f;
for(int i=0;i<5;i++)
{
// D3DXMatrixTranslation(&ObjWorldMatrices[i],0.0f,tray,0.0f);
// Set the world matrix that positions the object.
Device->SetTransform(D3DTS_WORLD, &ObjWorldMatrices[i]);
// Draw the object using the previously set world matrix.
Objects[i]->DrawSubset(0);
}
tray+=0.01f;
if(tray>=5.0f)
tray=-5.0f;
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, false, D3DDEVTYPE_HAL,