Sign in to follow this  
who_knows

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

Recommended Posts

who_knows    100
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
dave    2187
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
who_knows    100
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
who_knows    100
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
Hyunkel    401
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this