Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <dxgi.h>
- #include <d3d10_1.h>
- #include <d3dx10.h>
- #define SAFE_RELEASE(p) {if(p){ p->Release(); p = NULL;}}
- typedef struct
- {
- D3DXVECTOR4 Pos;
- D3DXVECTOR3 Col;
- } SimpleVertex;
- typedef struct
- {
- D3DXMATRIX WorldViewProjection;
- D3DXVECTOR4 Data;
- } ConstBuffer;
- LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
- void InitWindow();
- void InitDevice();
- void Render();
- void Cleanup();
- HWND hwnd = NULL;
- HINSTANCE hInstance = NULL;
- ID3D10Device1 * device = NULL;
- ID3D10Texture2D * tex = NULL;
- ID3D10RenderTargetView * rtv = NULL;
- ID3D10Buffer * vb = NULL;
- ID3D10Buffer * ib = NULL;
- ID3D10Buffer * cb = NULL;
- ID3D10VertexShader * vs = NULL;
- ID3D10PixelShader * ps = NULL;
- ID3D10InputLayout * il = NULL;
- UINT width = 0;
- UINT height = 0;
- float alpha = 0.75f;
- D3DXMATRIX world, view, projection;
- LPCTSTR wndClassName = TEXT("MyWindowCLass");
- LPCTSTR wndTitle = TEXT("Window");
- LPCTSTR shaderLocation = TEXT("..\\Shaders.fx");
- int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int nCmdShow)
- {
- hInstance = _hInstance;
- InitWindow();
- InitDevice();
- MSG msg = {0};
- while(GetMessage(&msg, NULL, 0, 0) > 0)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- Render();
- }
- Cleanup();
- return 0;
- }
- void Render()
- {
- UINT stride = sizeof(SimpleVertex);
- UINT offset = 0;
- device->IASetVertexBuffers(0, 1, &vb, &stride, &offset);
- device->IASetIndexBuffer(ib, DXGI_FORMAT_R32_UINT, 0);
- device->OMSetRenderTargets(1, &rtv, NULL);
- device->VSSetShader(vs);
- device->PSSetShader(ps);
- device->IASetInputLayout(il);
- device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- ConstBuffer buffer;
- buffer.WorldViewProjection = world * view * projection;
- D3DXMatrixTranspose(&buffer.WorldViewProjection, &buffer.WorldViewProjection);
- buffer.Data[0] = alpha;
- device->UpdateSubresource(cb, 0, NULL, &buffer, 0, 0);
- device->VSSetConstantBuffers(0, 1, &cb);
- FLOAT clearColor[4] = {0.0f, 0.0f, 0.0f, 0.0f};
- device->ClearRenderTargetView(rtv, clearColor);
- device->DrawIndexed(36, 0, 0);
- IDXGISurface1 * surface = NULL;
- HRESULT hr = tex->QueryInterface(__uuidof(surface), (void**)&surface);
- if(FAILED(hr))
- throw hr;
- HDC hdc;
- hr = surface->GetDC(FALSE, &hdc);
- if(FAILED(hr))
- throw hr;
- BLENDFUNCTION blend;
- SecureZeroMemory(&blend, sizeof(blend));
- blend.AlphaFormat = AC_SRC_ALPHA;
- blend.SourceConstantAlpha = 255;
- POINT srcPos = {0};
- POINT wndPos = {0};
- SIZE size = {width, height};
- UPDATELAYEREDWINDOWINFO info;
- SecureZeroMemory(&info, sizeof(info));
- info.cbSize = sizeof(info);
- info.dwFlags = ULW_ALPHA;
- info.hdcSrc = hdc;
- info.pblend = &blend;
- info.pptDst = &wndPos;
- info.pptSrc = &srcPos;
- info.psize = &size;
- if(FAILED(UpdateLayeredWindowIndirect(hwnd, &info)))
- throw GetLastError();
- surface->ReleaseDC(NULL);
- surface->Release();
- }
- void InitWindow()
- {
- WNDCLASSEX wc;
- SecureZeroMemory(&wc, sizeof(wc));
- wc.cbSize = sizeof(wc);
- wc.hbrBackground = NULL;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hInstance = hInstance;
- wc.lpfnWndProc = MyWindowProc;
- wc.lpszClassName = wndClassName;
- wc.style = 0;
- if(!RegisterClassEx(&wc))
- throw GetLastError();
- RECT rect;
- GetWindowRect(GetDesktopWindow(), &rect);
- if(!(hwnd = CreateWindowEx(
- WS_EX_LAYERED,
- wndClassName,
- wndTitle,
- WS_POPUP,
- 0,
- 0,
- width = rect.right,
- height = rect.bottom,
- NULL,
- NULL,
- hInstance,
- NULL)))
- throw GetLastError();
- ShowWindow(hwnd, SW_MAXIMIZE);
- }
- void InitDevice()
- {
- HRESULT hr;
- D3D10_CREATE_DEVICE_FLAG flag =
- (D3D10_CREATE_DEVICE_FLAG)(D3D10_CREATE_DEVICE_BGRA_SUPPORT | D3D10_CREATE_DEVICE_SINGLETHREADED );
- #ifdef DEBUG
- flag = (D3D10_CREATE_DEVICE_FLAG)(flag | D3D10_CREATE_DEVICE_DEBUG);
- #endif
- hr = D3D10CreateDevice1(
- NULL,
- D3D10_DRIVER_TYPE_HARDWARE,
- NULL,
- flag,
- D3D10_FEATURE_LEVEL_10_1,
- D3D10_1_SDK_VERSION,
- &device);
- if(FAILED(hr))
- throw hr;
- D3D10_TEXTURE2D_DESC td;
- SecureZeroMemory(&td, sizeof(td));
- td.ArraySize = 1;
- td.BindFlags = D3D10_BIND_RENDER_TARGET;
- td.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
- td.Width = width;
- td.Height = height;
- td.SampleDesc.Count = 1;
- td.MiscFlags = D3D10_RESOURCE_MISC_GDI_COMPATIBLE;
- td.MipLevels = 1;
- hr = device->CreateTexture2D(&td, NULL, &tex);
- if(FAILED(hr))
- throw hr;
- hr = device->CreateRenderTargetView(tex, NULL, &rtv);
- if(FAILED(hr))
- throw hr;
- ID3D10Blob * blob;
- hr = D3DX10CompileFromFile(
- shaderLocation,
- NULL,
- NULL,
- "PS",
- "ps_4_1",
- 0,
- 0,
- NULL,
- &blob,
- NULL,
- NULL);
- if(FAILED(hr))
- throw hr;
- hr = device->CreatePixelShader(blob->GetBufferPointer(), blob->GetBufferSize(), &ps);
- if(FAILED(hr))
- throw hr;
- blob->Release();
- hr = D3DX10CompileFromFile(
- shaderLocation,
- NULL,
- NULL,
- "VS",
- "vs_4_1",
- 0,
- 0,
- NULL,
- &blob,
- NULL,
- NULL);
- if(FAILED(hr))
- throw hr;
- hr = device->CreateVertexShader(blob->GetBufferPointer(), blob->GetBufferSize(), &vs);
- if(FAILED(hr))
- throw hr;
- D3D10_INPUT_ELEMENT_DESC layout[] =
- {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
- { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, sizeof(D3DXVECTOR4), D3D10_INPUT_PER_VERTEX_DATA, 0 },
- };
- hr = device->CreateInputLayout(layout, sizeof(layout)/sizeof(layout[0]), blob->GetBufferPointer(), blob->GetBufferSize(), &il);
- if(FAILED(hr))
- throw hr;
- blob->Release();
- SimpleVertex vertices[] =
- {
- {D3DXVECTOR4( -1.0f, -1.0f, -1.0f, 1.0f), D3DXVECTOR3( 1.0f, 0.0f, 0.6f)},
- {D3DXVECTOR4( -1.0f, 1.0f, -1.0f, 1.0f), D3DXVECTOR3( 0.0f, 1.0f, 0.0f)},
- {D3DXVECTOR4( 1.0f, 1.0f, -1.0f, 1.0f), D3DXVECTOR3( 0.0f, 0.75f, 1.0f)},
- {D3DXVECTOR4( 1.0f, -1.0f, -1.0f, 1.0f), D3DXVECTOR3( 1.0f, 0.84f, 0.0f)},
- {D3DXVECTOR4( 1.0f, -1.0f, 1.0f, 1.0f), D3DXVECTOR3( 0.0f, 1.0f, 0.5f)},
- {D3DXVECTOR4( 1.0f, 1.0f, 1.0f, 1.0f), D3DXVECTOR3( 0.94f, 0.5f, 0.5f)},
- {D3DXVECTOR4( -1.0f, 1.0f, 1.0f, 1.0f), D3DXVECTOR3( 1.0f, 1.0f, 0.0f)},
- {D3DXVECTOR4( -1.0f, -1.0f, 1.0f, 1.0f), D3DXVECTOR3( 0.41f, 0.35f, 0.8f)},
- };
- D3D10_SUBRESOURCE_DATA sd;
- SecureZeroMemory(&sd, sizeof(sd));
- sd.pSysMem = vertices;
- D3D10_BUFFER_DESC bd;
- SecureZeroMemory(&bd, sizeof(bd));
- bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
- bd.ByteWidth = sizeof(vertices);
- hr = device->CreateBuffer(&bd, &sd, &vb);
- if(FAILED(hr))
- throw hr;
- DWORD indices[] =
- {
- //front
- 0, 1, 2,
- 2, 3, 0,
- //back
- 4, 5, 6,
- 6, 7, 4,
- //top
- 1, 6, 5,
- 5, 2, 1,
- //bottom
- 3, 4, 7,
- 7, 0, 3,
- //left
- 7, 6, 1,
- 1, 0, 7,
- //right
- 3, 2, 5,
- 5, 4, 3,
- };
- sd.pSysMem = indices;
- bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
- bd.ByteWidth = sizeof(indices);
- hr = device->CreateBuffer(&bd, &sd, &ib);
- if(FAILED(hr))
- throw hr;
- D3D10_VIEWPORT vp;
- vp.Height = height;
- vp.MaxDepth = 1.0f;
- vp.MinDepth = 0.0f;
- vp.Width = width;
- vp.TopLeftX = 0;
- vp.TopLeftY = 0;
- device->RSSetViewports(1, &vp);
- D3DXVECTOR3 eye(0.0f, 0.0f, -5.0f);
- D3DXVECTOR3 at(0.0f, 0.0f, 0.0f);
- D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
- D3DXMatrixLookAtLH(&view, &eye, &at, &up);
- D3DXMatrixPerspectiveFovLH(&projection, (FLOAT)D3DX_PI/4, width/(FLOAT)height, 0.01f, 100.0f);
- D3DXMatrixIdentity(&world);
- bd.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
- bd.ByteWidth = sizeof(D3DXMATRIX)+sizeof(D3DXVECTOR4);
- hr = device->CreateBuffer(&bd, NULL, &cb);
- if(FAILED(hr))
- throw hr;
- }
- void Cleanup()
- {
- if(device) device->ClearState();
- SAFE_RELEASE(tex);
- SAFE_RELEASE(rtv);
- SAFE_RELEASE(vb);
- SAFE_RELEASE(ib);
- SAFE_RELEASE(vs);
- SAFE_RELEASE(ps);
- SAFE_RELEASE(il);
- SAFE_RELEASE(device);
- }
- LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_MOUSEWHEEL:
- {
- __int16 delta = HIWORD(wParam);
- delta /= WHEEL_DELTA;
- alpha += (delta / 20.0f);
- if(alpha > 1.0f)
- (alpha = 1.0f);
- else if (alpha < 0.0f)
- alpha = 0.0f;
- }
- case WM_MOUSEMOVE:
- {
- static int x = 0, y = 0;
- if( wParam & 0x0001) //Left button is down
- {
- if(x && y)
- {
- D3DXMATRIX rotate;
- D3DXMatrixRotationYawPitchRoll(
- &rotate,
- (FLOAT)(x - LOWORD(lParam))/360,
- (FLOAT)(y - HIWORD(lParam))/360,
- 0.0f);
- D3DXMatrixMultiply(&world, &world, &rotate);
- }
- x = LOWORD(lParam);
- y = HIWORD(lParam);
- }
- else
- {
- x = y = 0;
- }
- }
- case WM_KEYDOWN:
- switch(wParam)
- {
- case VK_ESCAPE:
- PostQuitMessage(0);
- break;
- }
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement