Advertisement
captmicro

Untitled

Jul 7th, 2010
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. #include "dxm.h"
  2.  
  3. void DXM::initD3D(HWND hwnd)
  4. {
  5.     //Create device & swap chain
  6.     DXGI_SWAP_CHAIN_DESC scd;
  7.     memset(&scd, 0, sizeof(DXGI_SWAP_CHAIN_DESC));
  8.  
  9.     scd.BufferCount = 1;
  10.     scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  11.     scd.BufferDesc.Width = 800;
  12.     scd.BufferDesc.Height = 600;
  13.     scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  14.     scd.OutputWindow = hwnd;
  15.     scd.SampleDesc.Count = 1;
  16.     scd.SampleDesc.Quality = 0;
  17.     scd.Windowed = TRUE;
  18.  
  19.     D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE,
  20.         NULL, 0, D3D10_SDK_VERSION, &scd, &swapchain, &device);
  21.  
  22.     ID3D10Texture2D *backBuffer;
  23.     swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D),
  24.         (LPVOID*)&backBuffer);
  25.     device->CreateRenderTargetView(backBuffer, NULL, &rtv);
  26.     backBuffer->Release();
  27.  
  28.     device->OMSetRenderTargets(1, &rtv, NULL);
  29.  
  30.     //Viewport
  31.     D3D10_VIEWPORT viewport;
  32.     memset(&viewport, 0, sizeof(D3D10_VIEWPORT));
  33.  
  34.     viewport.TopLeftX = 0;
  35.     viewport.TopLeftY = 0;
  36.     viewport.Width = 800;
  37.     viewport.Height = 600;
  38.  
  39.     device->RSSetViewports(1, &viewport);
  40.  
  41.     //Create effect
  42.     D3DX10CreateEffectFromFile(L"effect1.fx", NULL, NULL, "fx_4_0",
  43.         D3D10_SHADER_ENABLE_STRICTNESS, 0, device, 0, 0, &effect, 0, 0);
  44.  
  45.     technique = effect->GetTechniqueByName("Render");
  46.  
  47.     //Define input layout
  48.     D3D10_INPUT_ELEMENT_DESC elementDesc[] = {
  49.         {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
  50.         {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0}
  51.     };
  52.     UINT numElements = arrysize(elementDesc);
  53.  
  54.     //Create input layout
  55.     ID3D10InputLayout *vertexLayout = NULL;
  56.     D3D10_PASS_DESC passdesc;
  57.     technique->GetPassByIndex(0)->GetDesc(&passdesc);
  58.     device->CreateInputLayout(elementDesc, numElements, passdesc.pIAInputSignature,
  59.         passdesc.IAInputSignatureSize, &vertexLayout);
  60.    
  61.     device->IASetInputLayout(vertexLayout);
  62.  
  63.     //Create vertex buffer
  64.     cvertex verts[4];
  65.     verts[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
  66.     verts[0].color = 0xffff0000;
  67.     verts[1].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
  68.     verts[1].color = 0xff0000ff;
  69.     verts[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
  70.     verts[2].color = 0xff00ff00;
  71.     verts[3].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);
  72.     verts[3].color = 0xffffffff;
  73.  
  74.     //Fill buffer description
  75.     D3D10_BUFFER_DESC bd;
  76.     bd.Usage = D3D10_USAGE_DEFAULT;
  77.     bd.ByteWidth = sizeof(cvertex)*4;
  78.     bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  79.     bd.CPUAccessFlags = 0;
  80.     bd.MiscFlags = 0;
  81.  
  82.     D3D10_SUBRESOURCE_DATA initData;
  83.     initData.pSysMem = verts;
  84.     device->CreateBuffer(&bd, &initData, &vertexBuffer);
  85.  
  86.     //Set vertex buffer
  87.     UINT stride = sizeof(cvertex);
  88.     UINT offset = 0;
  89.     device->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
  90.  
  91.     //Set prim topology
  92.     device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
  93. }
  94.  
  95. void DXM::render()
  96. {
  97.     device->ClearRenderTargetView(rtv, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
  98.  
  99.     D3D10_TECHNIQUE_DESC techDesc;
  100.     technique->GetDesc(&techDesc);
  101.     for (UINT p = 0; p < techDesc.Passes; ++p)
  102.     {
  103.         technique->GetPassByIndex(p)->Apply(0);
  104.         device->Draw(4, 0);
  105.     }
  106.  
  107.     swapchain->Present(0, 0);
  108. }
  109.  
  110. void DXM::cleanD3D()
  111. {
  112.     swapchain->SetFullscreenState(FALSE, NULL);
  113.     swapchain->Release();
  114.     rtv->Release();
  115.  
  116.     effect->Release();
  117.     vertexBuffer->Release();
  118.  
  119.     device->Release();
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement