Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Include and link appropriate libraries and headers//
- #pragma comment(lib, "d3d11.lib")
- #pragma comment(lib, "d3dx11.lib")
- #pragma comment(lib, "d3dx10.lib")
- #include <windows.h>
- #include <d3d11.h>
- #include <d3dx11.h>
- #include <d3dx10.h>
- #include <xnamath.h>
- #include <time.h>
- //Global Declarations - Interfaces//
- IDXGISwapChain* SwapChain;
- ID3D11Device* d3d11Device;
- ID3D11DeviceContext* d3d11DevCon;
- ID3D11RenderTargetView* renderTargetView;
- ID3D11Buffer* squareIndexBuffer;
- ID3D11DepthStencilView* depthStencilView;
- ID3D11Texture2D* depthStencilBuffer;
- ID3D11Buffer* squareVertBuffer;
- ID3D11VertexShader* VS;
- ID3D11PixelShader* PS;
- ID3D10Blob* VS_Buffer;
- ID3D10Blob* PS_Buffer;
- ID3D11InputLayout* vertLayout;
- ID3D11Buffer* cbPerObjectBuffer;
- ///////////////**************new**************////////////////////
- ID3D11RasterizerState* WireFrame;
- ///////////////**************new**************////////////////////
- //Global Declarations - Others//
- LPCTSTR WndClassName = L"firstwindow";
- HWND hwnd = NULL;
- HRESULT hr;
- const int Width = 1280;
- const int Height = 720;
- XMMATRIX WVP;
- XMMATRIX terrainWorld;
- XMMATRIX cube2World;
- XMMATRIX camView;
- XMMATRIX camProjection;
- XMVECTOR camPosition;
- XMVECTOR camTarget;
- XMVECTOR camUp;
- XMMATRIX Rotation;
- XMMATRIX Scale;
- XMMATRIX Translation;
- float rot = 0.01f;
- ID3D11ShaderResourceView* tex;
- ID3D11ShaderResourceView* tex2;
- ID3D11SamplerState* texSamplerState;
- D3D11_SAMPLER_DESC sampDesc;
- //Function Prototypes//
- bool InitializeDirect3d11App(HINSTANCE hInstance);
- void CleanUp();
- bool InitScene();
- void UpdateScene();
- void DrawScene();
- bool InitializeWindow(HINSTANCE hInstance,
- int ShowWnd,
- int width, int height,
- bool windowed);
- int messageloop();
- LRESULT CALLBACK WndProc(HWND hWnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam);
- //Create effects constant buffer's structure//
- struct cbPerObject
- {
- XMMATRIX WVP;
- float t;
- };
- cbPerObject cbPerObj;
- //Vertex Structure and Vertex Layout (Input Layout)//
- struct Vertex //Overloaded Vertex Structure
- {
- Vertex() {}
- Vertex(float x, float y, float z, float u, float v) : pos(x, y, z), texCoord(u, v) {}
- XMFLOAT3 pos;
- XMFLOAT2 texCoord;
- };
- float vertexHeights[65][65];
- Vertex terrainVertices[(64 * 64) * 6];
- D3D11_INPUT_ELEMENT_DESC layout[] =
- {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
- { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
- };
- UINT numElements = ARRAYSIZE(layout);
- int seed;
- float getNoise(int x, int z) {
- srand(x * 49632 + z * 325176 + seed);
- return ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
- }
- float getSmoothNoise(int x, int z) {
- float corners = (getNoise(x - 1, z - 1) + getNoise(x + 1, z - 1) + getNoise(x - 1, z + 1) + getNoise(x + 1, z + 1)) / 16.0f;
- float sides = (getNoise(x - 1, z) + getNoise(x + 1, z) + getNoise(x, z - 1) + getNoise(x, z + 1)) / 8.0f;
- float center = getNoise(x, z) / 4.0f;
- return corners + sides + center;
- }
- float interpolate(float a, float b, float blend) {
- double theta = blend * XM_PI;
- float f = (float)(1.0f - cos(theta)) * 0.5f;
- return a * (1.0f - f) + b * f;
- }
- float getInterpolatedNoise(float x, float z) {
- int intX = (int)x;
- int intZ = (int)z;
- float fracX = x - intX;
- float fracZ = z - intZ;
- float v1 = getSmoothNoise(intX, intZ);
- float v2 = getSmoothNoise(intX + 1, intZ);
- float v3 = getSmoothNoise(intX, intZ + 1);
- float v4 = getSmoothNoise(intX + 1, intZ + 1);
- float i1 = interpolate(v1, v2, fracX);
- float i2 = interpolate(v3, v4, fracX);
- return interpolate(i1, i2, fracZ);
- }
- #define AMPLITUDE 20.0f
- float generateHeight(int x, int z) {
- return getInterpolatedNoise(x / 8.0f, z / 8.0f) * AMPLITUDE;
- }
- int WINAPI WinMain(HINSTANCE hInstance, //Main windows function
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nShowCmd)
- {
- srand(time(NULL));
- seed = rand() % 1000000000;
- for (int x = 0; x < 65; x++) {
- for (int z = 0; z < 65; z++) {
- vertexHeights[x][z] = generateHeight(x, z);
- }
- }
- if (!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
- {
- MessageBox(0, L"Window Initialization - Failed",
- L"Error", MB_OK);
- return 0;
- }
- if (!InitializeDirect3d11App(hInstance)) //Initialize Direct3D
- {
- MessageBox(0, L"Direct3D Initialization - Failed",
- L"Error", MB_OK);
- return 0;
- }
- if (!InitScene()) //Initialize our scene
- {
- MessageBox(0, L"Scene Initialization - Failed",
- L"Error", MB_OK);
- return 0;
- }
- messageloop();
- CleanUp();
- return 0;
- }
- bool InitializeWindow(HINSTANCE hInstance,
- int ShowWnd,
- int width, int height,
- bool windowed)
- {
- WNDCLASSEX wc;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = NULL;
- wc.cbWndExtra = NULL;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = WndClassName;
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClassEx(&wc))
- {
- MessageBox(NULL, L"Error registering class",
- L"Error", MB_OK | MB_ICONERROR);
- return 1;
- }
- hwnd = CreateWindowEx(
- NULL,
- WndClassName,
- L"Lesson 4 - Begin Drawing",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- width, height,
- NULL,
- NULL,
- hInstance,
- NULL
- );
- if (!hwnd)
- {
- MessageBox(NULL, L"Error creating window",
- L"Error", MB_OK | MB_ICONERROR);
- return 1;
- }
- ShowWindow(hwnd, ShowWnd);
- UpdateWindow(hwnd);
- return true;
- }
- bool InitializeDirect3d11App(HINSTANCE hInstance)
- {
- //Describe our SwapChain Buffer
- DXGI_MODE_DESC bufferDesc;
- ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
- bufferDesc.Width = Width;
- bufferDesc.Height = Height;
- bufferDesc.RefreshRate.Numerator = 60;
- bufferDesc.RefreshRate.Denominator = 1;
- bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
- bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
- //Describe our SwapChain
- DXGI_SWAP_CHAIN_DESC swapChainDesc;
- ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
- swapChainDesc.BufferDesc = bufferDesc;
- swapChainDesc.SampleDesc.Count = 1;
- swapChainDesc.SampleDesc.Quality = 0;
- swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swapChainDesc.BufferCount = 1;
- swapChainDesc.OutputWindow = hwnd;
- swapChainDesc.Windowed = TRUE;
- swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- //Create our SwapChain
- hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
- D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);
- //Create our BackBuffer
- ID3D11Texture2D* BackBuffer;
- hr = SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer);
- //Create our Render Target
- hr = d3d11Device->CreateRenderTargetView(BackBuffer, NULL, &renderTargetView);
- BackBuffer->Release();
- //Describe our Depth/Stencil Buffer
- D3D11_TEXTURE2D_DESC depthStencilDesc;
- depthStencilDesc.Width = Width;
- depthStencilDesc.Height = Height;
- depthStencilDesc.MipLevels = 1;
- depthStencilDesc.ArraySize = 1;
- depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
- depthStencilDesc.SampleDesc.Count = 1;
- depthStencilDesc.SampleDesc.Quality = 0;
- depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
- depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
- depthStencilDesc.CPUAccessFlags = 0;
- depthStencilDesc.MiscFlags = 0;
- //Create the Depth/Stencil View
- d3d11Device->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
- d3d11Device->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
- //Set our Render Target
- d3d11DevCon->OMSetRenderTargets(1, &renderTargetView, depthStencilView);
- return true;
- }
- void CleanUp()
- {
- //Release the COM Objects we created
- SwapChain->Release();
- d3d11Device->Release();
- d3d11DevCon->Release();
- renderTargetView->Release();
- squareVertBuffer->Release();
- VS->Release();
- PS->Release();
- VS_Buffer->Release();
- PS_Buffer->Release();
- vertLayout->Release();
- depthStencilView->Release();
- depthStencilBuffer->Release();
- cbPerObjectBuffer->Release();
- ///////////////**************new**************////////////////////
- WireFrame->Release();
- ///////////////**************new**************////////////////////
- tex->Release();
- texSamplerState->Release();
- }
- D3D11_RASTERIZER_DESC wfdesc;
- D3D11_SUBRESOURCE_DATA vertexBufferData;
- D3D11_BUFFER_DESC vertexBufferDesc;
- bool InitScene()
- {
- //Compile Shaders from shader file
- hr = D3DX11CompileFromFile(L"Terrain.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
- hr = D3DX11CompileFromFile(L"Terrain.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);
- //Create the Shader Objects
- hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
- hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);
- //Set Vertex and Pixel Shaders
- d3d11DevCon->VSSetShader(VS, 0, 0);
- d3d11DevCon->PSSetShader(PS, 0, 0);
- for (int i = 0; i < 64 * 64; i++) {
- terrainVertices[(i * 6)] = Vertex{(float)(i % 64), vertexHeights[(i % 64)][(i / 64) + 1], (float)((i / 64) + 1), 0.0f, 1.0f};
- terrainVertices[(i * 6) + 1] = Vertex{(float)((i % 64) + 1), vertexHeights[(i % 64) + 1][(i / 64)], (float)(i / 64), 1.0f, 0.0f};
- terrainVertices[(i * 6) + 2] = Vertex{(float)(i % 64), vertexHeights[(i % 64)][(i / 64)], (float)(i / 64), 0.0f, 0.0f,};
- terrainVertices[(i * 6) + 3] = Vertex{(float)(i % 64), vertexHeights[(i % 64)][(i / 64) + 1], (float)((i / 64) + 1), 0.0f, 1.0f};
- terrainVertices[(i * 6) + 4] = Vertex{(float)((i % 64) + 1), vertexHeights[(i % 64) + 1][(i / 64) + 1], (float)((i / 64) + 1), 1.0f, 1.0f};
- terrainVertices[(i * 6) + 5] = Vertex{(float)((i % 64) + 1), vertexHeights[(i % 64) + 1][(i / 64)], (float)(i / 64), 1.0f, 0.0f};
- }
- ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
- vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- vertexBufferDesc.ByteWidth = sizeof(Vertex) * (64*64)*6;
- vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
- vertexBufferDesc.CPUAccessFlags = 0;
- vertexBufferDesc.MiscFlags = 0;
- ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
- vertexBufferData.pSysMem = terrainVertices;
- hr = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &squareVertBuffer);
- //Set the vertex buffer
- UINT stride = sizeof(Vertex);
- UINT offset = 0;
- d3d11DevCon->IASetVertexBuffers(0, 1, &squareVertBuffer, &stride, &offset);
- //Create the Input Layout
- hr = d3d11Device->CreateInputLayout(layout, numElements, VS_Buffer->GetBufferPointer(),
- VS_Buffer->GetBufferSize(), &vertLayout);
- //Set the Input Layout
- d3d11DevCon->IASetInputLayout(vertLayout);
- //Set Primitive Topology
- d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- //Create the Viewport
- D3D11_VIEWPORT viewport;
- ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
- viewport.TopLeftX = 0;
- viewport.TopLeftY = 0;
- viewport.Width = Width;
- viewport.Height = Height;
- viewport.MinDepth = 0.0f;
- viewport.MaxDepth = 1.0f;
- //Set the Viewport
- d3d11DevCon->RSSetViewports(1, &viewport);
- //Create the buffer to send to the cbuffer in effect file
- D3D11_BUFFER_DESC cbbd;
- ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));
- cbbd.Usage = D3D11_USAGE_DEFAULT;
- cbbd.ByteWidth = sizeof(cbPerObject);
- cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- cbbd.CPUAccessFlags = 0;
- cbbd.MiscFlags = 0;
- hr = d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);
- //Camera information
- camPosition = XMVectorSet(0.0f, 10.0f, 0.0f, 0.0f);
- camTarget = XMVectorSet(32.0f, 0.0f, 32.0f, 0.0f);
- camUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
- //Set the View matrix
- camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
- //Set the Projection matrix
- camProjection = XMMatrixPerspectiveFovLH(0.4f*3.14f, Width / Height, 1.0f, 1000.0f);
- //camProjection._11 = -(camProjection._11);
- ///////////////**************new**************////////////////////
- ZeroMemory(&wfdesc, sizeof(D3D11_RASTERIZER_DESC));
- wfdesc.FillMode = D3D11_FILL_SOLID;
- wfdesc.CullMode = D3D11_CULL_NONE;
- hr = d3d11Device->CreateRasterizerState(&wfdesc, &WireFrame);
- d3d11DevCon->RSSetState(WireFrame);
- ///////////////**************new**************////////////////////
- ZeroMemory(&sampDesc, sizeof(sampDesc));
- sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
- sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
- sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
- sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
- sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
- sampDesc.MinLOD = 0;
- sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
- d3d11Device->CreateSamplerState(&sampDesc, &texSamplerState);
- D3DX11CreateShaderResourceViewFromFile(d3d11Device, L"res/seamless.png",NULL, NULL, &tex, NULL);
- D3DX11CreateShaderResourceViewFromFile(d3d11Device, L"res/shadowmap.png", NULL, NULL, &tex2, NULL);
- return true;
- }
- void UpdateScene()
- {
- //Keep the cubes rotating
- rot += .005f;
- camPosition = XMVectorSet(32 + (32 * sin(rot * 0.05f)), 10.0f, 32 + (32 * cos(rot * 0.05f)), 0.0f);
- //camPosition = XMVectorSet(0.0f, 100.0f, 0.0f, 0.0f);
- //Set the View matrix
- camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
- //Define cube1's world space matrix
- XMVECTOR rotaxis = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
- //Reset cube2World
- cube2World = XMMatrixIdentity();
- //Define cube2's world space matrix
- Rotation = XMMatrixRotationAxis(rotaxis, 0.0f);
- Scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
- //Scale = XMMatrixScaling(100.0f, 100.0f, 100.0f);
- //Set cube2's world space matrix
- cube2World = Rotation * Scale;
- }
- float t = 0.0f;
- void DrawScene()
- {
- //Clear our backbuffer
- float bgColor[4] = { (0.0f, 0.0f, 0.0f, 0.0f) };
- d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);
- //Refresh the Depth/Stencil view
- d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
- t += 0.00001f;
- WVP = cube2World * camView * camProjection;
- cbPerObj.WVP = XMMatrixTranspose(WVP);
- cbPerObj.t = t;
- d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
- d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);
- d3d11DevCon->PSSetShaderResources(0, 1, &tex);
- d3d11DevCon->PSSetShaderResources(1, 1, &tex2);
- d3d11DevCon->PSSetSamplers(0, 1, &texSamplerState);
- d3d11DevCon->Draw((64 * 64)*6, 0);
- //Present the backbuffer to the screen
- SwapChain->Present(0, 0);
- }
- int messageloop() {
- MSG msg;
- ZeroMemory(&msg, sizeof(MSG));
- while (true)
- {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- break;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- } else {
- // run game code
- UpdateScene();
- DrawScene();
- }
- }
- return msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
- {
- switch (msg)
- {
- case WM_KEYDOWN:
- if (wParam == VK_ESCAPE) {
- DestroyWindow(hwnd);
- }
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd,
- msg,
- wParam,
- lParam);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement