Transzformációk, textúrák, árnyalás Szécsi László
Modell /buggy.zip kibontani: GraphGame\media folderbe a buggy.x itt legyen: GraphGame\media\buggy\buggy.x
Új osztály Lab2Transform base class: EngineInterface
GraphGame.cpp Lab1 példány helyett Lab2 példányt hozzunk létre #include “Lab2Transform.h”
Vertex buffer helyett mesh class Lab2Transform : public EngineInterface { LPD3DXEFFECT effect; LPD3DXMESH mesh; int nSubMeshes; …
Lab2Transform.h metódusok public:Lab2Transform( LPDIRECT3DDEVICE9 device); HRESULT createDefaultResources(); HRESULT releaseDefaultResources(); HRESULT createManagedResources(); HRESULT releaseManagedResources(); void processMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); void animate(double dt, double t); void render(); };
Effect betöltése hibakezeléssel HRESULT Lab2Transform::createDefaultResources(){ LPD3DXBUFFER compilationErrors; if(FAILED( D3DXCreateEffectFromFile(device, L"trafo.fx", NULL, NULL, 0, NULL, &effect, &compilationErrors))){ if(compilationErrors) MessageBoxA( NULL, (LPSTR)compilationErrors->GetBufferPointer(), "Failed to load effect file!", MB_OK); exit(-1); } return S_OK; }
Mesh betöltése HRESULT Lab2Transform::createManagedResources () { D3DXLoadMeshFromX( L"media\\buggy\\buggy.x", D3DXMESH_MANAGED, device, NULL, NULL, NULL, (DWORD*)&nSubMeshes, &mesh);
Mesh felszabadítása HRESULT Lab2Transform::releaseManagedResources () { mesh->Release(); return S_OK; }
mesh rajzolása effect->BeginPass(0); for(int i=0; i<nSubMeshes; i++) { mesh->DrawSubset(i); } effect->EndPass();
kamera class Lab2Transform : public LabInterface {... CFirstPersonCamera camera;...
kamera init Lab2Transform::Lab2Transform( LPDIRECT3DDEVICE9 device){ camera.SetViewParams( &D3DXVECTOR3(10, 10, 10), &D3DXVECTOR3(0, 0, 0)); camera.SetProjParams(3.14f / 2.0f, 1.0, 0.1, ); } eye lookat FOV aspectbackfront
kamera vezérlés & animáció void Lab2Transform::processMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ camera.HandleMessages( hWnd, uMsg, wParam, lParam); } void Lab2Transform::animate(double dt, double t) { camera.FrameMove(dt); }
új fx file solution explorer add new item txt file trafo.fx
trafo.fx uniform paraméterek float4x4 modelViewProjMatrix; float4x4 modelMatrix; float4x4 modelMatrixInverse;
trafo.fx input output szemantika struct TrafoInput{ float4 pos: POSITION; float3 normal: NORMAL; float2 tex: TEXCOORD0; }; struct TrafoOutput{ float4 pos: POSITION; float3 normal: TEXCOORD2; float2 tex: TEXCOORD0; float4 worldPos: TEXCOORD1; };
Vertex shader TrafoOutput vsTrafo(TrafoInput input){ TrafoOutput output = (TrafoOutput)0; output.pos = mul(input.pos, modelViewProjMatrix); output.worldPos = mul(input.pos, modelMatrix); output.normal = mul(modelMatrixInverse, float4(input.normal.xyz, 0.0)); output.tex = input.tex; return output; } modell -> n.képernyő modell -> világ
pixel shader float4 psDiffuse(TrafoOutput input) : COLOR0 { return input.normal.y; }
technique technique show { pass ExamplePass { VertexShader = compile vs_2_0 vsTrafo(); PixelShader = compile ps_2_0 psDiffuse(); }
Lab2Transform cseréljük le az effect file és a technika nevét render metódusban, BeginScene előtt: rakjuk össze a trafómátrixokat D3DXMATRIX modelMatrix; D3DXMATRIX modelMatrixInverse; D3DXMATRIX modelViewProjMatrix;
mátrixok D3DXMatrixIdentity(&modelMatrix); D3DXMatrixInverse(&modelMatrixInverse, NULL, &modelMatrix); modelViewProjMatrix = modelMatrix * (*camera.GetViewMatrix()) * (*camera.GetProjMatrix());
paraméterátadás az effectnek effect-> SetMatrix("modelMatrix", &modelMatrix); effect-> SetMatrix("modelMatrixInverse", &modelMatrixInverse); effect-> SetMatrix("modelViewProjMatrix", &modelViewProjMatrix); effect->CommitChanges();
próba WASD + egér
textúrák class Lab2Transform : public LabInterface {... std::vector textures;
mesh betöltés anyaginfóval HRESULT Lab2Transform::createManagedResource s() { LPD3DXBUFFER materialBuffer; D3DXLoadMeshFromX( L"media\\buggy\\buggy.x", D3DXMESH_MANAGED, device, NULL, &materialBuffer, NULL, (DWORD*)&nSubMeshes, &mesh);
anyagok kibányászása D3DXMATERIAL* materialArray = (D3DXMATERIAL*) materialBuffer->GetBufferPointer();
textúrák betöltése for(int t=0; t<nSubMeshes; t++){ LPDIRECT3DTEXTURE9 texture; char textureFilePath[512]; strcpy(textureFilePath, "media\\buggy\\"); strcat(textureFilePath, materialArray[t].pTextureFilename); HRESULT hr = D3DXCreateTextureFromFileA( device, textureFilePath, &texture); if(hr == S_OK) textures.push_back(texture); else textures.push_back(NULL); }
textúrák felszabadítása HRESULT Lab2Transform::releaseManagedResources() { mesh->Release(); std::vector ::iterator i = textures.begin(); while(i != textures.end()) { if(*i != NULL) (*i)->Release(); } return S_OK; }
textúra sampler az effect fileban texture kdMap; sampler2D kdMapSampler = sampler_state{ texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; };
textúrázó pixel shader float4 psWhite(TrafoOutput input) : COLOR0 { return input.normal.y * tex2D(kdMapSampler, input.tex); }
rajzolás textúrákkal effect->BeginPass(0); for(int i=0; i<nSubMeshes; i++) { effect->SetTexture("kdMap", textures.at(i)); effect->CommitChanges(); mesh->DrawSubset(i); } effect->EndPass();