2014-08-28 69 views
0

我想在opengl中製作一個太空入侵者遊戲。所以我想用三角形來創造敵人。在製作遊戲之前,我想用動畫試試我的手。我有三角形。我想用動畫將它左移到某個點上(也就是說,三角形在一定的時間間隔後被翻譯出來,它應該看起來好像正在移動)。在到達左邊的某個點之後,我想要將它翻譯回原來的位置位置或一些距離。這個過程應該繼續直到屏幕打開。我使用了睡眠功能。但它不起作用。沒有顯示動畫。只有翻譯後的三角形繪製在不同的翻譯位置。幫我。用opengl和C++使用動畫(間隔一段時間後)逐個翻譯一個三角形

這裏是我的代碼 -

#include "windows.h" 
#include <gl/glut.h> 
#include<stdio.h> 

void init(void) 
{ 
    printf("OpenGL version: %s\n", (char*)glGetString(GL_VERSION)); 
    printf("OpenGL renderer: %s\n", (char*)glGetString(GL_RENDERER)); 

    //Configure basic OpenGL settings 

    glClearColor(1.0, 1.0, 0.0, 0.0); 
    glMatrixMode(GL_PROJECTION); 
    gluOrtho2D(0.0,640.0,0.0,480.0); 
    glColor3f(1.0,0.0,0.0); 
glPointSize(3); 
} 

void house(int x, int y,int z) 
{ 
    glPushMatrix(); 
    glTranslatef(x, y,z); 
    glBegin (GL_LINES);  
    glVertex2i (0,30); 
    glVertex2i (15,60); 
     glVertex2i (15,60); 
     glVertex2i (30,30); 
     glVertex2i (30,30); 
     glVertex2i (0,30); 
     glEnd(); 
     //Sleep(200); 
    glPopMatrix(); 
    //glutSwapBuffers(); 
} 


// Main drawing routine. Called repeatedly by GLUT's main loop 
void display(void) 
{ 
    //Clear the screen and set our initial view matrix 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 


    int i; 
    for(i = 10; i < 350; i = i + 50) 
    { 
    house(i,20,0); 
    Sleep(200); 
    } 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
glFlush(); 

} 

// Entry point - GLUT setup and initialization 
int main(int argc, char** argv) 
{ 

    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_DEPTH | GLUT_SINGLE| GLUT_RGB); 
    glutInitWindowSize (800, 600); 
    glutInitWindowPosition (100, 100); 
    glutCreateWindow("OpenGL Test"); 

    glutDisplayFunc(display); 


    init(); 

    glutMainLoop(); 

    return 0; 
} 

回答

1

main()你已經宣佈你display()display callback function。當GLUT確定窗口需要重畫或者通過函數glutPostRedisplay()告訴重繪它時,GLUT將調用該函數。

顯示功能預計會調用在特定時間點重新繪製窗口。 glFlush()將強制執行GL命令。

問題是你的動畫循環是在重繪函數內部,並且在最後調用glFlush(),一次顯示結果。你不告訴GLUT重新繪製窗戶。這就是爲什麼你看不到動畫。

爲了本教程的目的,我建議您爲房屋圖紙的初始位置定義一個全局變量。當然,只要你明白這一切是如何工作的,你就必須改善這一點。

static int pos = 10; // temporary work around, just for the demo 

然後定義一個定時器函數,它在一段時間間隔後被調用。這將是動畫的核心,組織移動,窗口重繪通過調用glutPostRedisplay()

void timerfunc(int value) // handle animation 
{ 
    pos += 50;    // update the postion 
    if (pos<350)    // as in your originial loop 
     glutTimerFunc(200, timerfunc, 0); // plan next occurence 
    glutPostRedisplay();  // redraw the window 
} 

Activate your timer function,在main()剛剛推出glutMainLoop()前:

glutTimerFunc(200, timerfunc, 0);  // call a timer function 
glutMainLoop();      // this call is already in your code 

你的顯示功能然後改爲:

void display(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
    house(pos, 20, 0); // draw the house at its last calculated position 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glFlush(); 
} 

然後它在動畫模式下工作!

+0

你可以請我提供一些算法或代碼來做到這一點?這也是一個三角形。我將如何翻譯三角形的數組? – user3757558 2014-08-28 19:17:17

+0

謝謝....我會看到它。如果我有一個三角形的瓷磚,即排列成瓷磚的三角形(使用視口和世界窗口),那麼我將如何移動這些瓷磚左和寫太空入侵者的敵人一樣寫。你能給我一些提示嗎? – user3757558 2014-08-29 02:42:53