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

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 )]

#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

#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?

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

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.


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.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.