2015-02-24 165 views
1

我想教我自己動畫沒有用戶輸入的對象,到目前爲止我已經想出瞭如何使場景旋轉。我如何讓對象改變顏色?我認爲我的代碼會做到這一點,但它仍然是一個白色的三角形(甚至不是一個不同的顏色)。如何使用glut和openGL更改顏色?

如何在三角形或透視旋轉的同時改變顏色?

這裏是我當前的代碼:

#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; 
} 

回答

2

浮動顏色必須在[0,1]區間,你只添加一些你的價值觀和永不它重置爲零,因此它變得大於1在前10幀(=非常快),OpenGL將它夾到1,所以你看它是白色的。

changeSize中的if s實際上應該在renderScene中。

0
  • 對於這樣的簡單的東西使用glutTimerFunc()回調與合理的超時
  • 更新角度/顏色在定時器的回調&開球重繪
  • 重新設置投影/模型視圖每次矩陣通過在glutDisplayFunc()回調,有助於防止奇怪的矩陣問題

一起:

#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; 
} 
+0

感謝您的替代解決方案。也可以隨意投我的問題,因爲我最近編輯它使其更好,需要反饋。另外,在另一個說明中,我最終使用了一個類似於你的解決方案(我認爲),這是一段時間後。 – cluemein 2016-01-05 13:23:51