1
\$\begingroup\$

I am wanting to be able to render objects with a geometry shader and allow them to rotate with the camera perspective...this is what I have so far, as a example

Vertex shader

#version 400 core

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main()
{
    vec4 worldPosition = transformationMatrix * vec4(0,0,0, 1.0);
    vec4 positionRelitiveToCam = viewMatrix * worldPosition;
    gl_Position = projectionMatrix * positionRelitiveToCam;
}

Geometry Shader

#version 400 core
layout(points) in;
layout(triangle_strip, max_vertices = 3) out;

void main() {
    gl_Position = gl_in[0].gl_Position + vec4(-1.0, 0.0, 0.0, 0.0);
    EmitVertex();

    gl_Position = gl_in[0].gl_Position + vec4(1.0, 0.0, 0.0, 0.0);
    EmitVertex();

    gl_Position = gl_in[0].gl_Position + vec4(0.0, 2.0, 0.0, 0.0);
    EmitVertex();

    EndPrimitive();
}

The triangle being rendered

As you can see, I am able to draw a triangle with POV working but as I rotate around it, the triangle does not rotate. Ideas?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You are applying transformation to the point and not to the vertices of the triangle.

Transformation should be applied to EACH vertex of the output triangle if you want the triangle to rotate.

Vertex shader

#version 400 core

void main()
{
    gl_Position = vec4(0,0,0, 1.0);
}

Geometry Shader

#version 400 core

layout(points) in;
layout(triangle_strip, max_vertices = 3) out;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main() {
    gl_Position = projectionMatrix * (viewMatrix * (transformationMatrix * (gl_in[0].gl_Position + vec4(-1.0, 0.0, 0.0, 0.0)))); 
    EmitVertex();

    gl_Position = projectionMatrix * (viewMatrix * (transformationMatrix * (gl_in[0].gl_Position + vec4(1.0, 0.0, 0.0, 0.0)))); 
    EmitVertex();

    gl_Position = projectionMatrix * (viewMatrix * (transformationMatrix * (gl_in[0].gl_Position + vec4(0.0, 2.0, 0.0, 0.0)))); 
    EmitVertex();

    EndPrimitive();
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .