• 14
• 15
• 9
• 10
• 9
• ### Similar Content

• By isu diss
I'm following rastertek tutorial 14 (http://rastertek.com/tertut14.html). The problem is, slope based texturing doesn't work in my application. There are plenty of slopes in my terrain. None of them get slope color.
float4 PSMAIN(DS_OUTPUT Input) : SV_Target { float4 grassColor; float4 slopeColor; float4 rockColor; float slope; float blendAmount; float4 textureColor; grassColor = txTerGrassy.Sample(SSTerrain, Input.TextureCoords); slopeColor = txTerMossRocky.Sample(SSTerrain, Input.TextureCoords); rockColor = txTerRocky.Sample(SSTerrain, Input.TextureCoords); // Calculate the slope of this point. slope = (1.0f - Input.LSNormal.y); if(slope < 0.2) { blendAmount = slope / 0.2f; textureColor = lerp(grassColor, slopeColor, blendAmount); } if((slope < 0.7) && (slope >= 0.2f)) { blendAmount = (slope - 0.2f) * (1.0f / (0.7f - 0.2f)); textureColor = lerp(slopeColor, rockColor, blendAmount); } if(slope >= 0.7) { textureColor = rockColor; } return float4(textureColor.rgb, 1); } Can anyone help me? Thanks.

• By cozzie
Hi all,
As a part of the debug drawing system in my engine,  I want to add support for rendering simple text on screen  (aka HUD/ HUD style). From what I've read there are a few options, in short:
1. Write your own font sprite renderer
2. Using Direct2D/Directwrite, combine with DX11 rendertarget/ backbuffer
3. Use an external library, like the directx toolkit etc.
I want to go for number 2, but articles/ documentation confused me a bit. Some say you need to create a DX10 device, to be able to do this, because it doesn't directly work with the DX11 device.  But other articles tell that this was 'patched' later on and should work now.
Can someone shed some light on this and ideally provide me an example or article on  how to set this up?
All input is appreciated.
• By stale
I've just started learning about tessellation from Frank Luna's DX11 book. I'm getting some very weird behavior when I try to render a tessellated quad patch if I also render a mesh in the same frame. The tessellated quad patch renders just fine if it's the only thing I'm rendering. This is pictured below:
'
However, when I attempt to render the same tessellated quad patch along with the other entities in the scene (which are simple triangle-lists), I get the following error:

I have no idea why this is happening, and google searches have given me no leads at all. I use the following code to render the tessellated quad patch:
for (unsigned int i = 0; i < scene->GetEntityList()->size(); i++) { Entity* entity = scene->GetEntityList()->at(i); if (entity->m_VisualComponent->m_visualType == VisualType::MESH) DrawMeshEntity(entity, cam, sun, point); else if (entity->m_VisualComponent->m_visualType == VisualType::BILLBOARD) DrawBillboardEntity(entity, cam, sun, point); else if (entity->m_VisualComponent->m_visualType == VisualType::TERRAIN) DrawTerrainEntity(entity, cam); } HR(m_swapChain->Present(0, 0)); Any help/advice would be much appreciated!

• Am trying a basebones tessellation shader and getting unexpected result when increasing the tessellation factor. Am rendering a group of quads and trying to apply tessellation to them.
OutsideTess = (1,1,1,1), InsideTess= (1,1)

OutsideTess = (1,1,1,1), InsideTess= (2,1)

I expected 4 triangles in the quad, not two. Any idea of whats wrong?
Structs:
struct PatchTess { float mEdgeTess[4] : SV_TessFactor; float mInsideTess[2] : SV_InsideTessFactor; }; struct VertexOut { float4 mWorldPosition : POSITION; float mTessFactor : TESS; }; struct DomainOut { float4 mWorldPosition : SV_POSITION; }; struct HullOut { float4 mWorldPosition : POSITION; }; Hull shader:
PatchTess PatchHS(InputPatch<VertexOut, 3> inputVertices) { PatchTess patch; patch.mEdgeTess[ 0 ] = 1; patch.mEdgeTess[ 1 ] = 1; patch.mEdgeTess[ 2 ] = 1; patch.mEdgeTess[ 3 ] = 1; patch.mInsideTess[ 0 ] = 2; patch.mInsideTess[ 1 ] = 1; return patch; } [domain("quad")] [partitioning("fractional_odd")] [outputtopology("triangle_ccw")] [outputcontrolpoints(4)] [patchconstantfunc("PatchHS")] [maxtessfactor( 64.0 )] HullOut hull_main(InputPatch<VertexOut, 3> verticeData, uint index : SV_OutputControlPointID) { HullOut ret; ret.mWorldPosition = verticeData[index].mWorldPosition; return ret; }
[domain("quad")] DomainOut domain_main(PatchTess patchTess, float2 uv : SV_DomainLocation, const OutputPatch<HullOut, 4> quad) { DomainOut ret; const float MipInterval = 20.0f; ret.mWorldPosition.xz = quad[ 0 ].mWorldPosition.xz * ( 1.0f - uv.x ) * ( 1.0f - uv.y ) + quad[ 1 ].mWorldPosition.xz * uv.x * ( 1.0f - uv.y ) + quad[ 2 ].mWorldPosition.xz * ( 1.0f - uv.x ) * uv.y + quad[ 3 ].mWorldPosition.xz * uv.x * uv.y ; ret.mWorldPosition.y = quad[ 0 ].mWorldPosition.y; ret.mWorldPosition.w = 1; ret.mWorldPosition = mul( gFrameViewProj, ret.mWorldPosition ); return ret; }
Any ideas what could be wrong with these shaders?
• By simco50
Hello,
I've stumbled upon Urho3D engine and found that it has a really nice and easy to read code structure.
I think the graphics abstraction looks really interesting and I like the idea of how it defers pipeline state changes until just before the draw call to resolve redundant state changes.
This is done by saving the state changes (blendEnabled/SRV changes/RTV changes) in member variables and just before the draw, apply the actual state changes using the graphics context.
It looks something like this (pseudo):
void PrepareDraw() { if(renderTargetsDirty) { pD3D11DeviceContext->OMSetRenderTarget(mCurrentRenderTargets); renderTargetsDirty = false } if(texturesDirty) { pD3D11DeviceContext->PSSetShaderResourceView(..., mCurrentSRVs); texturesDirty = false } .... //Some more state changes } This all looked like a great design at first but I've found that there is one big issue with this which I don't really understand how it is solved in their case and how I would tackle it.
I'll explain it by example, imagine I have two rendertargets: my backbuffer RT and an offscreen RT.
Say I want to render my backbuffer to the offscreen RT and then back to the backbuffer (Just for the sake of the example).
You would do something like this:
//Render to the offscreen RT pGraphics->SetRenderTarget(pOffscreenRT->GetRTV()); pGraphics->SetTexture(diffuseSlot, pDefaultRT->GetSRV()) pGraphics->DrawQuad() pGraphics->SetTexture(diffuseSlot, nullptr); //Remove the default RT from input //Render to the default (screen) RT pGraphics->SetRenderTarget(nullptr); //Default RT pGraphics->SetTexture(diffuseSlot, pOffscreenRT->GetSRV()) pGraphics->DrawQuad(); The problem here is that the second time the application loop comes around, the offscreen rendertarget is still bound as input ShaderResourceView when it gets set as a RenderTargetView because in Urho3D, the state of the RenderTargetView will always be changed before the ShaderResourceViews (see top code snippet) even when I set the SRV to nullptr before using it as a RTV like above causing errors because a resource can't be bound to both input and rendertarget.
What is usually the solution to this?

Thanks!

# DX11 Orthographic camera

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

## Recommended Posts

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.

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?

##### Share on other sites

#1: You could do it that way, but if you plan to do anything even moderately interesting with your camera, then you should consider option 2.

#2: It needs to be done in the shaders, as there is no fixed function pipeline anymore.  You can take a look at the old D3DX functions for inspiration in making the orthographic and view matrices though: D3DXMatrixOrthoLH and D3DXMatrixLookAtLH.

##### Share on other sites

I was fearing that might be the case.

I'll have to hit it head on and take up the challenge then.

##### 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 ;)

##### 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.

##### Share on other sites

After a lot of reading and googling I now have this in my render loop.

 Totally changed from what I posted before

I think I am close now.

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->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->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);

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 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(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

Edited by DarkRonin

##### 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.

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

##### 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).

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

##### Share on other sites

looks like you have your matrix multiplication reversed.

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

reverse the order.

##### 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

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...

...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

Edited by DarkRonin