本文介绍了更改照明DirectX的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何切换灯?该项目的代码如下.
how do i switch the lights ?? the code for this proj is below.
//main.cpp
#include "base.h"
int starttime = 0;
bool btype= true;
bool btorus = false;
bool bsphere = false;
bool bcube = false;
bool bteapot = false;
bool bcylinder = false;
int dirx;
int diry;
int dirz;
IDirect3DDevice9 * Device = 0;
IDirect3DVertexBuffer9* Triangle= 0;
int iWidth=1000;
int iHeight=1000;
static float ctorus=0;
static float ccube=0;
static float csphere=0;
static float ccylinder=0;
static float cteapot=0;
ID3DXMesh* cube = 0;//for mesh
ID3DXMesh* torus = 0;
ID3DXMesh* cylinder = 0;
ID3DXMesh* sphere = 0;
ID3DXMesh* teapot = 0;
LRESULT CALLBACK basewin::WinProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam)
{//LRESULt PAINTSTRUCT, HDC, etc
PAINTSTRUCT ps;
HDC hdc;
switch(uMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
}break;
case WM_RBUTTONDOWN:
{
PostQuitMessage(0);
}break;
case WM_COMMAND:
{
switch(wparam)
{
case MENU_FILE_EXIT:
{
PostQuitMessage(0);
}break;
case MENU_HELP_HOWTO:
{
MessageBox(NULL, "Made by Dakotah Tyler, 8/4/2010", "Help", MB_OK);
}break;
/* case MENU_TYPE_MESH:
{
btype = true;
}break;
case MENU_TYPE_SOLID:
{
btype = false;
}break;
case MENU_SHAPE_TORUS:
{
btorus = true;
bsphere = false;
bcube = false;
bteapot = false;
bcylinder = false;
}break;
case MENU_SHAPE_SPHERE:
{
btorus = false;
bsphere = true;
bcube = false;
bteapot = false;
bcylinder = false;
}break;
case MENU_SHAPE_CUBE:
{
btorus = false;
bsphere = false;
bcube = true;
bteapot = false;
bcylinder = false;
}break;
case MENU_SHAPE_TEAPOT:
{
btorus = false;
bsphere = false;
bcube = false;
bteapot = true;
bcylinder = false;
}break;
case MENU_SHAPE_CYLINDER:
{
btorus = false;
bsphere = false;
bcube = false;
bteapot = false;
bcylinder = true;
}break;
*/
default:break;
}
}break;
case WM_KEYDOWN:
{
if(wparam == VK_ESCAPE)
{
PostQuitMessage(0);
}
if(wparam == ''Q'')
{
dirx+=1;
}
if(wparam == ''A'')
{
dirx-=01;
}
if(wparam == ''W'')
{
diry+=1;
}
if(wparam == ''S'')//we control your spinning
{
diry-=1;
}
if(wparam == ''E'')
{
dirz+=1;
}
if(wparam == ''D'')
{
dirz-=1;
}
}break;
default:break;
}
return (::DefWindowProc(hwnd, uMsg, wparam, lparam));
}
bool Setup()
{//setup loop
D3DXCreateSphere(Device, 1, 32, 20, &sphere, 0);
D3DXCreateTorus(Device, 0.10f, 0.3f, 20, 30, &torus, 0);
D3DXCreateBox(Device, 1.0f, 1.0f, 1.0f, &cube, 0);
D3DXCreateCylinder(Device, 0.4f, 0.6f, 0.6f, 50, 4, &cylinder, 0);
D3DXCreateTeapot(Device, &teapot, 0);
D3DXVECTOR3 position(1.0f, 1.0f, -2.0);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
Device->LightEnable(0, true);
D3DXMATRIX view;
D3DXMatrixLookAtLH(&view, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &view);
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)iWidth / (float)iHeight, 1.0f, 1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
Device->SetRenderState(D3DRS_SPECULARENABLE, true);
//Triangle->Lock(0, 0, (void**)&vs, 0);
return true;
}
bool PlayGame()
{//Playgame!
starttime= (int)GetTickCount();
if(Device)
{
static float sphereCounter = 0;
static float boxCounter = 0;
static float cylCounter = 0;
static float torusCounter = 0;
D3DXVECTOR3 pos(dirx, diry, dirz);
D3DXVECTOR3 dir(0.0f, 0.0f, 1.0f);
D3DLIGHT9 Point;
D3DXCOLOR c = WHITE;
Point = InitPointLight(&pos, &c);
Device->SetLight(0, &Point);
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
Device->BeginScene();
D3DXMATRIX s_Translation;
D3DXMatrixTranslation(&s_Translation, 0, 0, 2);
Device->SetMaterial(&RED_MTRL);
Device->SetTransform(D3DTS_WORLD, &s_Translation);
sphere->DrawSubset(0);
D3DXMATRIX m_Translation;
D3DXMatrixTranslation(&m_Translation, -2.0f, 1.0f, 0);
Device->SetMaterial(&RED_MTRL);
Device->SetTransform(D3DTS_WORLD, &m_Translation);
cube->DrawSubset(0);
D3DXMATRIX c_Translation;
D3DXMatrixTranslation(&c_Translation, 1.5f, -1.0f, 0);
Device->SetMaterial(&YELLOW_MTRL);
Device->SetTransform(D3DTS_WORLD, &c_Translation);
cylinder->DrawSubset(0);
D3DXMATRIX t_Translation;
D3DXMatrixTranslation(&t_Translation, -1.0f, 0.8f, 0);
Device->SetMaterial(&GREEN_MTRL);
Device->SetTransform(D3DTS_WORLD, &t_Translation);
torus->DrawSubset(0);
D3DXMATRIX tp_Translation;
D3DXMatrixTranslation(&tp_Translation, -1.0f, -1.0f, 0);
Device->SetMaterial(&ORANGE_MTRL);
Device->SetTransform(D3DTS_WORLD, &tp_Translation);
teapot->DrawSubset(0);
Device->EndScene();
Device->Present(0, 0, 0, 0); }
while(((int)GetTickCount()-starttime)<(1000/30))
{
//wait ///this sets up the frame rate
}
return true;
}
void CleanUp()
{
//put cleanup information here.
basewin::Release<id3dxmesh*>(sphere);
basewin::Release<id3dxmesh*>(cube);
basewin::Release<id3dxmesh*>(cylinder);
basewin::Release<id3dxmesh*>(torus);
basewin::Release<id3dxmesh>(teapot);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{//Build the window
if(!basewin::BuildWindow(hInstance,300, 500, 150, 200,true,D3DDEVTYPE_HAL, &Device))
{ //Error message
MessageBox(NULL, "There is an error", "Error has happened", MB_OK);
return(0);
}
//ShowCursor(false);//gets rid of the cursor
if (!Setup())
{
return(0);
}
basewin::MessageLoop(PlayGame);//MessageLoop
CleanUp();
return(0);
}
//base.h
#ifndef __base_H
#define __base_H
#include <windows.h>
#define WIN32_LEAN_AND_MEAN
#include <d3dx9.h>
#include "resource.h"
extern HWND g_hwnd;
//add d3dx9.lib and d3d9.lib
namespace basewin
{
bool BuildWindow(HINSTANCE hinstance, int iHeight, int iWidth, int xPos, int yPos, bool windowed, D3DDEVTYPE devType, IDirect3DDevice9** device);
int MessageLoop(bool (*ptr_Game)());
LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam);
template <class t=""> void Release(T t)
{
if(t)
{
t->Release();
t=0;
}
}}
const D3DXCOLOR RED(D3DCOLOR_XRGB(255, 0, 0));
const D3DXCOLOR BLUE(D3DCOLOR_XRGB(0, 0, 255));
const D3DXCOLOR GREEN(D3DCOLOR_XRGB(0, 255, 0));
const D3DXCOLOR YELLOW(D3DCOLOR_XRGB(255, 255, 0));
const D3DXCOLOR ORANGE(D3DCOLOR_XRGB(255, 128, 0));
const D3DXCOLOR WHITE(D3DCOLOR_XRGB(255, 255, 255));
const D3DXCOLOR BLACK(D3DCOLOR_XRGB(0, 0, 0));
D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color);
D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color);
D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color);
D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p);
const D3DMATERIAL9 WHITE_MTRL = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);
const D3DMATERIAL9 RED_MTRL = InitMtrl(RED, RED, RED, BLACK, 2.0f);
const D3DMATERIAL9 GREEN_MTRL = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);
const D3DMATERIAL9 BLUE_MTRL = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);
const D3DMATERIAL9 ORANGE_MTRL = InitMtrl(ORANGE, ORANGE, ORANGE, BLACK, 2.0f);
#endif
#include "base.h"
HWND g_hwnd;
bool basewin::BuildWindow(HINSTANCE hinstance, int iHeight, int iWidth, int xPos, int yPos, bool windowed, D3DDEVTYPE devType, IDirect3DDevice9** device)
{//BuildWindowStuff like WNDCLASSEX
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hinstance;
wc.lpfnWndProc = (WNDPROC)basewin::WinProc;
wc.lpszClassName = "MyWindow";
wc.lpszMenuName = "MainMenu";
wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
if (!(RegisterClassEx(&wc)))
{
return false;
}
if (!(g_hwnd = CreateWindowEx(NULL, "MyWindow", "Tyler''s lovely window", WS_VISIBLE, xPos, yPos, iWidth, iHeight, NULL, NULL, hinstance, NULL)))
{
return false;
}
// step 1 initialize Direct3D
IDirect3D9* d3d9 = NULL;
d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if(!(d3d9))
{
//put messagebox here incase we got a progrmel
return false;
}
//step 2 - check for hardware vertex processing
D3DCAPS9 caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, devType, &caps);
int vp = 0;
if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vp=D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
//step3 - fill the D3DPRESENT_PARAMETERS struct with values
D3DPRESENT_PARAMETERS d3dpp;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;//this may be wrong
d3dpp.BackBufferHeight = iHeight;
d3dpp.BackBufferWidth = iWidth;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.Flags= 0;
d3dpp.FullScreen_RefreshRateInHz= 0;
d3dpp.hDeviceWindow= g_hwnd;
d3dpp.MultiSampleQuality=0;
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE;
d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed= windowed;
//step4 = create a device
HRESULT hr;
hr=d3d9->CreateDevice(D3DADAPTER_DEFAULT, devType, g_hwnd, vp, &d3dpp, device);
if FAILED(hr)
{
//dispatch message!! rawr i failed u!
return false;
}
d3d9->Release();
return true;
//return true;//maybe
}//program files, directx sdk, documentation, directx9 look this up.
int basewin::MessageLoop(bool (*ptr_Game)())
{//messageloop
MSG msg;
::ZeroMemory(&msg, sizeof(MSG));
while(TRUE)
{
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))//PeekMessage "used to handle messages"
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//put gameloop here
ptr_Game();
}
return 1;
}
D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)
{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = *color * 0.6f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Direction = *direction;
return light;
}
D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_POINT;
light.Ambient = *color * 0.6f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Position = *position;
light.Range = 1000.0f;
light.Falloff = 1.0f;
light.Attenuation0 = 1.0f;
light.Attenuation1 = 0.0f;
light.Attenuation2 = 0.0f;
return light;
}
D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_SPOT;
light.Ambient = *color * 0.0f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Position = *position;
light.Direction = *direction;
light.Range = 1000.0f;
light.Falloff = 1.0f;
light.Attenuation0 = 1.0f;
light.Attenuation1 = 0.0f;
light.Attenuation2 = 0.0f;
light.Theta = 0.4f;
light.Phi = 0.9f;
return light;
}
D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
{
D3DMATERIAL9 mtrl;
mtrl.Ambient = a;
mtrl.Diffuse = d;
mtrl.Specular = s;
mtrl.Emissive = e;
mtrl.Power = p;
return mtrl;
}
</class></d3dx9.h></windows.h></id3dxmesh>
推荐答案
这篇关于更改照明DirectX的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!