Na prvo stran

Glavni deli programa 'Skodelica z ročajem'

Vnos parametrov | Izris modela | Interaktivnost | Senčenje

   //Vnos glavnih parametrov
 void
   VnosParametrov(void)
  {
   VnosZ:
   printf( "Enter thickness (between 1 and 4):");
   scanf( "%f", &deb);
     if ((deb >= 1)&&(deb <= 4) )
       d = deb;
       else 
     {
       goto VnosZ;
     }
 
   VnosR:
   printf( "Enter radius (between 4 and 15):");
   scanf( "%f", &rv);
     if ((rv >= 4)&&(rv <= 15) )
      r = rv;
       else 
     {
       goto VnosR;
     }

   VnosH:
   printf( "Enter height (between 12 and 32):");
   scanf( "%f", &hv);
     if ((hv >= 12)&&(hv <= 32) )
      h = hv;
       else 
     {
       goto VnosH;
     }
  }
  
  int
  main(int argc, char **argv)
  {
  VnosParametrov();
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  glutInitWindowSize(512, 512);
  .
  .
  .
  

   //Podprogram za izris modela
/*Izris modela*/   
   void
   makeSKODELICA(void)
  {

   glNewList(SKODELICA, GL_COMPILE);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);
  
/*notranje dno '1'*/
   angle = 0;
   glNormal3f(1.0, 1.0, 1.0);
   glBegin(GL_TRIANGLE_FAN);
   glVertex3f(0.0,0.0,-h/2);
   for(angler = 2*PI; angler >= 0; angler -= 2*PI/delitev) 
   {
      glVertex3f(r*cos(angler),r*sin(angler),-h/2);
   }
   
   glEnd(); 
   
/*notranji spodnji radij '2'*/
   angled = 0;
   angler = 0;
   drn1 = 1;
   for (angler=0; angler <= 2*PI; angler+=2*PI/delitev)
    { glRotatef((2*PI/delitev)*180/PI, 0.0, 0.0, 1.0); 

        glNormal3f(1.0, -1.0, 1.0);
        glPushMatrix();
        glTranslatef(r-r/100,0.0,-h/2+drn1);
        glBegin(GL_QUAD_STRIP);
          for(angled = PI/2; angled >= 0; angled -= 2*PI/delitev)
           { 
             glVertex3f(drn1*sin(angled),(r*sin(2*PI/delitev)+(drn1*sin((angled)*2*PI/delitev)))/2,(-drn1*cos(angled)));
             glVertex3f(drn1*sin(angled),-(r*sin(2*PI/delitev)+(drn1*sin((angled)*2*PI/delitev)))/2,(-drn1*cos(angled)));
           } 
     
        glEnd(); 
        glPopMatrix();
    }
    .
    .
    .
   float deb;
   float delitev=72;
   float drn1; 
    .
    .
    .


   //Podprogrami, ki omogočajo rotiranje objekta z miško
   void
   mouse(int button, int state, int x, int y)
 {
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
    moving = 1;
    beginx = x;
    beginy = y;
  }
  if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
    moving = 0;
  }
 } 

   void
   motion(int x, int y)
  {
  if (moving) {
    angle = angle + (x - beginx);
    angle2 = angle2 + (y - beginy);
    beginx = x;
    beginy = y;
    newModel = 1;
    glutPostRedisplay();
  }
} 

   void
   tablet(int x, int y)
  {
   xloc = ((GLfloat) x) / 500 - 4;
   yloc = ((GLfloat) y) / 1000 - 2;
   newModel = 1;
   glutPostRedisplay();
  }


   void
   rotate(int x, int y, int z)
  {
   if (xr) {
     angle += x / 2.0;
     newModel = 1;
     glutPostRedisplay();
   }
  }



  //Postavitev luči in njihova inicializacija v glavnem programu
  .
  .
  .
  GLfloat lightZeroPosition[] = {0.0, 0.0, 10.0, 0.0};
  GLfloat lightZeroColor[] = {0.8, 1.0, 0.8, 1.0}; 
  GLfloat lightOnePosition[] = {0.0, 10.0, -10.0, 1.0};
  GLfloat lightOneColor[] = {0.6, 0.3, 0.2, 20.0}; 
  GLfloat skinColor[] = {0.1, 1.0, 0.1, 1.0}; 
  .
  .
     int
   main(int argc, char **argv)
  {
  VnosParametrov();
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  glutInitWindowSize(512, 512); 
  glutCreateWindow("CoffeeCup - Use mouse to rotate");
  glutDisplayFunc(redraw);
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  glutTabletMotionFunc(tablet);
  glutSpaceballRotateFunc(rotate);
  glutSpaceballButtonFunc(button);
  makeSKODELICA();
  glEnable(GL_DEPTH_TEST);
  glMatrixMode(GL_PROJECTION);
  gluPerspective(60.0, 1.0, 1.0, 100.0);
  glMatrixMode(GL_MODELVIEW);
  gluLookAt(0.0, 0.0, 50.0,  
    0.0, 0.0, 0.0,     
    0.0, 1.0, 0.);      
  glPushMatrix();       
  glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
  glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
  glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
  glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
  glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_LIGHTING);
  glutMainLoop();
  return 0;             
}