Кривая Леви c++ builder 6 OpenGl
Есть реализация без OpenGL с компонентом Image
Исходник и исполняемый файл здесь
=
=
=
=
=
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__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(Panel1->Handle);
if (!bSetupPixelFormat(ghDC))
Close();
ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
glClearColor(0.0, 0.0, 0.0, 0.0);
FormResize(Sender);
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)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glViewport(0, 0,Panel1->Width, Panel1->Height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if( RadioButton1->Checked ) gluOrtho2D(0, Panel1->Width, 0, Panel1->Height);
if( RadioButton2->Checked ) { glOrtho(-5,5, -5,5, 2,12);
gluLookAt(0,0,5, 0,0,0, 0,1,0); }
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(ghRC)
{
wglMakeCurrent(ghDC,0);
wglDeleteContext(ghRC);
}
if(ghDC)
ReleaseDC(Handle, ghDC);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Draw(void)
{
Form1->FormResize(Form1 );
int n=TrackBar1->Position;
Label1->Caption=n;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINES);
if( RadioButton2->Checked ){ drowLevy(2, 0.0, -2, 0.0, n);
drowLevy(-2, 0.0, 2, 0.0, n);}
if( RadioButton1->Checked ) drowLevy(170, 150, 460, 150, n); //
glEnd();
SwapBuffers(ghDC);
}
//-----------------------------------------------------------------------------
void __fastcall TForm1::drowLevy(double x1, double y1, double x2, double y2, int i)
{
if(i == 0) {
// glColor3d (2*x2, 1*x2, 2*x2);
glColor3d(3+x1 * 1, 4+i * 4, x2 * 2);
// glColor3f(10,10 ,10 );
glVertex2d(x1, y1); //
glVertex2d(x2, y2); //
}
else {
double x3 = (x1 + x2) / 2 - (y2 - y1) / 2; //
double y3 = (y1 + y2) / 2 + (x2 - x1) / 2; //
drowLevy(x1, y1, x3, y3, i - 1);
drowLevy(x3, y3, x2, y2, i - 1);
}
}
//-------------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Draw();
}
//---------------------------------------------------------------------------
=
=
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <ComCtrls.hpp>
#include <GL/gl.h>
#include <GL/glu.h>
//#include <GL/glaux.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TTimer *Timer1;
TPanel *Panel1;
TPanel *Panel2;
TTrackBar *TrackBar1;
TRadioButton *RadioButton1;
TRadioButton *RadioButton2;
TLabel *Label1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormResize(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall Timer1Timer(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 drowLevy(double x1, double y1, double x2, double y2, int i);
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
=
=
=
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::drowLevy(double x1, double y1, double x2, double y2, int i)
{
int k=500;
if(i == 0) {
// Image1->Canvas->Pen->Color=RGB(5,x1*2 ,y1*2 );
Image1->Canvas->Pen->Color=RGB(0, 0 ,0 );
Image1->Canvas->MoveTo(x1, k-y1);
Image1->Canvas->LineTo(x2, k-y2);
}
else {
double x3 = (x1 + x2) / 2 - (y2 - y1) / 2; //
double y3 = (y1 + y2) / 2 + (x2 - x1) / 2; //
drowLevy(x1, y1, x3, y3, i - 1);
drowLevy(x3, y3, x2, y2, i - 1);
}
}
//--------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
int n=TrackBar1->Position;
Label1->Caption=n;
Image1->Canvas->FillRect(Image1->Canvas->ClipRect );
drowLevy(170, 150, 460, 150, n);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
BitBtn1Click(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
BitBtn1Click(Sender);
}
//---------------------------------------------------------------------------
=
|