• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lonewolff

DX11
Orthographic camera

14 posts in this topic

Hi Guys,

 

I am currently moving from DX9 (fixed function) to DX11. All is going well so far but now I am creating the camera system. But, my shader knowlegde is next to zero, so it is a bit different.

 

Primarily, I'll be making 2D applications (at this point in time) so I'll need an orthographic setup.

 

I figure can go about this two ways.

 

1 - Cheat and use dynamic vertex buffers and move everything manually.

2 - Setup a camera system.

 

Is #1 a valid one or is it purely a hack?

 

With #2 do I have to do this all with shaders or is there a way to do this with function calls? Could anyone point me in the right direction on how to go about this?

 

Thanks in advance smile.png

1

Share this post


Link to post
Share on other sites

What Jason Z said.

Option 1 is how we did things before DirectX 7 introduced hardware TnL (transforms the vertices on the CPU every frame and send them through GPU, unviable past certain vertex count)

Option 2 (use shaders) is basically the same as Option 1 but the code runs on the GPU, hence no need to send the data every frame. It's already there.

Vertex Shaders are quite easy. Just think about it as a little program that gets executed for each vertex. One vertex in, one transformed vertex out (and each program execution can't see the contents of the other neighbouring vertices).

I've done both Options (option 1 a long, long time ago) and writting a vertex shader was just easier and quicker. Don't be scared of it just because you don't know it ;)

 

Thanks Matias,

 

Just trying to sift through all of the information I can google right now.

 

Do you know of any good links for this subject (preferably just ortho if possible).

 

Thanks again. smile.png

0

Share this post


Link to post
Share on other sites

After a lot of reading and googling I now have this in my render loop.
 
[edit] Totally changed from what I posted before
 
I think I am close now. smile.png
 
This is my render loop...
 

// Start Frame

float clearColor[4]={0.5f,0.5f,1.0f,1.0f};
d3dContext->ClearRenderTargetView(d3dBackBufferTarget,clearColor);

// tell DX11 to use this shader for the next renderable object
d3dContext->VSSetShader(pVS,0,0);
d3dContext->PSSetShader(pPS,0,0);
d3dContext->PSSetShaderResources(0,1,&colorMap);
d3dContext->PSSetSamplers(0,1,&colorMapSampler);

// Set up the view
XMMATRIX viewMatrix=XMMatrixIdentity();
XMMATRIX projMatrix=XMMatrixOrthographicOffCenterLH(0.0f,(float)width,0.0f,(float)height,0.0f,100.0f);	// 800 x 600
viewMatrix=XMMatrixTranspose(viewMatrix);		// What is this for?
projMatrix=XMMatrixTranspose(projMatrix);		// What is this for?
		
// position the object
XMMATRIX scaleMatrix=XMMatrixScaling(1.0f*256.0f,1.0f*256.0f,1.0f );	// use variables later
XMMATRIX translationMatrix=XMMatrixTranslation(0.0f,0.0f,0.0f);		// position at 0,0,0
XMMATRIX worldMat=scaleMatrix*translationMatrix;
		
d3dContext->UpdateSubresource(worldCB,0,0,&worldMat,0,0);
d3dContext->UpdateSubresource(viewCB,0,0,&viewMatrix,0,0);
d3dContext->UpdateSubresource(projCB,0,0,&projMatrix,0,0);

d3dContext->VSSetConstantBuffers(0,1,&worldCB);
d3dContext->VSSetConstantBuffers(1,1,&worldCB);
d3dContext->VSSetConstantBuffers(2,1,&worldCB);

// Render Geometry
UINT stride = sizeof(VERTEX);
UINT offset = 0;
d3dContext->IASetInputLayout(pLayout);
d3dContext->IASetVertexBuffers(0,1,&pVBuffer,&stride,&offset);
d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
		
d3dContext->Draw(4,0);

And this is my shader...
 

Texture2D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );

cbuffer cbChangesEveryFrame : register(b0)
{
	matrix worldMatrix;
};

cbuffer cbNeverChanges : register(b1)
{
	matrix viewMatrix;
};

cbuffer cbChangeOnResize : register(b2)
{
	matrix projMatrix;
}

struct VS_Input
{
	float4 pos  : POSITION;
	float2 tex0 : TEXCOORD0;
};

struct PS_Input
{
	float4 pos  : SV_POSITION;
	float2 tex0 : TEXCOORD0;
};

PS_Input VShader( VS_Input vertex )
{
	PS_Input vsOut = ( PS_Input )0;

	vsOut.pos=mul(vertex.pos,worldMatrix);
	vsOut.pos=mul(vsOut.pos,viewMatrix);
	vsOut.pos=mul(vsOut.pos,projMatrix);

	// vsOut.pos = vertex.pos;
	vsOut.tex0 = vertex.tex0;

	return vsOut;
}

float4 PShader( PS_Input frag ) : SV_TARGET
{
    return colorMap_.Sample( colorSampler_, frag.tex0 );
}

The problem that I have not is that when I run the code I get an exception and the debug window reports this
 

ID3D11DeviceContext::UpdateSubresource: First parameter is corrupt or NULL [ MISCELLANEOUS CORRUPTION #13: CORRUPTED_PARAMETER1]


Which relates to this code...
 

d3dContext->UpdateSubresource(worldCB,0,0,&worldMat,0,0);
d3dContext->UpdateSubresource(viewCB,0,0,&viewMatrix,0,0);
d3dContext->UpdateSubresource(projCB,0,0,&projMatrix,0,0);

Am I on the right track with how I am going about this? And any help as to what this error is would be awesome smile.png

Edited by DarkRonin
0

Share this post


Link to post
Share on other sites
A quick update.
 
I didn't realise I had to create the buffers so I added this before the render loop.
 
	ID3D11Buffer* viewCB=0;
	ID3D11Buffer* projCB=0;
	ID3D11Buffer* worldCB=0;

	D3D11_BUFFER_DESC constDesc;
	ZeroMemory(&constDesc,sizeof(constDesc));
	constDesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
	constDesc.ByteWidth=sizeof(XMMATRIX);
	constDesc.Usage=D3D11_USAGE_DEFAULT;

	if(FAILED(d3dDevice->CreateBuffer(&constDesc,0,&viewCB)))
		return 1;

	if(FAILED(d3dDevice->CreateBuffer(&constDesc,0,&projCB)))
		return 2;

	if(FAILED(d3dDevice->CreateBuffer(&constDesc,0,&worldCB)))
		return 3;
Things are better now, but the render results are not as expected. Hard to describe at the moment. smile.png

So, I'll play with the code a bit more to get my head around what is going on.
0

Share this post


Link to post
Share on other sites
This is what I get if I attempt to translate the sprite to 1,0 (supposedly in pixel co-ordinates). I also had to change the scale back to 1 (instead of 256 - the sprite width & height).
 
XMMATRIX scaleMatrix=XMMatrixScaling(1.0f,1.0f,1.0f );  // scale 256 was giving a black screen as something wrong here
XMMATRIX translationMatrix=XMMatrixTranslation(1.0f,0.0f,0.0f); // position at 1,0,0 
I am a lot closer than I was this morning. So, I am pretty happy that something is now happening. But, I am unsure whether the problem is in the shader or the code (both in a previous post).
 
Any adjustment to this line...
 
XMMATRIX projMatrix=XMMatrixOrthographicOffCenterLH(0.0f,(float)width,0.0f,(float)height,0.0f,100.0f);
 
...doesn't seem to make any effect at all.

This is what I am seeing when translation is 1,0,0
 
problem_dx11.png
0

Share this post


Link to post
Share on other sites

looks like you have your matrix multiplication reversed.

 

A * B * C != C * B * A

 

reverse the order.

0

Share this post


Link to post
Share on other sites

looks like you have your matrix multiplication reversed.
 
A * B * C != C * B * A
 
reverse the order.


In the shader do you mean?

I just tried reversing it and it gave the same result sad.png
 
[edit]
One thing I did notice in my code I had...
 

d3dContext->VSSetConstantBuffers(0,1,&worldCB);
d3dContext->VSSetConstantBuffers(1,1,&worldCB);
d3dContext->VSSetConstantBuffers(2,1,&worldCB);

So, I have changed that to...
 

d3dContext->VSSetConstantBuffers(0,1,&worldCB);
d3dContext->VSSetConstantBuffers(1,1,&viewCB);
d3dContext->VSSetConstantBuffers(2,1,&projCB);

But now I get a blank screen.

Is the way I am setting the constant buffers correct?

[edit 2]
Actually the scaling had to be re-adjusted as a result (so I added the 256 multiplication back in where I took out before).

So, we are even closer.

 

This is the current result...

 

problem_dx11_2.png

 

...which is closer to what I'd expect to see. But, it looks like the 0 on the y-axis is on the bottom of the screen instead of the top (probably something with my ortho setup) and I am getting an unwanted skew.

 

But, still on the right track I think.

 

Thanks for the help so far too guys smile.png
 

Edited by DarkRonin
0

Share this post


Link to post
Share on other sites

I would recommend that you work with one matrix at a time until you get each of them proven out.  Start with only your projection matrix, and make the other two identity matrices.  You should be able to draw your sprite within the bounds of your orthographic camera volume, and you should see it appear even if the view and world matrices are identity.

 

If you can get that working, I think the rest will be manageable...

 

Also, you have a comment up above about why you would transpose a matrix.  The reason is that the row / column order by default is not the same for the GPU and for the CPU (at least for their runtimes...) so you either have to transpose the matrices, or you can compile your shaders with an additional flag that flips the row / column order.

 

EDIT: Do you also have the debug device enabled?  When you create your D3D11 device, use the flag D3D11_CREATE_DEVICE_DEBUG.  You will get warnings if you try to use the API incorrectly, which can be really helpful too.

Edited by Jason Z
0

Share this post


Link to post
Share on other sites

Thanks again,

 

Yeah, I have debug device enabled and surprisingly no errors / warnings at all (I have seen a few of those today - LOL)

 

I just made the view and world matrices 'identity' and I get a 1 pixel dot at bottom left of the screen (which is roughly what i'd expect due to the removal of scaling, except for bottom being zero and top being 640 - that seems to be upside down).

 

Not sure where to go from here though.

 

Here is my current code...

 

// Set up the view
XMMATRIX viewMatrix=XMMatrixIdentity();
XMMATRIX projMatrix=XMMatrixOrthographicOffCenterLH(0.0f,(float)width,0.0f,(float)height,0.0f,100.0f); // 800 x 600
viewMatrix=XMMatrixTranspose(viewMatrix);
projMatrix=XMMatrixTranspose(projMatrix);
 
// position the object
//XMMATRIX scaleMatrix=XMMatrixScaling(1.0f*256.0f,1.0f*256.0f,0.0f);
//XMMATRIX translationMatrix=XMMatrixTranslation(0.0f,0.0f,0.0f);
//XMMATRIX worldMat=scaleMatrix*translationMatrix
 
XMATRIX worldMat=XMMatrixIdentity(); 
 
d3dContext->UpdateSubresource(worldCB,0,0,&worldMat,0,0);
d3dContext->UpdateSubresource(viewCB,0,0,&viewMatrix,0,0);
d3dContext->UpdateSubresource(projCB,0,0,&projMatrix,0,0);
 
d3dContext->VSSetConstantBuffers(0,1,&worldCB);
d3dContext->VSSetConstantBuffers(1,1,&viewCB);
d3dContext->VSSetConstantBuffers(2,1,&projCB);
Edited by DarkRonin
0

Share this post


Link to post
Share on other sites
Ok, I made some decent progress. The code now works perfectly, except the fact that the y axis starts from the bottom of the screen and upwards is positive.
 
// Set up the view
XMMATRIX viewMatrix=XMMatrixIdentity();
XMMATRIX projMatrix=XMMatrixOrthographicOffCenterLH(0.0f,(float)width,0.0f,(float)height,0.0f,100.0f);		// 800 x 450
viewMatrix=XMMatrixTranspose(viewMatrix);
projMatrix=XMMatrixTranspose(projMatrix);

// position the object
XMMATRIX scaleMatrix=XMMatrixScaling(1.0f*128.0f,1.0f*128.0f,0.0f);  // This is correct for 256px sprite as verts are 1 to -1 (fix later)
XMMATRIX rotationMatrix=XMMatrixRotationZ(0.0f);
XMMATRIX translationMatrix=XMMatrixTranslation(0.0f,0.0f,0.0f);
XMMATRIX worldMat=scaleMatrix*rotationMatrix*translationMatrix;
worldMat=XMMatrixTranspose(worldMat);

d3dContext->UpdateSubresource(worldCB,0,0,&worldMat,0,0);
d3dContext->UpdateSubresource(viewCB,0,0,&viewMatrix,0,0);
d3dContext->UpdateSubresource(projCB,0,0,&projMatrix,0,0);

d3dContext->VSSetConstantBuffers(0,1,&worldCB);
d3dContext->VSSetConstantBuffers(1,1,&viewCB);
d3dContext->VSSetConstantBuffers(2,1,&projCB);
So, overall I am pretty happy as I had no clue on shaders and DX11 just 24 hours ago. If I can nail this last issue (the y axis upside down) I'll be extremely happy.
1

Share this post


Link to post
Share on other sites

If you want to invert your y axis, just scale by a negative number along that axis.  Just remember that you will be flipping everything along there, so you will need to also apply a translation accordingly to put the object where you want it to be.

0

Share this post


Link to post
Share on other sites
Thanks Jason.

I also got around it using this method as well
 
XMMATRIX translationMatrix=XMMatrixTranslation(128.0f,(float)height-128.0f,0.0f);
(float)height being the height of the window. Just seemed a little hacky though as all of the documentation I can find says that 0,0 should be top-left of the screen.

At least I have a working solution though. smile.png

But, on the bright side. I came here yesterday with a blank window and now I have a working camera system, so I am glad I persisted with the shader method rather than persisting with dynamic vertex buffers. In the end, I know it was a far better method to go this way smile.png

Thanks for all of the help and guidance along the way too.

No doubt, I'll be posting in a weeks time asking how to do pixel perfect collisions (I am shuddering allready - LOL). Edited by DarkRonin
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By bowerbirdcn
      hi, guys, how to understand the math used in CDXUTDirectionWidget ::UpdateLightDir 
      the  following code snippet is taken from MS DXTU source code
       
        D3DXMATRIX mInvView;
          D3DXMatrixInverse( &mInvView, NULL, &m_mView );
          mInvView._41 = mInvView._42 = mInvView._43 = 0;
          D3DXMATRIX mLastRotInv;
          D3DXMatrixInverse( &mLastRotInv, NULL, &m_mRotSnapshot );
          D3DXMATRIX mRot = *m_ArcBall.GetRotationMatrix();
          m_mRotSnapshot = mRot;
          // Accumulate the delta of the arcball's rotation in view space.
          // Note that per-frame delta rotations could be problematic over long periods of time.
          m_mRot *= m_mView * mLastRotInv * mRot * mInvView;
          // Since we're accumulating delta rotations, we need to orthonormalize 
          // the matrix to prevent eventual matrix skew
          D3DXVECTOR3* pXBasis = ( D3DXVECTOR3* )&m_mRot._11;
          D3DXVECTOR3* pYBasis = ( D3DXVECTOR3* )&m_mRot._21;
          D3DXVECTOR3* pZBasis = ( D3DXVECTOR3* )&m_mRot._31;
          D3DXVec3Normalize( pXBasis, pXBasis );
          D3DXVec3Cross( pYBasis, pZBasis, pXBasis );
          D3DXVec3Normalize( pYBasis, pYBasis );
          D3DXVec3Cross( pZBasis, pXBasis, pYBasis );
       
       
      https://github.com/Microsoft/DXUT/blob/master/Optional/DXUTcamera.cpp
    • By YixunLiu
      Hi,
      I have a surface mesh and I want to use a cone to cut a hole on the surface mesh.
      Anybody know a fast method to calculate the intersected boundary of these two geometries?
       
      Thanks.
       
      YL
       
    • By hiya83
      Hi, I tried searching for this but either I failed or couldn't find anything. I know there's D11/D12 interop and there are extensions for GL/D11 (though not very efficient). I was wondering if there's any Vulkan/D11 or Vulkan/D12 interop?
      Thanks!
    • By lonewolff
      Hi Guys,
      I am just wondering if it is possible to acquire the address of the backbuffer if an API (based on DX11) only exposes the 'device' and 'context' pointers?
      Any advice would be greatly appreciated
    • By MarcusAseth
      bool InitDirect3D::Init() { if (!D3DApp::Init()) { return false; } //Additional Initialization //Disable Alt+Enter Fullscreen Toggle shortkey IDXGIFactory* factory; CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&factory)); factory->MakeWindowAssociation(mhWindow, DXGI_MWA_NO_WINDOW_CHANGES); factory->Release(); return true; }  
      As stated on the title and displayed on the code above, regardless of it Alt+Enter still takes effect...
      I recall something from the book during the swapChain creation, where in order to create it one has to use the same factory used to create the ID3D11Device, therefore I tested and indeed using that same factory indeed it work.
      How is that one particular factory related to my window and how come the MakeWindowAssociation won't take effect with a newly created factory?
      Also what's even the point of being able to create this Factories if they won't work,?(except from that one associated with the ID3D11Device) 
  • Popular Now