0
votes

I'm stuck with geometry shaders in OpenGL - c++ programming. I want to create simple cube by repeating 6 times drawing one rotated wall. Here is my vertex shader (everyting has #version 330 core in preamble):

uniform mat4 MVP;
uniform mat4 ROT;
layout(location=0) in vec3 vertPos;
void main(){
    vec4 pos=(MVP*ROT*vec4(vertPos,1));
    pos.x/=1.5;
    pos.y/=1.5;
    gl_Position=pos;
}

Now geometry shader:

layout (triangles) in;
layout (triangle_strip, max_vertices = 6) out;
out vec4 pos;
void main(void)
{
    pos=vec4(1,1,1,1);
    for (int i = 0; i < 3; i++)
    {
        vec4 offset=vec4(i/2.,0,0,0);
        gl_Position = gl_in[i].gl_Position+offset;
        EmitVertex();
    }
    EndPrimitive();
}

And now fragment shader:

uniform mat4 MVP;
in vec4 pos;
out vec3 color;
void main(){
    vec3 light=(MVP*vec4(0,0,0,1)).xyz;
    vec3 dd=pos.xyz-light;
    float cosTheta=length(dd)*length(dd);
    color=vec3(1,0,0);
}

Well, there is some junk, I wanted also put shading into my cube, but I've got a problem with sending coordinates. The main problem is - here I get my scaled square (by MVP matrix), I can even rotate it with basic interface (ROT matrix), but when I uncomment my "+offset" line I get some mess. What should I do to make clean 6-times repeating?

1
nothing personal but PLEASE INDENT YOUR CODEMightyPork
Lol, even my friends say so. I have special script to DELETE all indents in my code, i like it clean.Cheshire Cat
For real? This is everything but clean!MightyPork
YOU JUST DON'T UNDERSTAND MY WAYS :(Cheshire Cat
Really not sure if you're trolling now, but probably yes. Try also removing all newlines, it will be BEAUTIFUL.MightyPork

1 Answers

2
votes

It looks like the error is here, in your geometry shader.

gl_Position = gl_in[i].gl_Position+offset;

This adds an offset... but it adds the offset in clip space, which is probably not what you want. Add the offset in your vertex shader, or do the perspective projection in your geometry shader.

/* Passthrough vertex shader */
layout(location=0) in vec3 vertPos;
void main(){
    gl_Position = vec4(vertPos, 1.0);
}

/* Geometry shader */
...
    gl_Position = MVP * (ROT * (gl_in[i].gl_Position + offset));
    EmitVertex();
...

Also, I noticed something unusual in your vertex shader.

pos.x/=1.5;
pos.y/=1.5;

This is unusual because it is a linear transformation that directly follows a matrix multiplication. It would probably be more straightforward to multiply your MVP matrix by the following matrix:

1/1.5     0     0     0
    0 1/1.5     0     0
    0     0     1     0
    0     0     0     1

This would achieve the same result with less shader code.