Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: My 'view' direction in glsl

  1. #1
    Intern Newbie
    Join Date
    Apr 2012
    Posts
    45

    My 'view' direction in glsl

    How can i calculate where does my entire screen look at, in 3d vector ? I mean, what is my camera's direction in glsl ? I will try to code an outline shader so i must dot product terrain normals with my camera's view

  2. #2
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    129
    If your camera is uses Euler angles, try this:

    Code :
    Vec3f RotationToVector(float xRotRads, float yRotRads)
    {
        Vec3f dir;
        float cosY = cosf(yRotRads);
     
     
        dir.x = sinf(xRotRads) * cosY;
        dir.y = -sinf(yRotRads);
        dir.z = cosf(xRotRads) * cosY;
     
     
        return dir;
    }

    It may be pointing in the opposite direction. If that happens, just flip it.

  3. #3
    Intern Newbie
    Join Date
    Apr 2012
    Posts
    45
    thanks but isn't there any way to calculate it in vertex shader instead of passing variables ?

  4. #4
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    129
    Yes, you can just transform the point (1.0, 0.0, 0.0) by the modelview matrix, but why would you want to do that?

    You would needlessly recalculate it for every vertex! That would be expensive!

  5. #5
    Intern Newbie
    Join Date
    Apr 2012
    Posts
    45
    umm then passing variables -i mean computing in cpu instead of gpu- is the best way for now ?

  6. #6
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    129
    Yes, such precomputed things should always be passed as uniforms.

  7. #7
    Intern Newbie
    Join Date
    Apr 2012
    Posts
    45
    i think i must find another way to do an outline effect.. i had found another way but i couldn't make stencil buffer work with VBOs..

  8. #8
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Location
    Poland
    Posts
    111
    You can extract view direction from modelview matrix
    (no need to calculate anything in shader, just take apropriate column), look here:

    http://www.songho.ca/opengl/gl_transform.html#modelview
    Last edited by kowal; 06-25-2012 at 03:33 AM. Reason: typos

  9. #9
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,129
    Why don't you simply maintain a forward vector with your camera implementation? You can use the basis vectors of the cam coordinate system for ton of things. Since the basis needs only be recomputed once per frame (in most cases at least) you have a marginal performance impact. Furthermore you can simply pass the camera position to a shader directly with glUniform4fv().

  10. #10
    Intern Newbie
    Join Date
    Apr 2012
    Posts
    45
    what i can't understand is gl_position doesn't work correctly, or as i wanted it to work. I am passing camera position to glsl and substract it from gl_position, normalize and dot product with terrain normals, i also tried backwards but everywhere looks black.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •