1

我最近一直在学习顶点着色。当我只使用 glut/OpenGL 时,程序运行良好。但是,我想将 SFML 用于应用程序,因此我尝试将在此站点上找到的代码更改为SFML 可以使用的代码。但是,整个四边形只有纯色有效;每个顶点着色只产生一个白色方块。下面是一个重现我的问题的小例子:

#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;
}
4

1 回答 1

4

这是因为您打开了平面着色,它禁用了顶点之间的颜色插值。

删除 glShadeModel 调用,或将其替换为glShadeModel(GL_SMOOTH)(默认)。

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.

于 2012-08-08T04:38:48.820 回答