Search code examples
c++openglcolorssfmlvertex

C++/OpenGL/SFML Vertex Coloring


I have recently been learning about vertex coloring. The program worked fine when I used just glut/OpenGL. However, I want to use SFML for application, so I tried changing the code found on this site to something SFML could use. However, only a solid color for the entire quad works; per vertex coloring produces only a white square. Below is a small, minimal example that reproduces my problem:

#include <windows.h>
#include <SFML/Graphics.hpp>
#include <gl/gl.h>
#include <gl/glu.h>

class Scene {
public:
    void resize( int w, int h ) {
        // OpenGL Reshape
        glViewport( 0, 0, w, h );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective( 120.0, (GLdouble)w/(GLdouble)h, 0.5, 500.0 );
        glMatrixMode( GL_MODELVIEW );
    }
};

int main() {

    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");

    ///Setup the scene, materials, lighting
    Scene scene;
    scene.resize(800,600);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
    glEnable(GL_COLOR_MATERIAL);
    glShadeModel(GL_FLAT);
    glEnable(GL_LIGHT0);
    float XL = .5, YL = .1, ZL = 1;
    GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
    GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    GLfloat lightpos[] = {XL, YL, ZL, 0.};
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

    ///Start loop
    while( window.isOpen() ) {
        sf::Event event;
        while( window.pollEvent( event ) ) {
            if( event.type == sf::Event::Closed )
                window.close();
        }

        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(50.0, 1.0, 1.0, 50);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(1, 0, 1, 0, 0, 0, 0, 1, 0);

        glColor3f(1, 0, 0);

        glBegin(GL_QUADS);
           // glColor3f(1,0,0); //red
            glVertex3f(-0.5, -0.5, 0.0);
           // glColor3f(0,1,0); //green
            glVertex3f(-0.5, 0.5, 0.0);
           // glColor3f(0,0,1); //blue
            glVertex3f(0.5, 0.5, 0.0);
           // glColor3f(1,1,1); //white
            glVertex3f(0.5, -0.5, 0.0);
        glEnd();

        ///Reset env settings for SFML
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

        window.display();
    }
    return 1;
}

Solution

  • It's because you have turned on flat shading, which disables color interpolation between vertices.

    Remove the glShadeModel call, or replace it with glShadeModel(GL_SMOOTH) (the default).

    GL primitives can have either flat or smooth shading. Smooth shading, the default, causes the computed colors of vertices to be interpolated as the primitive is rasterized, typically assigning different colors to each resulting pixel fragment. Flat shading selects the computed color of just one vertex and assigns it to all the pixel fragments generated by rasterizing a single primitive. In either case, the computed color of a vertex is the result of lighting if lighting is enabled, or it is the current color at the time the vertex was specified if lighting is disabled.