在 OpenGL 中为不同对象设置单独的材质属性

发布于 2024-12-09 12:17:26 字数 1173 浏览 1 评论 0原文

如何为 OpenGL 中绘制的不同对象提供单独的材质属性? 我执行了以下代码,显然只显示了后面的颜色:

//************** Object 1 **************
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColor4f(149.0/255.0, 78.0/255.0, 22.0/255.0, 1.0);
float mat_specular[] = {0.992157, 0.941176, 0.807843, 1.0};
float shininess = 10;

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);

glPushMatrix();
glTranslatef(0, 3.0, 0);
drawSphere(0.1, 0.1, 0.1);
glRotatef(10, 1, 0, 0);

glDisable(GL_COLOR_MATERIAL);


//************** Object 2 *****************
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(48.0/255.0, 48.0/255.0, 48.0/255.0, 1.0);
float mat_specular_2[] = {0.992157, 0.941176, 0.807843, 1.0};
float shininess_2 = 10;

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular_2);
glMaterialf(GL_FRONT, GL_SHININESS, shininess_2);

glPushMatrix();
glTranslatef(-0.6, 0.2, 1.6/2.0);
drawSphere(0.1, 0.1, 0.1);
glPopMatrix();

glDisable(GL_COLOR_MATERIAL);

渲染时,为对象 2 设置的颜色将用于整个场景。因此,尽管对象 1 已经拥有自己的颜色集,但它也会以对象 2 的颜色进行渲染。

如何让这两个对象具有单独的材质属性,以便它们可以显示为不同的颜色,而不是整个场景中仅显示一种颜色?

How can I have separate material properties for different objects drawn in OpenGL?
I did the following code, which apparently only shows the later colour:

//************** Object 1 **************
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColor4f(149.0/255.0, 78.0/255.0, 22.0/255.0, 1.0);
float mat_specular[] = {0.992157, 0.941176, 0.807843, 1.0};
float shininess = 10;

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);

glPushMatrix();
glTranslatef(0, 3.0, 0);
drawSphere(0.1, 0.1, 0.1);
glRotatef(10, 1, 0, 0);

glDisable(GL_COLOR_MATERIAL);


//************** Object 2 *****************
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(48.0/255.0, 48.0/255.0, 48.0/255.0, 1.0);
float mat_specular_2[] = {0.992157, 0.941176, 0.807843, 1.0};
float shininess_2 = 10;

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular_2);
glMaterialf(GL_FRONT, GL_SHININESS, shininess_2);

glPushMatrix();
glTranslatef(-0.6, 0.2, 1.6/2.0);
drawSphere(0.1, 0.1, 0.1);
glPopMatrix();

glDisable(GL_COLOR_MATERIAL);

When rendered, the colour set for the Object 2 is used for the entire scene. So the Object 1 is also rendered in Object 2's colour despite having its own colour set already.

How can I have the 2 objects to have separate material properties so that they can be displayed as different colours instead of just one colours in the whole scene?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑶笙 2024-12-16 12:17:26

您应该将:

glEnable(GL_COLOR_MATERIAL);

作为渲染函数中的第一件事,然后设置灯光参数:

glDisable(GL_COLOR_MATERIAL);
glPushMatrix();

然后设置材质的属性并调用对象。从现在开始,所有对象都将具有此属性,如果您想在另一个对象上使用另一种材质,只需键入:

glDisable(GL_COLOR_MATERIAL);

再次,在建模第二个对象之前,依此类推。如果您还有疑问,请提问。

You should put:

glEnable(GL_COLOR_MATERIAL);

As the first thing in your render function, then set the light parameters:

glDisable(GL_COLOR_MATERIAL);
glPushMatrix();

Then set the properties of the material and call the object. All the objects from now on will have this property, if you want to use another material on another object just type:

glDisable(GL_COLOR_MATERIAL);

again, before modeling the second object and so on. If you still have questions, just ask.

只为一人 2024-12-16 12:17:26

首先,您的示例代码看起来很合理,并且您的对象确实应该具有不同的材质。

但请记住,您仅更改第二个对象的漫反射材质颜色,因为您为两个对象设置了完全相同的镜面反射颜色和光泽度值。第二个对象的环境也与第一个相同,因为您只为漫反射通道启用颜色材质,因此环境与第一个对象相比没有变化,因为 OpenGL 是一个状态机

因此,对象之间唯一的材质差异是它们的漫反射颜色,该差异为 (101, 30, 26)。那么,这种差异是否会被完全相等的环境条件和镜面反射项所抵消,因此太小以至于您无法注意到?尝试完全不同的材料,看看是否真的没有区别。

First, your example code looks reasonable and your objects should indeed have different materials.

But keep in mind that you only change the diffuse material color for your second object, as you set exactly the same specular colors and shininess values for both objects. And the ambient of the second object is also the same like for the first, as you only enable color material for the diffuse channel, so the ambient is unchanged from the first object, as OpenGL is a state machine.

So the only material difference between the objects is their diffuse color and this difference is (101, 30, 26). So can it be, that this difference is just outweighted by the ambient and specular terms that are completely equal and is therefore just too small for you to notice? Try completely different materials and see if there is really no difference.

留蓝 2024-12-16 12:17:26

在绘制第一个对象后,您似乎错过了 glPopMatrix 。我不知道这会如何改变事情。

It seems you have missed a glPopMatrix after drawing the first object. I don't know how that would change things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文