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();
}
}