Advertisement

Reading BSP map data into DX11 Buffers

Started by February 21, 2020 04:56 PM
50 comments, last by Tom_W 4 years, 10 months ago

Alright, well here's where I'm at. This is a picture of me making simple block in the map maker GTKRadiant:
https://drive.google.com/open?id=16HVyFVXzbqPNuaWtyWIE9QkFaDmj15fO

and as a result from d3d11DevCon->Draw(24, 0); I get what you see in the dead center, and as a result of d3d11DevCon->DrawIndexed( bsp.m_nNumMeshIndices, 0, 0 ); I get what you see in the top right:
https://drive.google.com/open?id=10UufSF7gNrRJgnwmd6c-5SXYkx4gCTA1

and this is the code:

//Include and link appropriate libraries and headers//
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")

#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include <xnamath.h>
#include "bspread.h"
//Global Declarations - Interfaces//
IDXGISwapChain* SwapChain;
ID3D11Device* d3d11Device;
ID3D11DeviceContext* d3d11DevCon;
ID3D11RenderTargetView* renderTargetView;
ID3D11Buffer* squareIndexBuffer;
ID3D11DepthStencilView* depthStencilView;
ID3D11Texture2D* depthStencilBuffer;
ID3D11Buffer* squareVertBuffer;
ID3D11VertexShader* VS;
ID3D11PixelShader* PS;
ID3D10Blob* VS_Buffer;
ID3D10Blob* PS_Buffer;
ID3D11InputLayout* vertLayout;
ID3D11Buffer* cbPerObjectBuffer;
///////////////**************new**************////////////////////
ID3D11ShaderResourceView* CubesTexture;
ID3D11SamplerState* CubesTexSamplerState;
///////////////**************new**************////////////////////




////////////////
//BSP
////////////////
CBSP bsp("dxtest.bsp");
struct stBSPVertex
{
D3DXVECTOR3 p;   // Position Vertice
D3DXVECTOR3 n;   // Vertice Normal
DWORD  colour;  // ARGB
float       tu1, tv1; // Texture Coordinates
float  lu2, lv2; // Light Tex Coords
};
int m_nNumVerts = bsp.m_nNumVerts;
stBSPVertex pVB[100000];

int m_nNumFaces = 0;
int m_nNumMeshIndices = 0;
int m_nNumTextures = 0;
int m_nNumNodes = 0;
int m_nNumLeafs = 0;
int m_nNumPlanes = 0;
int m_nNumLeafFaces = 0;
int m_nNumVisData = 0;
int m_nNumTrisDraw = 0;
int m_nNumBrushes = 0;
int m_nNumBrushSides = 0;
int m_nNumLeafBrushes = 0;

// Always initialise pointers to NULL
stFace *m_pFaces = NULL;
stVertex* m_pVerts = NULL;
int *m_pMeshIndices = NULL;
//m_pTextures = NULL;
stNode *m_pNodes = NULL;
//m_pLeafs = NULL;
//m_pPlanes = NULL;
//m_pLeafFaces = NULL;
//m_VisData.pBits = NULL;
//m_pBrushes = NULL;
//m_pBrushSides = NULL;
//m_pLeafBrushes = NULL;

void GetBSPData(void)
{
 
//pVB = new stBSPVertex;
m_nNumVerts = bsp.verts();
//m_nNumFaces = bsp.m_nNumFaces;
m_nNumMeshIndices = bsp.indices();
m_nNumTextures = 0;
m_nNumNodes = 0;
m_nNumLeafs = 0;
m_nNumPlanes = 0;
m_nNumLeafFaces = 0;
m_nNumVisData = 0;
m_nNumTrisDraw = 0;
m_nNumBrushes = 0;
m_nNumBrushSides = 0;
m_nNumLeafBrushes = 0;

// Always initialise pointers to NULL
m_pFaces = NULL;
stVertex* m_pVerts = bsp.pVerts();
m_pMeshIndices = bsp.pIndices();
/*m_pTextures = NULL;
m_pNodes = NULL;
m_pLeafs = NULL;
m_pPlanes = NULL;
m_pLeafFaces = NULL;
m_VisData.pBits = NULL;
m_pBrushes = NULL;
m_pBrushSides = NULL;
m_pLeafBrushes = NULL;*/
for (static int i = 0; i < m_nNumVerts; i++)
{

 // The put the y ans z the wrong way around!
 pVB[i].p.x = m_pVerts[i].vPoint[0];
 pVB[i].p.y = m_pVerts[i].vPoint[1];
 pVB[i].p.z = m_pVerts[i].vPoint[2];

 // tv is inverted!
 pVB[i].tu1 = m_pVerts[i].Tex[0];
 pVB[i].tv1 = m_pVerts[i].Tex[1];
 pVB[i].lu2 = m_pVerts[i].LightTexCoord[0];
 pVB[i].lv2 = m_pVerts[i].LightTexCoord[1];

 pVB[i].n.x = m_pVerts[i].vNormal[0];
 pVB[i].n.y = m_pVerts[i].vNormal[1];
 pVB[i].n.z = m_pVerts[i].vNormal[2];

 // Alpha hack - so our alpha value isn't 100% anymore
 //pVB[i].colour = 0x10ffffff & bsp.m_pVerts->RGBA; // ARGB
}
//bsp.~bsp();
}


//Global Declarations - Others//
LPCTSTR WndClassName = L"firstwindow";
HWND hwnd = NULL;
HRESULT hr;

const int Width  = 1024;
const int Height = 768;

XMMATRIX WVP;
XMMATRIX cube1World;
XMMATRIX cube2World;
XMMATRIX camView;
XMMATRIX camProjection;

XMVECTOR camPosition;
XMVECTOR camTarget;
XMVECTOR camUp;

XMMATRIX Rotation;
XMMATRIX Scale;
XMMATRIX Translation;
float rot = 0.01f;

//Function Prototypes//
bool InitializeDirect3d11App(HINSTANCE hInstance);
void CleanUp();
bool InitScene();
void UpdateScene();
void DrawScene();

bool InitializeWindow(HINSTANCE hInstance,
int ShowWnd,
int width, int height,
bool windowed);
int messageloop();

LRESULT CALLBACK WndProc(HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam);

//Create effects constant buffer's structure//
struct cbPerObject
{
XMMATRIX  WVP;
};

cbPerObject cbPerObj;

///////////////**************new**************////////////////////
//Vertex Structure and Vertex Layout (Input Layout)//
struct Vertex //Overloaded Vertex Structure
{
Vertex(){}
Vertex(float x, float y, float z,
 float u, float v)
 : pos(x,y,z), texCoord(u, v){}

XMFLOAT3 pos;
XMFLOAT2 texCoord;
};

//Above was the original Vertex struct that the tutorial used to draw two cubes, I'm using stBSPVertex to read the BSP map

D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },  
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },  
};
UINT numElements = ARRAYSIZE(layout);
///////////////**************new**************////////////////////

int WINAPI WinMain(HINSTANCE hInstance, //Main windows function
HINSTANCE hPrevInstance,  
LPSTR lpCmdLine,
int nShowCmd)
{
GetBSPData();
if(!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
{
 MessageBox(0, L"Window Initialization - Failed",
  L"Error", MB_OK);
 return 0;
}

if(!InitializeDirect3d11App(hInstance)) //Initialize Direct3D
{
 MessageBox(0, L"Direct3D Initialization - Failed",
  L"Error", MB_OK);
 return 0;
}
 
if(!InitScene()) //Initialize our scene
{
 MessageBox(0, L"Scene Initialization - Failed",
  L"Error", MB_OK);
 return 0;
}

messageloop();

CleanUp();    

return 0;
}

bool InitializeWindow(HINSTANCE hInstance,
int ShowWnd,
int width, int height,
bool windowed)
{
typedef struct _WNDCLASS {
 UINT cbSize;
 UINT style;
 WNDPROC lpfnWndProc;
 int cbClsExtra;
 int cbWndExtra;
 HANDLE hInstance;
 HICON hIcon;
 HCURSOR hCursor;
 HBRUSH hbrBackground;
 LPCTSTR lpszMenuName;
 LPCTSTR lpszClassName;
} WNDCLASS;

WNDCLASSEX wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
wc.lpszMenuName = NULL;
wc.lpszClassName = WndClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wc))
{
 MessageBox(NULL, L"Error registering class",  
  L"Error", MB_OK | MB_ICONERROR);
 return 1;
}

hwnd = CreateWindowEx(
 NULL,
 WndClassName,
 L"Lesson 4 - Begin Drawing",
 WS_OVERLAPPEDWINDOW,
 CW_USEDEFAULT, CW_USEDEFAULT,
 width, height,
 NULL,
 NULL,
 hInstance,
 NULL
 );

if (!hwnd)
{
 MessageBox(NULL, L"Error creating window",
  L"Error", MB_OK | MB_ICONERROR);
 return 1;
}

ShowWindow(hwnd, ShowWnd);
UpdateWindow(hwnd);

return true;
}

bool InitializeDirect3d11App(HINSTANCE hInstance)
{
//Describe our SwapChain Buffer
DXGI_MODE_DESC bufferDesc;

ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));

bufferDesc.Width = Width;
bufferDesc.Height = Height;
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;

//Describe our SwapChain
DXGI_SWAP_CHAIN_DESC swapChainDesc;  

ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));

swapChainDesc.BufferDesc = bufferDesc;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = hwnd;  
swapChainDesc.Windowed = TRUE;  
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;


//Create our SwapChain
hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
 D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);

//Create our BackBuffer
ID3D11Texture2D* BackBuffer;
hr = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (void**)&BackBuffer );

//Create our Render Target
hr = d3d11Device->CreateRenderTargetView( BackBuffer, NULL, &renderTargetView );
BackBuffer->Release();

//Describe our Depth/Stencil Buffer
D3D11_TEXTURE2D_DESC depthStencilDesc;

depthStencilDesc.Width     = Width;
depthStencilDesc.Height    = Height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format    = DXGI_FORMAT_D24_UNORM_S8_UINT;
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;

//Create the Depth/Stencil View
d3d11Device->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
d3d11Device->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);

//Set our Render Target
d3d11DevCon->OMSetRenderTargets( 1, &renderTargetView, depthStencilView );

return true;
}

void CleanUp()
{
//Release the COM Objects we created
SwapChain->Release();
d3d11Device->Release();
d3d11DevCon->Release();
renderTargetView->Release();
squareVertBuffer->Release();
squareIndexBuffer->Release();
VS->Release();
PS->Release();
VS_Buffer->Release();
PS_Buffer->Release();
vertLayout->Release();
depthStencilView->Release();
depthStencilBuffer->Release();
cbPerObjectBuffer->Release();
}

bool InitScene()
{
//Compile Shaders from shader file
hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);

//Create the Shader Objects
hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

//Set Vertex and Pixel Shaders
d3d11DevCon->VSSetShader(VS, 0, 0);
d3d11DevCon->PSSetShader(PS, 0, 0);

///////////////**************new**************////////////////////
//Create the vertex buffer
Vertex v[] =
{
 // Front Face
 Vertex(pVB[0].p.x, pVB[0].p.y, pVB[0].p.z, pVB[0].tu1, pVB[0].tv1),
 Vertex(pVB[1].p.x, pVB[1].p.y, pVB[1].p.z, pVB[1].tu1, pVB[1].tv1),
 Vertex(pVB[2].p.x, pVB[2].p.y, pVB[2].p.z, pVB[2].tu1, pVB[2].tv1),
 Vertex(pVB[3].p.x, pVB[3].p.y, pVB[3].p.z, pVB[3].tu1, pVB[3].tv1),

 // Back Face
 Vertex(pVB[4].p.x, pVB[4].p.y, pVB[4].p.z, pVB[4].tu1, pVB[4].tv1),
 Vertex(pVB[5].p.x, pVB[5].p.y, pVB[5].p.z, pVB[5].tu1, pVB[5].tv1),
 Vertex(pVB[6].p.x, pVB[6].p.y, pVB[6].p.z, pVB[6].tu1, pVB[6].tv1),
 Vertex(pVB[7].p.x, pVB[7].p.y, pVB[7].p.z, pVB[7].tu1, pVB[7].tv1),

 // Top Face
 Vertex(pVB[8].p.x, pVB[8].p.y, pVB[8].p.z, pVB[8].tu1, pVB[8].tv1),
 Vertex(pVB[9].p.x, pVB[9].p.y, pVB[9].p.z, pVB[9].tu1, pVB[9].tv1),
 Vertex(pVB[10].p.x, pVB[10].p.y, pVB[10].p.z, pVB[10].tu1, pVB[10].tv1),
 Vertex(pVB[11].p.x, pVB[11].p.y, pVB[11].p.z, pVB[11].tu1, pVB[11].tv1),

 // Bottom Face
 Vertex(pVB[12].p.x, pVB[12].p.y, pVB[12].p.z, pVB[12].tu1, pVB[12].tv1),
 Vertex(pVB[13].p.x, pVB[13].p.y, pVB[13].p.z, pVB[13].tu1, pVB[13].tv1),
 Vertex(pVB[14].p.x, pVB[14].p.y, pVB[14].p.z, pVB[14].tu1, pVB[14].tv1),
 Vertex(pVB[15].p.x, pVB[15].p.y, pVB[15].p.z, pVB[15].tu1, pVB[15].tv1),

 // Left Face
 Vertex(pVB[16].p.x, pVB[16].p.y, pVB[16].p.z, pVB[16].tu1, pVB[16].tv1),
 Vertex(pVB[17].p.x, pVB[17].p.y, pVB[17].p.z, pVB[17].tu1, pVB[17].tv1),
 Vertex(pVB[18].p.x, pVB[18].p.y, pVB[18].p.z, pVB[18].tu1, pVB[18].tv1),
 Vertex(pVB[19].p.x, pVB[19].p.y, pVB[19].p.z, pVB[19].tu1, pVB[19].tv1),

 // Right Face
 Vertex(pVB[20].p.x, pVB[20].p.y, pVB[20].p.z, pVB[20].tu1, pVB[20].tv1),
 Vertex(pVB[21].p.x, pVB[21].p.y, pVB[21].p.z, pVB[21].tu1, pVB[21].tv1),
 Vertex(pVB[22].p.x, pVB[22].p.y, pVB[22].p.z, pVB[22].tu1, pVB[22].tv1),
 Vertex(pVB[23].p.x, pVB[23].p.y, pVB[23].p.z, pVB[23].tu1, pVB[23].tv1),
 Vertex(pVB[24].p.x, pVB[24].p.y, pVB[24].p.z, pVB[24].tu1, pVB[24].tv1),
 Vertex(pVB[25].p.x, pVB[25].p.y, pVB[25].p.z, pVB[25].tu1, pVB[25].tv1),
 Vertex(pVB[26].p.x, pVB[26].p.y, pVB[26].p.z, pVB[26].tu1, pVB[26].tv1),
 Vertex(pVB[27].p.x, pVB[27].p.y, pVB[27].p.z, pVB[27].tu1, pVB[27].tv1),
 Vertex(pVB[28].p.x, pVB[28].p.y, pVB[28].p.z, pVB[28].tu1, pVB[28].tv1),
 Vertex(pVB[29].p.x, pVB[29].p.y, pVB[29].p.z, pVB[29].tu1, pVB[29].tv1),
 Vertex(pVB[30].p.x, pVB[30].p.y, pVB[30].p.z, pVB[30].tu1, pVB[30].tv1),
 Vertex(pVB[31].p.x, pVB[31].p.y, pVB[31].p.z, pVB[31].tu1, pVB[31].tv1),
 Vertex(pVB[32].p.x, pVB[32].p.y, pVB[32].p.z, pVB[32].tu1, pVB[32].tv1),
 Vertex(pVB[33].p.x, pVB[33].p.y, pVB[33].p.z, pVB[33].tu1, pVB[33].tv1),
 Vertex(pVB[34].p.x, pVB[34].p.y, pVB[34].p.z, pVB[34].tu1, pVB[34].tv1),
 Vertex(pVB[35].p.x, pVB[35].p.y, pVB[35].p.z, pVB[35].tu1, pVB[35].tv1),
 Vertex(pVB[36].p.x, pVB[36].p.y, pVB[36].p.z, pVB[36].tu1, pVB[36].tv1),
 Vertex(pVB[37].p.x, pVB[37].p.y, pVB[37].p.z, pVB[37].tu1, pVB[37].tv1),
 Vertex(pVB[38].p.x, pVB[38].p.y, pVB[38].p.z, pVB[38].tu1, pVB[38].tv1),
 Vertex(pVB[39].p.x, pVB[39].p.y, pVB[39].p.z, pVB[39].tu1, pVB[39].tv1),
 Vertex(pVB[40].p.x, pVB[40].p.y, pVB[40].p.z, pVB[40].tu1, pVB[40].tv1),
// Vertex(pVB[41].p.x, pVB[41].p.y, pVB[41].p.z, pVB[41].tu1, pVB[41].tv1),
};


DWORD indices[] = {
 // Front Face
 bsp.m_pMeshIndices[0], bsp.m_pMeshIndices[1], bsp.m_pMeshIndices[2],
 bsp.m_pMeshIndices[3], bsp.m_pMeshIndices[4], bsp.m_pMeshIndices[5],

 // Back Face
 bsp.m_pMeshIndices[6], bsp.m_pMeshIndices[7], bsp.m_pMeshIndices[8],
 bsp.m_pMeshIndices[9], bsp.m_pMeshIndices[10], bsp.m_pMeshIndices[11],

 // Top Face
 bsp.m_pMeshIndices[12], bsp.m_pMeshIndices[13], bsp.m_pMeshIndices[14],
 bsp.m_pMeshIndices[15], bsp.m_pMeshIndices[16], bsp.m_pMeshIndices[17],

 // Bottom Face
 bsp.m_pMeshIndices[18], bsp.m_pMeshIndices[19], bsp.m_pMeshIndices[20],
 bsp.m_pMeshIndices[21], bsp.m_pMeshIndices[22], bsp.m_pMeshIndices[23],

 // Left Face
 bsp.m_pMeshIndices[24], bsp.m_pMeshIndices[25], bsp.m_pMeshIndices[26],
 bsp.m_pMeshIndices[27], bsp.m_pMeshIndices[28], bsp.m_pMeshIndices[29],

 // Right Face
 bsp.m_pMeshIndices[30], bsp.m_pMeshIndices[31], bsp.m_pMeshIndices[32],
 bsp.m_pMeshIndices[33], bsp.m_pMeshIndices[34], bsp.m_pMeshIndices[35],
 bsp.m_pMeshIndices[36], bsp.m_pMeshIndices[37], bsp.m_pMeshIndices[38],
 bsp.m_pMeshIndices[39], bsp.m_pMeshIndices[40], bsp.m_pMeshIndices[41],
 bsp.m_pMeshIndices[42], bsp.m_pMeshIndices[43], bsp.m_pMeshIndices[44],
 bsp.m_pMeshIndices[45], bsp.m_pMeshIndices[46], bsp.m_pMeshIndices[47],
 bsp.m_pMeshIndices[48], bsp.m_pMeshIndices[49], bsp.m_pMeshIndices[50],
 bsp.m_pMeshIndices[51], bsp.m_pMeshIndices[52], bsp.m_pMeshIndices[53],
 bsp.m_pMeshIndices[52], bsp.m_pMeshIndices[53], bsp.m_pMeshIndices[54],
 bsp.m_pMeshIndices[55], bsp.m_pMeshIndices[56], bsp.m_pMeshIndices[57],
 bsp.m_pMeshIndices[58], bsp.m_pMeshIndices[59], bsp.m_pMeshIndices[60],
};

 
D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));

indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(WORD)* bsp.m_nNumMeshIndices;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA iinitData;

iinitData.pSysMem = indices;
d3d11Device->CreateBuffer(&indexBufferDesc, &iinitData, &squareIndexBuffer);

d3d11DevCon->IASetIndexBuffer(squareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);


D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));

vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(Vertex)* bsp.m_nNumVerts;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;


 

 

char buf[100];
sprintf(buf, "%d", bsp.m_nNumVerts);
MessageBoxA(NULL, buf, buf, MB_OK);
///////////////**************new**************////////////////////

D3D11_SUBRESOURCE_DATA vertexBufferData;  
 
ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
vertexBufferData.pSysMem = v;
//hr = d3d11Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &squareVertBuffer);
hr = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &squareVertBuffer);
//Set the vertex buffer
UINT stride = sizeof(Vertex);
UINT offset = 0;
d3d11DevCon->IASetVertexBuffers( 0, 1, &squareVertBuffer, &stride, &offset );

//Create the Input Layout
hr = d3d11Device->CreateInputLayout( layout, numElements, VS_Buffer->GetBufferPointer(),  
 VS_Buffer->GetBufferSize(), &vertLayout );

//Set the Input Layout
d3d11DevCon->IASetInputLayout( vertLayout );

//Set Primitive Topology
d3d11DevCon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

//Create the Viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = Width;
viewport.Height = Height;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;

//Set the Viewport
d3d11DevCon->RSSetViewports(1, &viewport);

//Create the buffer to send to the cbuffer in effect file
D3D11_BUFFER_DESC cbbd;  
ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));

cbbd.Usage = D3D11_USAGE_DEFAULT;
cbbd.ByteWidth = sizeof(cbPerObject);
cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbbd.CPUAccessFlags = 0;
cbbd.MiscFlags = 0;

hr = d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);

//Camera information
camPosition = XMVectorSet( 0.0f, 350.0f, -8.0f, 0.0f );
camTarget = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f );
camUp = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f );

//Set the View matrix
camView = XMMatrixLookAtLH( camPosition, camTarget, camUp );

//Set the Projection matrix
camProjection = XMMatrixPerspectiveFovLH( 0.4f*3.14f, Width/Height, 1.0f, 1000.0f);

///////////////**************new**************////////////////////
// strcat(bsp.m_pTextures[0].Name, ".jpg");
hr = D3DX11CreateShaderResourceViewFromFile(d3d11Device, L"textures\\gothic_floor\\largeblockfloor3.jpg",
 NULL, NULL, &CubesTexture, NULL );
//if (hr != S_OK)
// MessageBoxA(NULL, "Something went wrong reading the texture", (LPCSTR)bsp.m_pTextures[0].Name, MB_OK);
// Describe the Sample State
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory( &sampDesc, sizeof(sampDesc) );
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
   sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
   sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
   sampDesc.MinLOD = 0;
   sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
   
//Create the Sample State
hr = d3d11Device->CreateSamplerState( &sampDesc, &CubesTexSamplerState );
///////////////**************new**************////////////////////

return true;
}

void UpdateScene()
{
//Keep the cubes rotating
rot += .0005f;
if(rot > 6.26f)
 rot = 0.0f;

//Reset cube1World
cube1World = XMMatrixIdentity();

//Define cube1's world space matrix
XMVECTOR rotaxis = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
Rotation = XMMatrixRotationAxis( rotaxis, rot);
Translation = XMMatrixTranslation( -5.0f, 0.0f, 4.0f );

//Set cube1's world space using the transformations
cube1World = Translation * Rotation;

//Reset cube2World
cube2World = XMMatrixIdentity();

//Define cube2's world space matrix
Rotation = XMMatrixRotationAxis( rotaxis, -rot);
Scale = XMMatrixScaling( 1.3f, 1.3f, 1.3f );

//Set cube2's world space matrix
cube2World = Rotation * Scale;
}

void DrawScene()
{
//Clear our backbuffer
float bgColor[4] = {(0.0f, 0.0f, 0.0f, 0.0f)};
d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);

//Refresh the Depth/Stencil view
d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

//Set the WVP matrix and send it to the constant buffer in effect file
WVP = cube1World * camView * camProjection;
cbPerObj.WVP = XMMatrixTranspose(WVP);  
d3d11DevCon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
d3d11DevCon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
///////////////**************new**************////////////////////
d3d11DevCon->PSSetShaderResources( 0, 1, &CubesTexture );
d3d11DevCon->PSSetSamplers( 0, 1, &CubesTexSamplerState );
///////////////**************new**************////////////////////

//Draw the first cube
//d3d11DevCon->DrawIndexed( bsp.m_nNumMeshIndices, 0, 0 );
d3d11DevCon->Draw(24, 0);
WVP = cube2World * camView * camProjection;
cbPerObj.WVP = XMMatrixTranspose(WVP);  
d3d11DevCon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
d3d11DevCon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
///////////////**************new**************////////////////////
d3d11DevCon->PSSetShaderResources( 0, 1, &CubesTexture );
d3d11DevCon->PSSetSamplers( 0, 1, &CubesTexSamplerState );
///////////////**************new**************////////////////////

//Draw the second cube
d3d11DevCon->DrawIndexed( bsp.m_nNumMeshIndices, 0, 0 );

//Present the backbuffer to the screen
SwapChain->Present(0, 0);
}

int messageloop(){
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while(true)
{
 BOOL PeekMessageL(  
  LPMSG lpMsg,
  HWND hWnd,
  UINT wMsgFilterMin,
  UINT wMsgFilterMax,
  UINT wRemoveMsg
  );

 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
 {
  if (msg.message == WM_QUIT)
   break;
  TranslateMessage(&msg);  
  DispatchMessage(&msg);
 }
 else{
  // run game code            
  UpdateScene();
  DrawScene();
 }
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch( msg )
{
case WM_KEYDOWN:
 if( wParam == VK_ESCAPE ){
  DestroyWindow(hwnd);
 }
 return 0;

case WM_DESTROY:
 PostQuitMessage(0);
 return 0;
}
return DefWindowProc(hwnd,
 msg,
 wParam,
 lParam);
}

As you can see the Draw() method that relies solely on the vertex buffer is showing the block much more sucessfully than the one relying on the index buffer. I've made this map several times, with walls/cubes, and the indexed one draws it only to a certain point.

Is it in fact reading the data sucessfully and drawing, could this just be a texture issue of somekind? Maybe the texture isn't being drawn on certain areas and is on others? Or am I screwing this up(well obviously I am) with the filling of the buffers still?

I really need some help here; If I get past this brick wall I can port my entire game engine to DX11, I just need to understand vertex/index buffers in 11 better.

Help!!! Will pay money!! For real! This is like medieval torture! I am trapped in a tower and need rescuing! DAMSEL IN DISTRESS!! CQD SOS CQD HAVE STRUCK BERG CQD SOS THIS IS MYG CQD SOS CQD SOS WATER IS RISING TO THE BOILERS CANNOT HEAR YOU OLD MAN AS A RESULT OF STEAM CQD SOS CQD SOS

random screw-up post I can't delete, ignore. please read below

Advertisement

Tom_W said:

Alright, I'm guessing simply having all the vertex data and indices from a bsp map are not enough to actually draw it, so I really need help in converting my bsp header file to DX11.

I'm exactly one year and three days clean off heroin, and the only thing that keeps me going is working on my game engine. But I have to upgrade this header to DX11 before I can begin actually porting the rest of it. I really need help here, this is affecting my emotional state at this point.

Within the BSP header file, I have tried to convert the CreateIndexBuffer and CreateVertexBuffer functions, along with DrawFace().

bool CreateDXIndexBuffer(ID3D11Device* pDevice,
 ID3D11DeviceContext* pDevCon)
{
 HRESULT hr = S_OK;

 /*hr =
  pDevice->CreateIndexBuffer(m_nNumMeshIndices * sizeof(WORD),
  D3DUSAGE_WRITEONLY,
  D3DFMT_INDEX16,
  D3DPOOL_DEFAULT,
  &m_pIB,
  NULL);
 if (hr != S_OK)
 {
  ErrLog("<!>Error Creating Index Buffer/n");
  return false;
 }*/

 // We need to copy the data one at a time, as the data is loaded
 // in as int, but directx uses a word for each indices
 unsigned long pIB[10000];
 /*m_pIB->Lock(0,
  m_nNumMeshIndices*sizeof(WORD),
  (void**)&pIB,
  0);*/
 for (int i = 0; i<m_nNumMeshIndices; i++)
  pIB[i] = m_pMeshIndices[i];
 //m_pIB->Unlock();
 D3D11_BUFFER_DESC indexBufferDesc;
 D3D11_SUBRESOURCE_DATA indexData;

 indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
 indexBufferDesc.ByteWidth = sizeof(WORD) * m_nNumMeshIndices;
 indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
 indexBufferDesc.CPUAccessFlags = 0;
 indexBufferDesc.MiscFlags = 0;
 indexBufferDesc.StructureByteStride = 0;

 indexData.pSysMem = pIB;
 indexData.SysMemPitch = 0;
 indexData.SysMemSlicePitch = 0;

 hr = pDevice->CreateBuffer(&indexBufferDesc, &indexData, &m_pIB);

 if (FAILED(hr))
 {
  MessageBoxA(NULL, "CreateIndexBuffer() Failed.", "Error", MB_OK);
  return false;
 }

 return true;
}

bool CreateDXVertexBuffer(ID3D11Device* pDevice, ID3D11DeviceContext* pDevCon)
{
 HRESULT hr = S_OK;

 /*m_FVF_Format = D3DFVF_XYZ |
  D3DFVF_NORMAL |
  D3DFVF_DIFFUSE |
  D3DFVF_TEX2 |
  D3DFVF_TEXCOORDSIZE2(0) |
  D3DFVF_TEXCOORDSIZE2(1);
 hr =
  pDevice->CreateVertexBuffer(m_nNumVerts * sizeof(stBSPVertex),
  0,
  m_FVF_Format,
  D3DPOOL_MANAGED,
  &m_pVB,
  NULL);*/

 if (hr != S_OK)
 {
  ErrLog("<!>Error Creating Vertex Buffer/n");
  return false;
 }

 // Copy data into our dx buffer
 stBSPVertex pVB[10000];// = new stBSPVertex; //new?
 //m_pVB->Lock(0, 0, (void**)&pVB, 0);

 for (int i = 0; i<m_nNumVerts; i++)
 {
  // The put the y ans z the wrong way around!
  pVB[i].p.x = m_pVerts[i].vPoint[0];
  pVB[i].p.y = m_pVerts[i].vPoint[1];
  pVB[i].p.z = m_pVerts[i].vPoint[2];

  // tv is inverted!
  pVB[i].tu1 = m_pVerts[i].Tex[0];
  pVB[i].tv1 = m_pVerts[i].Tex[1];
  pVB[i].lu2 = m_pVerts[i].LightTexCoord[0];
  pVB[i].lv2 = m_pVerts[i].LightTexCoord[1];

  pVB[i].n.x = m_pVerts[i].vNormal[0];
  pVB[i].n.y = m_pVerts[i].vNormal[1];
  pVB[i].n.z = m_pVerts[i].vNormal[2];

  // Alpha hack - so our alpha value isn't 100% anymore
  pVB[i].colour = 0x10ffffff & m_pVerts->RGBA; // ARGB
 }
 //m_pVB->Unlock();
 D3D11_BUFFER_DESC vertexBufferDesc;
 D3D11_SUBRESOURCE_DATA vertexData;


 vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
 vertexBufferDesc.ByteWidth = sizeof(stBSPVertex)* m_nNumVerts;
 vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
 vertexBufferDesc.CPUAccessFlags = 0;
 vertexBufferDesc.MiscFlags = 0;
 vertexBufferDesc.StructureByteStride = 0;


 vertexData.pSysMem = pVB;
 vertexData.SysMemPitch = 0;
 vertexData.SysMemSlicePitch = 0;

 hr = pDevice->CreateBuffer(&vertexBufferDesc, &vertexData, &m_pVB);

 if (FAILED(hr))
   
//  bool bResult = CreateVertexBuffer(pDevice, pDevCon, &m_pVB, pVB, m_nNumVerts);
 {
  MessageBoxA(NULL, "CreateVertexBuffer() failed.", "Error", MB_OK);
  return false;
 }
 return true;
}


//….

//And then the DrawFace Function
void DrawFace(ID3D11Device* pDevice, ID3D11DeviceContext* pDevCon,
int nFace)
{

/* pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TEXTURE);

pDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);*/

// pDevice->SetIndices(m_pIB);

// pDevice->SetStreamSource(0, m_pVB, 0, sizeof(stBSPVertex));

// pDevice->SetFVF(m_FVF_Format);

// If its not a tri mesh
if (m_pFaces[nFace].nFaceType != 1)return;

int nFirstF = m_pFaces[nFace].nFirstMeshVerts;
int nNumF = m_pFaces[nFace].NumMeshVerts;

int nFirstV = m_pFaces[nFace].FirstVert;
int nNumV = m_pFaces[nFace].NumVerts;

int nTexIndex = m_pFaces[nFace].nTexture;

// pDevice->SetTexture(0, m_pT[nTexIndex]);

int nNumTris = nNumF / 3;

/*
pDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, // Type
nFirstV, // BaseVertexIndex
0, // MinIndex
nNumV, // NumVertices
nFirstF, // StartIndex
nNumTris ); // PrimitiveCount
*/

m_nNumTrisDraw += (m_pFaces[nFace].NumMeshVerts / 3);

/* pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
m_pFaces[nFace].FirstVert,
0,
m_pFaces[nFace].NumVerts,
m_pFaces[nFace].nFirstMeshVerts,
(m_pFaces[nFace].NumMeshVerts / 3));*/
pDevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pDevCon->DrawIndexed(m_pFaces[nFace].NumVerts, m_pFaces[nFace].nFirstMeshVerts, m_pFaces[nFace].FirstVert);
pDevCon->Draw(m_nNumVerts, 0);
pDevCon->DrawIndexed(m_nNumMeshIndices, 0, 0);

	
}

As you can see the DX9 code for creating/filling the vertex/index buffers are commented out, replaced by my attempt to upgrade it with DX11 code. This may be a C++ problem, or DX problem, I don't know, I really need help. Within DrawFace() you can see I call the device's Draw() and DrawIndexed() functions, but literally nothing draws.

The following is my main.cpp:

//Include and link appropriate libraries and headers//
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")

#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include <xnamath.h>
#include "bspread.h"
//Global Declarations - Interfaces//
IDXGISwapChain* SwapChain;
ID3D11Device* d3d11Device;
ID3D11DeviceContext* d3d11DevCon;
ID3D11RenderTargetView* renderTargetView;
ID3D11Buffer* squareIndexBuffer;
ID3D11DepthStencilView* depthStencilView;
ID3D11Texture2D* depthStencilBuffer;
ID3D11Buffer* squareVertBuffer;
ID3D11VertexShader* VS;
ID3D11PixelShader* PS;
ID3D10Blob* VS_Buffer;
ID3D10Blob* PS_Buffer;
ID3D11InputLayout* vertLayout;
ID3D11Buffer* cbPerObjectBuffer;
///////////////**************new**************////////////////////
ID3D11ShaderResourceView* CubesTexture;
ID3D11SamplerState* CubesTexSamplerState;
///////////////**************new**************////////////////////




////////////////
//BSP
////////////////
CBSP bsp;





//Global Declarations - Others//
LPCTSTR WndClassName = L"firstwindow";
HWND hwnd = NULL;
HRESULT hr;

const int Width  = 1024;
const int Height = 768;

XMMATRIX WVP;
XMMATRIX cube1World;
XMMATRIX cube2World;
XMMATRIX camView;
XMMATRIX camProjection;

XMVECTOR camPosition;
XMVECTOR camTarget;
XMVECTOR camUp;

XMMATRIX Rotation;
XMMATRIX Scale;
XMMATRIX Translation;
float rot = 0.01f;

//Function Prototypes//
bool InitializeDirect3d11App(HINSTANCE hInstance);
void CleanUp();
bool InitScene();
void UpdateScene();
void DrawScene();

bool InitializeWindow(HINSTANCE hInstance,
int ShowWnd,
int width, int height,
bool windowed);
int messageloop();

LRESULT CALLBACK WndProc(HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam);

//Create effects constant buffer's structure//
struct cbPerObject
{
XMMATRIX  WVP;
};

cbPerObject cbPerObj;

///////////////**************new**************////////////////////
//Vertex Structure and Vertex Layout (Input Layout)//
struct Vertex //Overloaded Vertex Structure
{
Vertex(){}
Vertex(float x, float y, float z,
 float u, float v)
 : pos(x,y,z), texCoord(u, v){}

XMFLOAT3 pos;
XMFLOAT2 texCoord;
};

//Above was the original Vertex struct that the tutorial used to draw two cubes, I'm using stBSPVertex to read the BSP map

D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },  
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },  
};
UINT numElements = ARRAYSIZE(layout);
///////////////**************new**************////////////////////

int WINAPI WinMain(HINSTANCE hInstance, //Main windows function
HINSTANCE hPrevInstance,  
LPSTR lpCmdLine,
int nShowCmd)
{

if(!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
{
 MessageBox(0, L"Window Initialization - Failed",
  L"Error", MB_OK);
 return 0;
}

if(!InitializeDirect3d11App(hInstance)) //Initialize Direct3D
{
 MessageBox(0, L"Direct3D Initialization - Failed",
  L"Error", MB_OK);
 return 0;
}
 
if(!InitScene()) //Initialize our scene
{
 MessageBox(0, L"Scene Initialization - Failed",
  L"Error", MB_OK);
 return 0;
}
bsp.Create(d3d11Device, d3d11DevCon, "dxtest.bsp");
messageloop();

CleanUp();    

return 0;
}

bool InitializeWindow(HINSTANCE hInstance,
int ShowWnd,
int width, int height,
bool windowed)
{
typedef struct _WNDCLASS {
 UINT cbSize;
 UINT style;
 WNDPROC lpfnWndProc;
 int cbClsExtra;
 int cbWndExtra;
 HANDLE hInstance;
 HICON hIcon;
 HCURSOR hCursor;
 HBRUSH hbrBackground;
 LPCTSTR lpszMenuName;
 LPCTSTR lpszClassName;
} WNDCLASS;

WNDCLASSEX wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
wc.lpszMenuName = NULL;
wc.lpszClassName = WndClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wc))
{
 MessageBox(NULL, L"Error registering class",  
  L"Error", MB_OK | MB_ICONERROR);
 return 1;
}

hwnd = CreateWindowEx(
 NULL,
 WndClassName,
 L"Lesson 4 - Begin Drawing",
 WS_OVERLAPPEDWINDOW,
 CW_USEDEFAULT, CW_USEDEFAULT,
 width, height,
 NULL,
 NULL,
 hInstance,
 NULL
 );

if (!hwnd)
{
 MessageBox(NULL, L"Error creating window",
  L"Error", MB_OK | MB_ICONERROR);
 return 1;
}

ShowWindow(hwnd, ShowWnd);
UpdateWindow(hwnd);

return true;
}

bool InitializeDirect3d11App(HINSTANCE hInstance)
{
//Describe our SwapChain Buffer
DXGI_MODE_DESC bufferDesc;

ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));

bufferDesc.Width = Width;
bufferDesc.Height = Height;
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;

//Describe our SwapChain
DXGI_SWAP_CHAIN_DESC swapChainDesc;  

ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));

swapChainDesc.BufferDesc = bufferDesc;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = hwnd;  
swapChainDesc.Windowed = TRUE;  
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;


//Create our SwapChain
hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
 D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);

//Create our BackBuffer
ID3D11Texture2D* BackBuffer;
hr = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (void**)&BackBuffer );

//Create our Render Target
hr = d3d11Device->CreateRenderTargetView( BackBuffer, NULL, &renderTargetView );
BackBuffer->Release();

//Describe our Depth/Stencil Buffer
D3D11_TEXTURE2D_DESC depthStencilDesc;

depthStencilDesc.Width     = Width;
depthStencilDesc.Height    = Height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format    = DXGI_FORMAT_D24_UNORM_S8_UINT;
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;

//Create the Depth/Stencil View
d3d11Device->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
d3d11Device->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);

//Set our Render Target
d3d11DevCon->OMSetRenderTargets( 1, &renderTargetView, depthStencilView );

return true;
}

void CleanUp()
{
//Release the COM Objects we created
SwapChain->Release();
d3d11Device->Release();
d3d11DevCon->Release();
renderTargetView->Release();
// squareVertBuffer->Release();
// squareIndexBuffer->Release();
VS->Release();
PS->Release();
VS_Buffer->Release();
PS_Buffer->Release();
vertLayout->Release();
depthStencilView->Release();
depthStencilBuffer->Release();
cbPerObjectBuffer->Release();
}

bool InitScene()
{
//Compile Shaders from shader file
hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);

//Create the Shader Objects
hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

//Set Vertex and Pixel Shaders
d3d11DevCon->VSSetShader(VS, 0, 0);
d3d11DevCon->PSSetShader(PS, 0, 0);

///////////////**************new**************////////////////////
//Create the vertex buffer
Vertex v[10000] = { Vertex(1.0f, 1.0f, 1.0f, 1.0f, 1.0f) };
int indices[] = { 1 };
/* for (int k = 0; k < bsp.m_nNumVerts; k++)
{
 v[k] = Vertex(pVB[k].p.x, pVB[k].p.y, pVB[k].p.z, pVB[k].tu1, pVB[k].tv1);
}

DWORD indices[10000];
for (int k = 0; k < bsp.m_nNumMeshIndices; k++)
{
 indices[k] = bsp.m_pMeshIndices[k];
}*/

/* char buffer[100];
for (int x = 0; x < bsp.m_nNumVerts; x++)
{
 sprintf(buffer, "Vert %d: X:%f Y:%f Z:%f", x, pVB[x].p.x, pVB[x].p.y, pVB[x].p.z);
 MessageBoxA(NULL, buffer, buffer, MB_OK);
}
for (int x = 0; x < bsp.m_nNumMeshIndices; x++)
{
 sprintf(buffer, "Indice %d: %d", x, bsp.m_pMeshIndices[x]);
 MessageBoxA(NULL, buffer, buffer, MB_OK);
}*/

/*D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));

indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(WORD)* bsp.m_nNumMeshIndices;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA iinitData;

iinitData.pSysMem = indices;
d3d11Device->CreateBuffer(&indexBufferDesc, &iinitData, &squareIndexBuffer);

d3d11DevCon->IASetIndexBuffer(squareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);


D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));

vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(Vertex)* bsp.m_nNumVerts;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;*/


 

 

char buf[100];
sprintf(buf, "bsp.m_nNumFaces: %d bsp.m_nNumVerts: %d bsp.m_nNumMeshIndices: %d ",bsp.m_nNumFaces, bsp.m_nNumVerts, bsp.m_nNumMeshIndices);
MessageBoxA(NULL, buf, buf, MB_OK);
///////////////**************new**************////////////////////

