gpt4 book ai didi

C++/OpenGL/SFML 顶点着色

转载 作者:行者123 更新时间:2023-11-30 02:03:28 25 4
gpt4 key购买 nike

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

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

最佳答案

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

删除 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.

关于C++/OpenGL/SFML 顶点着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11857565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com