Jump to content

  • Log In with Google      Sign In   
  • Create Account


Kevn

Member Since 22 Feb 2009
Offline Last Active Aug 06 2014 05:09 PM

Topics I've Started

Problems Initiating DirectX 11

07 July 2014 - 09:33 PM

the code breaks at the  pSwapChain->GetBuffer() function when I try to compile it. I believe the swapchain isn't getting created and I am not sure how to fix it sad.png. Also, I did get it to compile one time and for some reason the blue window created didn't respond to me trying to drag it around the screen or close it. 

 

My code: 

#include <Windows.h>
#include <D3D11.h>
 
D3D_DRIVER_TYPE pd3dDriverType = D3D_DRIVER_TYPE_HARDWARE;
ID3D11Device* pd3dDevice;
ID3D11DeviceContext* pd3dImmediateContext;
IDXGISwapChain* pSwapChain;
ID3D11Texture2D* pDepthStencilBuffer;
ID3D11RenderTargetView* pRenderTargetView;
ID3D11DepthStencilView* pDepthStencilView;
 
UINT m4xMsaaQuality = 0;
bool enable4xMsaa;
int clientWidth = 800;
int clientHeight = 600;
const float blue[4] = { 0.0f, 0.0f, 1.0f, 0.0f };
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void InitWindow(WNDCLASSEX&, HWND&, HINSTANCE, int);
bool InitDirect3D(HWND&);
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
 
  WNDCLASSEX wc;
  HWND hwnd; 
  MSG msg; 
 
  InitWindow(wc, hwnd, hInstance, nCmdShow);
  InitDirect3D(hwnd);
 
  while (GetMessage(&msg, NULL, 0, 0) > 0)
  {
    if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    } else {
      pd3dImmediateContext->ClearRenderTargetView(pRenderTargetView, blue);
      pd3dImmediateContext->ClearDepthStencilView(pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL      , 1.0f, 0);
 
      pSwapChain->Present(0, 0);
    }
  }
 
  return msg.wParam; 
}
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg)  
  {
  case WM_CLOSE:
    DestroyWindow(hwnd);
    break;
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
  }
 
  return 0;
}
 
void InitWindow(WNDCLASSEX& wc, HWND& hwnd, HINSTANCE hInstance, int nCmdShow)
{
  wc.cbSize = sizeof(WNDCLASSEX); 
  wc.style = 0;
  wc.lpfnWndProc = WndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  wc.lpszMenuName = NULL;
  wc.lpszClassName = (const char*)"MyWindow";
  wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 
  hwnd = CreateWindowEx( 
                       WS_EX_CLIENTEDGE,
                      (const char*)"MyWindow",
                      "First Window",
                       WS_OVERLAPPEDWINDOW,
                       CW_USEDEFAULT, CW_USEDEFAULT, clientWidth, clientHeight,
                       NULL, NULL, hInstance, NULL);
 
  ShowWindow(hwnd, nCmdShow); 
  UpdateWindow(hwnd);       
}
 
bool InitDirect3D(HWND& hwnd)
{
  D3D_FEATURE_LEVEL featureLevel;
  HRESULT HR = D3D11CreateDevice(
                                 0,                 
                                 pd3dDriverType,
                                 0,                 
                                 NULL,
                                 NULL, NULL,             
                                 D3D11_SDK_VERSION,
                                 &pd3dDevice,
                                 &featureLevel,
                                 &pd3dImmediateContext);
 

  pd3dDevice->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, 4, &m4xMsaaQuality);
 
  DXGI_SWAP_CHAIN_DESC sd;
  sd.BufferDesc.Width = clientWidth;
  sd.BufferDesc.Height = clientHeight;
  sd.BufferDesc.RefreshRate.Numerator = 60;
  sd.BufferDesc.RefreshRate.Denominator = 1;
  sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  if (enable4xMsaa) {
    sd.SampleDesc.Count = 4;
    sd.SampleDesc.Quality = m4xMsaaQuality - 1;
  } else {
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
  }
  sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  sd.BufferCount = 1;
  sd.OutputWindow = hwnd;
  sd.Windowed = true;
  sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  sd.Flags = 0;
 
  IDXGIDevice* dxgiDevice = 0;
  pd3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
 
  IDXGIAdapter* dxgiAdapter = 0;
  dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter);
 
  IDXGIFactory* dxgiFactory = 0;
  dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory);
 
  dxgiFactory->CreateSwapChain(pd3dDevice, &sd, &pSwapChain);
 
  dxgiDevice->Release();
  dxgiAdapter->Release();
  dxgiFactory->Release();
 
  ID3D11Texture2D* backBuffer;
  pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backBuffer));
  pd3dDevice->CreateRenderTargetView(backBuffer, 0, &pRenderTargetView);
 
  backBuffer->Release();
 
  D3D11_TEXTURE2D_DESC depthStencilDesc;
  depthStencilDesc.Width = clientWidth;
  depthStencilDesc.Height = clientHeight;
  depthStencilDesc.MipLevels = 1;
  depthStencilDesc.ArraySize = 1;
  depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  if (enable4xMsaa) {
    depthStencilDesc.SampleDesc.Count = 4;
    depthStencilDesc.SampleDesc.Quality = m4xMsaaQuality - 1;
  } else {
    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;
 
  pd3dDevice->CreateTexture2D(&depthStencilDesc, 0, &pDepthStencilBuffer);
  pd3dDevice->CreateDepthStencilView(pDepthStencilBuffer, 0, &pDepthStencilView);
 
  pd3dImmediateContext->OMSetRenderTargets(1, &pRenderTargetView, pDepthStencilView);
 
  D3D11_VIEWPORT pScreenViewport;
  pScreenViewport.TopLeftX = 0;
  pScreenViewport.TopLeftY = 0;
  pScreenViewport.Width = static_cast<float>(clientWidth);
  pScreenViewport.Height = static_cast<float>(clientHeight);
  pScreenViewport.MinDepth = 0.0f;
  pScreenViewport.MaxDepth = 1.0f;
 
  pd3dImmediateContext->RSSetViewports(1, &pScreenViewport);
 
  return true;
}
 

Understanding graphics pipeline

07 May 2013 - 03:03 PM

Are these processes in the correct order?

 

1 - orthogonal or perspective projection

2 - vertex transformation

3 - clipping

4 - geometry definition

5 - rasterization

6 - texture mapping

7 - update frame buffer

 

My practice midterm on computer graphics listed these seven processes to be placed in order. I wasn't given any solutions to the practice midterm, so I would like to know if I am correct. If I am not, can you please point me in the right direction? Thank you.


Rotating an object along a curve

29 April 2013 - 05:04 PM

Hey, I have an object that follows a Bezier Curve(in 2D), however I need it to rotate with the tangent of the curve as it follows. Currently I am taking the derivative of the Bezier Curve with respect to a time variable u, and taking the x and y coordinates at that u derivative location to use in my rotation matrix. My matrix looks like this:

 

| x/r  -y/r  0  0  |

| y/r   x/r  0  0  |

|  0     0   1  0  |

|  0     0   0  1  |

 

where the x and y values are the ones I took from the position at the derivative with respect to u and r = sqrt(x^2 + y^2).

Then I multiply this matrix to the object matrix.

 

Am I going about this correctly? If so, i'm probably messing up on my derivative. If not, what should be I doing?


DirectX 10, Collision detection with mouse question.

02 January 2013 - 06:47 PM

I'm trying to make it so I can drag objects, like a box, around with my mouse in a DirectX 10/Windows application. So far I only have a plane for the floor, a few boxes, and my mouse coordinates displayed to the screen. What would be the best technique to detect when my mouse is over an object? Do I need to figure out where the box edges are in terms of screen coordinates and then do a compare with my mouse coordinates? If so, how do I determine the location of an object in terms of screen coordinates instead of my world coordinate system?


Win32 Edit Control Help

06 December 2012 - 01:10 AM

I am working on a chat portion of a program and I am using two edit control windows and one button for the chat. One edit control window(lets call it chat window) is read only and is where all the text in the chat is displayed. The other edit control window(input window) is where the user inputs text to the chat window. The button(send) takes the text from the input window and adds it onto a string containing the text in the chat window and then updates the chat window text with that string. Now that works fine for placing the text in the chat window.However, when the string length in the chat window is greater than the window size and the scroll bar becomes active, if new text is inputed to the chat window, the scroll bar resets to the top position instead of staying scrolled all the way down. This is problematic because if two users are typing to eachother, they will have to continuely scroll down whenever something new is said to see it.
So my question is, how would I go about fixing this or is there a better way to make the chat window portion then using an edit control window?

PARTNERS