Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Relative Positioning

  1. #1
    Newbie Newbie
    Join Date
    May 2013
    Posts
    3

    Relative Positioning

    I am trying to draw a small box (a Handle) around each of the vertices for shape (in my case another box) (e.g. PowerPoint's resize handles). I am using a vertex shader to position and scale the main shape and that is working fine.

    That shader is
    Code :
    uniform mat4 u_transform;
    void main() {
      gl_Position = gl_ProjectionMatrix * u_transform * gl_Vertex;
    }

    I am struggling with try to layout the small box / handle at each of the vertices using a shader. I want the handle to be a fixed size in screen space and centered around each vertex of the main shape.

    I naively tried.

    A Triangle Fan with coordinates of [(-5, -5), (-5, 5), (5, 5), (5, -5 )]

    Code :
    #version 120
    uniform mat4 u_transform;
    uniform vec2 u_position; // The vertex from the original shape (untransformed)
     
    void main() {
     
    // Center in SCREEN
    	vec4 center =  gl_ProjectionMatrix * u_transform * vec4(u_position, 0, 1);
     
    // Vertex in SCREEN
    	vec4 offsetFromCenter = gl_ProjectionMatrix * gl_Vertex;
     
    //OFFSET VERTEX
      	gl_Position =  center + offsetFromCenter;
    }

    And I also tried

    Code :
    #version 120
    uniform mat4 u_transform;
    uniform vec2 u_position;
     
    void main() {
     
    // Center in SCREEN
    	vec4 center  = gl_ProjectionMatrix * u_transform * vec4(u_position, 0, 1);
    	vec3 centerN = center.xyz/center.w;
     
    // Vertex in SCREEN
    	vec4 offsetFromCenter = gl_ProjectionMatrix * gl_Vertex;
    	vec3 offsetFromCenterN = offsetFromCenter.xyz/offsetFromCenter.w;
     
    //OFFSET VERTEX
    	vec3 sum = centerN + offsetFromCenterN;
      	gl_Position =  vec4(sum,offsetFromCenter.w); 
    }

    What am I missing?

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Code :
    vec4 offsetFromCenter = gl_ProjectionMatrix * gl_Vertex;

    This looks odd since there is no translation from world coordinates to camera coordinates (view-model-matrix).
    Also are you working perspective view? If so, your handles will not stay a fixed size in screen pixels

  3. #3
    Newbie Newbie
    Join Date
    May 2013
    Posts
    3
    I am using ortho2d . I was attempting to use the main shape to get the center of my handle in world coordinates. Then I was trying to use the gl_Vertex that was passed into the handle vertex shader to compute a relative offset (in screen space). It looks like the center is correct in world coordinates. But I am not sure how to make the next step into the screen space to compute the offset.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Code :
    vec4 vertex_location  = gl_ProjectionMatrix * u_transform * gl_Vertex;
    vec4 bottom_left = gl_ProjectionMatrix  * (vec4(-u_Offsets,-u_Offset,0.0,0.0)) + vertex_location; // offset is in the range 0 < offset <= 1

    This will give you the position of the a point a fixed screen distance to the left and below the vertex. You need 4 locations to render the handle.
    This is easy to do with a geometry shader.

Posting Permissions

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