/*D3D11_SUBRESOURCE_DATA vertexBufferData;  
 
ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
vertexBufferData.pSysMem = v;
//hr = d3d11Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &squareVertBuffer);
hr = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &squareVertBuffer);*/
//Set the vertex buffer
// UINT stride = sizeof(Vertex);
// UINT offset = 0;
/* d3d11DevCon->IASetVertexBuffers( 0, 1, &squareVertBuffer, &stride, &offset );
*/
//Create the Input Layout
hr = d3d11Device->CreateInputLayout( layout, numElements, VS_Buffer->GetBufferPointer(),  
 VS_Buffer->GetBufferSize(), &vertLayout );

//Set the Input Layout
d3d11DevCon->IASetInputLayout( vertLayout );

//Set Primitive Topology
d3d11DevCon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

//Create the Viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = Width;
viewport.Height = Height;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;

//Set the Viewport
d3d11DevCon->RSSetViewports(1, &viewport);

//Create the buffer to send to the cbuffer in effect file
D3D11_BUFFER_DESC cbbd;  
ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));

cbbd.Usage = D3D11_USAGE_DEFAULT;
cbbd.ByteWidth = sizeof(cbPerObject);
cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbbd.CPUAccessFlags = 0;
cbbd.MiscFlags = 0;

hr = d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);

//Camera information
camPosition = XMVectorSet( 0.0f, 50.0f, -8.0f, 0.0f );
camTarget = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f );
camUp = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f );

//Set the View matrix
camView = XMMatrixLookAtLH( camPosition, camTarget, camUp );

//Set the Projection matrix
camProjection = XMMatrixPerspectiveFovLH( 0.4f*3.14f, Width/Height, 1.0f, 1000.0f);

///////////////**************new**************////////////////////
// strcat(bsp.m_pTextures[0].Name, ".jpg");
hr = D3DX11CreateShaderResourceViewFromFile(d3d11Device, L"textures\\gothic_floor\\largeblockfloor3.jpg",
 NULL, NULL, &CubesTexture, NULL );
//if (hr != S_OK)
// MessageBoxA(NULL, "Something went wrong reading the texture", (LPCSTR)bsp.m_pTextures[0].Name, MB_OK);
// Describe the Sample State
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory( &sampDesc, sizeof(sampDesc) );
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
   sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
   sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
   sampDesc.MinLOD = 0;
   sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
   
//Create the Sample State
hr = d3d11Device->CreateSamplerState( &sampDesc, &CubesTexSamplerState );
///////////////**************new**************////////////////////

return true;
}

void UpdateScene()
{
//Keep the cubes rotating
rot += .0005f;
if(rot > 6.26f)
 rot = 0.0f;

//Reset cube1World
cube1World = XMMatrixIdentity();

//Define cube1's world space matrix
XMVECTOR rotaxis = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
Rotation = XMMatrixRotationAxis( rotaxis, rot);
Translation = XMMatrixTranslation( -5.0f, 0.0f, 4.0f );

//Set cube1's world space using the transformations
cube1World = Translation * Rotation;

//Reset cube2World
cube2World = XMMatrixIdentity();

//Define cube2's world space matrix
Rotation = XMMatrixRotationAxis( rotaxis, -rot);
Scale = XMMatrixScaling( 1.3f, 1.3f, 1.3f );

//Set cube2's world space matrix
cube2World = Rotation * Scale;
}

void DrawScene()
{
//Clear our backbuffer
float bgColor[4] = {(0.0f, 0.0f, 0.0f, 0.0f)};
d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);

//Refresh the Depth/Stencil view
d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

//Set the WVP matrix and send it to the constant buffer in effect file
WVP = cube1World * camView * camProjection;
cbPerObj.WVP = XMMatrixTranspose(WVP);  
d3d11DevCon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
d3d11DevCon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
///////////////**************new**************////////////////////
d3d11DevCon->PSSetShaderResources( 0, 1, &CubesTexture );
d3d11DevCon->PSSetSamplers( 0, 1, &CubesTexSamplerState );
///////////////**************new**************////////////////////

//Draw the first cube
//d3d11DevCon->DrawIndexed( bsp.m_nNumMeshIndices, 0, 0 );
for (int k = 0; k < bsp.m_nNumFaces; k++)
{
 bsp.DrawFace(d3d11Device, d3d11DevCon, k); //This fails to draw, even though DrawFace() itself calls Draw() and DrawIndexed() 
}
d3d11DevCon->Draw(bsp.m_nNumVerts, 0); //This draws nothing as well
WVP = cube2World * camView * camProjection;
cbPerObj.WVP = XMMatrixTranspose(WVP);  
d3d11DevCon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
d3d11DevCon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
///////////////**************new**************////////////////////
d3d11DevCon->PSSetShaderResources( 0, 1, &CubesTexture );
d3d11DevCon->PSSetSamplers( 0, 1, &CubesTexSamplerState );
///////////////**************new**************////////////////////

//Draw the second cube
//d3d11DevCon->DrawIndexed( bsp.m_nNumVerts, 0, 0 );

//Present the backbuffer to the screen
SwapChain->Present(0, 0);
}

int messageloop(){
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while(true)
{
 BOOL PeekMessageL(  
  LPMSG lpMsg,
  HWND hWnd,
  UINT wMsgFilterMin,
  UINT wMsgFilterMax,
  UINT wRemoveMsg
  );

 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
 {
  if (msg.message == WM_QUIT)
   break;
  TranslateMessage(&msg);  
  DispatchMessage(&msg);
 }
 else{
  // run game code            
  UpdateScene();
  DrawScene();
 }
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch( msg )
{
case WM_KEYDOWN:
 if( wParam == VK_ESCAPE ){
  DestroyWindow(hwnd);
 }
 return 0;

case WM_DESTROY:
 PostQuitMessage(0);
 return 0;
}
return DefWindowProc(hwnd,
 msg,
 wParam,
 lParam);
}

As you can see I call bsp.DrawFace() and nothing draws. I really, really, need some help here. The only way I've been staying clean off heroin for over a year and three days now has been working on my game engine, but I can't get past this point of updating this header file to DX11. It may be a C++ problem or a DX problem, or both, I don't know, I just don't have the experience.

Even though I am able to obtain all the vertex/indices from a bsp map, it seems when I draw them manually they're only half-drawn due to the way BSP tree maps save on memory and rendering(Am I correct about that?) which if true, leaves me no alternative but to have this header work with DX11 the way it has been working with DX9.

I really, really need somebody's help on this. Hell even a reply of praise or saying I'm doing something right might help. I'm really getting back to the point where I was and I'm feeling the urge to use heroin again, I don't want to go back to do those dark days, I want to be a game developer. If anybody reading this knows anybody who is proficient in DirectX, please refer me to them, I'm willing to pay up to $500 just to have someone help me upgrade this header to DX11 and have it function when I call it from main.cpp like it does on my game engine in DX9. I've been having this problem for over a week now, and for months I've wanted to upgrade my game engine to DX10/11 but this has always been the reason I can't. If I go on IRC channels they're only C++ based, not graphics API centered. And this is the best gamedev forum on the internet, I don't know where else to go. Please, somebody please spot my mistakes and help me to correct them, and if you can't, if you could find someone who does have the expertise I'd be willing to pay half a grand(what'd I'd spend in one day on heroin/fentanyl when I was using) just to learn and continue to write my game engine, it's the only thing that keeps me sober. I don't want my life going back to hell and I really need some help.

Thank you all for your help and support.

Tom_W said:
Even though I am able to obtain all the vertex/indices from a bsp map, it seems when I draw them manually they're only half-drawn due to the way BSP tree maps save on memory and rendering

So you think it's related to BSP traversal and clipping?

That's a bit unlikely, because this logic should not be affected by changing rendering API.

The buggy triangles you got from the simple box level look somehow corrupted. Does it change shape only if you move / look, or does it flicker all the time, without any reason?

If you're clueless how to continue right now, i suggest you implement the debug renderer i talked about in older threads.

You remember, just drawing points and lines, independent from game and engine code.

This way you can visually verify the data and make sure it is right, at least before you upload to GPU.

The very first steps to develop a realtime application are mostlyu about debugging:

Setup a way to log text, e.g. using vprintf, to check variables etc.

Make window and initialize all that drawing crap.

Get your debug drawing of points and lines working, to verify / visualize any kind of geometric data.

Make a first person camera to navigate through space and scene, like fly mode in Quake, so you can find stuff and look at it from all angles.

Optional: Add some GUI like imGui, because that's the ultimate tool to change settings on the fly, do quick actions with a button click, etc. This way you can add interface to change any variable anywhere in your code at runtime with one line of code.

You need this basic stuff so you can help yourself to make sure stuff works, or to narrow things down if they go wrong.

It's some work, but it is aklways worth on the long run, even if it's not your current goal. Start saving time now, to avoid frustration tomorrow.

As soon as you are able to visualize stuff, e.g. draw a box just with code without need for an editor, you will feel better, and you get feedback to have things under control, and you will find your bugs without help…

I think you're trying to do too many things at once.

First, try uploading the vertices and indices for ONE TRIANGLE.

See if that draws. Does it draw with the D3D9 code? Does it draw with the D3D11 code?

Run the code under “Debug” runtime, and make sure it doesn't complain about any errors.

Run the code with the visual studio D3D debugger, that lets you step through vertex transform calls and pixel shader calls. Verify that the data come out the way you expect them. Look at all the different states in the transform and shading pipeline, verify that they all look like you think they should.

Draw without textures, using a simple vertex shader that outputs just red. Turn off depth, stencil, back cull, alpha test, and all the rest of the things that might cause a pixel to not get shaded.

Keep doing The Simplest Possible Thing and looking at all the output values of that simple triangle, until you actually get it rendering on screen.

Now, you can start building things up again. Turn on each thing you turned off, one at a time, to make sure that none of them get in the way. If the triangle goes away, you know where the problem is.

Once you have the pipeline up and running for the single triangle, draw something with two, three, or four triangles (but no more.) Again, verify that the data are all the way you expect them to be, before and after transforms.

THEN you can start loading the data from the BSP trees. If it draws correctly with D3D9, then it should draw correctly with D3D11 – the shape of graphics data didn't really change between the versions, just how you expressed that data. So, if it works with D3D9, then the difference is some bit of state you're not setting right – be it shader parameter, vertex layout declaration parameters, constant blocks, or what have you.

enum Bool { True, False, FileNotFound };
Advertisement

hplus0603 said:
I think you're trying to do too many things at once.

Yeah, pretty much what i think since day one.

Tom, you try to get big things working in one step, and then if something does not work it becomes a needle in the haystack and you get stuck.

Above advice is more on point than mine, but you see we both suggest to begin with small things that are more easy to verify and then progress step by step.
This applies to everything, not just the usage of a gfx API. So the real problem presists even after you solve your current specific issue.

There is not only this game to make at any cost, you also have to develop those ‘debug’ or ‘help yourself’ skill to get there. It's about the ability to subdivide a larger problem into multiple smaller ones in order, and having the tools handy to verify each step certainly. Trial and error is a powerful tool, but it only works for very small problems. ; )

Thanks for the help guys. But when I say I've been at it at days, I've taken up(some) of your advice prior to you giving it. I've read the data from the map one vertex at a time, with the map being a simple cube. I used an earlier DX11 tutorial that uses color rather than textures to make sure it wasn't a texturing problem. It read in six faces, 24 vertices, and 18 indices(Only 18? The tutorial I'm using that renders two cubes has an index buffer that reads in 36 indices for a cube; This is why I think it's BSP traversal of somekind, this header never, ever, gives me faulty info, and it's 100% the same header as it is in my DX9 game engine, at the point of this screenshot).

This is the rendered result:
https://drive.google.com/open?id=1k-8nwqNpV1aV6OvcYr6fn-EtX_npEdQT

And this was with the header file completely unchanged; No change in API whatsoever, I simply made a function to retrieve the BSP input's info one vertex at a time(literally, I made it have out that went “vertex #0: X: -7.000 0.000 7.000 indices #0: 0” all the way up to 24. So this makes what JoeJ said very important--is this due to BSP traversal? Because this header is flawless, I've been using it for a year. It would not give me faulty info.

And no, no flickering whatsoever. Does having the total vertex count, the first vertex to the last one, all the indices, and trying to draw them, you're saying this should render the map? As you can see in the picture, it very nearly does render that little cube(which was made in GTKRadiant and exported as a BSP map).

Thank you for your advice thus far, I'm still clueless. I need to know if this a traversal problem or not, to quote Ken Wright, author of the header itself:

“In my opinion the trickiest part of getting the data, and rendering it, is taking into account the various offsets. As you have the list of faces, which has offsets to the start of the mesh data and vertices data.”

I can't isolate the problem if I don't know the root cause--does a cube with 24 vertices and 18 indices make sense?

Thank you guys for your help thus far. I didn't expect so many responses when I woke up, I fell asleep quite depressed.

Tom_W said:
I can't isolate the problem if I don't know the root cause--does a cube with 24 vertices and 18 indices make sense?

18 / 3 = 6 for 6 triangles. makes sense.

But why 24 vertices? Cube has 8 corners, shared by 3 planes - each having it's own UVs, so 3 * 8 = 24. Also makes sense.

Tom_W said:
“In my opinion the trickiest part of getting the data, and rendering it, is taking into account the various offsets. As you have the list of faces, which has offsets to the start of the mesh data and vertices data.”

So an indexing bug. Could well be. (When you said BSP i thought you mean it's function, which should be unaffected. But it's possible you somehow break various offsets so wrong data becomes associated)

But looking at the image, it could form a box by adding some missing triangles. Almost. But the box would become thicker than your level IIRC, and also vertices do not really form an exact box. They are somewhat off.

This makes me think the vertex format is different, for example:

DX9:

pos xyz

uv0 xy

uv1 xy

but DX11 might be set up so it expects:

pos xyzw (w unused)

uv0 xy

uv1 xy

As a result it would look like random data because everything becomes off while iterating vertices. Same would happen if precision does not match, like 32bits vs. 16bits for the numbers.

If you can't find something like this, setting up a single triangle, then a quad, then a cube manually (not using all the BSP and loading stuff) would assure you know how the render data has to be.

Next step is to convert the loaded data into this same layout. At this point you already can visualize triangles which should be helpful for proof.

… looking at your code this layout stuff looks correct to me :(

So filling such a vertex buffer with manual numbers to draw a cube is the only idea i have.

This topic is closed to new replies.

Advertisement