如何在OpenGL中将颜色更改为随机?
我已经试过无数次了,试着让它发挥作用。我已经把它搬来搬去了,但还是没用。当我按下M键时,我的灯光将变为随机颜色。然而,它们只会变成白色 这就是我所拥有的 浮点彩色数组[100][3];//为随机颜色创建一个数组 按键功能:如何在OpenGL中将颜色更改为随机?,opengl,Opengl,我已经试过无数次了,试着让它发挥作用。我已经把它搬来搬去了,但还是没用。当我按下M键时,我的灯光将变为随机颜色。然而,它们只会变成白色 这就是我所拥有的 浮点彩色数组[100][3];//为随机颜色创建一个数组 按键功能: case 'm' | 'M': updateLights(2); break; for (int i = 0; i < 3; i++) { glPushMatrix(); glColor3f(colorArray[i]
case 'm' | 'M':
updateLights(2);
break;
for (int i = 0; i < 3; i++)
{
glPushMatrix();
glColor3f(colorArray[i][0],colorArray[i][1],colorArray[i][2]);
glTranslatef(-50*i/2,-20,0.5); // Begin the first circle at -50, -20. Then multiply by i to create a space between them.
drawLights(2.0f);
glPopMatrix();
if(i <= 3)
{
glPushMatrix();
glColor3f(colorArray[i][0],colorArray[i][1],colorArray[i][2]);
glTranslatef(-38,-20,0.5);
drawLights(2.0f);
glPopMatrix();
glPushMatrix();
glColor3f(colorArray[i][0],colorArray[i][1],colorArray[i][2]);
glTranslatef(-12,-20,0.5);
drawLights(2.0f);
glPopMatrix();
}
}
{
cout << "update lights" << endl;
for(int i = 0; i < 3; i++)
{
colorArray[i][0] = rand() % 255;
colorArray[i][1] = rand() % 255;
colorArray[i][2] = rand() % 255;
glutPostRedisplay();
}
}
定义的\u到\u openGL函数:
case 'm' | 'M':
updateLights(2);
break;
for (int i = 0; i < 3; i++)
{
glPushMatrix();
glColor3f(colorArray[i][0],colorArray[i][1],colorArray[i][2]);
glTranslatef(-50*i/2,-20,0.5); // Begin the first circle at -50, -20. Then multiply by i to create a space between them.
drawLights(2.0f);
glPopMatrix();
if(i <= 3)
{
glPushMatrix();
glColor3f(colorArray[i][0],colorArray[i][1],colorArray[i][2]);
glTranslatef(-38,-20,0.5);
drawLights(2.0f);
glPopMatrix();
glPushMatrix();
glColor3f(colorArray[i][0],colorArray[i][1],colorArray[i][2]);
glTranslatef(-12,-20,0.5);
drawLights(2.0f);
glPopMatrix();
}
}
{
cout << "update lights" << endl;
for(int i = 0; i < 3; i++)
{
colorArray[i][0] = rand() % 255;
colorArray[i][1] = rand() % 255;
colorArray[i][2] = rand() % 255;
glutPostRedisplay();
}
}
for(int i=0;i<3;i++)
{
glPushMatrix();
glColor3f(COLORRAY[i][0],COLORRAY[i][1],COLORRAY[i][2]);
glTranslatef(-50*i/2,-20,0.5);//从-50,-20开始第一个圆。然后乘以i在它们之间创建一个空间。
吊灯(2.0f);
glPopMatrix();
如果(i您使用的是glColor3f
,它在[0.0,1.0]
中为每个颜色强度接受3个浮点参数,而rand()%255
则产生[0254]
中的输出
您可以切换到glColor3ub(GLubyte红色、GLubyte绿色、GLubyte蓝色)
,它接受一个无符号字节(并且将模更改为%256
,因为您用255
跳过一个值),或者通过将随机生成更改为,在[0.0,1.0]
中生成一个值
rand()/((浮点)rand\u MAX+1)
但这意味着你必须将colorArray
的类型改为GLFloat都德……我现在真的爱上你了。非常感谢你!glColor3f让我受不了了!请注意,整个opengl库提供了不同的方法,用不同的输入类型调用相同的方法,这对许多函数都是如此。@杰克:请不要把OpenGL称为库,因为它不是。它是一个API规范,可以作为库来实现。是的,我知道“L”最初代表库,但我个人将其命名为layer。