[英]How do I get color to change using glut and openGL?
I am trying to teach myself to animate an object without user input, so far I have figured out how to make a scene rotate. 我试图教自己在没有用户输入的情况下为一个对象设置动画,到目前为止我已经想出了如何使一个场景旋转。 How do I get an object to change color though?
如何让对象改变颜色? I thought the code I have would do it, but it remains a white triangle (not even a different color).
我认为我的代码会做到,但它仍然是一个白色三角形(甚至不是一个不同的颜色)。
How do I get it to change color at the same time that the triangle or perspective rotates? 如何在三角形或透视图旋转的同时更改颜色?
Here is my current code: 这是我目前的代码:
#include <GL/glut.h>
float color1;
float color2;
float color3;
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
//changeColor?
color1 += 0.1f;
color2 += 0.3;
color3 += color2;
if (color1 > 1.0)
color1 = 0;
if (color2 > 1.0)
color2 = 0;
if (color3 > 1.0)
color3 = 0;
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
float angle = 0.0f;
void renderScene(void) {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(0.0f, 0.0f, 10.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(color1, color2, color3);
glVertex3f(2.0f, -2.0f, 0.0f);
glVertex3f(2.0f, 0.0f, 0.0);
glVertex3f(0.0f, 2.0f, 0.0);
glEnd();
angle += 0.1f;
color1 += 0.1f;
color2 += 0.3;
color3 += color2;
glutSwapBuffers();
}
int main(int argc, char **argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(320, 320);
glutCreateWindow("tutorial example");
// register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
Float colors have to be in range of [0,1] and you only add something to your values and never reset it to zero, so it becomes greater than 1 in first 10 frames (=very fast), OpenGL clamps it to 1, so you see it white. 浮点颜色必须在[0,1]的范围内,你只需要在你的值上添加一些东西,而不要将它重置为零,所以它在前10帧中变得大于1(=非常快),OpenGL将它固定为1,所以你看到它是白色的。
Ie that bunch of if
s in changeSize
should actually be in renderScene
. 也就是说,
changeSize
中的一堆if
应该实际上在renderScene
。
glutTimerFunc()
callback with a reasonable timeout glutTimerFunc()
回调 glutDisplayFunc()
callback, helps prevent weird matrix problems glutDisplayFunc()
回调重新设置投影/模型视图矩阵,有助于防止奇怪的矩阵问题 All together: 全部一起:
#include <GL/glut.h>
float angle = 0.0f;
float color1 = 0.0f;
float color2 = 0.0f;
float color3 = 0.0f;
void timer( int value )
{
angle += 3.0f;
//changeColor?
color1 += 0.001f;
color2 += 0.003f;
color3 += color2;
if (color1 > 1.0)
color1 = 0;
if (color2 > 1.0)
color2 = 0;
if (color3 > 1.0)
color3 = 0;
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(45.0f, w / h, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
0.0f, 0.0f, 10.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(color1, color2, color3);
glVertex3f(2.0f, -2.0f, 0.0f);
glVertex3f(2.0f, 0.0f, 0.0);
glVertex3f(0.0f, 2.0f, 0.0);
glEnd();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(320, 320);
glutCreateWindow("tutorial example");
glutDisplayFunc(renderScene);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.