Jump to content
  • Advertisement
Sign in to follow this  
who_knows

[GLSL] Get the direction vector between my camera and a vertex

This topic is 3119 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, I'd like to get the direction vector between my camera and the actual point in the vertex shader. Imagine that the black square is my terrain, the green circle is my camera, the red line is my current view direction vector, and the purple line is the vector i'd like to have (to calculate the angle between both lines : the blue one). I have no idea how to do it.

Share this post


Link to post
Share on other sites
Advertisement
What's wrong with just subtracting camera position from the vertex position, or am i missing something here? Maybe if you post your shader i can be more helpful.

Share this post


Link to post
Share on other sites
My vertex shader is pretty empty, because i dont know what to do...

[Vertex Shader]
uniform vec3 cameraPosition, viewDirection;

void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}

I think my frangment shader is useless for my problem but here it is

[Fragment Shader]
uniform sampler2D maTexture,
maTextureDetail,
lignes,
stripes,
vertStripes,
contour;

uniform vec3 maCamera, viewDirection;
varying vec3 eyeVec;

void main(void)
{
const float testCouleur = 0.9, nbRepetHerbe = 40.0;

float direction_z = viewDirection.z,
direction_x = viewDirection.x,
niveauLignes = 1.0,
niveauLignesVert = 1.0;

//Création des textures
vec4 herbe = texture2D(maTexture, gl_TexCoord[0].st * nbRepetHerbe),
herbeDetail = texture2D(maTextureDetail, gl_TexCoord[0].st * nbRepetHerbe),
lignesBlanches = texture2D(lignes, gl_TexCoord[0].st),
contourMasque = texture2D(contour, gl_TexCoord[0].st),
bandesVert = texture2D(vertStripes, gl_TexCoord[0].st * 18.0),
bandes = texture2D(stripes, gl_TexCoord[0].st * 24.0);

vec4 rendu = (lignesBlanches + herbe) * herbeDetail;

if(contourMasque.r > testCouleur)
{
rendu = (lignesBlanches + herbe) * herbeDetail;
}
else
{
if(direction_z < 0.001 && direction_z > 0.0) { direction_z = 0.0; }
if(direction_z > -0.001 && direction_z < 0.0) { direction_z = 0.0; }

if(direction_x < 0.001 && direction_x > 0.0) { direction_x = 0.0; }
if(direction_x > -0.001 && direction_x < 0.0) { direction_x = 0.0; }

if(direction_z >= 0.0)
{
if(bandes.r >= testCouleur)
{
niveauLignes -= direction_z / 10.0;
rendu = (lignesBlanches + herbe) * herbeDetail / vec4(niveauLignes, niveauLignes, niveauLignes, 0.0);

if(direction_x >= 0.0)
{
if(bandesVert.b >= testCouleur)
{
niveauLignesVert -= direction_x / 20.0;
rendu /= vec4(niveauLignesVert, niveauLignesVert, niveauLignesVert, 0.0);
}
}
else if (direction_x < 0.0)
{
direction_x *= -1.0;

if(bandesVert.b < testCouleur)
{
niveauLignesVert -= direction_x / 20.0;
rendu /= vec4(niveauLignesVert, niveauLignesVert, niveauLignesVert, 0.0);
}
}
}
else
{
rendu = (lignesBlanches + herbe) * herbeDetail;

if(direction_x >= 0.0)
{
if(bandesVert.b >= testCouleur)
{
niveauLignesVert -= direction_x / 20.0;
rendu /= vec4(niveauLignesVert, niveauLignesVert, niveauLignesVert, 0.0);
}
}
else if (direction_x < 0.0)
{
direction_x *= -1.0;

if(bandesVert.b < testCouleur)
{
niveauLignesVert -= direction_x / 20.0;
rendu /= vec4(niveauLignesVert, niveauLignesVert, niveauLignesVert, 0.0);
}
}
}
}
else if (direction_z < 0.0)
{
direction_z *= -1.0;
if(bandes.r < testCouleur)
{
niveauLignes -= direction_z / 10.0;
rendu = (lignesBlanches + herbe) * herbeDetail / vec4(niveauLignes, niveauLignes, niveauLignes, 0.0);

if(direction_x >= 0.0)
{
if(bandesVert.b >= testCouleur)
{
niveauLignesVert -= direction_x / 20.0;
rendu /= vec4(niveauLignesVert, niveauLignesVert, niveauLignesVert, 0.0);
}
}
else if (direction_x < 0.0)
{
direction_x *= -1.0;

if(bandesVert.b < testCouleur)
{
niveauLignesVert -= direction_x / 20.0;
rendu /= vec4(niveauLignesVert, niveauLignesVert, niveauLignesVert, 0.0);
}
}
}
else
{
rendu = (lignesBlanches + herbe) * herbeDetail;

if(direction_x >= 0.0)
{
if(bandesVert.b >= testCouleur)
{
niveauLignesVert -= direction_x / 20.0;
rendu /= vec4(niveauLignesVert, niveauLignesVert, niveauLignesVert, 0.0);
}
}
else if (direction_x < 0.0)
{
direction_x *= -1.0;

if(bandesVert.b < testCouleur)
{
niveauLignesVert -= direction_x / 20.0;
rendu /= vec4(niveauLignesVert, niveauLignesVert, niveauLignesVert, 0.0);
}
}
}
}
}

gl_FragColor = rendu;
}

Share this post


Link to post
Share on other sites
So what exactly are you trying to achieve, what do you need this direction vector for?

Share this post


Link to post
Share on other sites
Well, I need this vector to calculate the angle between the two lines.
The final goal is complicated to explain, it's for the dark stripes of a soccer field.

Share this post


Link to post
Share on other sites
As Dave already mentionned, the standard way of doing this is:
vertex position (in world space) - camera position.

float4x4 world;
float4x4 view;
float4x4 projection;
float3 cameraPosition;
...
struct VS_IN
{
float4 position : POSITION;
...
};

struct PS_IN
{
float4 pos : SV_POSITION;
float3 Position : TEXCOORD0;
...
};

PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN)0;
float4 worldPos = mul(input.pos, world);
float4 viewPos = mul(worldPos, view);
output.pos = mul(viewPos, projection);
output.Position = worldPos.xyz;
...
return output;
}

float4 PS( PS_IN input ) : SV_Target
{
//This is actually the distance between the vertex (the surface point actually) and camera
float3 vertexToCameraVector = input.Position - cameraPosition;

//You'll probably want to normalize this to get the direction
//instead of the distance
vertexToCameraVector = normalize(vertexToCameraVector);
...
}




Edit: sorry, this is hlsl, not glsl, but the idea is the same :)

[Edited by - Hyunkel on May 4, 2010 1:35:20 PM]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!