From acfffaac185282b862ade368278918258df13a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Wiesner?= Date: Sat, 1 Aug 2020 18:14:33 +0200 Subject: [PATCH] =?UTF-8?q?utols=C3=B3,=20teljes=20v=C3=A1ltozat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cam.cpp | 5 +++++ Cam.h | 2 ++ DXWindow.cpp | 16 ++++++++++++++++ DXWindow.h | 3 +++ RUNTIME/shaders/basic_shader.hlsl | 22 ++++++++++++++++------ const_bufs.h | 2 ++ 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/Cam.cpp b/Cam.cpp index 8525521..5c8a012 100644 --- a/Cam.cpp +++ b/Cam.cpp @@ -118,6 +118,11 @@ void eg3d::Cam::pitch(float drho) { constructViewMatrix(); } +const XMFLOAT3& eg3d::Cam::getViewDirection() const +{ + return mVViewDirection; +} + // ----------------------------- eg3d::ProjParams::ProjParams() { diff --git a/Cam.h b/Cam.h index 6e88f6c..3ceaccf 100644 --- a/Cam.h +++ b/Cam.h @@ -64,6 +64,8 @@ namespace eg3d { void strafe(float ds); // kamera mozgatása a nézetre merőlegesen void yaw(float dphi); // kamera forgatása void pitch(float drho); // kamera billentése + + const XMFLOAT3& getViewDirection() const; // nézet irányának elkérése }; } diff --git a/DXWindow.cpp b/DXWindow.cpp index 4f17902..c6c30f3 100644 --- a/DXWindow.cpp +++ b/DXWindow.cpp @@ -384,6 +384,22 @@ namespace eg3d { XMFLOAT3 camPos = pCam->getViewParams().position; constantBuffer.camPosition = { camPos.x, camPos.y, camPos.z, 0.0 }; + + // megvilágítás iránya + //XMVECTOR lightDir = XMVectorSet(1.0, 1.0, 1.0, 0.0); + XMFLOAT3 viewDir = pCam->getViewDirection(); + XMVECTOR lightDir; + lightDir = XMLoadFloat3(&viewDir); + lightDir = -XMVector4Normalize(lightDir); + //lightDir = XMVector4Transform(lightDir, XMMatrixRotationY(lightAngle)); + lightDir = XMVector4Transform(lightDir, XMMatrixRotationY(0)); + XMStoreFloat4(&constantBuffer.lightDir, lightDir); + + // fény iránya + lightAngle += 0.01f; + if (lightAngle > XM_2PI) { + lightAngle -= XM_2PI; + } } bufferMapCopy(cbResource, static_cast(&constantBuffer), sizeof(constantBuffer)); diff --git a/DXWindow.h b/DXWindow.h index 20b48e2..fbd1993 100644 --- a/DXWindow.h +++ b/DXWindow.h @@ -86,6 +86,9 @@ namespace eg3d { // virtuális kamera Cam * pCam; + + // egyebek + float lightAngle = 0.0f; void initDrawObjects(); // kirajzolandó objektumok inicializálása diff --git a/RUNTIME/shaders/basic_shader.hlsl b/RUNTIME/shaders/basic_shader.hlsl index 92c30df..26a82f2 100644 --- a/RUNTIME/shaders/basic_shader.hlsl +++ b/RUNTIME/shaders/basic_shader.hlsl @@ -7,7 +7,8 @@ struct VSInput // vertex shader bemeneti strukt struct VSOutput // vertex shader kimeneti struktúra { float4 position_H : SV_Position; - float3 color : COLOR; + float3 diffuse : DIFFUSE; + float3 ambient : AMBIENT; float3 worldPosition : WORLDPOS; }; @@ -17,6 +18,8 @@ cbuffer CFrame : register(b0) float4x4 viewMatrix; float4x4 projMatrix; float4 camPosition; + + float4 lightDir; }; cbuffer CEntity : register(b1) { @@ -32,12 +35,18 @@ VSOutput vs_main(VSInput input) output.position_H = mul(worldCoords, mul(viewMatrix, projMatrix)); // TODO fények - float3 L = normalize(float3(1.0, 1.0, 0.5)); + float3 L = lightDir; float3 I = float3(1.0, 1.0, 0.8); - float3 md = float3(1.0, 0.1, 0.1); + float3 md = color.xyz; //float3(1.0, 0.9, 0.9); + float3 Ia = float3(0.2, 0.2, 0.2); //float3 p = float3(1440, 370, -1365); - output.color = (I * color.xyz) * max(dot(input.normal, L), 0); + output.diffuse = (I * md) * max(dot(input.normal, L), 0); + output.ambient = (Ia * md); + + + /* / length(worldCoords.xyz - + camPosition.xyz) * 200;*/ output.worldPosition = worldCoords; @@ -54,8 +63,9 @@ PSOutput ps_main(VSOutput input) PSOutput output; //output.color = float4(0.0f, 1.0f, 0.0f, 1.0f); - output.color = float4(input.color, 1.0f) / length(input.worldPosition - - camPosition.xyz) * 200; + output.color = float4(input.diffuse, 1.0f) / length(input.worldPosition - + camPosition.xyz) * 200 + float4(input.ambient, 1.0); + //output.color = float4(input.color, 1.0); return output; } diff --git a/const_bufs.h b/const_bufs.h index 23e1fa4..0485401 100644 --- a/const_bufs.h +++ b/const_bufs.h @@ -15,6 +15,8 @@ struct CB_Frame DirectX::XMFLOAT4X4 viewMatrix; // nézet-mátrix DirectX::XMFLOAT4X4 projMatrix; // vetítési mátrix DirectX::XMFLOAT4 camPosition; // kamerapozíció + + DirectX::XMFLOAT4 lightDir; // fény iránya }; struct CB_Entity {