All pastes #526700 Raw Edit

Something

public text v1 · immutable
#526700 ·published 2007-05-31 22:32 UTC
rendered paste body
/*
 * FreeGLUT Shapes Demo
 *
 * Written by Nigel Stewart November 2003
 *
 * This program is test harness for the sphere, cone 
 * and torus shapes in FreeGLUT.
 *
 * Spinning wireframe and smooth shaded shapes are
 * displayed until the ESC or q key is pressed.  The
 * number of geometry stacks and slices can be adjusted
 * using the + and - keys.
 */

#include <GL/glut.h>
#include <cmath>
#include <stdlib.h>
int glebia = 0;
int max_g = 7 ;
int mode = 1 ;

   float  x1=-4.0 , yy1=-3.0 , z1=-5.0,
          x2=4.0  , yy2=-3.0 , z2=-5.0,
          x3=0.0  , yy3=3.0, z3=-5,
          x4=0   , yy4=0  , z4=-5 ,
          angle3=0.005;

   float  kx1=-3.5f  , ky1=3.5f   , kz1=-5.0 ,
          kx2=-3.5f  , ky2=-3.5f   , kz2=-5.0 ,
          kx3=3.5f  , ky3=-3.5f , kz3=-5.0 ,
          kx4=3.5f  , ky4=3.5f , kz4=-5.0 ;


void rysuj(float x1, float yy1, float x2, float yy2, float x3, float y3) ;
void krysuj(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) ;

void obroc(float &a, float a0, float &b, float b0, float kat) {
     
        a = a0 + (a  - a0) * cos(kat) + (b  - b0) * sin(kat);
        b = b0 - (a  - a0) * sin(kat) + (b  - b0) * cos(kat);
        
    //    x2 = x + (x2  - x) * cos(angle3) + (yy2  - y) * sin(angle3);
    //    yy2 = y - (x2  - x) * sin(angle3) + (yy2  - y) * cos(angle3);
          return ;
    
     }
void wywolaj(float x1, float yy1, float x2, float yy2, float x3, float y3, int limit)
{

float a_x, a_y, b_x, b_y, c_x, x_y, c_y ;
a_x=(float) (x1+x2)/2.0 ;
a_y=(float) (yy1+yy2)/2.0 ;
b_x=(float) (x1+x3)/2.0 ;
b_y=(float) (yy1+y3)/2.0 ;
c_x=(float) (x2+x3)/2.0 ;
c_y=(float) (yy2+y3)/2.0 ;   
  
if(limit==0) {
             rysuj(x1,yy1,x2,yy2,x3,y3);
}     
else {
        wywolaj(x1,yy1,a_x,a_y,b_x,b_y,limit-1) ;
        wywolaj(x2,yy2,a_x,a_y,c_x,c_y,limit-1) ;
        wywolaj(x3,y3,c_x,c_y,b_x,b_y,limit-1) ;

     };
  return ;
}


void kwywolaj(float x1, float y1, float x2, float y2, float x3, float y3,float x4, float y4,  int limit)
{

float a_x, a_y, b_x, b_y;///, c_x, c_y ;
a_x= (float) ((float) std::abs ( (float)((float)x4-(float)x1)))/3.0f ;
a_y=(float) std::abs(y4-y1)/3.0 ;
b_x=(float) std::abs(x2-x1)/3.0 ;
b_y=std::abs(y2-y1)/3.0f ;
 
  
if(limit==0) {
             krysuj(x1,y1,x2,y2,x3,y3,x4,y4);
}     
else {
       for(int i=0;i<3;i++)
         for(int j=0;j<3;j++)
           if((i!=1)||(j!=1)) {
              
              
              kwywolaj(x1-(a_x*i)-b_x*j,a_y*(j+1), 
              
                       b_x*(i+1),b_y*(j+1),
                       b_x*(i+1)-b_x*j, b_y*(j+1)-a_y*i,
                       x1*i+a_x*(i+1),y1-a_y*(j+1),
                        limit-1
                       )
              ;
              };
     };
  return ;
}


void rysuj(float x1, float yy1, float x2, float yy2, float x3, float y3) {
     float z=-5;
 
        glBegin(GL_TRIANGLES);
        glColor3ub(49,128,248);
        glVertex3f( x1,  yy1, z);
//        glColor3ub(0, 255, 0);
        glVertex3f( x2,  yy2, z);
 ///       glColor3ub(0, 0, 255);
        glVertex3f( x3,  y3, z);
        glEnd();
    
}

void krysuj(float x1, float yy1, float x2, float yy2, float x3, float yy3, float x4, float yy4) 
{
     float z=-5;
 
        glBegin(GL_QUADS);
        glColor3ub(49,128,248);
           glVertex3f( x1,  yy1, z);
           glVertex3f( x2,  yy2, z);
           glVertex3f( x3,  yy3, z);
           glVertex3f( x4,  yy4, z);
        glEnd();
    
}


/* GLUT callback Handlers */

static void 
resize(int width, int height)
{
    const float ar = (float) width / (float) height;
    
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 1, 60.0);
    
    
    
    
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

