Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transzformációk, árnyalás, textúrák Szécsi László.

Similar presentations


Presentation on theme: "Transzformációk, árnyalás, textúrák Szécsi László."— Presentation transcript:

1 Transzformációk, árnyalás, textúrák Szécsi László

2 Modell www.iit.bme.hu/~szecsi/GraphGame/ media/buggy.zip kibontani: GraphGame\media folderbe a buggy.x itt legyen: GraphGame\media\buggy\buggy.x

3 class name: Lab2Transform base class: EngineInterface gyári konstruktor, destruktor kidobása, saját konstruktor írása Új labor, új osztály FILEEngineInterface.hOPnew classCLASSLab2Transform

4 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ése FILEGraphGame.cppOPdel/add codeFUNCOnD3D9CreateDevice

5 class Lab2Transform : public EngineInterface { LPD3DXEFFECT effect; LPD3DXMESH mesh; int nSubMeshes; Mesh referencia FILELab2Transform.hOPnew memberCLASSLab2Transfrom

6 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ódusok FILELab2Transform.hOPnew methodCLASSLab2Transfrom

7 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éssel FILELab2Transform.cppOPmethod implCLASSLab2Transform METHODcreateDefaultResources

8 HRESULT Lab2Transform:: releaseDefaultResources() { effect->Release(); return S_OK; } Effect felszabadítás FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODreleaseDefaultResources

9 HRESULT Lab2Transform::createManagedResources () { D3DXLoadMeshFromX( L"media\\buggy\\buggy.x", D3DXMESH_MANAGED, device, NULL, NULL, NULL, (DWORD*)&nSubMeshes, &mesh); return S_OK; } Mesh betöltése FILELab2Transform.cppOPmethod implCLASSLab2Transform METHODcreateManagedResources

10 HRESULT Lab2Transform::releaseManagedResources () { mesh->Release(); return S_OK; } Mesh felszabadítása FILELab2Transform.cppOPmethod implCLASSLab2Transform METHODreleaseManagedResources

11 // 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ása FILELab2Transform.cppOPmethod implCLASSLab2Transform METHODrender

12 #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 objektum FILELab2Transform.hOPnew memberCLASSLab2Transform

13 /* class CDXUTDirectionWidget... D3DXMATRIX m_mView; }; */ DXUTCamera ártalmatlanná tétele FILEDXUTCamera.hOPdel codeCLASSCDXUTDirectionWidget

14 /* IDirect3DDevice9* CDXUTDirectionWidget::s_pd3d9Device = NULL;... D3DXVec3TransformNormal( &m_vCurrentDir, &m_vDefaultDir, &m_mRot ); return S_OK; } */ DXUTCamera ártalmatlanná tétele FILEDXUTCamera.cppOPdel codeCLASSCDXUTDirectionWidget

15 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 aspectbackfront Kamera init FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODconstructor

16 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ó FILELab2Transform.cppOPmethod implCLASSLab2Transform METHODprocessMessage, animate

17 Solution exporer/Add new item/Utility/text file File name: trafo.fx Új effect file FILELab1HLSL.fxOPnew fileCLASSN/A

18 float4x4 modelViewProjMatrix; float4x4 modelMatrix; float4x4 modelMatrixInverse; Uniform paraméterek FILEtrafo.fxOPnew variableFUNC::

19 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 szemantika FILEtrafo.fxOPnew structFUNC::

20 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 Vertex shader FILEtrafo.fxOPnew funcFUNCvsTrafo

21 float4 psDiffuse(TrafoOutput input) : COLOR0 { return abs(input.normal.y); } Pixel shader FILEtrafo.fxOPnew funcFUNCpsDiffuse

22 technique show { pass ExamplePass { VertexShader = compile vs_2_0 vsTrafo(); PixelShader = compile ps_2_0 psDiffuse(); } Új technique FILEtrafo.fxOPnew techniqueFUNC::

23 // copied from Lab1HLSL::render effect->SetTechnique("white"); effect->SetTechnique("show"); Rajzolás az új technikával FILELab2Transform.cppOPdel/add codeCLASSLab2Transform METHODrender

24 //render metódusban, BeginScene előtt: D3DXMATRIX modelMatrix; D3DXMATRIX modelMatrixInverse; D3DXMATRIX modelViewProjMatrix; //... Mátrix változók FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODrender

25 D3DXMatrixIdentity(&modelMatrix); D3DXMatrixInverse(&modelMatrixInverse, NULL, &modelMatrix); modelViewProjMatrix = modelMatrix * (*camera.GetViewMatrix()) * (*camera.GetProjMatrix()); //... Mátrixok kiszámítása FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODrender

26 effect-> SetMatrix("modelMatrix", &modelMatrix); effect-> SetMatrix("modelMatrixInverse", &modelMatrixInverse); effect-> SetMatrix("modelViewProjMatrix", &modelViewProjMatrix); effect->CommitChanges(); Mátrixok átadása az effectnek (uniform paraméter) FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODrender

27 próba WASD + egér

28 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éterek FILEtrafo.fxOPnew variableFUNC::

29 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, diffuse FILEtrafo.fxOPedit funcFUNCpsDiffuse

30 próba matt sárga buggy pont fényforrással

31 float3 eyePos; float3 ks = float3(10, 10, 10); float n = 10; Fényforrás és Phong uniform paraméterek FILEtrafo.fxOPnew variableFUNC::

32 effect->SetFloatArray("eyePos", (float*)camera.GetEyePt(), 3); effect->CommitChanges(); Szempozíció átadása az effectnek (uniform paraméter) FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODrender

33 // 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 + Phong FILEtrafo.fxOPnew funcFUNCpsSpecular ezek mehetnének a vertex shaderbe is

34 technique showspecular { pass ExamplePass { VertexShader = compile vs_2_0 vsTrafo(); PixelShader = compile ps_2_0 psSpecular(); } Új technique FILEtrafo.fxOPnew techniqueFUNC::

35 // copied from Lab1HLSL::render effect->SetTechnique("white"); effect->SetTechnique("show"); effect->SetTechnique("showspecular"); Rajzolás az új technikával FILELab2Transform.cppOPdel/add codeCLASSLab2Transform METHODrender

36 próba fényes sárga buggy pont fényforrással

37 #include class Lab2Transform : public LabInterface { std::vector textures; Textúra referenciák STL vectorban FILELab2Transform.cppOPnew memberCLASSLab2Transform

38 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óval FILELab2Transform.cppOPedit codeCLASSLab2Transform METHODcreateManagedResources

39 D3DXMATERIAL* materialArray = (D3DXMATERIAL*) materialBuffer->GetBufferPointer(); //... Anyagok kibányászása FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODcreateManagedResources

40 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ése FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODcreateManagedResources

41 HRESULT Lab2Transform:: releaseManagedResources() { mesh->Release(); std::vector ::iterator i = textures.begin(); while(i != textures.end()) { if(*i != NULL) (*i)->Release(); i++; } return S_OK; } Textúrák felszabadításe FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODreleaseManagedResources

42 texture kdMap; sampler2D kdMapSampler = sampler_state{ texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; Textúra sampler az effect fileban FILEtrafo.fxOPnew samplerFUNC::

43 float4 psDiffuse(TrafoOutput input) : COLOR0 { return input.normal.y * tex2D(kdMapSampler, input.tex); } Textúrázó pixel shader FILEtrafo.fxOPedit codeFUNCpsDiffuse

44 effect->BeginPass(0); for(int i=0; i<nSubMeshes; i++) { effect->SetTexture("kdMap", textures.at(i)); effect->CommitChanges(); mesh->DrawSubset(i); } effect->EndPass(); Rajzolás textúrákkal FILELab2Transform.cppOPadd codeCLASSLab2Transform METHODrender


Download ppt "Transzformációk, árnyalás, textúrák Szécsi László."

Similar presentations


Ads by Google