Results 1 to 6 of 6

Thread: Transform to eye-space with shader

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2009
    Posts
    17

    Transform to eye-space with shader

    Hi!

    Im reading the orange book at the moment and i have a
    small problem.

    To transform incoming vertices to eye-space they suggest
    the following method:

    Code :
     
    in vec4 vertex;
     
    uniform mat4 mvmatrix;
    vec4 epos;
    vec3 epos3;
     
    void main() {
    ...
    epos = mvmatrix * vertex;
    epos3 = (vec3(epos)) / epos.w
     
    }

    What does the last line do? My vertices are available in homogeneous
    coordinates, but the fourth component is always 1 so i could just
    write epos.xyz to get the same result or am i wrong?

    Later in the book there are several examples where the author use this
    method. But from time to time he divided the transformed vertex by w
    and i don't know why.

    Can someone help me out?

  2. #2
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,214

    Re: Transform to eye-space with shader

    If you are sure that w is always 1 in your data, than you can skip that line. Generally, it shouldn't always be 1. That's why Randi wrote so.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2009
    Posts
    17

    Re: Transform to eye-space with shader

    Hi!

    In my OpenGL client code i store only vertices with 3 coordinates
    in a vbo. Since i read out a vec4 in the vertex shader i guess opengl sets the homogeneous coordinates for me. Can i assume in this case that they are always equal to one?

    Thanks for your help so far.

  4. #4
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,214

    Re: Transform to eye-space with shader


    Whenever you need 4 coordinates, just call vec4 constructor

    Code :
    in  vec3 in_Position;
    //...
    void main()
    {
       gl_Position = matPMV * vec4(in_Position, 1.0);
    }
    AFAIK, OpenGL shouldn't provide implicit conversion.

  5. #5
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,677

    Re: Transform to eye-space with shader

    Generally, it shouldn't always be 1. That's why Randi wrote so.
    Actually, it is usually one. Eye space is generally not a homogeneous coordinate system. At least, I seriously doubt that the lighting equations take into account the possibility that the coordinate system will be a non-linear homogeneous coordinate system.

    The division is there for the sake of completeness. In any practical application, you won't need it.

  6. #6
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,214

    Re: Transform to eye-space with shader

    That was my error. English is not my native language, so the reason is obvious, and I even didn't read what I had written. Shame on me.
    W can be different from 1 for some spatial curves, when it is more convenient, and for vectors when it is 0. But, as you've said, usually it is 1.

Similar Threads

  1. How to transform into world space
    By apapaxionga in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 04-04-2011, 08:45 AM
  2. transform from camera space into world space?
    By apapaxionga in forum OpenGL: General
    Replies: 1
    Last Post: 04-04-2011, 08:39 AM
  3. Transform a vertex position into world space
    By halo in forum OpenGL: GLSL
    Replies: 7
    Last Post: 12-05-2006, 10:55 AM
  4. Transform to Tangent Space
    By einstein in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 02-29-2004, 09:54 AM
  5. Object-Space Transform
    By wis mak in forum OpenGL: Advanced Coding
    Replies: 16
    Last Post: 07-21-2002, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean