0
\$\begingroup\$

I was following this tutorial (http://www.lighthouse3d.com/tutorials/glsl-12-tutorial/point-light-per-pixel/) but when the camera is at a certain angle, if I had alpha test and blend enabled the cube i have in the scene wouldn't draw, otherwise they would be black(if alpha and blend was disabled) Any thoughts on what's going on?

here is some relevant code:

glClearColor(0.6, 0.6, 0.6, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80, (float)window.getSize().x / (float)window.getSize().y, 0.01f, 1000.0);
glMatrixMode(GL_MODELVIEW);

glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glEnable(GL_ALPHA_TEST);
//glAlphaFunc(GL_GREATER, 0.1f);

glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
    .....

void Lighting(Camera cam, sf::Vector3f Pos) {
GLfloat amb[] = {0.2, 0.2, 0.2, 1};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);

GLfloat diff[] = { 1, 1, 1, 1 };
GLfloat pos[] = { Pos.x, Pos.y, Pos.z, 1 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diff);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
}
....
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cam.up(delta, window);
Lighting(cam, Pos);
sf::Shader::bind(&shader);

cube.draw();
cube1.draw();
window.display();
sf::Shader::bind(NULL);

draw function for cubes:

glPushMatrix();
    glTranslatef(pos.x, pos.y, pos.z);
    glBegin(GL_QUADS);
        glColor3f(color.r / 255, color.g / 255, color.b / 255);

        glVertex3f(0, 0, 0);
        glVertex3f(width, 0, 0);
        glVertex3f(width, height, 0);
        glVertex3f(0, height, 0);

        glVertex3f(width, 0, 0);
        glVertex3f(width, 0, -length);
        glVertex3f(width, height, -length);
        glVertex3f(width, height, 0);

        glVertex3f(0, 0, -length);
        glVertex3f(0, 0, 0);
        glVertex3f(0, height, 0);
        glVertex3f(0, height, -length);

        glVertex3f(width, 0, -length);
        glVertex3f(0, 0, -length);
        glVertex3f(0, height, -length);
        glVertex3f(width, height, -length);

        glVertex3f(width, 0, 0);
        glVertex3f(0, 0, 0);
        glVertex3f(0, 0, -length);
        glVertex3f(width, 0, -length);

        glVertex3f(0, height, 0);
        glVertex3f(width, height, 0);
        glVertex3f(width, height, -length);
        glVertex3f(0, height, -length);
    glEnd();
glPopMatrix();
\$\endgroup\$
0
\$\begingroup\$

Without digging too deep, I noticed a couple things in your code:

First, you're using old style OpenGL functions (pre-3.0) which haven't been relevant for years. I suggest studying from a more up-to-date tutorial site, such as this one I've been using: http://ogldev.atspace.co.uk/

Second, in your call to glColor3f (which is outdated) you're dividing your color by integers. I can't see your declaration of 'color' but if it is also integral then all those values are being rounded down to 0 so your color is all black. Get in the habit of specifying things as floats when that's what they're expected to be, like this: 256.0f

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy