rate up
0
rate down
Directx not Drawing Onto Screen
Here is the code I have: #include <DXGI.h> #include <D3D11.h> #include <D3DX11.h> #include <D3DX10.h> #include <xnamath.h> int mwidth, mheight; HWND window; LPCSTR appName; HRESULT hr; IDXGISwapChain *sc; ID3D11Device *device; ID3D11DeviceContext *context; ID3D11RenderTargetView *rtv; ID3D11Buffer *triVertBuffer; ID3D11VertexShader *triVS; ID3D11PixelShader *triPS; ID3D10Blob *VS_Buffer; ID3D10Blob *PS_Buffer; ID3D11InputLayout *vertLayout; D3D11_INPUT_ELEMENT_DESC layout[] = { { \"POSITION\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; UINT elements = ARRAYSIZE(layout); LRESULT CALLBACK WinProc(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_QUIT: DestroyWindow(window); PostQuitMessage(0); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CLOSE: DestroyWindow(window); PostQuitMessage(0); return 0; case WM_KEYDOWN: if (wParam == VK_ESCAPE) { DestroyWindow(window); PostQuitMessage(0); return 0; } default: return DefWindowProc(window, msg, wParam, lParam); } } namespace Math { struct Vertex { XMFLOAT3 pos; Vertex() {} Vertex(float x, float y, float z) : pos(x, y, z) {} }; } namespace DX { bool init(HINSTANCE instance) { DXGI_MODE_DESC bufferDesc; ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC)); bufferDesc.Width = mwidth; bufferDesc.Height = mheight; 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; DXGI_SWAP_CHAIN_DESC scd; ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); scd.BufferDesc = bufferDesc; scd.SampleDesc.Count = 1; scd.SampleDesc.Quality = 0; scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; scd.BufferCount = 1; scd.OutputWindow = window; scd.Windowed = TRUE; scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &scd, &sc, &device, NULL, &context); ID3D11Texture2D *backBuffer; hr = sc->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer); hr = device->CreateRenderTargetView(backBuffer, NULL, &rtv); backBuffer->Release(); context->OMSetRenderTargets(1, &rtv, NULL); return true; } bool initScene() { hr = D3DX11CompileFromFile(\"triVS.hlsl\", NULL, NULL, \"main\", \"vs_4_0\", NULL, NULL, NULL, &VS_Buffer, NULL, NULL); hr = D3DX11CompileFromFile(\"triPS.hlsl\", NULL, NULL, \"main\", \"ps_4_0\", NULL, NULL, NULL, &PS_Buffer, NULL, NULL); if (FAILED(hr)) { MessageBox(NULL, \"Failed to compile shaders\", \"ERROR\", MB_OK); return false; } hr = device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &triVS); hr = device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &triPS); if (FAILED(hr)) { MessageBox(NULL, \"Failed to create shaders\", \"ERROR\", MB_OK); return false; } context->VSSetShader(triVS, NULL, NULL); context->PSSetShader(triPS, NULL, NULL); Math::Vertex v[] = { Math::Vertex(0.0f, 0.5f, 0.0f), Math::Vertex(0.5f, -0.5f, 0.0f), Math::Vertex(-0.5f, -0.5f, 0.0f) }; D3D11_BUFFER_DESC vbd; ZeroMemory(&vbd, sizeof(vbd)); vbd.Usage = D3D11_USAGE_DEFAULT; vbd.ByteWidth = sizeof(Math::Vertex) / 3; vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = NULL; vbd.MiscFlags = NULL; D3D11_SUBRESOURCE_DATA vbdata; ZeroMemory(&vbdata, sizeof(vbdata)); vbdata.pSysMem = v; hr = device->CreateBuffer(&vbd, &vbdata, &triVertBuffer); if (FAILED(hr)) { MessageBox(NULL, \"Failed to create buffer\", \"ERROR\", MB_OK); return false; } UINT stride = sizeof(Math::Vertex); UINT offset = 0; context->IASetVertexBuffers(0, 1, &triVertBuffer, &stride, &offset); device->CreateInputLayout(layout, elements, VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &vertLayout); context->IASetInputLayout(vertLayout); context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); D3D11_VIEWPORT viewport; ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT)); viewport.TopLeftX = 0; viewport.TopLeftY = 0; viewport.Width = mwidth; viewport.Height = mheight; context->RSSetViewports(1, &viewport); return true; } void releaseOBJS() { sc->Release(); device->Release(); context->Release(); triVertBuffer->Release(); triVS->Release(); triPS->Release(); VS_Buffer->Release(); PS_Buffer->Release(); vertLayout->Release(); } void update() { } void draw() { D3DXCOLOR bg(0, 0, 0, 0); context->ClearRenderTargetView(rtv, bg); context->Draw(3, 0); sc->Present(0, 0); } } namespace Windows { bool init(HINSTANCE instance, int width, int height, LPCSTR title) { mwidth = width; mheight = height; appName = title; WNDCLASSEX wc; wc.cbClsExtra = NULL; wc.cbSize = sizeof(WNDCLASSEX); wc.cbWndExtra = NULL; wc.hbrBackground = (HBRUSH)8; wc.hCursor = LoadCursor(instance, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); wc.hIconSm = wc.hIcon; wc.hInstance = instance; wc.lpfnWndProc = WinProc; wc.lpszClassName = appName; wc.lpszMenuName = NULL; wc.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClassEx(&wc)) { MessageBox(NULL, \"Class Register Failed\", \"ERROR\", MB_OK); return false; } int x = (GetSystemMetrics(SM_CXSCREEN) - width) / 2; int y = (GetSystemMetrics(SM_CYSCREEN) - height) / 2; window = CreateWindowEx(NULL, appName, appName, WS_OVERLAPPEDWINDOW, x, y, width, height, NULL, NULL, instance, NULL); if (!window) { MessageBox(NULL, \"Window Register Failed\", \"ERROR\", MB_OK); return false; } ShowWindow(window, TRUE); UpdateWindow(window); return true; } void loop() { MSG msg; ZeroMemory(&msg, sizeof(MSG)); while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { DX::update(); DX::draw(); } } } } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int mCmdShow) { if (!Windows::init(hInstance, 1024, 768, \"Portal Stories: The Scientific Method\")) { return -1; } if (!DX::init(hInstance)) { return -1; } if (!DX::initScene()) { return -1; } Windows::loop(); DX::releaseOBJS(); return 0; }
Chosen Answer
rate up
0
rate down
I think your defining your vertex buffer desc wrong vbd.ByteWidth = sizeof(Math::Vertex) / 3; Should be vbd.ByteWidth = sizeof(Math::Vertex) * 3;
Sign in to answer!