Files
hwr2023_a/GDP/D3D11/main.cpp
T
2025-12-03 15:50:25 +01:00

115 lines
2.8 KiB
C++

#include <Windows.h>
#include <random>
#include "Window.h"
#include "Direct3D.h"
#include "Mesh.h"
#include "Material.h"
#include "Camera.h"
#include "Time.h"
#include "Light.h"
#include "Utilities.h"
int WINAPI WinMain(
HINSTANCE hInstance, // handle to instance of application
HINSTANCE hPrevInstance, // deprecated
PSTR szCmdLine, // command line
int nCmdShow // how should the window be opened (minimized, maximized or normal)
)
{
// some constant values
const UINT width = 640;
const UINT height = 480;
const BOOL isFullscreen = FALSE;
const INT r = 0;
const INT g = 0;
const INT b = 0;
UINT error = 0; // error code for identifying errors
// 1. create window
Window window = {};
error = window.init(hInstance, width, height);
CheckError(error);
// 2. create connection to Direct3D
Direct3D d3d = {};
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
CheckError(error);
// 3. create a mesh
Mesh mesh = {};
error = mesh.init(d3d.getDevice());
CheckError(error);
// 4. create a camera
Camera camera = {};
error = camera.init(width, height);
CheckError(error);
// 5. create time
Time time = {};
error = time.init();
CheckError(error);
// 6. create a material
Material material = {};
error = material.init(d3d.getDevice(), TEXT("wall.jpg"));
CheckError(error);
// 7. create light source
Light light = {};
Light::LightBuffer lightData = {};
//directional light
lightData.LightVector = { 1.0f, -1.0f, 1.0f };
//lightData.Type = D3DLIGHT_POINT;
//lightData.Position = { 0.0f, 0.0f, -1.0f };
//lightData.Range = 10.0f;
//lightData.Falloff = 1.0f;
//lightData.Attenuation0 = 1.0f;
//lightData.Attenuation1 = 0.2f;
//lightData.Attenuation2 = 0.1f;
lightData.DiffuseColor = { 1.0f, 1.0f, 1.0f, 1.0f };
lightData.SpecularColor = { 1.0f, 0.0f, 1.0f, 1.0f };
lightData.LightIntensity = 1.0f;
lightData.Phong = 8.0f;
lightData.CameraPosition = { 0.0f, 0.0f, -1.0f };
error = light.init(d3d.getDevice(), lightData);
CheckError(error);
// 8. run application
// random colors
//std::default_random_engine e;
//std::uniform_real_distribution<FLOAT> d(0.0f, 1.0f);
while (window.run())
{
// 8.1. update objects
time.update();
mesh.update(time.getDeltaTime());
camera.update(time.getDeltaTime());
light.update(time.getDeltaTime());
// 8.2. render objects
//d3d.beginScene(d(e), d(e), d(e));
d3d.beginScene(r, g, b);
light.render(d3d.getDeviceContext());
material.setMatrixBuffer(d3d.getDeviceContext(), mesh.getWorldMatrix(), camera.getViewMatrix(), camera.getProjectionMatrix());
material.render(d3d.getDeviceContext());
mesh.render(d3d.getDeviceContext());
d3d.endScene();
}
// 9. tidy up
light.deInit();
time.deInit();
camera.deInit();
material.deInit();
mesh.deInit();
d3d.deInit();
window.deInit();
return 0;
}