2011-07-03 26 views
-4

可能重複:下面
how to rotate this openGl code…旋轉一圈5問題

是我的代碼,它做工精細..我在此提請5環。

#include <GL/glut.h> 
#include <math.h> 

static void redraw(void); 
#define PI 3.14159265 
#define EDGES 90 
#define factor 10 


void display (void) 
{ 
    glClearColor(1.0,1.0,1.0,0.0); 
    glMatrixMode (GL_MODELVIEW); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 


//ring1 
glPushMatrix(); 
glColor3f (0.0, 0.0 ,1.0); 
glTranslatef(-30.0,10.0,-100.0); 
redraw(); 
glutSwapBuffers(); 
glPopMatrix(); 

//ring2 
glPushMatrix(); 
glColor3f (0.0, 0.0, 0.0); 
glTranslatef(-8.0,10.0,-100.0); 
redraw(); 
glutSwapBuffers(); 
glPopMatrix(); 

//ring3 
glPushMatrix(); 
glColor3f (1.0, 0.0 ,0.0); 
glTranslatef(14.0,10.0,-100.0); 
redraw(); 
glutSwapBuffers(); 
glPopMatrix(); 

//ring4 
glPushMatrix(); 
glColor3f (1.0, 1.0, 0.0); 
glTranslatef(-19.0,-2.0,-100.0); 
redraw(); 
glutSwapBuffers(); 
glPopMatrix(); 

//ring5 
glPushMatrix(); 
glColor3f (0.0, 1.0, 0.0); 
glTranslatef(4.0,-2.0,-100.0); 
redraw(); 
glutSwapBuffers(); 
glPopMatrix(); 

} 

static void redraw(void) 
{ 
for (int i = 0; i < EDGES; i++) 
    { 
     glBegin(GL_LINE_LOOP); 
     glVertex2f(factor*cos((2*PI*i)/EDGES),factor*sin((2*PI*i)/EDGES)); 
     glVertex2f(factor*cos((2*PI*(i+1))/EDGES),factor*sin((2*PI*(i+1))/EDGES)); 
     glEnd(); 
    } 
} 

int main(int argc, char **argv) 
{ 
glutInit(&argc,argv); 
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 
glutInitWindowPosition(100,100); 
glutInitWindowSize(110*3, 110*3); 
glutCreateWindow("draw circle"); 
glPointSize(3); 
glShadeModel (GL_FLAT); 
glutDisplayFunc(display); 
glMatrixMode(GL_PROJECTION);  
gluPerspective(45,1.0,10.0,200.0); 
glMatrixMode(GL_MODELVIEW); 
glutMainLoop(); 
return 0; 
} 

所以我想旋轉所有這個圓..我怎麼辦?

+2

請務必使用正斜槓作爲目錄分隔符 - 它適用於所有平臺! –

+0

我不明白你想要什麼。你想如何旋轉它們? –

+1

如果你旋轉一個圓圈,它仍然是一個圓圈 - 也許你可以更清楚地解釋你試圖達到的目標是什麼? –

回答

3

您應該在繪圖函數中只調用一次glutSwapBuffers。

對於你必須調用glutPostRedisplay,以確保顯示功能將被再次調用的動畫。

將在glBegin和glEnd調用只需要調用每圈一次。在for循環中,您只需提供調用glVertex的頂點。

爲了簡化調試我裝按下ESC鍵時,即關閉窗口鍵盤處理程序。

// compile in linux: gcc ogl.c -lglut -lGLU 

#include <GL/glut.h> 
#include <math.h> 
#include <stdlib.h> 

static void redraw(void); 
#define PI 3.14159265 

enum{ 
    EDGES=90, 
}; 

static void 
circle(float radius) 
{ 
    int i; 
    glBegin(GL_LINE_LOOP); 
    for (i = 0; i < EDGES; i++){ 
    glVertex2f(radius*cos((2*PI*i)/EDGES), 
      radius*sin((2*PI*i)/EDGES)); 
    glVertex2f(radius*cos((2*PI*(i+1))/EDGES), 
      radius*sin((2*PI*(i+1))/EDGES)); 
    } 
    glEnd(); 
} 


int count=0; 
void display (void) 
{ 
    float r=10; 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 

    glRotated(count,0,0,1); 
    count++; 
    if(count>360) 
    count=0; 

    glPushMatrix(); 
    // ring1 
    glColor3f (0.0, 0.0 ,1.0); 
    glTranslatef(-30.0,10.0,-100.0); 
    circle(r); 
    glPopMatrix(); 

    glPushMatrix(); 
    // ring2 
    glColor3f (0.0, 0.0, 0.0); 
    glTranslatef(-8.0,10.0,-100.0); 
    circle(r); 
    glPopMatrix(); 

    glPushMatrix(); 
    //ring3 
    glColor3f (1.0, 0.0 ,0.0); 
    glTranslatef(14.0,10.0,-100.0); 
    circle(r); 
    glPopMatrix(); 

    glPushMatrix(); 
    //ring4 
    glColor3f (1.0, 1.0, 0.0); 
    glTranslatef(-19.0,-2.0,-100.0); 
    circle(r); 
    glPopMatrix(); 

    glPushMatrix(); 
    //ring5 
    glColor3f (0.0, 1.0, 0.0); 
    glTranslatef(4.0,-2.0,-100.0); 
    circle(r); 
    glPopMatrix(); 

    glutSwapBuffers(); 
    glutPostRedisplay(); 
} 

static void 
keyb(unsigned char key, int x, int y) 
{ 
    switch (key) { 
    case 27: /* Escape key */ 
    exit(0); 
    } 
} 


void 
init() 
{ 
    glPointSize(3); 
    glShadeModel (GL_FLAT); 
    glMatrixMode(GL_PROJECTION);  
    gluPerspective(45,1.0,10.0,200.0); 
    glMatrixMode(GL_MODELVIEW); 
    glClearColor(1.0,1.0,1.0,0.0); 
    glLineWidth(2); 
} 

int 
main(int argc, char **argv) 
{ 
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 
    glutInitWindowPosition(100,100); 
    glutInitWindowSize(110*3, 110*3); 
    glutCreateWindow("draw circle"); 
    glutDisplayFunc(display); 
    glutKeyboardFunc(keyb); 
    init(); 
    glutMainLoop(); 
    return 0; 
} 
+0

謝謝你的回答..看看我的代碼我有5個戒指看起來像奧運會的戒指,當我用你的代碼只是1圈的旋轉.. –

+0

@hesam這只是一個環,因爲glTranslate調用是不相對的。我修好了它。如果您想要,現在再試一次就可以了。 – whoplisp

+0

whoplisp»謝謝你的幫助示例 –