static void 
display(void)
{
  

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  //  glColor3d(0,0,0);


  
      
       
        if(mode==1) wywolaj(x1,yy1,x2,yy2,x3,yy3,glebia);
        else if(mode==2) kwywolaj(kx1,ky1,kx2,ky2,kx3,ky3,kx4,ky4,glebia) ;

//         slep(1000);
      //   i++;

        /* Obrót wzgl. osi X */
/*
        yy1 = y + (yy1  - y) * cos(angle1) + (z1  - z) * sin(angle1);
        z1 = z - (yy1  - y) * sin(angle1) + (z1  - z) * cos(angle1);
       
        yy2 = y + (yy2  - y) * cos(angle1) + (z2  - z) * sin(angle1);
        z2 = z - (yy2  - y) * sin(angle1) + (z2  - z) * cos(angle1);
       
        y3 = y + (y3  - y) * cos(angle1) + (z3  - z) * sin(angle1);
        z3 = z - (y3  - y) * sin(angle1) + (z3  - z) * cos(angle1);

        /* Obrót wzgl. osi Y 

        x1 = x + (x1  - x) * cos(angle3) + (z1  - z) * sin(angle2);
        z1 = z - (x1  - x) * sin(angle3) + (z1  - z) * cos(angle2);

        x2 = x + (x2  - x) * cos(angle3) + (z2  - z) * sin(angle2);
        z2 = z - (x2  - x) * sin(angle3) + (z2  - z) * cos(angle2);
       
        x3 = x + (x3  - x) * cos(angle3) + (z3  - z) * sin(angle2);
        z3 = z - (x3  - x) * sin(angle3) + (z3  - z) * cos(angle2);
*/
        /* Obrót wzgl. osi Z */
//if(key[KEY_A]) {angle3=0.05;nico=1;}
//else if(key[KEY_S]) {angle3+=0.005;nico=1;}
//else nico=0;

 /*
        x2 = x + (x2  - x) * cos(angle3) + (yy2  - y) * sin(angle3);
        yy2 = y - (x2  - x) * sin(angle3) + (yy2  - y) * cos(angle3);
       
        x3 = x + (x3  - x) * cos(angle3) + (yy3  - y) * sin(angle3);
        yy3 = y - (x3  - x) * sin(angle3) + (yy3  - y) * cos(angle3);
   */    


    glutSwapBuffers();
}


static void 
key(unsigned char key, int x, int y)
{
    switch (key) 
    {
        case 27 : 
        case 'p':
            exit(0);
            break;

        case '+':
           if(glebia<max_g) glebia++;
           break;

        case '-':
          if (glebia>0)
            {
                glebia--;
                
            }
            break;
        case 'a':    
            if(mode==1) { 
            obroc(x1, x4, yy1, yy4, angle3);
            obroc(x2, x4, yy2, yy4, angle3);
            obroc(x3, x4, yy3, yy4, angle3);
            }
            else if(mode==2) {
                  obroc(kx1, 0, ky1, 0, angle3);
                  obroc(kx2, 0, ky2, 0, angle3);
                  obroc(kx3, 0, ky3, 0, angle3);
                  obroc(kx4, 0, ky4, 0, angle3);      
                 }
            break ;
        case 'd':    
            obroc(x1, x4, yy1, yy4, 2*M_PI-angle3);
            obroc(x2, x4, yy2, yy4, 2*M_PI-angle3);
            obroc(x3, x4, yy3, yy4, 2*M_PI-angle3);
            break ;
         case 'w':    
            obroc(z1, z4, yy1, yy4, 2*M_PI-angle3);
            obroc(z2, z4, yy2, yy4, 2*M_PI-angle3);
            obroc(z3, z4, yy3, yy4, 2*M_PI-angle3);
            break ;
         case 's':    
            obroc(z1, z4, yy1, yy4, angle3);
            obroc(z2, z4, yy2, yy4, angle3);
            obroc(z3, z4, yy3, yy4, angle3);
            break ;
         case 'q':    
            obroc(z1, z4, x1, x4, angle3);
            obroc(z2, z4, x2, x4, angle3);
            obroc(z3, z4, x3, x4, angle3);
            break ;
         case 'e':    
            obroc(z1, z4, x1, x4, 2*M_PI-angle3);
            obroc(z2, z4, x2, x4, 2*M_PI-angle3);
            obroc(z3, z4, x3, x4, 2*M_PI-angle3);
            break ; 
         case '1':
             mode=1;
             max_g=7;
             glebia=0;
             break;
         case '2':
              mode=2;
              max_g=6;
              glebia=0;
              break ;            
    }

    glutPostRedisplay();
}

static void 
idle(void)
{
    glutPostRedisplay();
}
/*
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
*/
/* Program entry point */

int 
main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(800,600);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("Obroty figurowe :P");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glClearColor(0,0,0,1);
  //  glEnable(GL_CULL_FACE);
  //  glCullFace(GL_BACK);

  /*  glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
*/


    glutMainLoop();

    return EXIT_SUCCESS;
}