2011-06-03 1 views
1

dans ce code je suis essayer de dessiner anneau olympique simple et le faire pivoter ... le travail ci-dessous bien mais je ne peux pas faire pivoter les anneaux .. m'aider à résoudre ce problème ... fonctioncomment faire pivoter ce code openGl

void myReshape (int width, int height) 
{ 
    glViewport (0, 0, width, height);  
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D (-5, 105, -5, 105); 
    glMatrixMode (GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef (0.375, 0.375, 0.0); 
} 


int main (int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowPosition(100,100); 
    glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE); 
    glutCreateWindow ("Olymipc Rings || rotation "); 
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glPointSize(PIXEL_SIZE);   
    glShadeModel (GL_FLAT);  
    glutDisplayFunc(display); 
    glutReshapeFunc(myReshape); 
    glutMainLoop(); 
    return 0; 
} 

Répondre

1

Essayez ceci:

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

#define PIXEL_SIZE 3 
#define MESSAGE  "hello world !" 

void draw_circle(int x, int y, int r); 

int ring_radius = 19; 
int color[5][3]={{0,0,1}, {0,0,0},{1,0,0}, {1,1,0},{0,1,0}}; 
int center[5][2]={{15,60},{50,60},{85,60},{33,45},{68,45}}; 
//========================================================= 

void drawText(const char * message) 
{ 
    glRasterPos2f((GLfloat)0, (GLfloat)-400); 
    while (*message) 
    { 
     glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *message); 
     glutStrokeCharacter(GLUT_STROKE_ROMAN,*message++); 
    } 
} 

void display (void) 
{ 
    int ms = glutGet(GLUT_ELAPSED_TIME); 

    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D (-5, 105, -5, 105); 
    glMatrixMode (GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef (0.375, 0.375, 0.0); 

    glClear(GL_COLOR_BUFFER_BIT); 
    glBegin(GL_LINE_LOOP); 
    glVertex2i(-1,-1); 
    glVertex2i(100,-1); 
    glVertex2i(100,100); 
    glVertex2i(-1,100); 
    glEnd(); 

    const float deg_per_sec = 60.0f; 
    float angle = deg_per_sec * ((float)ms/1000.0f); 

    for(int i = 0 ; i <5; i++) 
    { 
     glColor3f(color[i][0],color[i][1],color[i][2]); 

     glPushMatrix(); 
     glTranslatef(center[i][0], center[i][1], 0); 
     glRotatef(angle, 0, 0, 1); 
     glTranslatef(-center[i][0], -center[i][1], 0); 
     draw_circle(center[i][0],center[i][1],ring_radius); 
     glPopMatrix(); 
    } 
    glScalef(0.001, 0.001, 0.001); 
    drawText(MESSAGE); 
    glFlush();    

    glutSwapBuffers(); 
} 

void draw_circle(int center_x, int center_y , int radius) 
{ 
    int r = radius; 
    int h = 1 - r ; /*initialization */ 
    int x = 0; 
    int y = r; 
    int dU=3; 
    int dD = 5 - 2*r; 
    int i = center_x; 
    int j = center_y; 

    glPointSize(6); 
    glBegin(GL_POINTS); 

    while(y > x) 
    { 
     if (h<0) 
     { 
      dU= dU + 2; 
      h = h + dU; 
      x = x + 1; 
      dD = dD + 2; 
     } 
     else 
     { 
      dD = 2*(x-y) + 5; 
      h = h + dD; 
      x = x + 1; 
      y = y - 1; 
      dU = dU + 2; 
      dD = dD + 4; 
     } 

     glVertex2i(x+i, y+j); 
     glVertex2i(-x+i, y+j); 
     glVertex2i(x+i, -y+j); 
     glVertex2i(-x+i,-y+j); 
     glVertex2i(y+i, x+j); 
     glVertex2i(y+i, -x+j); 
     glVertex2i(-y+i, x+j); 
     glVertex2i(-y+i, -x+j); 

    } 

    glEnd(); 
} 

void myReshape (int width, int height) 
{ 
    glViewport (0, 0, width, height);  
} 

void idle() 
{ 
    glutPostRedisplay(); 
} 


int main (int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowPosition(100,100); 
    glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE); 
    glutCreateWindow ("Olymipc Rings || rotation "); 
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glPointSize(PIXEL_SIZE);   
    glShadeModel (GL_FLAT);  
    glutDisplayFunc(display); 
    glutReshapeFunc(myReshape); 
    glutIdleFunc(idle); 
    glutMainLoop(); 
    return 0; 
} 
+0

merci mon ami ... c'est un travail bien;) merci merci et merci;) –

+0

@hesam: Pas de problème. Je ne savais pas ce que vous vouliez que les anneaux tournent * autour * mais j'ai pensé que le centre serait un pari sûr :) – genpfault

0
+0

j'essaie d'utiliser glRotated mais qui ne fonctionne pas .... Merci pour your'e attention .. –

1

utilisation glRotatef (axis_x, axis_y, un axe z, l'angle) avant de tirer les anneaux. Si vous voulez continuer à tourner l'anneau, utilisez toujours glutIdle (myidle) dans votre fonction main() et augmentez la valeur de l'angle et utilisez également glutPostRedisplay(). Utilisez glPushMatrix() avant et glPopMatrix() et après vos anneaux si vous ne voulez pas que la rotation affecte d'autres dessins. par exemple si vous souhaitez faire pivoter vos anneaux sur les axe x votre code ressemblera

float angle=0; 

void display (void) { 

    glClear(GL_COLOR_BUFFER_BIT); 
    glBegin(GL_LINE_LOOP); 
    glVertex2i(-1,-1); 
    glVertex2i(100,-1); 
    glVertex2i(100,100); 
    glVertex2i(-1,100); 
    glEnd(); 

glPushMatrix(); //enters temporarily in a stack 

    for(int i = 0 ; i <5; i++) 
    { 
     glRotatef(1,0,0, angle) 
     glColor3f(color[i][0],color[i][1],color[i][2]); 
     draw_circle(center[i][0],center[i][1],ring_radius); 

    } 
glPopMatrix(); // comes out of the stack 

    glScalef(0.001, 0.001, 0.001); 
    drawText(MESSAGE); 
    glFlush();    

} 


void myidle() 
{ 
angle++; //angle value keeps on increasing 
glutPostRedisplay(); // draws your drawing with updated value of angle to the screen 
} 

int main (int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowPosition(100,100); 
    glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE); 
    glutCreateWindow ("Olymipc Rings || rotation "); 
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glPointSize(PIXEL_SIZE);   
    glShadeModel (GL_FLAT);  
    glutDisplayFunc(display); 
    glutIdleFunc(myidle); //just like DisplayFunc keeps on getting calls 
    glutReshapeFunc(myReshape); 
    glutMainLoop(); 
    return 0; 

En savoir plus sur glPopMatrix(), glPushMatrix() et de rappel des fonctions comme glutIdleFunc().

J'espère que cela aidera !!