⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 好东西,自己看,要慢慢品味,适用于学生搞各种设计,有立体效果,效果特棒
💻 CPP
字号:
//------------------------------------------------------------------------------
#include "stdio.h"
#include <vcl.h>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"
double t1,t2,t3,t4,t10,t11,t12,t13;
int flag;
double T;
int move[7];
//int sign[7];
TMainForm *MainForm;



//------------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Sender):TForm(Sender)
{
  hdc = GetDC(Handle);

//下面初始化窗口中的Panel。
  Control_Zone->Top   = 0;     //-----------------------------------------------//
  Control_Zone->Left  = 3*ClientWidth/4+1;                                      //定义Panel(Control_Zone)的位置和大小。
  Control_Zone->Width =   ClientWidth/4-1;                                      //
  Control_Zone->Height=   ClientHeight;  //-------------------------------------//

  Application->OnIdle = IdleLoop;                                               //告诉程序空闲下来时候做什么。

  Left_Button_Down   = FALSE;
  key_and_mouse_down = FALSE;
  inclined           = FALSE;
  zoom               = 0.99;
  PI = 3.1415926;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormResize(TObject *Sender)                          //FormResize()是在窗口大小发生变化后必定执行的函数
{
  Control_Zone->Top   = 0;     //-----------------------------------------------//
  Control_Zone->Left  = 3*ClientWidth/4+1;                                      //定义Panel(Control_Zone)的位置和大小。
  Control_Zone->Width =   ClientWidth/4-1;                                      //
  Control_Zone->Height=   ClientHeight;  //-------------------------------------//

  float nRange = 50.0;         //-----------------------------------------------//
  w = 3*ClientWidth/4-1;                                                        //定义GL空间位置和大小。
  h =   ClientHeight;                                                           //
  if( h==0 ) h = 1;            //-----------------------------------------------//
  Elements._glSpace( w,h,nRange,1 );

  inclined           = TRUE;//FALSE;                                                   //如果坐标系已经倾斜了(0,45度,15度),就不要再倾斜了。
  key_and_mouse_down = FALSE;
}
void __fastcall TMainForm::IdleLoop(TObject*, bool& done)
{
  done = false;
  if( inclined==FALSE )
  {
    glRotatef(  15.0, 1.0, 0.0, 0.0 );
    glRotatef( -45.0, 0.0, 1.0, 0.0 );
    inclined = TRUE;
  }
  if( key_and_mouse_down==FALSE )
    glRotatef(-1, 0.0, 1.0, 0.0);

  Draw_All();
  SwapBuffers(hdc);
}
void __fastcall TMainForm::FormPaint( TObject *Sender )
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  glFlush();
}
void __fastcall TMainForm::FormDestroy( TObject *Sender )
{
  wglMakeCurrent(   NULL,NULL );
  wglDeleteContext( hrc       );
}
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
  hdc = GetDC( Handle );
  Elements.SetPixelFormatDescriptor(hdc,PixelFormat);

  hrc = wglCreateContext( hdc );
  if( hrc == NULL )                       ShowMessage("对不起,GL设备描述表是空的。");
  if( wglMakeCurrent(hdc, hrc) == false ) ShowMessage("对不起,场景建立不起来。");
  w = ClientWidth;
  h = ClientHeight;
  glEnable( GL_DEPTH_TEST );
  glEnable( GL_CULL_FACE  );
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  Elements.SetupLighting();
  Prepare_All();
}
void __fastcall TMainForm::FormMouseDown(TObject *Sender,TMouseButton Button, TShiftState Shift, int X, int Y)
{
  if( Button==mbLeft )
  {
    Left_Button_Down   = TRUE;
    key_and_mouse_down = TRUE;
    x                  = X;
    y                  = Y;
  }
  else if( Button==mbRight )
  {
    glLoadIdentity();
    glTranslatef(0,0,-150);
    glRotatef(  15.0, 1.0, 0.0, 0.0 );
    glRotatef( -45.0, 0.0, 1.0, 0.0 );
    inclined           = TRUE;

    Left_Button_Down   = FALSE;
    key_and_mouse_down = FALSE;
  }
  else
    Left_Button_Down   = FALSE;
}
void __fastcall TMainForm::FormMouseUp(TObject *Sender,TMouseButton Button, TShiftState Shift, int X, int Y)
{
  Left_Button_Down = FALSE;
}
void __fastcall TMainForm::FormMouseMove(TObject *Sender,TShiftState Shift, int X, int Y)
{
  if( Left_Button_Down==TRUE )
  {
    if( x != X )
        glRotatef( (X-x)/4, 0.0, 1.0, 0.0);
    if( y != Y )
        glRotatef( (Y-y)/4, 1.0, 0.0, 0.0);
    x = X;
    y = Y;
  }
}
void __fastcall TMainForm::FormMouseWheelDown(TObject *Sender,TShiftState Shift, TPoint &MousePos, bool &Handled)
{
  glScalef( zoom,zoom,zoom );
}
void __fastcall TMainForm::FormMouseWheelUp(TObject *Sender,TShiftState Shift, TPoint &MousePos, bool &Handled)
{
  glScalef( 1/zoom,1/zoom,1/zoom );
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////    以上程序无需改动      ///////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

void __fastcall TMainForm::Prepare_All()
{
  axials   = glGenLists(1);
  Elements._glAxials(   axials,  50,1,1,1,1,0,0 );



  cylinder = glGenLists(3);
  disk     = glGenLists(4);




  Elements._glDisk(     disk,    4.8,14,360,360,100,100,     1,1,1,16 );            //半反射面
  Elements._glDisk(     disk+1,    0,14,360,360,100,100,     0,154,126,16 );            //全反射面
  Elements._glCylinder( cylinder,    15,15,49          ,360,50,50,      1,1,1,0.6,true );            // 圆筒
  Elements._glCylinder( cylinder+1,  3.5,3.5,32/*长度*/,360,50,50,      50,0,0,1,  true );//红宝石
  Elements._glCylinder( cylinder+2, 4.4,3.5,36,360,50,50,      0.8,0.1,0.1,0.4,true );  //红光区域



  curve  = glGenLists(2);
  surface  = glGenLists(1);
  Calculate_Surface();
  Elements._glSurface(  surface,150,150,0.8,0.2,0.4,1,true );

  sphere   = glGenLists(6);
  Elements._glSphere(   sphere,1, 5,360,100,90,    255,0,0,1,true );
  Elements._glSphere(   sphere+1,1, 5,360,100,90,    255,0,0,1,true );
  Elements._glSphere(   sphere+2,1, 5,360,100,90,   255,0,0,1,true );
  Elements._glSphere(   sphere+3,1, 5,360,100,90,    255,0,0,1,true );
  Elements._glSphere(   sphere+4,1, 5,360,100,90,    255,0,0,1,true );
  Elements._glSphere(   sphere+5,1, 5,360,100,90,    255,0,0,1,true );


}

void __fastcall TMainForm::Draw_All()   //调整图象的位置
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

  glPushMatrix();
  glCallList( axials );
   glCallList( disk );     //碟:小面的大小,大面的大小,在XY面的偏转,开口度,边数 ,?
    glTranslatef(0,0,49);
     glCallList( disk+1 );

     glTranslatef(0,0,-49);
        glTranslatef(0,0,10);
        glCallList( disk+2 );

       glTranslatef(0,0,-10);
       glCallList( disk+3 );

  glCallList( cylinder );


glTranslatef(0,0,9);
glCallList( cylinder+1 );
glTranslatef(0,0,-9);
glTranslatef(0,0,-27);
 glCallList( cylinder+2);
 glTranslatef(0,0,27);

    glCallList( curve+1 );
    glCallList( curve) ;

    if(t<100)
    {

    glTranslatef( 0,3,0 );
    glCallList( curve );
    glTranslatef( 0,-6,0 );
    glCallList( curve+1 );
    glTranslatef( 0,3,0 );

    }
    else
    { state:
      if((t-100)*450<40)
      {
      int x=-(t-100)*450;
      glTranslatef( 0,3,x );
      glCallList( sphere );
      glTranslatef( 0,-3,-x );
      glTranslatef( 0,-3,x );
      glCallList( sphere+1 );
      glTranslatef( 0,3,-x );
      glTranslatef( 3,0,x-5 );
      glCallList( sphere+2 );
      glTranslatef( -3,0,-x+5 );
      glTranslatef( -3,0,x-5 );
      glCallList( sphere+3 );
      glTranslatef( 3,0,-x+5 );
      glTranslatef( 0,3,x-10 );
      glCallList( sphere+4 );
      glTranslatef( 0,-3,-x+10 );
      glTranslatef( 0,-3,x-10 );
      glCallList( sphere+5 );
      glTranslatef( 0,3,-x+10 );
      }
      else {t=100;goto state;}
    }








  glTranslatef(0,0,-5);
   glCallList( surface );
   glTranslatef(0,0,5);
  glPopMatrix();

  glFlush();
}



void __fastcall TMainForm::Calculate_Curve1()
{
  int n       = 81;//即为描出点个数
  float A=1 ;//振幅
  float c= 100.0     ; //光速
  float wave_n=3   ; //波数
  float wave_l=10   ; //波长
  float k=wave_n*wave_l/n;//每个绘出点的间隔
  float start_place=10;//起点位置
  float m=2*PI/wave_l ;
  float t1=t-start_point;

  for(int i=0;i<n;i++)
  {

    Elements.p_1d[i].z = k*i+start_place+c*t1;
    Elements.p_1d[i].y = -A*sin(m*k*i );
    Elements.p_1d[i].x = 0;
  }
  for(int i=0;i<n;i++)
   {
        if(Elements.p_1d[i].z>49)//the first turn point
       { Elements.p_1d[i].z=98-Elements.p_1d[i].z ;  }
   }
   for(int flag=0;flag<50;flag+=3)
  {
    if(t>2+flag)
    {for(int i=0;i<n;i++)Elements.p_1d[i].z = k*i+start_place+c*(t-2-flag);}

    for(int i=0;i<n;i++)
   {
        if(Elements.p_1d[i].z>49)//the first turn point
       { Elements.p_1d[i].z=98-Elements.p_1d[i].z ;  }
   }
  }


}
void __fastcall TMainForm::Calculate_Surface()          //螺线管
{
  int   m          = 150;
  int   n          = 150;
   int headandtail=20 ;/*留出一段圆柱体的长度*/

  float tube_length = 59;
  int   waveNumber = 3;

  float r; //   螺线管的管半径
  float R=9;
  for( int i=0;i<m;i++ )
  {
    r =1.2;
    for( int j=0;j<n;j++ )
    {
      Elements.p_2d[i][j].z = (i*tube_length)/n;
      Elements.p_2d[i][j].x = r*cos( j*2.0*PI/(n-1) );
      Elements.p_2d[i][j].y = r*sin( j*2.0*PI/(n-1) );
    }
    Elements.p_2d[i][n].z = Elements.p_2d[i][0].z;
    Elements.p_2d[i][n].x = Elements.p_2d[i][0].x;
    Elements.p_2d[i][n].y = Elements.p_2d[i][0].y;
  }
  for( int i=headandtail/*留出一段圆柱体*/;i<(m-headandtail);i++ )   //将圆柱变螺线管
  {
      for( int j=0;j<n;j++ )
    {                                                    //only be related to 'i'
      Elements.p_2d[i][j].x =Elements.p_2d[i][j].x+R*cos( i*waveNumber*2.0*PI/(m-1-2*headandtail) );
      Elements.p_2d[i][j].y =Elements.p_2d[i][j].y+R*sin( i*waveNumber*2.0*PI/(m-1-2*headandtail) );
    }

  }
  for( int i=0 ;i<headandtail;i++ )         // 将圆柱体与螺线管连接
  {
      for( int j=0;j<n;j++ )
    {
      Elements.p_2d[i][j].x =Elements.p_2d[headandtail][j].x;
      Elements.p_2d[i][j].y =Elements.p_2d[headandtail][j].y;

      Elements.p_2d[m-1-i][j].x =Elements.p_2d[headandtail][j].x;
      Elements.p_2d[m-1-i][j].y =Elements.p_2d[headandtail][j].y;
    }
  }

}
void __fastcall TMainForm::Calculate_Curve()    //要求此波反复振荡
{
  int n       = 81;//即为描出点个数
 // if(t>)
  float A=1 ;//振幅
  float c= 100.0     ; //光速
  float wave_n=3   ; //波数
  float wave_l=10   ; //波长
  float k=wave_n*wave_l/n;//每个绘出点的间隔
  float start_place=10;//起点位置
  float m=2*PI/wave_l ;
  float t1=t-start_point    ;

  for(int i=0;i<n;i++)
  {

    Elements.p_1d[i].z = k*i+start_place+c*t1;
    Elements.p_1d[i].y = A*sin(m*k*i );
    Elements.p_1d[i].x = 0;
  }
  for(int j=0;j<100;j++)
  {for(int i=0;i<n;i++)
   {
        if(Elements.p_1d[i].z>49 )//the first turn point
       { Elements.p_1d[i].z=98-Elements.p_1d[i].z ;   }
   }

     for(int i=0;i<n;i++)
   {
        if(Elements.p_1d[i].z<0)//the second turn point
       {Elements.p_1d[i].z=-Elements.p_1d[i].z ;  }
   }

   }



}


