Jump to content

  • Log In with Google      Sign In   
  • Create Account


SteveHatcher

Member Since 27 Nov 2013
Offline Last Active Jul 29 2014 07:11 AM
-----

Topics I've Started

Correct texture mapping protocol

23 February 2014 - 12:28 AM

Hi All,

 

I am following the Microsoft example of applying textures to primitives: http://msdn.microsoft.com/en-us/library/windows/apps/jj552949.aspx

I am trying to turn it into oop code so I can adapt it to my 'game'. I am wondering how to take out the CreateDDSTextureFromFile() function and have it called first, to initialize the texture into memory.

 

I thought simply having a function which ONLY calls CreateDDSTextureFromFile(), to load up individual texture sheets before anything else, but when I do that in this example it just draws a black quad (I turned it from a cube into a 2d quad).

 

Where have I gone wrong in my thinking? EDIT - I am an idiot, was just using the wrong file path to load the texture. It works now. But part two of my question remains.

 

Thanks

P.S Unrelated to this issue but roughly 50% of the time when I run this code the program will crash and point to

	CBNeverChanges cbNeverChanges;
->	cbNeverChanges.mView = XMMatrixTranspose(g_View);
	g_pImmediateContext->UpdateSubresource(g_pCBNeverChanges, 0, nullptr, &cbNeverChanges, 0, 0);

with an access violation reading location 0xFFFFFFFF.

 

But I can just re-run the build and it will again either crash or work fine. any insight onto what would cause temperamental crashes?

 

Edit, the original source file is from here: http://code.msdn.microsoft.com/windowsdesktop/Direct3D-Tutorial-Win32-829979ef and I am using example07


Highly confused about simple quad texturing

27 January 2014 - 08:08 AM

Hello,

 

I am trying to texture a simple quad. My 'engine' can currently create a quad and I would like to expand it to be able to load textures.

 

My current attempt is as follows:

	Vertex vtx[] =
	{
		Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		Vertex(1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f),
		Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		Vertex(1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
	};

	createVertexBuffer(ARRAYSIZE(vtx), vtx, &vertexBuffer);
	createShaders();

	D3DX11CreateShaderResourceViewFromFile(d3d11Device, "textures\\t1.bmp", NULL, NULL, &texture, NULL);

	d3d11DevCon->PSSetShaderResources(0, 1, &texture);
	d3d11DevCon->Draw(4, 0);

where texture is of type ID3D11ShaderResourceView

 

The problem is I am reusing the same.fx file which is as follows:

cbuffer cbPerObject
{
	float4x4 WVP;
};

struct VS_OUTPUT
{
	float4 Pos : SV_POSITION;
	float4 Color : COLOR;
};

VS_OUTPUT VS(float4 inPos : POSITION, float4 inColor : COLOR)
{
    VS_OUTPUT output;
    output.Pos = mul(inPos, WVP);
    output.Color = inColor;

    return output;
}

float4 PS(VS_OUTPUT input) : SV_TARGET
{
    return input.Color;
}

I am stuck understanding how to edit the .fx file to allow for textures. And if I have correctly laid out code that will draw my textured quad.

 

Thank for your yout time

 

 


Two directX 11 questions.

16 January 2014 - 11:02 PM

Hi Guys,

 

When creating primitives for instance a quad, is it better to define the vertices a scaled size eg -0.2, 0.2 etc and draw them as is, or is it better to keep them all as 1.0f and apply a scaling transformation later on.

 

Secondly, once we are in world space my object appears with its (model space) center at 0,0 (staying with 2D). Is there a preferred way to organize objects so teh top left pixel of my object start at the top left of the screen (eg, top left pixel is 0,0)?

 

Thank you for your time


How to draw multiple objects to screen reusing draw commands?

12 January 2014 - 12:35 AM

Hi,

 

I am working on a very simple game in which the player controls a quad and there are some triangles and other shapes on the screen. Right now there is a separate class for quad and triangle as I want to be able to specify different parameters and 'behaviors' for them.

 

The problem is I am a bit confused in the cleanest way to draw multiple vertexBuffers to screen.

 

Right now my quad's initialization is as follows:

bool Quad::initialize(Graphics *g)
{
	graphics = g;

	Vertex vtx[] =
	{
		Vertex(-0.2f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		Vertex(0.2f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		Vertex(-0.2f, -0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		Vertex(0.2f, -0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
	};

	graphics->createVertexBuffer(ARRAYSIZE(vtx), vtx, &vertexBuffer);
	graphics->createShaders();

	return true;
}

and my triangles initialize is exactly the same just with different vertex points.

 

The problem is when it comes to the draw command in my code eg quad.draw() and then triangle.draw(), they get sent to:

bool Graphics::draw(ID3D11Buffer *vertexBuffer, const ShapeData &shapeData)
{
	float bgColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; //Clear our backbuffer
	d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);

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

	UINT stride = sizeof(Vertex);
	UINT offset = 0;

	d3d11DevCon->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);

	camPosition = XMVectorSet(0.0f, 0.0f, -3.0f, 0.0f);
	camTarget = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
	camUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

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

	//Set the Projection matrix
	camProjection = XMMatrixPerspectiveFovLH(0.4f*3.14f, (float)GAME_WIDTH / GAME_HEIGHT, 1.0f, 1000.0f);

	XMMATRIX WVP = XMMatrixIdentity();

	Translation = XMMatrixTranslation((float)shapeData.x, (float)shapeData.y, 0.0f);

	XMMATRIX World = Translation;

	WVP = World;

	cbPerObj.WVP = XMMatrixTranspose(WVP);
	d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
	d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);

	d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
	d3d11DevCon->Draw(4, 0);

	return true;
}

So as you can see the vertex buffer just gets overwritten with whatever the last .draw command contains. It works currently but I can only ever have one thing on screen at once (whatever is the last shape.draw() I have specified).

 

How would I go about reusing this draw command but allowing me to draw multiple vertexBuffers?

 

Thanks


Simple draw command not working.

25 December 2013 - 07:26 AM

Hi Guys,

 

I am working on a very primitive 'engine' that should just display a quad. The quad is its own class, and right now the program is working but just displaying the back buffers single color. I will post what I think is the relevant code and any help in identifying the problem would be greatly appreciated.

 

The quad 'object' is initialized in my engine::initialize routene

quad = new quad();
quad->initialize(graphics);

The quads initialization routine is:

bool quad::initialize(Graphics *g)
{
	graphics = g;                

	Vertex vtx[] =
		{
			Vertex(-0.2f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
			Vertex(0.2f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
			Vertex(-0.2f, -0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
			Vertex(0.2f, -0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		};

	graphics->createVertexBuffer(ARRAYSIZE(vtx), vtx, vertexBuffer);
	graphics->createShaders();

	return true;
}

The graphics createVertexBuffer member function is:

HRESULT Graphics::createVertexBuffer(unsigned int numVertices, Vertex *vertexData, ID3D11Buffer* vertexBuffer)
{

	HRESULT result = E_FAIL;

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

	VertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
	VertexBufferDesc.ByteWidth = sizeof(Vertex)*numVertices;
	VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	VertexBufferDesc.CPUAccessFlags = 0;
	VertexBufferDesc.MiscFlags = 0;

	D3D11_SUBRESOURCE_DATA VertexBufferData;

	ZeroMemory(&VertexBufferData, sizeof(VertexBufferData));
	VertexBufferData.pSysMem = vertexData;
	result = d3d11Device->CreateBuffer(&VertexBufferDesc, &VertexBufferData, &vertexBuffer);

	return result;
}

The graphics createShaders() function is:

HRESULT Graphics::createShaders()
{
	D3DX11CompileFromFile("Effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
	D3DX11CompileFromFile("Effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);

	d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
	d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

	d3d11DevCon->VSSetShader(VS, 0, 0);
	d3d11DevCon->PSSetShader(PS, 0, 0);

	return result;
}

The main loop contains the quad->draw command which is:

const void quad::draw()
{
	graphics->drawQuad(vertexBuffer); 
}

and finally drawQuad has a lot of stuff dumped in it which probably should be in other DirectX initialization areas but I have been trying everything to get it working.

bool Graphics::drawQuad(ID3D11Buffer *vertexBuffer)
{
	float bgColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; //Clear our backbuffer
	d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);

	d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	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;
	d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);

	d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	UINT stride = sizeof(Vertex);
	UINT offset = 0;

	d3d11DevCon->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);

	D3D11_VIEWPORT viewport;
	ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

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

	d3d11Device->CreateInputLayout(layout, numElements, VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &vertLayout);
	d3d11DevCon->IASetInputLayout(vertLayout);

	d3d11DevCon->RSSetViewports(1, &viewport);

	XMMATRIX WVP = XMMatrixIdentity();
	cbPerObj.WVP = XMMatrixTranspose(WVP);
	d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
	d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);

	d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
	d3d11DevCon->Draw(4, 0);

	return true;
}

and finally in the loop graphics->showBackbuffer(); is called which is simply

HRESULT Graphics::showBackbuffer()
{
	SwapChain->Present(0, 0);
	return result;
}

My base graphics.h class is set out like:

struct Vertex	//Overloaded Vertex Structure
{
	Vertex(){}
	Vertex(float x, float y, float z, float cr, float cg, float cb, float ca) : pos(x, y, z), color(cr, cg, cb, ca){}

	XMFLOAT3 pos;
	XMFLOAT4 color;
};

struct cbPerObject
{
	XMMATRIX  WVP;
};

const D3D11_INPUT_ELEMENT_DESC layout[] =
{
	{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

const UINT numElements = ARRAYSIZE(layout);

class Graphics
{
private:
	//DirectX pointers
	IDXGISwapChain* SwapChain;
	ID3D11Device* d3d11Device = NULL;
	ID3D11DeviceContext* d3d11DevCon;
	ID3D11RenderTargetView* renderTargetView;
	ID3D11Texture2D* depthStencilBuffer;
	ID3D11DepthStencilView* depthStencilView;
	ID3D11Buffer* vertexBuffer;
	ID3D11Buffer* cbPerObjectBuffer;
	ID3D11VertexShader* VS;
	ID3D11PixelShader* PS;
	ID3D10Blob* VS_Buffer;
	ID3D10Blob* PS_Buffer;
	ID3D11InputLayout* vertLayout;
	ID3D11Buffer* vertexBufferInternal;

I have set the WVP to an identity matrix just because I don't want to worry about a camera class just yet. So I am setting as much to a simple value that should work as I can.

There is much more but hopefully the error lies somewhere in what I have posted. Any help is greatly appreciated.

 

Thanks


PARTNERS