Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

OpenGL Geometry Shader rendering triangle instead of square from a point

I am trying to render a square from a single point, here is my geometry shader code:

#version 330 core
layout (points) in;
layout(triangle_strip, max_vertices=4) out;

void main(){
    vec4 pos=gl_in[0].gl_Position;
    gl_Position=pos+vec4(1,1,0,0);
    EmitVertex();
    gl_Position=pos+vec4(1,-1,0,0);
    EmitVertex();
    gl_Position=pos+vec4(-1,-1,0,0);
    EmitVertex();
    gl_Position=pos+vec4(-1,1,0,0);
    EmitVertex();
    EndPrimitive();
}

Vertex shader looks like this:

#version 330 core
layout (location=0) in vec3 position;

uniform mat4 view;
uniform mat4 projection;
uniform mat4 model;

void main(){
    gl_Position=projection*view*model*vec4(position,1.0);
}

It only renders a triangle, I’m not sure why it only renders a triangle.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The vertices define a triangle strip. Every group of 3 adjacent vertices forms a triangle and the winding order is defined by the winding of the first triangle (successive triangles have reversed order). When face culling is enabled, you need to care about the winding order. The default winding order of front faces is counter clockwise. Therefore you have to swap the 1st and 2nd vertex:

void main(){
    vec4 pos=gl_in[0].gl_Position;
    gl_Position=pos+vec4( 1, -1, 0, 0);
    EmitVertex();
    gl_Position=pos+vec4( 1,  1, 0, 0);
    EmitVertex();
    gl_Position=pos+vec4(-1, -1, 0, 0);
    EmitVertex();
    gl_Position=pos+vec4(-1,  1, 0, 0);
    EmitVertex();
    EndPrimitive();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading