[C++ DX11] Somewhat basic HLSL problem

Started by
4 comments, last by n3Xus 12 years, 11 months ago
I'm new to DirectX 11 and trying to draw my index buffer, however I'm having problems with transformations. My vertex are getting weird transformations, and none of them are visible on screen, for example (-160, 2, -160) becomes (290, 276, -2100, -11).

Here's my vertex shader:
cbuffer cbObject : register(b0) {
matrix mWorld : packoffset(c0);
};

cbuffer cbFrame : register(b1) {
matrix mView : packoffset(c0);
matrix mProjection : packoffset(c4);
};

struct VS_INPUT {
float3 Position : POSITION;
float3 Normal : NORMAL;
float2 Tex0 : TEXCOORD0;
};

struct VS_OUTPUT {
float4 Position : SV_POSITION;
float3 Normal : NORMAL;
float2 Tex0 : TEXCOORD0;
};

VS_OUTPUT MainVS(VS_INPUT input) {
VS_OUTPUT output;
output.Position = mul(input.Position, mul(mWorld, mul(mView, mProjection)));
output.Normal = mul(input.Normal, mWorld);
output.Tex0 = input.Tex0;
return output;
}



Inside OnD3D11CreateDevice I setup camera parameters:
D3DXVECTOR3 vecEye( 0.0f, 50.0f, 50.0f );
D3DXVECTOR3 vecAt ( 0.0f, 0.0f, 0.0f );
g_Camera.SetViewParams( &vecEye, &vecAt );



Same with OnD3D11ResizedSwapChain:
float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height;
g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f );
g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
g_Camera.SetButtonMasks( MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_MIDDLE_BUTTON );



Before calling Draw I setup some parameters:

// shader
deviceContext->VSSetShader(pVertexShader, 0, 0);
deviceContext->PSSetShader(pPixelShader, 0, 0);

extern CModelViewerCamera g_Camera;
D3D11_MAPPED_SUBRESOURCE MappedResource;

// frame's view and proj matrices
deviceContext->Map(bufferFrame, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
cbFrame *pPerFrame = (cbFrame*)MappedResource.pData;
pPerFrame->mView = *g_Camera.GetViewMatrix();
pPerFrame->mProjection = *g_Camera.GetProjMatrix();
deviceContext->Unmap(bufferFrame, 0);
deviceContext->VSSetConstantBuffers(1, 1, &bufferFrame);

// primitive topology, vertex format and index buffer
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
deviceContext->IASetInputLayout(vertexFormat);
deviceContext->IASetIndexBuffer(bufferIndex, DXGI_FORMAT_R16_UINT, 0);

// world matrix
deviceContext->Map(bufferObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
cbObject *pPerObject = (cbObject*)MappedResource.pData;
pPerObject->mWorld = pHeightmap->mWorld;
deviceContext->Unmap(bufferObject, 0);
deviceContext->VSSetConstantBuffers(0, 1, &bufferObject);

// vertex buffer
UINT stride = sizeof(Vertex);
UINT offset = 0;
deviceContext->IASetVertexBuffers(0, 1, &bufferVertex, &stride, &offset);

// and finally draw
deviceContext->DrawIndexed(127 * 127 * 2 * 3, 0, 0);



I'm not sure what other information I have to provide. Let me know if I missed anything.

Thank you in advance.
Advertisement
Try changing

output.Position = mul(input.Position, mul(mWorld, mul(mView, mProjection)));

to

output.Position = mul(input.Position, mul(mWorld, mul(mView, mProjection)));

I myself never use the HLSL 'matrix' type, I prefer float4x4 or float3x3 since if you use these two the compiler will tell you that you have an error at this line.

You could also try using row_major or column_major to see if any of these help, like this:




cbuffer cbObject : register(b0) {
row_major matrix mWorld : packoffset(c0);
};

// or


cbuffer cbObject : register(b0) {
column_major matrix mWorld : packoffset(c0);
};



Try changing

output.Position = mul(input.Position, mul(mWorld, mul(mView, mProjection)));

to

output.Position = mul(input.Position, mul(mWorld, mul(mView, mProjection)));



I can't seem tell any difference between these lines. Are you sure you got them right, or I'm blind?

Try changing

output.Position = mul(input.Position, mul(mWorld, mul(mView, mProjection)));

to

output.Position = mul(input.Position, mul(mWorld, mul(mView, mProjection)));
[/quote]

Aw you know your two lines are identically? Thought I tell you..

Still, the bug should be located in that lines. Try change them to:


output.Position = mul(float4(input.Position, 1), mul(mWorld, mul(mView, mProjection)));


Or, try to change your VS_INPUT position-value to
float4 Position : POSITION;

I don't know exactly why but this kept messing my code up an awful lot until I figured you need your position to be a float4 with an 1.0f-value for the w-component.

float4(input.Position, 1) together with row_major fixed the issue, however my triangles were upside down, which is strange, as I copied index/vertex data straight from DirectX9 project, had to edit index data to fix that, hopefully it won't cause anymore issues later on.

Thank you both for your help.
Lol my bad laugh.gif

I wanted to suggest what The King2 said but I guess I screwed up copy/pasting somehow.

This topic is closed to new replies.

Advertisement