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: this is what i should get:
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();
}
}
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!