Social Icons

Sabtu, 08 Juni 2013

Cara Membuat Proyeksi dari Gabungan Grafik dengan OpenGL

Run:

Syntax :
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>

double rx=1.0;
double ry=1.0;

float l[]={0.0,10.0,0.0}; // koordinat sumber cahaya
float n[]={0.0,-1.0,0.0}; 
float e[]={0.0,-5.0,0.0}; 

void help();

// obyek yang akan digambar
void draw()
{
    

     //kotak di atas meja
      glPushMatrix(); 
     gluLookAt(0.0, 3.0, 5.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0); 
     glTranslatef(0.0,1,-7.05);
     glRotatef(30,1,0,1);
     glutSolidCube(2.2f);
     glPopMatrix();

     //papan meja
     glPushMatrix();
     gluLookAt(0.0, 2.0, 3.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0); 
     glTranslated(0.1,-0.7,-7.0);
     glRotatef(-90,0,1,0);
     glScaled(5,0.5,7);
     glutSolidCube(1.8);
     glPopMatrix();

     //kaki meja
     glPushMatrix();
     gluLookAt(0.0, 2.0, 3.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);               //kaki meja ke-1 kanan depan
     glTranslated(4.0,-2.5,-5.0);
     glRotatef(-90,0,1,0);
     glScaled(1,4,1);
     glutSolidCube(0.7);
     glPopMatrix();

     glPushMatrix();
     gluLookAt(0.0, 2.0, 3.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);               //kaki meja ke-2 kanan belakang
     glTranslated(4.0,-2.5,-9.0);
     glRotatef(-90,0,1,0);
     glScaled(1,4,1);
     glutSolidCube(0.7);
     glPopMatrix();

     glPushMatrix();
     gluLookAt(0.0, 2.0, 3.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);              //kaki meja ke-3 kiri belakang
     glTranslated(-4.0,-2.5,-9.0);
     glRotatef(-90,0,1,0);
     glScaled(1,4,1);
     glutSolidCube(0.7);
     glPopMatrix();

     glPushMatrix(); 
     gluLookAt(0.0, 2.0, 3.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);              //kaki meja ke-4 kiri depan
     glTranslated(-4.0,-2.5,-5.0);
     glRotatef(-90,0,1,0);
     glScaled(1,4,1);
     glutSolidCube(0.7);
     glPopMatrix();

     glFlush();  //untuk memastikan bahwa perintah gambar dieksekusi
}

//membuat proyeksi bayangan
void glShadowProjection(float*l,float*e,float*n)
{
  float d, c;
  float mat[18];

  d = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
  c = e[0]*n[0] + e[1]*n[1] + e[2]*n[2] - d;

  mat[0]  = l[0]*n[0]+c;            // membuat matrik. OpenGL menggunakan kolom matrik
  mat[4]  = n[1]*l[0]; 
  mat[8]  = n[2]*l[0]; 
  mat[12] = -l[0]*c-l[0]*d;

  mat[1]  = n[0]*l[1];        
  mat[5]  = l[1]*n[1]+c;
  mat[9]  = n[2]*l[1]; 
  mat[13] = -l[1]*c-l[1]*d;

  mat[2]  = n[0]*l[2];        
  mat[6]  = n[1]*l[2]; 
  mat[10] = l[2]*n[2]+c; 
  mat[14] = -l[2]*c-l[2]*d;

  mat[3]  = n[0];        
  mat[7]  = n[1]; 
  mat[11] = n[2]; 
  mat[15] = -d;

  glMultMatrixf(mat);              // kalikan matrik
}

void render()
{
  glClearColor(1.0,1.0,1.0,1.0);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  //warna di atas meja

  glLightfv(GL_LIGHT0, GL_POSITION, l);
  glDisable(GL_CULL_FACE);
  glDisable(GL_LIGHTING);
  glColor3f(0.0,1.0,0.0);

  glBegin(GL_POINTS);
     glVertex3f(l[0],l[1],l[2]);
  glEnd();

   glColor3f(0.7,0.7,0.0);
  glBegin(GL_QUADS);
     glNormal3f(0.0,1.0,0.0);
     glVertex3f(-1300.0,e[1]-0.1, 1300.0);
     glVertex3f( 1300.0,e[1]-0.1, 1300.0);
     glVertex3f( 1300.0,e[1]-0.1,-1300.0);
     glVertex3f(-1300.0,e[1]-0.1,-1300.0);
  glEnd();
 
 

  // gambar bayangan  
  glEnable(GL_LIGHTING);
  glColor3f(1.0,.8,0.5); //warna objek
  draw();
  glPopMatrix();

  //sekarang  gambar bayangan yang muncul
  glPushMatrix();
  glShadowProjection(l,e,n);  
  glDisable(GL_LIGHTING);
  glColor3f(0.1,0.1,0.2); //warna bayangan
  draw();
  glPopMatrix();
  glutSwapBuffers();
                    
}

void keypress(unsigned char c, int a, int b)
{
  if ( c==27 ) exit(0);
  else if ( c=='s' ) l[1]-=5.0;
  else if ( c=='w' ) l[1]+=5.0;
  else if ( c=='a' ) l[0]-=5.0;
  else if ( c=='d' ) l[0]+=5.0;
  else if ( c=='q' ) l[2]-=5.0;
  else if ( c=='e' ) l[2]+=5.0;
  else if ( c=='h' ) help();
}

void help()
{
  printf("proyeksi bayangan sebuah cube diatas meja\n");
}

void idle()
{
  rx+=0.4;
  ry+=0.7;
  render();
}

void resize(int w, int h)
{
  glViewport(0,0,w,h);
}

int main(int argc,char*argv[])
{
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
  glutCreateWindow("Cube di atas meja");
  glutReshapeFunc(resize);
  glutReshapeWindow(400,400);
  glutKeyboardFunc(keypress);
  glutDisplayFunc(render);
  glutIdleFunc(idle);

  glEnable(GL_NORMALIZE);
  glEnable(GL_LIGHTING);
  glEnable(GL_COLOR_MATERIAL);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHT0);
  glEnable(GL_TEXTURE_2D);
  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();
  gluPerspective(40.0f,1.0,1.0,400.0);

  // Reset koordinat  sebelum dimodifikasi/diubah
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0,0.0,-20.0);
  glutMainLoop();
  return 0;
}


Tidak ada komentar:

Posting Komentar

 
 
Blogger Templates