我尝试在练习中使用传统OpenGL(Freeglut+Glew)更改单个多边形形状(四边形)的颜色,但很明显,当我调用glColor3f时,场景中整个模型(包括文本)的颜色会被该函数设置的新颜色状态覆盖。
代码相当简单:
void drawScene(void) {
// Cleaning procedures
glClear(GL_COLOR_BUFFER_BIT);
// Setting the color of the models as green
glColor3f(0.0, 1.0, 0.0);
// Drawing a square that will rotate based on the mouse coord on the screen
glPushMatrix();
glRotated(theta, 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
// HERE: i'd like to change only the color of the square, but instead
// everything get modified to the new color.
glColor3f(colors.r, colors.g, colors.b);
glVertex3f(-2.0, 2.0, 0.0);
glVertex3f(-2.0, -2.0, 0.0);
glVertex3f(2.0, -2.0, 0.0);
glVertex3f(2.0, 2.0, 0.0);
glEnd();
glPopMatrix();
// Text formatting
auto coords = std::format("Mouse coordinate is x ({}) , y ({})", xm, ym);
auto angle = std::format("Rotation angle is {}", xm/10);
glRasterPos3f(-6.0, -6.0, 0.0);
writeBitmapString(GLUT_BITMAP_9_BY_15, coords.data());
glRasterPos3f(-6.0, -8.0, 0.0);
writeBitmapString(GLUT_BITMAP_9_BY_15, angle.data());
// Swapping buffer to the back and front
glutSwapBuffers();
}
关于如何在模型上实现单一着色,有什么建议吗?
1条答案
按热度按时间xoefb8l81#
glColor3f
设置全局状态。此状态将一直保持,直到再次更改。在每个对象之前显式设置状态。或者,您可以在绘制对象后将颜色属性重置为“白色”。