OpenGL [obracanie sześcianów]

0

Siema, napisałem kod, w którym poruszają się dwa sześciany, jeden - środkowy - wokół własnej osi i drugi wokół niego, czy może mi ktoś napisać jak zmodyfikować ten kod, żeby ten drugi sześcian, oprócz tego, że kręci się wokół środkowego, kręcił się także wokół własnej osi?

KOD:

#include <windows.h>
#include <GL/glut.h>
 
const int w = 800;
const int h = 600;
const float delta_alfa=0.5;
float alfa=0.0;


void sciana(){
    glBegin(GL_QUADS);
      glVertex3f(-0.5, 0.0, -0.5);
      glVertex3f (-0.5, 0.0, 0.5);
      glVertex3f(0.5, 0.0, 0.5);
      glVertex3f(0.5, 0.0, -0.5);
    glEnd();
}

void szescian(){

   //dolna podstawa
   glPushMatrix();
     glTranslatef(0.0, -0.5, 0.0);
     glColor3f(0.0, 0.0, 1.0);
     sciana();
   glPopMatrix();

   //gorna podstawa
   glPushMatrix();
     glTranslatef(0.0, 0.5, 0.0);
     glColor3f(0.0, 1.0, 0.0);
     sciana();
   glPopMatrix();
 
   //lewa sciana
   glPushMatrix();
     glColor3f(1.0, 0.0, 0.0);
     glTranslatef(-0.5, 0.0, 0.0);
     glRotatef(90.0, 0.0, 1.0, 0.0);
     glRotatef(90.0, 1.0, 0.0, 0.0);
     sciana();
   glPopMatrix();

   //prawa sciana
   glPushMatrix();
     glColor3f(0.0, 1.0, 1.0);
     glTranslatef(0.5, 0.0, 0.0);
     glRotatef(90.0, 0.0, 1.0, 0.0);
     glRotatef(90.0, 1.0, 0.0, 0.0);
     sciana();
   glPopMatrix();

   //tylna sciana
   glPushMatrix();
     glColor3f(1.0, 1.0, 0.0);
     glTranslatef(0.0, 0.0, -0.5);
     glRotatef(90.0, 1.0, 0.0, 0.0);
     sciana();
   glPopMatrix();

   //przednia sciana
   glPushMatrix();
     glColor3f(1.0, 1.0, 1.0);
     glTranslatef(0.0, 0.0, 0.5);
     glRotatef(90.0, 1.0, 0.0, 0.0);
     sciana();
   glPopMatrix();

}

void szescian2(){

   //dolna podstawa
   glPushMatrix();
     glTranslatef(0.0, -0.5, 0.0);
     glColor3f(0.0, 0.0, 1.0);
     sciana();
   glPopMatrix();

   //gorna podstawa
   glPushMatrix();
     glTranslatef(0.0, 0.5, 0.0);
     glColor3f(0.0, 1.0, 0.0);
     sciana();
   glPopMatrix();
 
   //lewa sciana
   glPushMatrix();
     glColor3f(1.0, 0.0, 0.0);
     glTranslatef(-0.5, 0.0, 0.0);
     glRotatef(90.0, 0.0, 0.0, 1.0);
     glRotatef(90.0, 0.0, 1.0, 0.0);
     sciana();
   glPopMatrix();

   //prawa sciana
   glPushMatrix();
     glColor3f(0.0, 1.0, 1.0);
     glTranslatef(0.5, 0.0, 0.0);
     glRotatef(90.0, 0.0, 0.0, 1.0);
     glRotatef(90.0, 0.0, 10.0, 0.0);
     sciana();
   glPopMatrix();

   //tylna sciana
   glPushMatrix();
     glColor3f(1.0, 1.0, 0.0);
     glTranslatef(0.0, 0.0, -0.5);
     glRotatef(90.0, 1.0, 0.0, 0.0);
     sciana();
   glPopMatrix();

   //przednia sciana
   glPushMatrix();
     glColor3f(1.0, 1.0, 1.0);
     glTranslatef(0.0, 0.0, 0.5);
     glRotatef(90.0, 1.0, 0.0, 0.0);
     sciana();
   glPopMatrix();

}
 
void anim(){
   alfa+=delta_alfa;
    glutPostRedisplay();
}
 
void init(){
glClearColor(0.0, 0.0, 0.0, 1.0);
   glEnable(GL_DEPTH_TEST);
   glMatrixMode(GL_PROJECTION);
   gluPerspective(60.0,1.0,1.0,20.0);
   gluLookAt(0.0,7.0,0.0, 0.0,0.0,0.0, 0.0,0.0,1.0);
   glMatrixMode(GL_MODELVIEW);
}
 
void drawScene(void) {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
     glRotatef(alfa, 1.0, 0.0, 0.0);
     glTranslatef(0.0, 0.0, 0.0);
     szescian();

     glLoadIdentity();
     glRotatef(alfa, 0.0, 0.0, 1.0);
     glTranslatef(-2.0, 0.0, 0.0);
     szescian2();
    glFlush();
    glutSwapBuffers();
}
 
int main (int argc , char **argv) {
   glutInit(&argc , argv);
    glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(w, h);
    glutCreateWindow ("szescian animacja w GLUT");
    glutIdleFunc(anim);
    glutDisplayFunc(drawScene);
    init();
    glutMainLoop();
    return 0;
} 
0

Jedna rotacja przed translacją, a druga po.

1

Weź pod uwagę kolejność przekształceń. Dołóż jeszcze rotate bezpośrednio nad wywołaniem funkcji szescian2.

0

Dzięki za pomoc, wszystko działa :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1