Jump to content
  • Advertisement
Sign in to follow this  
RythBlade

incorrect depth test result

This topic is 2533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folks I was wondering if you guys could help me with a DirectX problem.

I'm trying to implement deferred shading. I've got a G-buffer set up and mostly working and a full screen quad ready to render it all to. Basically what's stopping me is what appears to be an incorrect depth test result when rendering.

I reverted back to drawing a simple cube and sphere on screen with some phong shading, but now these are suffering the same problem.

Simply if I don't set up the depth buffer - everything renders, albeit with obvious problems due to no depth test - if I do set it up and use it nothing renders and I'm stuck with a lovely clear screen.

Pix says the pixels are being eliminated by the depth test, but there's nothing to occlude anything....The history of the pixel wthin Pix however does display the correct colour before eiliminating the pixel.....
The directX debug output doesn't seem to give any clues either.....

I'm at a complete loss on this one, I've diffed the code between a working and non-working version and can't find what/where I've done wrong so here comes a code dump for your perusal:

Thanks very much for any help in advance!

This is the main class I'm doing the work in for now. It contains the setup/destruction and render code (see initD3D and render functions)
You should also see some of the deferred shading stuff commented out within this class.


#include "D3DApplication.h"
/*----------------------------------------------------------------------------\
* Initialisation and Clean up |
*----------------------------------------------------------------------------*/
/*
Constructor
Parameter list
eventBroker: the event broker for the application
inputHandler: the input handler for the application
*/
D3DApplication::D3DApplication(InputHandler *inputHandler)
{
crashed = false;
pInputHandler = inputHandler;
}
/*
Destructor
*/
D3DApplication::~D3DApplication()
{
}
/*
Initialises Direct3D
Parameter list
hWnd: handle of window
*/
HRESULT D3DApplication::initD3D(HWND hWnd)
{
// create a struct to hold information about the swap chain
DXGI_SWAP_CHAIN_DESC scd;
// clear out the struct for use
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
// fill the swap chain description struct
scd.BufferCount = 1; // On use one backbuffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit colour
scd.BufferDesc.Width = SCREEN_WIDTH; // set the back buffer width
scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how the swap chain is to be used
scd.OutputWindow = hWnd; // the window to be used
scd.SampleDesc.Count = D3D_SAMPLE_DESC_COUNT; // how many multisamples - anti-aliasing
scd.SampleDesc.Quality = D3D_SAMPLE_DESC_QUALITY;
scd.Windowed = TRUE; // windowed/full-screen mode
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching
scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
scd.Flags = 0;
// for this to work we need a directX 11 or 10.1 device so check for these
D3D_FEATURE_LEVEL levelsWanted[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
};
D3D_FEATURE_LEVEL *acceptable = NULL;
// create a device, device context and swap chaing using the scd struct
HrCheck(D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE, // we don't want to use the software reference device so only specify the hardware device
NULL,
NULL,
levelsWanted,
sizeof( levelsWanted ) / sizeof( levelsWanted[0] ),
D3D11_SDK_VERSION,
&scd,
&pSwapchain,
&pDev,
acceptable,
&pDevcon));
// now we have a device, check it supports directx 11
HrCheck(checkFeatureSupport());
// get the address of the back buffer
ID3D11Texture2D *pBackBufferTex;
// the first back buffer defined in the swap chain using ID3D11Texture2D COM object.
HrCheck(pSwapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBufferTex));
// use the back buffer address to create the render target
HrCheck(pDev->CreateRenderTargetView(pBackBufferTex, NULL, &pBackbuffer));
// we're done with the texture COM object so we can release it (this doesn't release the actual back
// buffer - just the COM object accessing it
SafeRelease(pBackBufferTex);
// prepare the depth stencil - create the depth texture
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory( &descDepth, sizeof(descDepth) );
descDepth.Width = SCREEN_WIDTH;
descDepth.Height = SCREEN_HEIGHT;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = D3D_SAMPLE_DESC_COUNT;
descDepth.SampleDesc.Quality = D3D_SAMPLE_DESC_QUALITY;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
HrCheck(pDev->CreateTexture2D( &descDepth, NULL, &pDepthStencil));
// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory( &descDSV, sizeof(descDSV) );
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
HrCheck(pDev->CreateDepthStencilView( pDepthStencil, &descDSV, &pDepthStencilView ));
// set the render target as the back buffer, using the depth stencil view
pDevcon->OMSetRenderTargets(1, &pBackbuffer, pDepthStencilView);
D3D11_BLEND_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.AlphaToCoverageEnable = false;
desc.IndependentBlendEnable = true;
D3D11_RENDER_TARGET_BLEND_DESC targetDesc;
ZeroMemory(&targetDesc, sizeof(targetDesc));
desc.RenderTarget[0] = targetDesc;
desc.RenderTarget[0].BlendEnable = true;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
HrCheck(pDev->CreateBlendState(&desc, &pBlend));
// text rendering requires alpha blending
float blendFactors[] = { 0.0f, 0.0f, 0.0f, 0.0f };
pDevcon->OMSetBlendState(pBlend, blendFactors, 0xffffffff);
// set the viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = D3D_VIEWPORT_TOPLEFT_X;
viewport.TopLeftY = D3D_VIEWPORT_TOPLEFT_Y;
viewport.Width = D3D_VIEWPORT_WIDTH;
viewport.Height = D3D_VIEWPORT_HEIGHT;
viewport.MinDepth = D3D_VIEWPORT_MIN_DEPTH;
viewport.MaxDepth = D3D_VIEWPORT_MAX_DEPTH;
pDevcon->RSSetViewports(1, &viewport);
HrCheck(initPipeline());
HrCheck(initGraphics());

HrCheck(myCube.init(pDev, pDevcon));
HrCheck(mySphere.init(pDev, pDevcon, 1.0f, 10, 10));
HrCheck(myPlane.init(pDev, pDevcon));
lightPosition.x = 10.0f;
lightPosition.y = 10.0f;
lightPosition.z = 10.0f;
cubeRotation.x = 0.0f;
cubeRotation.y = 0.0f;
cubeRotation.z = 0.0f;
cubePosition.x = 0.0f;
cubePosition.y = 0.0f;
cubePosition.z = 0.0f;
D3D11_BUFFER_DESC bufferDesc;
//----------------------------- create the constants buffer ------------------------
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(ToolVertexShaderConstantBuffer);
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;

HRReturn(pDev->CreateBuffer( &bufferDesc, NULL, &m_pConstantBuffer ));
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(LightConstantBuffer);
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
HRReturn(pDev->CreateBuffer( &bufferDesc, NULL, &m_pLightingConstantBuffer ));

D3D11_TEXTURE2D_DESC textureDesc;
HRESULT result;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
// Initialize the render target texture description.
ZeroMemory(&textureDesc, sizeof(textureDesc));
// Setup the render target texture description.
textureDesc.Width = SCREEN_WIDTH;
textureDesc.Height = SCREEN_HEIGHT;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
textureDesc.SampleDesc.Count = D3D_SAMPLE_DESC_COUNT;
textureDesc.SampleDesc.Quality = D3D_SAMPLE_DESC_QUALITY;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
// Create the render target texture.
HRReturn(pDev->CreateTexture2D(&textureDesc, NULL, &pGBufNormalTex));
HRReturn(pDev->CreateTexture2D(&textureDesc, NULL, &pGBufColourTex));
// Setup the description of the render target view.
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
// Create the render target view.
HRReturn(pDev->CreateRenderTargetView(pGBufNormalTex, &renderTargetViewDesc, &pGBufNormalTarget));
HRReturn(pDev->CreateRenderTargetView(pGBufColourTex, &renderTargetViewDesc, &pGBufColourTarget));
/*
// Setup the description of the shader resource view.
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
// Create the shader resource view.
result = pDev->CreateShaderResourceView(m_renderTargetTexture, &shaderResourceViewDesc, &m_shaderResourceView);
if(FAILED(result))
{
return false;
}*/
return S_OK;
}
/*
Checks whether the hardware supports the compute shader
*/
HRESULT D3DApplication::checkFeatureSupport()
{
D3D11_FEATURE_DATA_D3D10_X_HARDWARE_OPTIONS options;
HrCheck(pDev->CheckFeatureSupport(
D3D11_FEATURE_D3D10_X_HARDWARE_OPTIONS,
&options,
sizeof(D3D11_FEATURE_D3D10_X_HARDWARE_OPTIONS)));
if( !options.ComputeShaders_Plus_RawAndStructuredBuffers_Via_Shader_4_x )
{
int returned = MessageBox(NULL, "COMPUTE NOTE SUPPORTED", "UNSUPPORTED HARDWARE", MB_YESNO | MB_ICONERROR);
// if the user decides to continue with no compute acceleration, make sure we switch it off
if(returned == IDYES)
{
//m_allowDirectCompute = false;
}
else
{
// something went wrong on the message box, or the user doesn't want to continue.
// In this instance we quite the application be returning E_FAIL
return E_FAIL;
}
}
return S_OK;
}
/*
Initialises the rendering pipeline
*/
HRESULT D3DApplication::initPipeline()
{
// load and compile the two shaders
ID3DBlob *VS, *PS, *gVS, *gPS, *gLightVS, *gLightPS;

HrCheck(CompileShaderFromFile(".\\Resources\\ToolsShaders.hlsl", "VShader", "vs_4_0", &VS));
HrCheck(CompileShaderFromFile(".\\Resources\\ToolsShaders.hlsl", "PShader", "ps_4_0", &PS));
HrCheck(CompileShaderFromFile(".\\Resources\\GBufferOutput.hlsl", "VShader", "vs_4_0", &gVS));
HrCheck(CompileShaderFromFile(".\\Resources\\GBufferOutput.hlsl", "PShader", "ps_4_0", &gPS));
HrCheck(CompileShaderFromFile(".\\Resources\\GBufferLighting.hlsl", "VShader", "vs_4_0", &gLightVS));
HrCheck(CompileShaderFromFile(".\\Resources\\GBufferLighting.hlsl", "PShader", "ps_4_0", &gLightPS));
// encapsulate both shaders into shader objects
HrCheck(pDev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &p3DVS));
HrCheck(pDev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &p3DPS));
HrCheck(pDev->CreateVertexShader(gVS->GetBufferPointer(), gVS->GetBufferSize(), NULL, &pGBufferVS));
HrCheck(pDev->CreatePixelShader(gPS->GetBufferPointer(), gPS->GetBufferSize(), NULL, &pGBufferPS));
HrCheck(pDev->CreateVertexShader(gLightVS->GetBufferPointer(), gLightVS->GetBufferSize(), NULL, &pGBufferLightingVS));
HrCheck(pDev->CreatePixelShader(gLightPS->GetBufferPointer(), gLightPS->GetBufferSize(), NULL, &pGBufferLightingPS));
// create the input layout object
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
HrCheck(pDev->CreateInputLayout(ied, 3, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout));
pDevcon->IASetInputLayout(pLayout);
SafeRelease(VS);
SafeRelease(PS);
SafeRelease(gVS);
SafeRelease(gPS);
SafeRelease(gLightVS);
SafeRelease(gLightPS);

return S_OK;
}
/*
Initialises the graphics
*/
HRESULT D3DApplication::initGraphics()
{
// initiaise the camera position
camera.setRadius(10.0f);
camera.setMatrices();
D3D11_BUFFER_DESC bufferDesc;
//----------------------------- create the constants buffer ------------------------
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(VertexShaderConstantBuffer);
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;

HrCheck(pDev->CreateBuffer( &bufferDesc, NULL, &pConstantBuffer ));
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(LightConstantBuffer);
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
HrCheck(pDev->CreateBuffer( &bufferDesc, NULL, &pLightingConstantBuffer ));
return S_OK;
}
/*
Cleans up Direct3D and COM
Parameter list
hWnd: handle of window
*/
HRESULT D3DApplication::cleanD3D()
{
// direct x can't close properly when in full screen mode - so ensure everything is windowed
HrCheck(pSwapchain->SetFullscreenState(FALSE, NULL)); // switch to windowed mode
// close and release all existing COM objects
SafeRelease(pBlend);
SafeRelease(m_pConstantBuffer);
SafeRelease(pGBufferVS);
SafeRelease(pGBufferPS);
SafeRelease(pGBufferLightingVS);
SafeRelease(pGBufferLightingPS);
SafeRelease(pGBufNormalTarget);
SafeRelease(pGBufColourTarget);
SafeRelease(pGBufNormalTex);
SafeRelease(pGBufColourTex);
SafeRelease(m_pLightingConstantBuffer);
SafeRelease(pConstantBuffer);
SafeRelease(pLightingConstantBuffer);
SafeRelease(pDepthStencil);
SafeRelease(pDepthStencilView);
SafeRelease(pLayout);
SafeRelease(p3DVS);
SafeRelease(p3DPS);
SafeRelease(pSwapchain);
SafeRelease(pBackbuffer);
SafeRelease(pDev);
SafeRelease(pDevcon);
return S_OK;
}
/*----------------------------------------------------------------------------\
* Update |
*----------------------------------------------------------------------------*/
HRESULT D3DApplication::run()
{
if(crashed)
{
return E_FAIL;
}
if(FAILED(update()))
{
return E_FAIL;
}
if(FAILED(renderFrame()))
{
return E_FAIL;
}
return S_OK;
}
/*
Runs the update for the frame.
*/
HRESULT D3DApplication::update()
{
handleCameraInput();
handleInput();
return S_OK;
}
void D3DApplication::handleInput()
{
if((*pInputHandler->getKeyboardMap())[KEY_Q] == KEY_HELD)
{
lightPosition.x += 0.001;
}
if((*pInputHandler->getKeyboardMap())[KEY_A] == KEY_HELD)
{
lightPosition.x -= 0.001;
}
if((*pInputHandler->getKeyboardMap())[KEY_W] == KEY_HELD)
{
lightPosition.y += 0.001;
}
if((*pInputHandler->getKeyboardMap())[KEY_S] == KEY_HELD)
{
lightPosition.y -= 0.001;
}
if((*pInputHandler->getKeyboardMap())[KEY_E] == KEY_HELD)
{
lightPosition.z += 0.001;
}
if((*pInputHandler->getKeyboardMap())[KEY_D] == KEY_HELD)
{
lightPosition.z -= 0.001;
}
}
void D3DApplication::handleCameraInput()
{
if((*pInputHandler->getMouseButtonMap())[MOUSE_LBUTTON] == KEY_HELD)
{
D3DXVECTOR3 *delta = pInputHandler->getMousePosDelta();
if(delta->x != 0)
{
camera.setHorizontalRotation(camera.getHorizontalRotation() + MOUSE_X_SCROLL_SPEED * delta->x);
}
if(delta->y != 0)
{
camera.setVerticalRotation(camera.getVerticalRotation() + MOUSE_Y_SCROLL_SPEED * delta->y);
}
}
float mouseWheelDelta = pInputHandler->getMouseWheelDelta();
if(mouseWheelDelta != 0)
{
camera.setRadius(camera.getRadius() + MOUSE_WHEEL_SCROLL_SPEED * mouseWheelDelta);
}
}
/*----------------------------------------------------------------------------\
* Rendering |
*----------------------------------------------------------------------------*/
/*
Function used to render a single frame
*/
HRESULT D3DApplication::renderFrame()
{
// clear the screen and depth buffer
pDevcon->ClearRenderTargetView(pBackbuffer, D3DXCOLOR(D3D_CLEAR_COLOUR));
pDevcon->ClearRenderTargetView(pGBufColourTarget, D3DXCOLOR(1.0f, 1.0f, 1.0f, 0.0f));
pDevcon->ClearRenderTargetView(pGBufNormalTarget, D3DXCOLOR(1.0f, 0.0f, 0.0f, 0.0f));
pDevcon->ClearDepthStencilView(pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
// render the 3D world first
HrCheck(render3DWorld());
// switch the back buffer and front buffer
HrCheck(pSwapchain->Present(0, 0));
return S_OK;
}
HRESULT D3DApplication::render3DWorld()
{
// set the shader objects to be the standard 3D shaders.
pDevcon->VSSetShader(p3DVS, 0, 0);
pDevcon->PSSetShader(p3DPS, 0, 0);
// set the vertex and index buffers for the pipeline
UINT stride = sizeof(VERTEX);
UINT offset = 0;

// finalise the view and projection matrices for this frame
camera.setMatrices();
LightConstantBuffer lightBuffer;
lightBuffer.eyePos.x = camera.getCameraPos()->x;
lightBuffer.eyePos.y = camera.getCameraPos()->y;
lightBuffer.eyePos.z = camera.getCameraPos()->z;
lightBuffer.eyePos.w = 99.99;
lightBuffer.lightPositon.x = lightPosition.x;
lightBuffer.lightPositon.y = lightPosition.y;
lightBuffer.lightPositon.z = lightPosition.z;
lightBuffer.lightPositon.w = 99.99;
pDevcon->UpdateSubresource( m_pLightingConstantBuffer, 0, NULL, &lightBuffer, sizeof(LightConstantBuffer), 0 );
ToolVertexShaderConstantBuffer cb;
// pass the constant buffer information into the vertex shader constant buffer
// due to the way the graphics card arranges it's memory, the matrices need to be transposed
D3DXMatrixTranspose(&cb.mView, camera.getViewMat() );
D3DXMatrixTranspose(&cb.mProjection, camera.getProjMat() );

D3DXMATRIXA16 tempx;
D3DXMATRIXA16 tempy;
D3DXMATRIXA16 tempz;
cubeRotation.x += 0.001f;
cubeRotation.y += 0.001f;
cubeRotation.z += 0.001f;
cubePosition.x = 0.0f;
cubePosition.y = 0.0f;
cubePosition.z = 0.0f;
D3DXMatrixTranslation(
&cb.mWorld,
cubePosition.x,
cubePosition.y,
cubePosition.z);
D3DXMatrixRotationX(&tempx, cubeRotation.x);
D3DXMatrixRotationY(&tempy, cubeRotation.y);
D3DXMatrixRotationZ(&tempz, cubeRotation.z);
D3DXMatrixMultiply(&tempx, &tempx, &tempy);
D3DXMatrixMultiply(&tempx, &tempx, &tempz);
D3DXMatrixMultiply(&cb.mWorld, &tempx, &cb.mWorld);
D3DXMatrixTranspose(&cb.mWorld, &cb.mWorld );
cb.materialColour.x = 1.0f;
cb.materialColour.y = 0.0f;
cb.materialColour.z = 0.0f;
cb.materialColour.w = 1.0f;
pDevcon->UpdateSubresource( m_pConstantBuffer, 0, NULL, &cb, 0, 0 );
pDevcon->VSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
pDevcon->VSSetConstantBuffers( 1, 1, &m_pLightingConstantBuffer );
pDevcon->PSSetConstantBuffers( 1, 1, &m_pLightingConstantBuffer );
// select which primtive type we are using
pDevcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//myCube.setBuffers();
//myCube.draw();
mySphere.setBuffers();
mySphere.draw();
// mark the light position.
D3DXMatrixTranslation(
&cb.mWorld,
lightPosition.x,
lightPosition.y,
lightPosition.z);
D3DXMatrixTranspose(&cb.mWorld, &cb.mWorld );
pDevcon->UpdateSubresource( m_pConstantBuffer, 0, NULL, &cb, 0, 0 );
pDevcon->VSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
pDevcon->VSSetConstantBuffers( 1, 1, &m_pLightingConstantBuffer );
pDevcon->PSSetConstantBuffers( 1, 1, &m_pLightingConstantBuffer );
myCube.setBuffers();
myCube.draw();

/*
// set the shader objects to be the standard 3D shaders.
pDevcon->VSSetShader(pGBufferVS, 0, 0);
pDevcon->PSSetShader(pGBufferPS, 0, 0);
// set the vertex and index buffers for the pipeline
UINT stride = sizeof(VERTEX);
UINT offset = 0;

// finalise the view and projection matrices for this frame
camera.setMatrices();
ToolVertexShaderConstantBuffer cb;
// pass the constant buffer information into the vertex shader constant buffer
// due to the way the graphics card arranges it's memory, the matrices need to be transposed
D3DXMatrixTranspose(&cb.mView, camera.getViewMat() );
D3DXMatrixTranspose(&cb.mProjection, camera.getProjMat() );

D3DXMATRIXA16 tempx;
cubeRotation.x += 0.001f;
D3DXMatrixRotationX(&cb.mWorld, cubeRotation.x);
D3DXMatrixTranspose(&cb.mWorld, &cb.mWorld );
cb.materialColour.x = 1.0f;
cb.materialColour.y = 0.0f;
cb.materialColour.z = 0.0f;
cb.materialColour.w = 1.0f;
pDevcon->UpdateSubresource( m_pConstantBuffer, 0, NULL, &cb, 0, 0 );
pDevcon->VSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
// select which primtive type we are using
pDevcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//myCube.setBuffers();
mySphere.setBuffers();
ID3D11RenderTargetView *targets[2];
targets[0] = pGBufNormalTarget;
targets[1] = pGBufColourTarget;
pDevcon->OMSetRenderTargets(2, targets, pDepthStencilView);
// render to the GBuffer
//myCube.draw();
mySphere.draw();

// mark the light position.
D3DXMatrixTranslation(
&cb.mWorld,
lightPosition.x,
lightPosition.y,
lightPosition.z);
D3DXMatrixTranspose(&cb.mWorld, &cb.mWorld );
pDevcon->UpdateSubresource( m_pConstantBuffer, 0, NULL, &cb, 0, 0 );
pDevcon->VSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
pDevcon->VSSetConstantBuffers( 1, 1, &m_pLightingConstantBuffer );
pDevcon->PSSetConstantBuffers( 1, 1, &m_pLightingConstantBuffer );
mySphere.setBuffers();
mySphere.draw();


/* // set the shader objects to be the standard 3D shaders.
pDevcon->VSSetShader(pGBufferLightingVS, 0, 0);
pDevcon->PSSetShader(pGBufferLightingPS, 0, 0);

// when doing the final render
// set the render target as the back buffer, using the depth stencil view
pDevcon->OMSetRenderTargets(1, &pBackbuffer, pDepthStencilView);
D3DXMatrixIdentity(&cb.mWorld);
D3DXMatrixTranspose(&cb.mWorld, &cb.mWorld );
cb.materialColour.x = 1.0f;
cb.materialColour.y = 0.0f;
cb.materialColour.z = 0.0f;
cb.materialColour.w = 1.0f;
pDevcon->UpdateSubresource( m_pConstantBuffer, 0, NULL, &cb, 0, 0 );
LightConstantBuffer lightBuffer;
lightBuffer.eyePos.x = camera.getCameraPos()->x;
lightBuffer.eyePos.y = camera.getCameraPos()->y;
lightBuffer.eyePos.z = camera.getCameraPos()->z;
lightBuffer.eyePos.w = 99.99;
lightBuffer.lightPositon.x = lightPosition.x;
lightBuffer.lightPositon.y = lightPosition.y;
lightBuffer.lightPositon.z = lightPosition.z;
lightBuffer.lightPositon.w = 99.99;
pDevcon->UpdateSubresource( m_pLightingConstantBuffer, 0, NULL, &lightBuffer, sizeof(LightConstantBuffer), 0 );
pDevcon->VSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
pDevcon->VSSetConstantBuffers( 1, 1, &m_pLightingConstantBuffer );
pDevcon->PSSetConstantBuffers( 1, 1, &m_pLightingConstantBuffer );
myPlane.setBuffers();
//render to the back buffer
myPlane.draw();*/
return S_OK;
}


This is the sphere class I'm drawing:

// LCCREDIT start of LunaDX10.2
#include "D3DSphere.h"

D3DSphere::D3DSphere()
: mNumVertices(0), mNumFaces(0), md3dDevice(0), mVB(0), mIB(0),
mRadius(0.0f), mNumSlices(0), mNumStacks(0)
{
}
D3DSphere::~D3DSphere()
{
// ensure the COM objects are all cleaned up
SafeRelease(mVB);
SafeRelease(mIB);
}
HRESULT D3DSphere::init(ID3D11Device* device, ID3D11DeviceContext *pDeviceContext, float radius, UINT numSlices, UINT numStacks)
{
md3dDevice = device;
pDevcon = pDeviceContext;
mRadius = radius;
mNumSlices = numSlices;
mNumStacks = numStacks;
std::vector<VERTEX> vertices;
std::vector<DWORD> indices;
buildStacks(vertices, indices);

mNumVertices = (UINT)vertices.size();
mNumFaces = (UINT)indices.size()/3;
//----------------------------- create the vertex buffer---------------------------
D3D11_BUFFER_DESC bufferDesc;
D3D11_SUBRESOURCE_DATA defaultData;
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
ZeroMemory(&defaultData, sizeof(defaultData));
defaultData.pSysMem = &vertices[0];
bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
bufferDesc.ByteWidth = sizeof(VERTEX) * mNumVertices;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
HRReturn(device->CreateBuffer(&bufferDesc, &defaultData, &mVB));
//----------------------------- create the index buffer----------------------------
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
ZeroMemory(&defaultData, sizeof(defaultData));
defaultData.pSysMem = &indices[0];
bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
bufferDesc.ByteWidth = sizeof(DWORD) * mNumFaces * 3;
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
HRReturn(device->CreateBuffer(&bufferDesc, &defaultData, &mIB));
return S_OK;
}
void D3DSphere::setBuffers()
{
UINT stride = sizeof(VERTEX);
UINT offset = 0;
pDevcon->IASetVertexBuffers(0, 1, &mVB, &stride, &offset);
pDevcon->IASetIndexBuffer(mIB, DXGI_FORMAT_R32_UINT, 0);
}
void D3DSphere::draw()
{
pDevcon->DrawIndexed(mNumFaces*3, 0, 0);
}
void D3DSphere::buildStacks(VertexList& vertices, IndexList& indices)
{
float phiStep = D3DX_PI / mNumStacks;
// do not count the poles as rings
UINT numRings = mNumStacks-1;
// Compute vertices for each stack ring.
for(UINT i = 1; i <= numRings; ++i)
{
float phi = i*phiStep;
// vertices of ring
float thetaStep = 2.0f * D3DX_PI /mNumSlices;
for(UINT j = 0; j <= mNumSlices; ++j)
{
float theta = j*thetaStep;
VERTEX v;
// spherical to cartesian
v.Position.x = mRadius*sinf(phi)*cosf(theta);
v.Position.y = mRadius*cosf(phi);
v.Position.z = mRadius*sinf(phi)*sinf(theta);
D3DXVec3Normalize(&v.normal, &v.Position);
vertices.push_back( v );
}
}
// poles: note that there will be texture coordinate distortion

VERTEX poles[] =
{
{D3DXVECTOR3(0.0f, -mRadius, 0.0f), D3DXVECTOR3(0.0f, -1.0f, 0.0f), 0.0f, 1.0f},
{D3DXVECTOR3(0.0f, mRadius, 0.0f), D3DXVECTOR3(0.0f, 1.0f, 0.0f), 0.0f, 0.0f}
};
vertices.push_back( poles[0] );
vertices.push_back( poles[1] );
UINT northPoleIndex = (UINT)vertices.size()-1;
UINT southPoleIndex = (UINT)vertices.size()-2;
UINT numRingVertices = mNumSlices+1;
// Compute indices for inner stacks (not connected to poles).
for(UINT i = 0; i < mNumStacks-2; ++i)
{
for(UINT j = 0; j < mNumSlices; ++j)
{
indices.push_back(i*numRingVertices + j);
indices.push_back(i*numRingVertices + j+1);
indices.push_back((i+1)*numRingVertices + j);
indices.push_back((i+1)*numRingVertices + j);
indices.push_back(i*numRingVertices + j+1);
indices.push_back((i+1)*numRingVertices + j+1);
}
}
// Compute indices for top stack. The top stack was written
// first to the vertex buffer.
for(UINT i = 0; i < mNumSlices; ++i)
{
indices.push_back(northPoleIndex);
indices.push_back(i+1);
indices.push_back(i);
}
// Compute indices for bottom stack. The bottom stack was written
// last to the vertex buffer, so we need to offset to the index
// of first vertex in the last ring.
UINT baseIndex = (numRings-1)*numRingVertices;
for(UINT i = 0; i < mNumSlices; ++i)
{
indices.push_back(southPoleIndex);
indices.push_back(baseIndex+i);
indices.push_back(baseIndex+i+1);
}
}
// end of LunaDX10.2


and this is the cube class I'm drawing:

#include "D3DCube.h"

D3DCube::D3DCube()
: mNumVertices(0), mNumFaces(0), pDev(0), m_pVertexBuffer(0), m_pIndexBuffer(0)
{
}
D3DCube::~D3DCube()
{
// ensure the COM objects are all cleaned up
SafeRelease(m_pVertexBuffer);
SafeRelease(m_pIndexBuffer);
}
HRESULT D3DCube::init(ID3D11Device* device, ID3D11DeviceContext *pDeviceContext)
{
pDev = device;
pDevcon = pDeviceContext;
std::vector<VERTEX> vertices;
std::vector<DWORD> indices;
buildCube(vertices, indices);

mNumVertices = (UINT)vertices.size();
mNumFaces = (UINT)indices.size()/3;
//----------------------------- create the vertex buffer---------------------------
D3D11_BUFFER_DESC bufferDesc;
D3D11_SUBRESOURCE_DATA defaultData;
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
ZeroMemory(&defaultData, sizeof(defaultData));
defaultData.pSysMem = &vertices[0];
bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
bufferDesc.ByteWidth = sizeof(VERTEX) * mNumVertices;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
HRReturn(pDev->CreateBuffer(&bufferDesc, &defaultData, &m_pVertexBuffer));
//----------------------------- create the index buffer----------------------------
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
ZeroMemory(&defaultData, sizeof(defaultData));
defaultData.pSysMem = &indices[0];
bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
bufferDesc.ByteWidth = sizeof(DWORD) * mNumFaces * 3;
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
HRReturn(pDev->CreateBuffer(&bufferDesc, &defaultData, &m_pIndexBuffer));
return S_OK;
}
void D3DCube::setBuffers()
{
UINT stride = sizeof(VERTEX);
UINT offset = 0;
pDevcon->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
pDevcon->IASetIndexBuffer(m_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
}
void D3DCube::draw()
{
pDevcon->DrawIndexed(mNumFaces*3, 0, 0);
}
void D3DCube::buildCube(VertexList& vertices, IndexList& indices)
{
VERTEX vertex1;
vertex1.Position = SIDE_A;
vertex1.normal = NORMAL_1;
vertices.push_back(vertex1);
VERTEX vertex2;
vertex2.Position = SIDE_B;
vertex2.normal = NORMAL_1;
vertices.push_back(vertex2);
VERTEX vertex3;
vertex3.Position = SIDE_C;
vertex3.normal = NORMAL_1;
vertices.push_back(vertex3);

/**********************************/
VERTEX vertex4;
vertex4.Position = SIDE_B;
vertex4.normal = NORMAL_1;
vertices.push_back(vertex4);
VERTEX vertex5;
vertex5.Position = SIDE_D;
vertex5.normal = NORMAL_1;
vertices.push_back(vertex5);
VERTEX vertex6;
vertex6.Position = SIDE_C;
vertex6.normal = NORMAL_1;
vertices.push_back(vertex6);
/**********************************/
VERTEX vertex7;
vertex7.Position = SIDE_B;
vertex7.normal = NORMAL_2;
vertices.push_back(vertex7);
VERTEX vertex8;
vertex8.Position = SIDE_F;
vertex8.normal = NORMAL_2;
vertices.push_back(vertex8);
VERTEX vertex9;
vertex9.Position = SIDE_D;
vertex9.normal = NORMAL_2;
vertices.push_back(vertex9);
/**********************************/
VERTEX vertex10;
vertex10.Position = SIDE_D;
vertex10.normal = NORMAL_2;
vertices.push_back(vertex10);
VERTEX vertex11;
vertex11.Position = SIDE_F;
vertex11.normal = NORMAL_2;
vertices.push_back(vertex11);
VERTEX vertex12;
vertex12.Position = SIDE_G;
vertex12.normal = NORMAL_2;
vertices.push_back(vertex12);
/**********************************/
VERTEX vertex13;
vertex13.Position = SIDE_G;
vertex13.normal = NORMAL_3;
vertices.push_back(vertex13);
VERTEX vertex14;
vertex14.Position = SIDE_F;
vertex14.normal = NORMAL_3;
vertices.push_back(vertex14);
VERTEX vertex15;
vertex15.Position = SIDE_E;
vertex15.normal = NORMAL_3;
vertices.push_back(vertex15);
/**********************************/
VERTEX vertex16;
vertex16.Position = SIDE_G;
vertex16.normal = NORMAL_3;
vertices.push_back(vertex16);
VERTEX vertex17;
vertex17.Position = SIDE_E;
vertex17.normal = NORMAL_3;
vertices.push_back(vertex17);
VERTEX vertex18;
vertex18.Position = SIDE_H;
vertex18.normal = NORMAL_3;
vertices.push_back(vertex18);
/**********************************/
VERTEX vertex19;
vertex19.Position = SIDE_E;
vertex19.normal = NORMAL_4;
vertices.push_back(vertex19);
VERTEX vertex20;
vertex20.Position = SIDE_A;
vertex20.normal = NORMAL_4;
vertices.push_back(vertex20);
VERTEX vertex21;
vertex21.Position = SIDE_H;
vertex21.normal = NORMAL_4;
vertices.push_back(vertex21);
/**********************************/
VERTEX vertex22;
vertex22.Position = SIDE_A;
vertex22.normal = NORMAL_4;
vertices.push_back(vertex22);
VERTEX vertex23;
vertex23.Position = SIDE_C;
vertex23.normal = NORMAL_4;
vertices.push_back(vertex23);
VERTEX vertex24;
vertex24.Position = SIDE_H;
vertex24.normal = NORMAL_4;
vertices.push_back(vertex24);
/**********************************/
VERTEX vertex25;
vertex25.Position = SIDE_E;
vertex25.normal = NORMAL_6;
vertices.push_back(vertex25);
VERTEX vertex26;
vertex26.Position = SIDE_F;
vertex26.normal = NORMAL_6;
vertices.push_back(vertex26);
VERTEX vertex27;
vertex27.Position = SIDE_B;
vertex27.normal = NORMAL_6;
vertices.push_back(vertex27);
/**********************************/
VERTEX vertex28;
vertex28.Position = SIDE_A;
vertex28.normal = NORMAL_6;
vertices.push_back(vertex28);
VERTEX vertex29;
vertex29.Position = SIDE_E;
vertex29.normal = NORMAL_6;
vertices.push_back(vertex29);
VERTEX vertex30;
vertex30.Position = SIDE_B;
vertex30.normal = NORMAL_6;
vertices.push_back(vertex30);
/**********************************/
VERTEX vertex31;
vertex31.Position = SIDE_D;
vertex31.normal = NORMAL_5;
vertices.push_back(vertex31);
VERTEX vertex32;
vertex32.Position = SIDE_G;
vertex32.normal = NORMAL_5;
vertices.push_back(vertex32);
VERTEX vertex33;
vertex33.Position = SIDE_H;
vertex33.normal = NORMAL_5;
vertices.push_back(vertex33);
/**********************************/
VERTEX vertex34;
vertex34.Position = SIDE_D;
vertex34.normal = NORMAL_5;
vertices.push_back(vertex34);
VERTEX vertex35;
vertex35.Position = SIDE_H;
vertex35.normal = NORMAL_5;
vertices.push_back(vertex35);
VERTEX vertex36;
vertex36.Position = SIDE_C;
vertex36.normal = NORMAL_5;
vertices.push_back(vertex36);
/**********************************/
for(int i = 0; i < vertices.size(); i++)
{
indices.push_back(i);
}
}

Share this post


Link to post
Share on other sites
Advertisement
Hi there, thanks very much for your speedy reply, and sorry for my late reply :P

Apparently so! However I added a state and it's still not cured the problem........I'm thinking there's a dodgy variable setup somewhere and I've mismatched something so I've started again from scratch to make sure that all of my variables are properly in-line as I wasn't too far through.
Thanks for the suggestion though - it led to me discovering a whole load of new state set ups I didn't know about which will be useful which is another reason I've started again.

Now I've run into another problem :P I can't get DirectX 11 to create a feature level 11 objects for me. I know my hardware supports this however. I've started a new thread for this problem which you can find here.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!