Advertisement
Guest User

FancyCube.cpp

a guest
Feb 7th, 2011
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.94 KB | None | 0 0
  1. #include <windows.h>
  2. #include <dxgi.h>
  3. #include <d3d10_1.h>
  4. #include <d3dx10.h>
  5.  
  6. #define SAFE_RELEASE(p) {if(p){ p->Release(); p = NULL;}}
  7.  
  8. typedef struct
  9. {
  10.     D3DXVECTOR4 Pos;
  11.     D3DXVECTOR3 Col;
  12. } SimpleVertex;
  13.  
  14. typedef struct
  15. {
  16.     D3DXMATRIX WorldViewProjection;
  17.     D3DXVECTOR4 Data;
  18. } ConstBuffer;
  19.  
  20. LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  21. void InitWindow();
  22. void InitDevice();
  23. void Render();
  24. void Cleanup();
  25.  
  26. HWND hwnd = NULL;
  27. HINSTANCE hInstance = NULL;
  28. ID3D10Device1 * device = NULL;
  29. ID3D10Texture2D * tex = NULL;
  30. ID3D10RenderTargetView * rtv  = NULL;
  31. ID3D10Buffer * vb = NULL;
  32. ID3D10Buffer * ib = NULL;
  33. ID3D10Buffer * cb = NULL;
  34. ID3D10VertexShader * vs = NULL;
  35. ID3D10PixelShader * ps = NULL;
  36. ID3D10InputLayout * il = NULL;
  37. UINT width = 0;
  38. UINT height = 0;
  39. float alpha = 0.75f;
  40. D3DXMATRIX world, view, projection;
  41. LPCTSTR wndClassName = TEXT("MyWindowCLass");
  42. LPCTSTR wndTitle = TEXT("Window");
  43. LPCTSTR shaderLocation = TEXT("..\\Shaders.fx");
  44.  
  45. int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int nCmdShow)
  46. {
  47.     hInstance = _hInstance;
  48.    
  49.     InitWindow();
  50.     InitDevice();
  51.  
  52.     MSG msg = {0};
  53.     while(GetMessage(&msg, NULL, 0, 0) > 0)
  54.     {
  55.         TranslateMessage(&msg);
  56.         DispatchMessage(&msg); 
  57.         Render();
  58.     }
  59.  
  60.     Cleanup();
  61.  
  62.     return 0;
  63. }
  64.  
  65. void Render()
  66. {  
  67.     UINT stride = sizeof(SimpleVertex);
  68.     UINT offset = 0;   
  69.     device->IASetVertexBuffers(0, 1, &vb, &stride, &offset);
  70.     device->IASetIndexBuffer(ib, DXGI_FORMAT_R32_UINT, 0);
  71.     device->OMSetRenderTargets(1, &rtv, NULL);
  72.     device->VSSetShader(vs);
  73.     device->PSSetShader(ps);
  74.     device->IASetInputLayout(il);
  75.     device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  76.  
  77.     ConstBuffer buffer;
  78.     buffer.WorldViewProjection = world * view * projection;
  79.     D3DXMatrixTranspose(&buffer.WorldViewProjection, &buffer.WorldViewProjection);
  80.     buffer.Data[0] = alpha;
  81.    
  82.     device->UpdateSubresource(cb, 0, NULL, &buffer, 0, 0);
  83.     device->VSSetConstantBuffers(0, 1, &cb);
  84.  
  85.     FLOAT clearColor[4] = {0.0f, 0.0f, 0.0f, 0.0f};
  86.     device->ClearRenderTargetView(rtv, clearColor);
  87.    
  88.     device->DrawIndexed(36, 0, 0);
  89.    
  90.     IDXGISurface1 * surface = NULL;
  91.  
  92.     HRESULT hr = tex->QueryInterface(__uuidof(surface), (void**)&surface);
  93.     if(FAILED(hr))
  94.         throw hr;
  95.  
  96.     HDC hdc;
  97.     hr = surface->GetDC(FALSE, &hdc);
  98.     if(FAILED(hr))
  99.         throw hr;
  100.  
  101.     BLENDFUNCTION blend;
  102.     SecureZeroMemory(&blend, sizeof(blend));
  103.     blend.AlphaFormat = AC_SRC_ALPHA;
  104.     blend.SourceConstantAlpha = 255;
  105.  
  106.     POINT srcPos = {0};
  107.     POINT wndPos = {0};
  108.     SIZE size = {width, height};
  109.    
  110.     UPDATELAYEREDWINDOWINFO info;
  111.     SecureZeroMemory(&info, sizeof(info));
  112.     info.cbSize = sizeof(info);
  113.     info.dwFlags = ULW_ALPHA;
  114.     info.hdcSrc = hdc;
  115.     info.pblend = &blend;
  116.     info.pptDst = &wndPos;
  117.     info.pptSrc = &srcPos;
  118.     info.psize = &size;
  119.  
  120.     if(FAILED(UpdateLayeredWindowIndirect(hwnd, &info)))
  121.         throw GetLastError();
  122.  
  123.     surface->ReleaseDC(NULL);
  124.     surface->Release();
  125. }
  126.  
  127. void InitWindow()
  128. {
  129.     WNDCLASSEX wc;
  130.     SecureZeroMemory(&wc, sizeof(wc));
  131.     wc.cbSize = sizeof(wc);
  132.     wc.hbrBackground = NULL;
  133.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  134.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  135.     wc.hInstance = hInstance;
  136.     wc.lpfnWndProc = MyWindowProc;
  137.     wc.lpszClassName = wndClassName;
  138.     wc.style = 0;
  139.  
  140.     if(!RegisterClassEx(&wc))
  141.         throw GetLastError();
  142.  
  143.     RECT rect;
  144.     GetWindowRect(GetDesktopWindow(), &rect);
  145.  
  146.     if(!(hwnd = CreateWindowEx(
  147.                     WS_EX_LAYERED,
  148.                     wndClassName,
  149.                     wndTitle,
  150.                     WS_POPUP,
  151.                     0,
  152.                     0,
  153.                     width = rect.right,
  154.                     height = rect.bottom,
  155.                     NULL,
  156.                     NULL,
  157.                     hInstance,
  158.                     NULL)))
  159.         throw GetLastError();
  160.  
  161.     ShowWindow(hwnd, SW_MAXIMIZE);
  162. }
  163.  
  164. void InitDevice()
  165. {
  166.     HRESULT hr;
  167.  
  168.     D3D10_CREATE_DEVICE_FLAG flag =
  169.         (D3D10_CREATE_DEVICE_FLAG)(D3D10_CREATE_DEVICE_BGRA_SUPPORT | D3D10_CREATE_DEVICE_SINGLETHREADED );
  170.  
  171. #ifdef DEBUG
  172.     flag = (D3D10_CREATE_DEVICE_FLAG)(flag | D3D10_CREATE_DEVICE_DEBUG);
  173. #endif
  174.  
  175.     hr = D3D10CreateDevice1(
  176.                 NULL,
  177.                 D3D10_DRIVER_TYPE_HARDWARE,
  178.                 NULL,
  179.                 flag,
  180.                 D3D10_FEATURE_LEVEL_10_1,
  181.                 D3D10_1_SDK_VERSION,
  182.                 &device);
  183.  
  184.     if(FAILED(hr))
  185.         throw hr;
  186.    
  187.     D3D10_TEXTURE2D_DESC td;
  188.     SecureZeroMemory(&td, sizeof(td));
  189.     td.ArraySize = 1;
  190.     td.BindFlags = D3D10_BIND_RENDER_TARGET;
  191.     td.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  192.     td.Width = width;
  193.     td.Height = height;
  194.     td.SampleDesc.Count = 1;
  195.     td.MiscFlags = D3D10_RESOURCE_MISC_GDI_COMPATIBLE;
  196.     td.MipLevels = 1;
  197.  
  198.     hr = device->CreateTexture2D(&td, NULL, &tex);
  199.     if(FAILED(hr))
  200.         throw hr;
  201.    
  202.     hr = device->CreateRenderTargetView(tex, NULL, &rtv);
  203.     if(FAILED(hr))
  204.         throw hr;
  205.  
  206.     ID3D10Blob * blob;
  207.     hr = D3DX10CompileFromFile(
  208.             shaderLocation,
  209.             NULL,
  210.             NULL,
  211.             "PS",
  212.             "ps_4_1",
  213.             0,
  214.             0,
  215.             NULL,
  216.             &blob,
  217.             NULL,
  218.             NULL);
  219.     if(FAILED(hr))
  220.         throw hr;
  221.  
  222.     hr = device->CreatePixelShader(blob->GetBufferPointer(), blob->GetBufferSize(), &ps);
  223.     if(FAILED(hr))
  224.         throw hr;
  225.  
  226.     blob->Release();
  227.  
  228.     hr = D3DX10CompileFromFile(
  229.             shaderLocation,
  230.             NULL,
  231.             NULL,
  232.             "VS",
  233.             "vs_4_1",
  234.             0,
  235.             0,
  236.             NULL,
  237.             &blob,
  238.             NULL,
  239.             NULL);
  240.     if(FAILED(hr))
  241.         throw hr;
  242.  
  243.     hr = device->CreateVertexShader(blob->GetBufferPointer(), blob->GetBufferSize(), &vs);
  244.     if(FAILED(hr))
  245.         throw hr;
  246.    
  247.     D3D10_INPUT_ELEMENT_DESC layout[] =
  248.     {
  249.         { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
  250.         { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, sizeof(D3DXVECTOR4), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  251.     };
  252.  
  253.     hr = device->CreateInputLayout(layout, sizeof(layout)/sizeof(layout[0]), blob->GetBufferPointer(), blob->GetBufferSize(), &il);
  254.     if(FAILED(hr))
  255.         throw hr;
  256.  
  257.     blob->Release();
  258.  
  259.     SimpleVertex vertices[] =
  260.     {
  261.         {D3DXVECTOR4( -1.0f, -1.0f, -1.0f, 1.0f), D3DXVECTOR3( 1.0f,  0.0f,  0.6f)},
  262.         {D3DXVECTOR4( -1.0f,  1.0f, -1.0f, 1.0f), D3DXVECTOR3( 0.0f,  1.0f,  0.0f)},
  263.         {D3DXVECTOR4(  1.0f,  1.0f, -1.0f, 1.0f), D3DXVECTOR3( 0.0f,  0.75f, 1.0f)},
  264.         {D3DXVECTOR4(  1.0f, -1.0f, -1.0f, 1.0f), D3DXVECTOR3( 1.0f,  0.84f, 0.0f)},
  265.         {D3DXVECTOR4(  1.0f, -1.0f,  1.0f, 1.0f), D3DXVECTOR3( 0.0f,  1.0f,  0.5f)},
  266.         {D3DXVECTOR4(  1.0f,  1.0f,  1.0f, 1.0f), D3DXVECTOR3( 0.94f, 0.5f,  0.5f)},
  267.         {D3DXVECTOR4( -1.0f,  1.0f,  1.0f, 1.0f), D3DXVECTOR3( 1.0f,  1.0f,  0.0f)},
  268.         {D3DXVECTOR4( -1.0f, -1.0f,  1.0f, 1.0f), D3DXVECTOR3( 0.41f, 0.35f, 0.8f)},
  269.     };
  270.  
  271.     D3D10_SUBRESOURCE_DATA sd;
  272.     SecureZeroMemory(&sd, sizeof(sd));
  273.     sd.pSysMem = vertices;
  274.  
  275.     D3D10_BUFFER_DESC bd;
  276.     SecureZeroMemory(&bd, sizeof(bd));
  277.     bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  278.     bd.ByteWidth = sizeof(vertices);
  279.  
  280.     hr = device->CreateBuffer(&bd, &sd, &vb);
  281.     if(FAILED(hr))
  282.         throw hr;
  283.  
  284.     DWORD indices[] =
  285.     {
  286.         //front
  287.         0, 1, 2,
  288.         2, 3, 0,
  289.  
  290.         //back
  291.         4, 5, 6,
  292.         6, 7, 4,
  293.  
  294.         //top
  295.         1, 6, 5,
  296.         5, 2, 1,
  297.  
  298.         //bottom
  299.         3, 4, 7,
  300.         7, 0, 3,
  301.  
  302.         //left
  303.         7, 6, 1,
  304.         1, 0, 7,
  305.  
  306.         //right
  307.         3, 2, 5,
  308.         5, 4, 3,
  309.     };
  310.  
  311.     sd.pSysMem = indices;
  312.    
  313.     bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
  314.     bd.ByteWidth = sizeof(indices);
  315.  
  316.     hr = device->CreateBuffer(&bd, &sd, &ib);
  317.     if(FAILED(hr))
  318.         throw hr;
  319.  
  320.     D3D10_VIEWPORT vp;
  321.     vp.Height = height;
  322.     vp.MaxDepth = 1.0f;
  323.     vp.MinDepth = 0.0f;
  324.     vp.Width = width;
  325.     vp.TopLeftX = 0;
  326.     vp.TopLeftY = 0;
  327.     device->RSSetViewports(1, &vp);
  328.  
  329.     D3DXVECTOR3 eye(0.0f, 0.0f, -5.0f);
  330.     D3DXVECTOR3 at(0.0f, 0.0f, 0.0f);
  331.     D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  332.     D3DXMatrixLookAtLH(&view, &eye, &at, &up);
  333.  
  334.     D3DXMatrixPerspectiveFovLH(&projection, (FLOAT)D3DX_PI/4, width/(FLOAT)height, 0.01f, 100.0f);
  335.  
  336.     D3DXMatrixIdentity(&world);
  337.  
  338.     bd.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
  339.     bd.ByteWidth = sizeof(D3DXMATRIX)+sizeof(D3DXVECTOR4);
  340.     hr = device->CreateBuffer(&bd, NULL, &cb);
  341.     if(FAILED(hr))
  342.         throw hr;
  343. }
  344.  
  345. void Cleanup()
  346. {
  347.     if(device) device->ClearState();
  348.     SAFE_RELEASE(tex);
  349.     SAFE_RELEASE(rtv);
  350.     SAFE_RELEASE(vb);
  351.     SAFE_RELEASE(ib);
  352.     SAFE_RELEASE(vs);
  353.     SAFE_RELEASE(ps);
  354.     SAFE_RELEASE(il);
  355.     SAFE_RELEASE(device);
  356. }
  357.  
  358. LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  359. {
  360.     switch(msg)
  361.     {
  362.         case WM_MOUSEWHEEL:
  363.             {
  364.                 __int16 delta = HIWORD(wParam);
  365.                 delta /= WHEEL_DELTA;
  366.                 alpha += (delta / 20.0f);
  367.                 if(alpha > 1.0f)
  368.                     (alpha = 1.0f);
  369.                 else if (alpha < 0.0f)
  370.                     alpha = 0.0f;
  371.             }
  372.         case WM_MOUSEMOVE:
  373.             {
  374.                 static int x = 0, y = 0;
  375.                 if( wParam & 0x0001) //Left button is down
  376.                 {
  377.                     if(x && y)
  378.                     {
  379.                         D3DXMATRIX rotate;
  380.                         D3DXMatrixRotationYawPitchRoll(
  381.                             &rotate,
  382.                             (FLOAT)(x - LOWORD(lParam))/360,
  383.                             (FLOAT)(y - HIWORD(lParam))/360,
  384.                             0.0f);
  385.                         D3DXMatrixMultiply(&world, &world, &rotate);
  386.                     }
  387.                     x = LOWORD(lParam);
  388.                     y = HIWORD(lParam);
  389.                 }
  390.                 else
  391.                 {
  392.                     x = y = 0;
  393.                 }
  394.             }
  395.         case WM_KEYDOWN:
  396.             switch(wParam)
  397.             {
  398.                 case VK_ESCAPE:
  399.                     PostQuitMessage(0);
  400.                     break;
  401.  
  402.             }  
  403.             return 0;
  404.  
  405.         case WM_DESTROY:
  406.             PostQuitMessage(0);
  407.             return 0;
  408.  
  409.         default:
  410.             return DefWindowProc(hwnd, msg, wParam, lParam);
  411.     }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement