是否更改OpenGL场景中单个实体的颜色?

cwdobuhd  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(177)

我尝试在练习中使用传统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();

}

关于如何在模型上实现单一着色,有什么建议吗?

xoefb8l8

xoefb8l81#

glColor3f设置全局状态。此状态将一直保持,直到再次更改。在每个对象之前显式设置状态。或者,您可以在绘制对象后将颜色属性重置为“白色”。

glColor3f(colors.r, colors.g, colors.b);

glBegin(GL_QUADS);
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();

glColor3f(1.0f, 1.0f, 1.0f);

相关问题