开发者

GLSL - Front vs. Back faces of polygons

开发者 https://www.devze.com 2023-02-05 07:18 出处:网络
I made some simple shading in GLSL of a checkers board: f(P) = [ floor(Px)+floor(Py)+floor(Pz) ] mod 2

I made some simple shading in GLSL of a checkers board:

f(P) = [ floor(Px)+floor(Py)+floor(Pz) ] mod 2

It seems to work well except the fact that i see the interior of the objects but i want to see only the front face. Any ideas how to fix this? Thanks!

Teapot (glutSolidTeapot()):

GLSL - Front vs. Back faces of polygons

Cube (glutSolidCube开发者_如何学Go):

GLSL - Front vs. Back faces of polygons

The vertex shader file is:

varying float x,y,z;
void main(){
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
    x = gl_Position.x;
    y = gl_Position.y;
    z = gl_Position.z;
} 

And the fragment shader file is:

varying float x,y,z;
void main(){    
    float _x=x;
    float _y=y;
    float _z=z;

    _x=floor(_x);
    _y=floor(_y);
    _z=floor(_z);

    float sum = (_x+_y+_z);
    sum = mod(sum,2.0);
    gl_FragColor = vec4(sum,sum,sum,1.0);
}


The shaders are not the problem - the face culling is.

You should either disable the face culling (which is not recommended, since it's bad for performance reasons):

glDisable(GL_CULL_FACE);

or use glCullFace and glFrontFace to set the culling mode, i.e.:

glEnable(GL_CULL_FACE); // enables face culling    
glCullFace(GL_BACK); // tells OpenGL to cull back faces (the sane default setting)
glFrontFace(GL_CW); // tells OpenGL which faces are considered 'front' (use GL_CW or GL_CCW)

The argument to glFrontFace depends on application conventions, i.e. the matrix handedness.

0

精彩评论

暂无评论...
验证码 换一张
取 消