See-through Teapot with GLSL shaders (solved)

Started by
5 comments, last by Guy Meh 15 years, 3 months ago
I've decided to try out GLSL, and wrote a demo that shows a rotating teapot. The problem is that the teapot is "see-through." My vertex shader:

varying vec3 normal;
varying vec3 vecToLight;

void main()
{
    gl_Position = ftransform();

    normal = normalize(gl_NormalMatrix * gl_Normal);

    vecToLight = normalize(gl_LightSource[0].position.xyz);
}
My fragment shader:

varying vec3 normal;
varying vec3 vecToLight;

void main()
{
    float intensity;
    vec4 color;

    intensity = dot(vecToLight, normal);

    if (intensity > 0.95)
        color = vec4(0.50, 0.50, 1.0, 1.0);
    else if (intensity > 0.5)
        color = vec4(0.3750, 0.3750, 0.750, 1.0);
    else if (intensity > 0.25)
        color = vec4(0.250, 0.250, 0.50, 1.0);
    else
        color = vec4(0.1250, 0.1250, 0.250, 1.0);

    gl_FragColor = color;
}
My main program (at least, the parts I think are relevant):

#include "glut/glut.h"

...

void render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

    /* ANGLE incremented in each GLUT update */
    glRotatef((GLfloat)ANGLE, 0.0f, 1.0f, 0.0f);

    glutSolidTeapot(1.0);

    glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    float lightPos[] = {3.0f, 3.0f, 3.0f};

    glutInit(&argc, argv);

    glutInitWindowPosition(0, 0);
    glutInitWindowSize(SCR_WIDTH, SCR_HEIGHT);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow(SCR_TITLE);

    ...

    glutDisplayFunc(render);

    initShaders(); /* Load, compile, and link my shaders */

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glEnable(GL_DEPTH_TEST | GL_LIGHTING | GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

    glutMainLoop();

    return 0;
}
[Edited by - Guy Meh on December 26, 2008 8:17:19 PM]
Advertisement
Try reversing the draw-order, glCullFace(GL_BACK) or glCullFace(GL_FRONT);
"Game Maker For Life, probably never professional thou." =)
I tried adding glCullFace to my initialization code. The teapot is still see-through.
how are you depth testing? is culling enabled?
I think so.

Here is what my initialization code looks like right now:
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);    ...    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);    glEnable(GL_DEPTH_TEST | GL_LIGHTING | GL_LIGHT0 | GL_CULL_FACE);    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);    glCullFace(GL_BACK);
Quote:Original post by Guy Meh
glEnable(GL_DEPTH_TEST | GL_LIGHTING | GL_LIGHT0 | GL_CULL_FACE);


I don't believe you can 'or' together the args for glEnable()

try doing them one at a time

glEnable( GL_DEPTH_TEST );
glEnable( GL_LIGHTING );
etc.
That's what it was. Thank you Riraito.

My teapot finally looks right.

This topic is closed to new replies.

Advertisement