Monday, November 18, 2013

Translasi dan Rotasi pada Ruang 3 Dimensi

  No comments
#include<windows.h>
#include<gl/glut.h>

void init(){
 glClearColor(0.0,0.0,0.0,0.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-15.0,15.0,-15.0,15.0,-15.0,15.0);
}


void resize(int width, int height){
 if (height == 0){
  height = 1;
 }
 glLoadIdentity();
 /* note we divide our width by our height to get the aspect ratio */
 gluPerspective(45.0, width / height, 1.0, 400.0);
 /* set initial position */
 glTranslatef(0.0, -5.0, -150.0);
 glMatrixMode(GL_MODELVIEW);
}

void segiEmpat(){
  glBegin(GL_QUADS);
  glColor3f(0.0,0.0,1.0);
  glVertex3f(-3.0,3.0,3.0); //front
  glVertex3f(-3.0,-3.0,3.0);
  glVertex3f(3.0,-3.0,3.0);
  glVertex3f(3.0,3.0,3.0);
  //   glColor3f(1.0,0.0,0.0);
  //glVertex3f(-3.0,3.0,-3.0); //left
  //glVertex3f(-3.0,-3.0,-3.0);
  //glVertex3f(-3.0,-3.0,3.0);
  //glVertex3f(-3.0,3.0,3.0);
  //glColor3f(1.0,0.0,1.0);
  //glVertex3f(3.0,3.0,-3.0); //back
  //glVertex3f(3.0,-3.0,-3.0);
  //glVertex3f(-3.0,-3.0,-3.0);
  //glVertex3f(-3.0,3.0,-3.0);
  //glColor3f(0.0,1.0,0.0);
  //glVertex3f(3.0,3.0,3.0); //right
  //glVertex3f(3.0,-3.0,3.0);
  //glVertex3f(3.0,-3.0,-3.0);
  //glVertex3f(3.0,3.0,-3.0);
  //glColor3f(1.0,1.0,1.0);
  //glVertex3f(-3.0,3.0,-3.0); //top
  //glVertex3f(3.0,3.0,-3.0);
  //glVertex3f(3.0,3.0,3.0);
  //glVertex3f(-3.0,3.0,3.0);
  //glColor3f(1.0,1.0,1.0);
  //glVertex3f(-3.0,-3.0,-3.0); //bottom
  //glVertex3f(3.0,-3.0,-3.0);
  //glVertex3f(3.0,-3.0,3.0);
  //glVertex3f(-3.0,-3.0,3.0);
 glEnd();

}

void display(){
 //glClear(GL_COLOR_BUFFER_BIT);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 segiEmpat();
 glutSwapBuffers();
}

void keyboardChar(unsigned char key,int x,int y){
 switch(key){
 case 'w' :
 case 'W':
         /* angle, x,   y,  z */

  glRotated(3.0, 1.0, 0.0, 0.0); // Rotasi ke atas terhadap sumbu x
  break;
 case 's':
 case 'S':
  glRotatef(-3.0,1.0,0.0,0.0); // Rotasi ke bawah terhadap sumbu x
  break;
 case 'a':
 case 'A':
  glRotatef(3.0,0.0,1.0,0.0); // Rotasi ke kiri terhadap sumbu y
  break;
 case 'd':
 case 'D':
  glRotatef(-3.0,0.0,1.0,0.0); // Rotasi ke kanan terhadap sumbu y
  break;
 case 'o':
 case 'O':
  glRotatef(3.0,0.0,0.0,1.0);
  break;
 case 'p':
 case 'P':
  glRotatef(-3.0,0.0,0.0,1.0);
  break;
 case 'l':
 case 'L':
  glTranslatef(-0.1,0.0,0.0);
  break;
 case 'r':
 case 'R':
  glTranslatef(0.1,0.0,0.0);
  break;
 case 'u':
 case 'U':
  glTranslatef(0.0,0.1,0.0);
  break;
 case 'y':
 case 'Y':
  glTranslatef(0.0,-0.1,0.0);
  break;
 case 'm':
 case 'M':
  glTranslatef(0.0,0.0,5.0);
  break;
 case 'n':
 case 'N':
  glTranslatef(0.0,0.0,-5.0);
  break;
 case 'h':
 case 'H':

  glScalef(1.0,1.1,0.0);

  break;

 }

 display();// Repaint the window

}





void keyboard(int key,int x,int y){

 switch(key){

 case 'w' :

 case 'W':

         /* angle, x,   y,  z */

  glRotated(3.0, 1.0, 0.0, 0.0); // Rotasi ke atas terhadap sumbu x

  break;

 case 's':

 case 'S':

  glRotatef(-3.0,1.0,0.0,0.0); // Rotasi ke bawah terhadap sumbu x

  break;

 case 'a':

 case 'A':

  glRotatef(3.0,0.0,1.0,0.0); // Rotasi ke kiri terhadap sumbu y

  break;

 case 'd':

 case 'D':

  glRotatef(-3.0,0.0,1.0,0.0); // Rotasi ke kanan terhadap sumbu y

  break;



 case 'o':

 case 'O':

  glRotatef(3.0,0.0,0.0,1.0);

  break;

 case 'p':

 case 'P':

  glRotatef(-3.0,0.0,0.0,1.0);

  break;



 case GLUT_KEY_UP:

 //case 'L':

  glTranslatef(-0.1,0.0,0.0);

  break;



 case 'r':

 case 'R':

  glTranslatef(0.1,0.0,0.0);

  break;



 case 'u':

 case 'U':

  glTranslatef(0.0,0.1,0.0);

  break;



 case 'y':

 case 'Y':

  glTranslatef(0.0,-0.1,0.0);

  break;



 case 'm':

 case 'M':

  glTranslatef(0.0,0.0,5.0);

  

  break;



 case 'n':

 case 'N':

  glTranslatef(0.0,0.0,-5.0);

  break;



 case 'h':

 case 'H':

  

  glScalef(1.0,1.1,0.0);

  break;

 }



 display();// Repaint the window

}





int main(int args1, char **args2){

 glutInit(&args1, args2);

 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

 glutInitWindowSize(600,600);

 glutInitWindowPosition(50,50);

 glutCreateWindow("A 3D Object");

 init();

 glutDisplayFunc(display);   // memanggil fungsi display

 glutKeyboardFunc(keyboardChar);  // keyboard

 glutSpecialFunc(keyboard);

 glutReshapeFunc(resize);     // risize objek

 glutMainLoop();

 return 0;

}

No comments :