Transzformációk, árnyalás, textúrák Szécsi László.
-
Upload
jacquelyn-rhymes -
Category
Documents
-
view
213 -
download
0
Transcript of Transzformációk, árnyalás, textúrák Szécsi László.
Modell
www.iit.bme.hu/~szecsi/GraphGame/
media/buggy.zip
kibontani:
GraphGame\media folderbe
a buggy.x itt legyen:
GraphGame\media\buggy\buggy.x
class name: Lab2Transform
base class: EngineInterface
gyári konstruktor, destruktor kidobása, saját konstruktor írása
Új labor, új osztályFILE EngineInterface.hOP new class CLASS Lab2Transform
Lab1 példány helyett Lab2 példányt hozzunk létre
#include “Lab2Transform.h”
HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
engine = new Lab1HLSL(pd3dDevice);
engine = new Lab2Transform(pd3dDevice);
Globális példány kicseréléseFILE GraphGame.cppOP del/add code FUNC OnD3D9CreateDevice
class Lab2Transform :
public EngineInterface
{
LPD3DXEFFECT effect;
LPD3DXMESH mesh;
int nSubMeshes;
Mesh referenciaFILE Lab2Transform.hOP new member CLASS Lab2Transfrom
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();
};
Eseménykezelő metódusokFILE Lab2Transform.hOP new method CLASS Lab2Transfrom
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; }
Effect betöltése hibakezelésselFILE Lab2Transform.cppOP method impl CLASS Lab2Transform
METHOD createDefaultResources
HRESULT Lab2Transform::
releaseDefaultResources()
{
effect->Release();
return S_OK;
}
Effect felszabadításFILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD releaseDefaultResources
HRESULT Lab2Transform::createManagedResources()
{
D3DXLoadMeshFromX(
L"media\\buggy\\buggy.x", D3DXMESH_MANAGED, device, NULL, NULL, NULL, (DWORD*)&nSubMeshes, &mesh);
return S_OK; }
Mesh betöltéseFILE Lab2Transform.cppOP method impl CLASS Lab2Transform
METHOD createManagedResources
HRESULT Lab2Transform::releaseManagedResources()
{
mesh->Release();
return S_OK;
}
Mesh felszabadításaFILE Lab2Transform.cppOP method impl CLASS Lab2Transform
METHOD releaseManagedResources
// copy from Lab1HLSL::render
effect->BeginPass(0);
for(int i=0; i<nSubMeshes; i++)
{
mesh->DrawSubset(i);
}
effect->EndPass();
// copy from Lab1HLSL::render
Mesh rajzolásaFILE Lab2Transform.cppOP method impl CLASS Lab2Transform
METHOD render
#include "DXUTCamera.h"
// project proporties: additional include dir: ./DXUT/Optional
// solution explorer/add existing item: ./DXUT/Optimonal/DXUTCamera.*
class Lab2Transform :
public LabInterface
{
CFirstPersonCamera camera;
Kamera objektumFILE Lab2Transform.hOP new member CLASS Lab2Transform
/*
class CDXUTDirectionWidget
...
D3DXMATRIX m_mView;
};
*/
DXUTCamera ártalmatlanná tételeFILE DXUTCamera.hOP del code CLASS CDXUTDirectionWidget
/*
IDirect3DDevice9* CDXUTDirectionWidget::s_pd3d9Device = NULL;
...
D3DXVec3TransformNormal( &m_vCurrentDir, &m_vDefaultDir, &m_mRot );
return S_OK;
}
*/
DXUTCamera ártalmatlanná tételeFILE DXUTCamera.cppOP del code CLASS CDXUTDirectionWidget
Lab2Transform::Lab2Transform(LPDIRECT3DDEVICE9 device)
:EngineInterface(device){
camera.SetViewParams(
&D3DXVECTOR3(10, 10, 10),&D3DXVECTOR3(0, 0, 0));
camera.SetProjParams(3.14f / 2.0f, 1.0, 0.1, 1000.0);
}
eye
lookat
FOV
aspect backfront
Kamera initFILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD constructor
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);
}
Kamera vezérlés és animációFILE Lab2Transform.cppOP method impl CLASS Lab2Transform
METHOD processMessage, animate
Solution exporer/Add new item/Utility/text file
File name: trafo.fx
Új effect fileFILE Lab1HLSL.fxOP new file CLASS N/A
float4x4 modelViewProjMatrix;
float4x4 modelMatrix;
float4x4 modelMatrixInverse;
Uniform paraméterekFILE trafo.fxOP new variable FUNC ::
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;
};
Input-output szemantikaFILE trafo.fxOP new struct FUNC ::
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
modell -> világ
Vertex shaderFILE trafo.fxOP new func FUNC vsTrafo
float4 psDiffuse(TrafoOutput input) : COLOR0
{
return abs(input.normal.y);
}
Pixel shaderFILE trafo.fxOP new func FUNC psDiffuse
technique show
{
pass ExamplePass
{
VertexShader = compile vs_2_0
vsTrafo();
PixelShader = compile ps_2_0
psDiffuse();
}
}
Új techniqueFILE trafo.fxOP new technique FUNC ::
// copied from Lab1HLSL::render
effect->SetTechnique("white");
effect->SetTechnique("show");
Rajzolás az új technikávalFILE Lab2Transform.cppOP del/add code CLASS Lab2Transform
METHOD render
//render metódusban, BeginScene előtt:
D3DXMATRIX modelMatrix;
D3DXMATRIX modelMatrixInverse;
D3DXMATRIX modelViewProjMatrix;
//...
Mátrix változókFILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD render
D3DXMatrixIdentity(&modelMatrix);
D3DXMatrixInverse(&modelMatrixInverse, NULL, &modelMatrix);
modelViewProjMatrix =
modelMatrix * (*camera.GetViewMatrix()) * (*camera.GetProjMatrix());
//...
Mátrixok kiszámítása FILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD render
effect->
SetMatrix("modelMatrix", &modelMatrix);
effect->
SetMatrix("modelMatrixInverse", &modelMatrixInverse);
effect->
SetMatrix("modelViewProjMatrix", &modelViewProjMatrix);
effect->CommitChanges();
Mátrixok átadása az effectnek (uniform paraméter)FILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD render
float3 lightPos = float3(10, 10, 10);
float3 lightPower =
float3(500, 500, 500);
float3 kd = float3(1, 1, 0); //sárga
Fényforrás uniform paraméterekFILE trafo.fxOP new variable FUNC ::
float4 psDiffuse(TrafoOutput input) : COLOR0 {
float3 normal = normalize(input.normal);
float3 toLight = lightPos –
input.worldPos.xyz;
float3 lightDist2 = dot(toLight, toLight);
float3 lightDir = normalize(toLight);
return float4(
kd * dot(lightDir, normal)
* lightPower
/ (4 * 3.14 * lightDist2), 1);
}
Pixel shader: pont fény, diffuseFILE trafo.fxOP edit func FUNC psDiffuse
float3 eyePos;
float3 ks = float3(10, 10, 10);
float n = 10;
Fényforrás és Phong uniform paraméterekFILE trafo.fxOP new variable FUNC ::
effect->SetFloatArray("eyePos", (float*)camera.GetEyePt(), 3);
effect->CommitChanges();
Szempozíció átadása az effectnek (uniform paraméter)FILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD render
// copy paste from psDiffuse
float4 psSpecular(TrafoOutput input) : COLOR0 {float3 normal = normalize(input.normal);
float3 toLight = lightPos - input.worldPos.xyz;
float3 lightDist2 = dot(toLight, toLight);
float3 lightDir = normalize(toLight);
float3 toEye = eyePos - input.worldPos.xyz;
float3 viewDir = normalize(toEye);
return float4(
(kd * dot(lightDir, normal)
+ ks * pow(dot(reflect(-viewDir, normal),
lightDir), n) )
* lightPower
/ (4 * 3.14 * lightDist2), 1);
}
Új pixel shader: pont fény, diffúz + PhongFILE trafo.fxOP new func FUNC psSpecular
ezek mehetnéneka vertex shaderbe is
technique showspecular
{
pass ExamplePass
{
VertexShader = compile vs_2_0
vsTrafo();
PixelShader = compile ps_2_0
psSpecular();
}
}
Új techniqueFILE trafo.fxOP new technique FUNC ::
// copied from Lab1HLSL::render
effect->SetTechnique("white");
effect->SetTechnique("show");
effect->SetTechnique("showspecular");
Rajzolás az új technikávalFILE Lab2Transform.cppOP del/add code CLASS Lab2Transform
METHOD render
#include <vector>
class Lab2Transform :
public LabInterface
{
std::vector<LPDIRECT3DTEXTURE9> textures;
Textúra referenciák STL vectorbanFILE Lab2Transform.cppOP new member CLASS Lab2Transform
HRESULT Lab2Transform::
createManagedResources()
{
LPD3DXBUFFER materialBuffer;
D3DXLoadMeshFromX(
L"media\\buggy\\buggy.x", D3DXMESH_MANAGED, device, NULL, &materialBuffer, NULL, (DWORD*)&nSubMeshes, &mesh);
//...
Mesh betöltés anyaginformációvalFILE Lab2Transform.cppOP edit code CLASS Lab2Transform
METHOD createManagedResources
D3DXMATERIAL* materialArray = (D3DXMATERIAL*)
materialBuffer->GetBufferPointer();
//...
Anyagok kibányászásaFILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD createManagedResources
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 betöltéseFILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD createManagedResources
HRESULT Lab2Transform::
releaseManagedResources() {
mesh->Release();
std::vector<LPDIRECT3DTEXTURE9>::iterator i = textures.begin();
while(i != textures.end())
{
if(*i != NULL)
(*i)->Release(); i++;
}
return S_OK;
}
Textúrák felszabadításeFILE Lab2Transform.cppOP add code CLASS Lab2Transform
METHOD releaseManagedResources
texture kdMap;
sampler2D kdMapSampler =sampler_state{
texture = < kdMap >;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
Textúra sampler az effect filebanFILE trafo.fxOP new sampler FUNC ::
float4 psDiffuse(TrafoOutput input) : COLOR0
{
return input.normal.y * tex2D(kdMapSampler, input.tex);
}
Textúrázó pixel shaderFILE trafo.fxOP edit code FUNC psDiffuse