• Advertisement
Sign in to follow this  

HLSL Camera Position ?

This topic is 3627 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 guys, I can't figure out how to get the camera position in my shader. Please take a look: // set shader constants D3DXMATRIX compMat; D3DXMatrixMultiply(&compMat,&worldMatrix,&viewMatrix); D3DXMatrixMultiply(&compMat,&compMat,&projectionMatrix); constantTable->SetMatrix(g_d3d_device,"WorldViewProj",&compMat); constantTable->SetMatrix(g_d3d_device,"World",&worldMatrix); // the shader float4x4 WorldViewProj : WORLDVIEWPROJ; float4x4 World : WORLD; struct VS_INPUT { float4 Pos : POSITION; } VS_OUTPUT main(VS_INPUT In) { // vertex position in world coords - is that right ? float4 vPos=mul(In.Pos,World); // isn't the camera at (0,0,0) in world ? // if not, what's the best way to get it ? } Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I do it like this:


Effect->SetMatrix("ViewProj", &((mRotX * mRotY * mRotZ) * mView * mProjection));
Effect->SetMatrix("World", &(mRotX * mRotY * mRotZ));
Effect->SetVector("Eye", &D3DXVECTOR4(pixie->GetEye().x, pixie->GetEye().y, pixie->GetEye().z, 0)); //The cameras position.

//GetEye() =
D3DXVECTOR3 GetEye()
{
return Eye;
}

//Which is the vector used for
D3DXMatrixLookAtLH(&matView, &Eye, &At, &Up);




My model is only rotating at the moment, so I only worry about that one translation.

HLSL:


float4x4 ViewProj;
float4x4 World;
float3 Eye;




I don't have semantics set up yet. I'm not sure it does it automagically. Maybe that is the problem you are having.

Edit:

Sorry, and the struct etc:


struct a2v //App->Vertex.
{
float4 Position : POSITION0;
float3 Normal : NORMAL;
float3 Tang : TANGENT;
float3 BiTang : BINORMAL;
float2 UV : TEXCOORD0;
};

Then in VP, the same as you:

float4 PosWorld = mul(IN.Position, World);

Share this post


Link to post
Share on other sites
Quote:
// isn't the camera at (0,0,0) in world ?

No. The camera is located at (0,0,0) once in view space, not world space.

Share this post


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

  • Advertisement