void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
  t+=0.01;
  float start_point=0.7  ;
  if (t>=start_point )
  {
  Calculate_Curve();
  Elements._glCurve(    curve,81,0,1,0 );
  Calculate_Curve1();
  Elements._glCurve(    curve+1,81,1,0,0 );
  }
  for (float j=0;j<40;j=j+1.5)
  {
  if(t>=(start_point+j-0.2)&&t<=(start_point+j-0.1))
  {
  Elements._glCylinder( cylinder,    15,15,49          ,360,50,50,      1,1,1,1,false );            // 圆筒
  Elements._glSurface(  surface,150,150,1,1,1,1,true );
  }
  if(t>=(start_point+j)&&t<=(start_point+j+0.1))
  {
   Elements._glCylinder( cylinder,    15,15,49          ,360,50,50,      1,1,1,0.6,true );            // 圆筒
   Elements._glSurface(  surface,150,150,0.8,0.2,0.4,1,true );
  }
  }

  Calculate_Curve();
  Elements._glCurve(    curve,81,255,0,0 );
  Calculate_Curve1();
  Elements._glCurve(    curve+1,81,255,0,0 );





        }
void __fastcall TMainForm::Button1Click(TObject *Sender)

{  static a;
  if(a%2==0)t=100;
  else t=0;
  a++ ;
}

//---------------------------------------------------------------------------









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -