Frage

Im trying to make a geometry shader (with shader maker) to legolize a model. First of all im trying to do a voxelization but the results are not good and i can't find what its wrong.

En the following code the idea is to find the baricenter of the input triangle and then make it the center of the box im going to create.

I know is not the most elegant code in the world but first goes first and i need to make it work...

this is what im getting: enter image description here this is what i should get: enter image description here

uniform float stepi;

void main( void ){

    float step = stepi/2.;

    vec3 bari = {(gl_PositionIn[0].x + gl_PositionIn[1].x +gl_PositionIn[2].x)/3,
(gl_PositionIn[0].y + gl_PositionIn[1].y +gl_PositionIn[2].y)/3,
(gl_PositionIn[0].z + gl_PositionIn[1].z +gl_PositionIn[2].z)/3};

    vec3 bar = bari;

    float dist = 0;

    for( int i = 0 ; i < gl_VerticesIn ; i++ )
    {
        gl_FrontColor  = gl_FrontColorIn[ i ];
        gl_TexCoord[0] = gl_TexCoordIn  [ i ][ 0 ];
//-x
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y-step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y+step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y-step,bar.z-step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y+step,bar.z-step,1) ;
        EmitVertex();
EndPrimitive();

//-y
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y-step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y-step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y-step,bar.z-step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y-step,bar.z-step,1) ;
        EmitVertex();
        EndPrimitive();

//-z
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y+step,bar.z-step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y-step,bar.z-step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y+step,bar.z-step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y-step,bar.z-step,1) ;
        EmitVertex();
EndPrimitive();
//+x
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y-step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y+step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y+step,bar.z-step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y+step,bar.z-step,1) ;
        EmitVertex();
EndPrimitive();

//+z
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y+step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y-step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y+step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y-step,bar.z+step,1) ;
        EmitVertex();
EndPrimitive();

//+y
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y+step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y+step,bar.z+step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x-step,bar.y+step,bar.z-step,1) ;
        EmitVertex();
        gl_Position    = gl_ModelViewProjectionMatrix * vec4(bar.x+step,bar.y+step,bar.z-step,1) ;
        EmitVertex();
        EndPrimitive();

    }
}
War es hilfreich?

Lösung 2

It now works just fine, the main problem was the calculation of the center of the cube (and the actual disposition of the vertex to form the cube.

the center nos is calculed like:

    vec3 bari = vec3((gl_PositionIn[0].xyz+gl_PositionIn[1].xyz+gl_PositionIn[2].xyz)/3);

vec3 centre = floor((bari/stepi)+(0.5,0.5,0.5));
centre = centre * stepi;

where bari is the baricenter of the incoming triangle and centre is the center of the cube. Is NOT the best solution, because in some shapes appear holes, however this is what i was suposed to get.

Thank you all, for your answers!

Andere Tipps

I think the problem is that you are emitting your vertices in the wrong order. The vertices of a primitive should be emitted in a clockwise order (from the viewpoint of outside the mesh). For example on your +z quad, you emit the vertices in this order (top left), (bottom left), (top right), (bottom right) but it should be (top left), (top right), (bottom right), (bottom left).

Also, you should keep the code that is inside of your for loop, but get rid of the loop itself. You should only emit one cube each time the geometry shader is run. Right now you are emitting the same cube multiple times.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top