Фрактал биоморф на OpenGL c++ builder 6 под windows 10
Касс комплексных чисел из STL
Исходник и исполняемый файл здесь
=
ЦВЕТ СЛУЧАЙНЫЙ
=
ЦВЕТ ПО ИНДЕКСУ
=
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define WIDTH 800
#define HEIGHT 640
#define RANGE 80
#define MAX 50
#define INDEX 0.01
#define RE_C 1.07
#define IM_C 0.0001
TForm1 *Form1;
using namespace std;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
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::FormCreate(TObject *Sender)
{
ghDC = GetDC(Handle);
if (!bSetupPixelFormat(ghDC))
Close();
ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
glClearColor(0.0, 0.0, 0.0, 0.0);
FormResize(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(ghRC)
{
wglMakeCurrent(ghDC,0);
wglDeleteContext(ghRC);
}
if(ghDC)
ReleaseDC(Handle, ghDC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
glClearColor ( 0.0, 0.0, 0.0, 0.0 );
glPointSize ( 4.0 );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ();
gluOrtho2D ( -WIDTH / 2, WIDTH / 2, -HEIGHT / 2, HEIGHT / 2 );
Draw();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::draw_dot ( int x, int y, GLubyte *clr ) {
glColor3ub (clr[0]*2, clr[1]*2, clr[2]*2);
glBegin ( GL_POINTS );
glVertex2i ( x, y );
glEnd ();
}
//=========================================================================
void __fastcall TForm1::draw_biomorph (void) {
complex <double> z; //
//
const complex <double> constant( RE_C, IM_C );
double re_z, im_z; //
//
GLubyte clr_black[] = { 0, 0, 0 }, new_clr[3];
srand ( time (0) );
for (register int x = -WIDTH / 2; x < WIDTH / 2; x++ ) {
for (register int y = -HEIGHT / 2; y < HEIGHT / 2; y++ ) {
z._M_re= x * INDEX;
z._M_im= y * INDEX ;
re_z = fabs ( z.real () );
im_z = fabs ( z.imag () );
register int i;
for (i = 0; ( re_z < RANGE || im_z < RANGE || abs(z) < RANGE ) && i < MAX; i++ )
{
z = pow ( z, 3 ) + constant;
re_z = fabs ( z.real () );
im_z = fabs ( z.imag () );
}
if ( re_z < MAX || im_z < MAX * 50 ) {
// new_clr[0] = rand() % 255;
// new_clr[1] = rand() % 255;
// new_clr[2] = rand() % 255;
if(i==2){new_clr[0] =255;new_clr[1] =0;new_clr[2] =0; }
if(i==3){new_clr[0] =0;new_clr[1] =255;new_clr[2] =0; }
if(i==4){new_clr[0] =0;new_clr[1] =0;new_clr[2] =255; }
if(i==5){new_clr[0] =0;new_clr[1] =255;new_clr[2] =255; }
if(i==6){new_clr[0] =255;new_clr[1] =0;new_clr[2] =255; }
if(i==7){new_clr[0] =255;new_clr[1] =255;new_clr[2] =0; }
if(i==8){new_clr[0] =200;new_clr[1] =0;new_clr[2] =150; }
draw_dot ( x, y, new_clr );
}
else
draw_dot( x, y, clr_black );
}
}
glFlush();
}
//----------------------------------------------------------------------------
void __fastcall TForm1::Draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_biomorph ();
SwapBuffers(ghDC);
}
//-------------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
// Draw();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
Draw();
}
//---------------------------------------------------------------------------
=
=
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Buttons.hpp>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <complex>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TTimer *Timer1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall FormResize(TObject *Sender);
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall FormPaint(TObject *Sender);
private: // User declarations
public: // User declarations
HGLRC ghRC;
HDC ghDC;
void __fastcall Draw(void);
void __fastcall IdleLoop(TObject*, bool&);
BOOL __fastcall bSetupPixelFormat(HDC hdc);
void __fastcall draw_dot ( int x, int y, GLubyte *clr );
void __fastcall draw_biomorph (void);
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
=
=
|