我试图制作一个几何着色器(使用着色器制造商)来使模型合法化。首先,我试图进行体素化,但结果并不好,我找不到它的问题所在。
在下面的代码中,想法是找到输入三角形的重心,然后使其成为我要创建的框的中心。
我知道这不是世界上最优雅的代码,但首先要先行,我需要让它工作......
这就是我得到的: 这就是我应该得到的:
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();
}
}