Advertisement Jump to content
Sign in to follow this  

HLSL Camera Position ?

This topic is 3960 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
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() =
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.


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.


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

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!