Суббота, 11.05.2024, 16:14
Главная Регистрация RSS
Приветствую Вас, Гость
Меню сайта
Категории раздела
Форма входа
ДРУЗЬЯ САЙТА

Вызвать мастера в Красноярске

8 983 207 5474

8 902 918 9334

8 933 332 3164

---------------------------------

Запчасти бытткхники

  • Велес т.2935600
  • СЦ Близнецов т.2296595
  • Вираж
  • Красраб 110 т.2589503
  • Комплекстур Щорса30 т.2606797
  • Радиодетали

  • Якорный 9 т.2688317
  • Воронова 16 т.2202990
  • Красраб 125а т.2456544
  • Профи т.2702737
  • Дайте две т.+79237713323
  • Электрика

  • Электро +
  • Планета электрика
  • ЗАКЛАДКИ
    Поиск
    Статистика

    Онлайн всего: 1
    Гостей: 1
    Пользователей: 0
    Главная » Статьи » C++ Builder » OpenGL

    Броуновское движение OpenGL c++ builder 6

    Броуновское движение OpenGL c++ builder 6

    Исходник и исполняемый файл здесь

    =

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

    #include <vcl.h>
    #pragma hdrstop

    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
       Application->OnIdle = IdleLoop;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::IdleLoop(TObject*, bool& done)
    {
     //   done = false;
     //   Draw();
    }
    //------------------------------------------------------------------------
    void __fastcall TForm1::Draw(void)
    {
      //
    }
    //-----------------------------------------------------------------------------
    void __fastcall TForm1::fMy_metod(void)
    {
      //
    }
    //---------------------------------------------------------------------------
    BOOL __fastcall TForm1::bSetupPixelFormat(HDC hdc)
    {

              PIXELFORMATDESCRIPTOR pfd, *ppfd;
              int pixelformat;
              ppfd = &pfd;
              ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
              ppfd->nVersion = 1;
              ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
              ppfd->dwLayerMask = PFD_MAIN_PLANE;
              ppfd->iPixelType = PFD_TYPE_RGBA;
              ppfd->cColorBits = 16;
              ppfd->cDepthBits = 16;
              ppfd->cAccumBits = 0;
              ppfd->cStencilBits = 0;
              if ((pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0)
              {
                  MessageBox(NULL, "ChoosePixelFormat failed", "Error", 
            MB_OK);
                  return FALSE;
              }
              if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
              {
                  MessageBox(NULL, "SetPixelFormat failed", "Error", 
            MB_OK);
                  return FALSE;
              }
              return TRUE;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
    {
        if(ghRC)
            {
              wglMakeCurrent(ghDC,0);
              wglDeleteContext(ghRC);
            }
            if(ghDC)
              ReleaseDC(Handle, ghDC);

           for(int i=0;i<Balls->Count ;i++){
             delete  Balls->Items[i] ;
           }

           delete Balls;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
            Balls=new TList ;

             n=150;
             r=1;
             M=1;
             sh=true;
            randomize;

            ghDC = GetDC(Handle);
             if (!bSetupPixelFormat(ghDC))
                Close();
             ghRC = wglCreateContext(ghDC);
             wglMakeCurrent(ghDC, ghRC);
             RandomBall();
             glClearColor(0.0, 0.0, 0.0, 0.0);
             glEnable(GL_COLOR_MATERIAL);
             glEnable(GL_DEPTH_TEST);
             glEnable(GL_LIGHTING);
             glEnable(GL_LIGHT0);
               float p[4]={3,3,3,1},
                     d[3]={-1,-1,-3};
             glLightfv(GL_LIGHT0,GL_POSITION,p);
             glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,d);
                          
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormResize(TObject *Sender)
    {
        glViewport(0, 0,Width, Height );
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-5,5, -5,5, 2,12);//
        gluLookAt(0,0,5, 0,0,0, 0,1,0); //
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        InvalidateRect(Handle, 0, false);
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::RandomBall(void)
    {
       int i;   int j;  double L; int p=0;
       TBall* ballz;
    //  randomize;
     for( i=0;i<= n; i++){
        ballz=0;
        ballz=new TBall;
        ballz->x=(double)random(100)/10-5+random(100)/100;
        ballz->y=(double)random(80)/10-4+random(100)/100;

           for( j= 0; j< Balls->Count;j++ )
            {

               ball4=(TBall*) Balls->Items[j] ;
    L= ((ball4->x - ballz->x)*(ball4->x - ballz->x )) + ((ball4->y - ballz->y)*(ball4->y - ballz->y)) ;
              if (sqrt(L) <= (0.2 + 0.2)){ i=i-1; goto g;}
            }
        if(p==0){  p=1;
        ballz->Vx= (double)random(50)/500+random(100)/500;//
        ballz->Vy= (double)random(50)/500+random(100)/500;//
                 }
        else {    p=0;
        ballz->Vx= (double)random(50)/500*(-1)+random(100)/500;//
        ballz->Vy= (double)random(50)/500*(-1)+random(100)/500;//
        }
        ballz->id=i;
        Balls->Add(ballz);
       g:
      }

    }
    //----------------------------------------------------------------------
    void __fastcall  TForm1::Strike(TBall *ball1,TBall *ball2)
    {
       double Vcmx,Vcmy;

        Vcmx=(ball1->Vx * M + ball2->Vx * M) / (M + M);
        Vcmy=(ball1->Vy * M + ball2->Vy * M) / (M + M);

        ball1->Vx=2 * Vcmx - ball1->Vx;
        ball1->Vy=2 * Vcmy - ball1->Vy;
        ball2->Vx=2 * Vcmx - ball2->Vx;
        ball2->Vy=2 * Vcmy - ball2->Vy;
    if (sqrt((ball2->x - ball1->x) * (ball2->x - ball1->x) +
    (ball2->y - ball1->y) * (ball2->y - ball1->y)) <= 0.2 + 0.2)
           {
            ball1->x= ball1->x + ball1->Vx * 0.06;
            ball1->y= ball1->y + ball1->Vy * 0.1;
            ball2->x= ball2->x + ball2->Vx * 0.06;
            ball2->y= ball2->y + ball2->Vy * 0.1;
          }

    }
    //----------------------------------------------------------------------------
    void __fastcall TForm1::BallDraw(double x,double y)
    {

      if(sh){
      GLUquadricObj *quadObj;//
      quadObj=gluNewQuadric(); //
      gluQuadricDrawStyle(quadObj, GLU_FILL);//
      glColor3f(1,0,1);
      glTranslatef(x,y,-0);
      gluSphere(quadObj, 0.2,20,20);//
      glTranslatef(-x,-y,0);
      gluDeleteQuadric(quadObj); //
              }
                  
       else {
              glColor3f(0,1,0);
              glPointSize(20);    //
              glEnable(GL_POINT_SMOOTH);//
              glBegin(GL_POINTS);
              glVertex2f(x,y);
              glEnd();
             }
    }
    //-------------------------------------------------------------


    void __fastcall TForm1::FormPaint(TObject *Sender)
    {
     int   i,j;
     double  L;

     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glClearColor(0,0.1,0.2,1);

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

          ball=(TBall*) Balls->Items[i] ;
          BallDraw(ball->x,ball->y);
          ball->x=ball->x+ball->Vx;
          ball->y=ball->y+ball->Vy;

            if (ball->x <= -5.8+r){    // левая
            if (ball->Vx<0)
              ball->Vx=ball->Vx*=-1;}

          if (ball->x >=5.6-r){        //правая
            if (ball->Vx>0)
              ball->Vx=ball->Vx*=-1; }

          if (ball->y <= -5.8+r){     // нижняя
            if (ball->Vy<0)
              ball->Vy=ball->Vy*=-1; }

          if (ball->y >=5-r){       // верхняя
            if (ball->Vy>0)
              ball->Vy=ball->Vy*=-1; }

          if (i<n) {
          for( j= i+1; j<= Balls->Count-1;j++ )
            {

               ball2=(TBall*) Balls->Items[j] ;
    L= ((ball2->x - ball->x)*(ball2->x - ball->x )) + ((ball2->y - ball->y)*(ball2->y - ball->y)) ;
              if (sqrt(L) <= (0.2 + 0.2))
                 Strike(ball,ball2);
            }
            }
        }

      SwapBuffers(ghDC);

    }
    //---------------------------------------------------------------------------

    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
        Form1->Resize();

    }
    //---------------------------------------------------------------------------

    void __fastcall TForm1::N1Click(TObject *Sender)
    {
        float p[4]={3,3,3,1};
        glLightfv(GL_LIGHT0,GL_POSITION,p);
       sh=true;     
    }
    //---------------------------------------------------------------------------

    void __fastcall TForm1::N2Click(TObject *Sender)
    {
        float p[4]={3,5,0,0};
        glLightfv(GL_LIGHT0,GL_POSITION,p);
       sh=false;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::N51Click(TObject *Sender)
    {
       Timer1->Interval=5;
    }
    //---------------------------------------------------------------------------

    void __fastcall TForm1::N102Click(TObject *Sender)
    {
       Timer1->Interval=10;
    }
    //---------------------------------------------------------------------------

    void __fastcall TForm1::N201Click(TObject *Sender)
    {
       Timer1->Interval=20;      
    }
    //---------------------------------------------------------------------------

    void __fastcall TForm1::N1002Click(TObject *Sender)
    {
        Timer1->Interval=100;     
    }
    //---------------------------------------------------------------------------

    =

    =

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

    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    #include <ExtCtrls.hpp>
    #include <ComCtrls.hpp>
    #include <Menus.hpp>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <math.h>
    #include <Math.hpp>

    //-------------------------------------------------------------
    class TBall
     {
     public:
     double x, y;
     double Vx, Vy;
     int id;

     };
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:    // IDE-managed Components
            TTimer *Timer1;
            TPopupMenu *PopupMenu1;
            TMenuItem *N1;
            TMenuItem *N2;
            TMenuItem *N4;
            TMenuItem *N51;
            TMenuItem *N102;
            TMenuItem *N201;
            TMenuItem *N1002;
            void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
            void __fastcall FormCreate(TObject *Sender);
            void __fastcall FormResize(TObject *Sender);
            void __fastcall FormPaint(TObject *Sender);
            void __fastcall Timer1Timer(TObject *Sender);
            void __fastcall N1Click(TObject *Sender);
            void __fastcall N2Click(TObject *Sender);
            void __fastcall N51Click(TObject *Sender);
            void __fastcall N102Click(TObject *Sender);
            void __fastcall N201Click(TObject *Sender);
            void __fastcall N1002Click(TObject *Sender);
    private:    // User declarations
    public:        // User declarations
           HGLRC ghRC;
           HDC   ghDC;
           void __fastcall fMy_metod(void);
           void __fastcall Draw(void);
           void __fastcall IdleLoop(TObject*, bool&);
           BOOL __fastcall bSetupPixelFormat(HDC hdc);

    //      TBall *TPBall;
          TList  *Balls;
          TBall *ball,*ball2, *ball4;
          int n;
          double r ;
          int M, vz;
          bool sh;
          void __fastcall BallDraw(double x,double y);
          void __fastcall Strike(TBall *ball1,TBall *ball2);
          void __fastcall RandomBall(void);

            __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
     

    =

    =

    Категория: OpenGL | Добавил: doka (16.09.2020)
    Просмотров: 552 | Рейтинг: 0.0/0
    Добавлять комментарии могут только зарегистрированные пользователи.
    [ Регистрация | Вход ]