glEnable(GL_COLOR_MATERIAL) 应用于所有绘制的对象 [英] glEnable(GL_COLOR_MATERIAL) applying to all drawn objects

查看:29
本文介绍了glEnable(GL_COLOR_MATERIAL) 应用于所有绘制的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大学项目开发​​一个版本的 Frogger 游戏,我们进入了灯光和材料部分.我遇到的问题是:当我为青蛙设置材质时,它会将它们应用于绘制的所有其他对象.我如何使它只适用于青蛙?如果我在绘制后或在设置材料属性后禁用 GL_COLOR_MATERIAL,一切都会变成红色.

void Frog::draw(void){Vector3 _pos = Entity::getPosition();glPushMatrix();glEnable(GL_COLOR_MATERIAL);GLfloat greenEmissiveMaterial[] = {1.0, 0.0, 0.0};glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);GLfloat mat_ambient[] = {0.7, 0.0, 0.0, 1.0};GLfloat mat_diffuse[] = { 0.7, 0.0, 0.0, 1.0 };GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 0.0 };GLfloat mat_shininess[] = {1.8 };glColor3f(1.0, 0.0, 0.0);glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);glDisable(GL_COLOR_MATERIAL);glScalef(0.1, 0.1, 0.1);glTranslatef( _pos.getX()*U , _pos.getY()*U , 1 );//企业glPushMatrix();glColor3f(0.0, 1.0, 0.0);glutSolidSphere(0.5,50,50);glPopMatrix();glPushMatrix();glColor3f(0.0, 1.0, 0.0);glTranslatef(0.0, 0.5, 0.0);glutSolidSphere(0.3,50,50);glPopMatrix();glPopMatrix();}

解决方案

启用状态 GL_COLOR_MATERIAL 会使材质颜色跟踪您在调用 glColorMaterial 时指定的面的当前颜色(...).换句话说,当 GL_COLOR_MATERIAL 已启用且 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) 已设置时,GL_AMBIENTGL_DIFFUSE 由正面的当前颜色独家决定.

因此,以下调用:

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

在您的软件中与此等效:

glColor3f (1.0f, 0.0f, 0.0f);//设置当前"颜色GLfloat 红色 [3] = { 1.0f, 0.0f, 0.0f };glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, 红色);

您可以看到您已将GL_AMBIENTGL_DIFFUSE 的值更改为红色.并且您还尝试在原始代码中启用该状态时手动更改环境/漫反射材质颜色,但这不会做任何事情.

此处更详细地描述了上述行为:

<块引用>

姓名

<块引用>

glColorMaterial — 原因用于跟踪当前颜色的材质颜色

C 规范

<块引用>

void glColorMaterial( GLenum face,GLenum 模式);

说明

<块引用>

glColorMaterial 指定跟踪当前颜色的材质参数.启用GL_COLOR_MATERIAL时,材质参数或mode指定的参数,面指定的一个或多个材质,始终跟踪当前颜色.

要启用和禁用 GL_COLOR_MATERIAL,请使用参数调用 glEnableglDisableGL_COLOR_MATERIAL.GL_COLOR_MATERIAL 最初是禁用.

<小时>

如果您想停止跟踪当前颜色并使用 mat_ambientmat_diffuse 中的值,那么您应该在之后添加以下代码你禁用了GL_COLOR_MATERIAL:

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);

I'm developing a version of the Frogger game for a University project, and we got to the light and materials part. The problem I have is: when I set the materials for the frog, it applies them to all other objects that are drawn. How do I make it so that it only applies to the frog? If I disable GL_COLOR_MATERIAL after the draw or after the material properties have been set, everything becomes red.

void Frog::draw(void)
{
    Vector3 _pos = Entity::getPosition();

    glPushMatrix();

    glEnable(GL_COLOR_MATERIAL);

    GLfloat greenEmissiveMaterial[] = {1.0, 0.0, 0.0};
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

    GLfloat mat_ambient[] = {0.7, 0.0, 0.0, 1.0};
    GLfloat mat_diffuse[] = { 0.7, 0.0, 0.0, 1.0 };
    GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 0.0 };
    GLfloat mat_shininess[] = {1.8 };

    glColor3f(1.0, 0.0, 0.0);

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

glDisable(GL_COLOR_MATERIAL);

    glScalef(0.1, 0.1, 0.1);
    glTranslatef( _pos.getX()*U , _pos.getY()*U , 1 );
    //CORPO
    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glutSolidSphere(0.5,50,50);
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glTranslatef(0.0, 0.5, 0.0);
    glutSolidSphere(0.3,50,50);
    glPopMatrix();


    glPopMatrix();

}

解决方案

Enabling the state GL_COLOR_MATERIAL causes the material color to track the current color for the face you specify in a call to glColorMaterial (...). In other words, while GL_COLOR_MATERIAL is enabled and glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) has been set, the value of GL_AMBIENT and GL_DIFFUSE are determined exclusively by the current color for the front-face.

Thus, the the following call:

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

Is effectively equivalent to this in your software:

glColor3f (1.0f, 0.0f, 0.0f); // Set the "current" color

GLfloat red [3] = { 1.0f, 0.0f, 0.0f };

glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);

You can see that you have changed the value of GL_AMBIENT and GL_DIFFUSE to red. And you also tried to change the ambient / diffuse material color manually while that state was enabled in your original code, which is not going to do anything.

The behavior above is described in more detail here:

Name

glColorMaterial — cause a material color to track the current color

C Specification

void glColorMaterial( GLenum face, GLenum mode);

Description

glColorMaterial specifies which material parameters track the current color. When GL_COLOR_MATERIAL is enabled, the material parameter or parameters specified by mode, of the material or materials specified by face, track the current color at all times.

To enable and disable GL_COLOR_MATERIAL, call glEnable and glDisable with argument GL_COLOR_MATERIAL. GL_COLOR_MATERIAL is initially disabled.


If you want to stop tracking the current color and use the values in mat_ambient and mat_diffuse, then you should add the following code sometime after you disable GL_COLOR_MATERIAL:

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);

这篇关于glEnable(GL_COLOR_MATERIAL) 应用于所有绘制的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