How to draw a square from a given point using a geometric shader

I want to draw a square from point data using a geometric shader.

In the vertex shader I emit one dot.

 #version 330 core void main() { gl_Position = vec4(0, 0, 0, 1.0); } 

In the geometric shader, I want to create a triangular strip that forms a square.

At the moment, the size does not matter, so the model should have a size of 1 (from (-0.5, -0.5) from the initial position of the point to (+0.5, +0.5).

I need help calculating the position of the emitted vertices, as seen in the code:

 #version 330 core layout(points) in; layout(triangle_strip, max_vertices=4) out; out vec2 tex_coord; uniform mat4x4 model; uniform mat4x4 view; uniform mat4x4 projection; void main() { int i; for(i = 0; i < gl_in.length(); ++i) { mat4x4 trans; trans = //??? gl_Position = projection * view * model * trans * gl_in[i].gl_Position; tex_coord = vec2(0, 0); EmitVertex(); trans = //??? gl_Position = projection * view * model * trans * gl_in[i].gl_Position; tex_coord = vec2(1, 0); EmitVertex(); trans = //??? gl_Position = projection * view * model * trans * gl_in[i].gl_Position; tex_coord = vec2(1, 1); EmitVertex(); trans = //??? gl_Position = projection * view * model * trans * gl_in[i].gl_Position; tex_coord = vec2(0, 1)); EmitVertex(); } EndPrimitive(); } 

I thought of using trans to translate the starting point to the desired coordinates. How do I understand that?

Edit for clarity.

I want to generate from one point what else will be given by the vertex buffer; one plane:

 float vertex[] { -0.5, 0.5, 0.0, 0.5, 0.5, 0.0, 0.5, -0.5, 0.0, -0.5, -0.5, 0.0 } 

Instead, I give only a point in the middle of these points and want to generate real points by subtracting and adding differences to the center (0.5 and -0.5, etc.). I only need to know how to apply this conversion in code (where ??? ).

+4
source share
1 answer

Judging by your updated question, I think this pseudo code should point you in the right direction. It seems to me that all you want to do is to shift the x and y coordinates of your point by a constant value, so an array is the perfect way to do this.

 const vec3 offset [4] = vec3 [] ( vec3 (-0.5, 0.5, 0.0), vec3 ( 0.5, 0.5, 0.0), vec3 ( 0.5, -0.5, 0.0), vec3 (-0.5, -0.5, 0.0) ); const vec2 tc [4] = vec2 [] ( vec2 (0.0, 0.0), vec2 (1.0, 0.0), vec2 (1.0, 1.0), vec2 (0.0, 1.0) ); void main (void) { int i; for (i = 0; i < gl_in.length (); ++i) { gl_Position = projection * view * model * (gl_in [i].gl_Position + offset [0]); tex_coord = tc [0]; EmitVertex (); gl_Position = projection * view * model * (gl_in [i].gl_Position + offset [1]); tex_coord = tc [1]; EmitVertex (); gl_Position = projection * view * model * (gl_in [i].gl_Position + offset [2]); tex_coord = tc [2]; EmitVertex (); gl_Position = projection * view * model * (gl_in [i].gl_Position + offset [3]); tex_coord = tc [3]; EmitVertex (); } EndPrimitive (); } 
+3
source

All Articles