Main Page Alphabetical List Compound List File List Compound Members Related Pages

# MDIGradient.cpp

    00001 //---------------------------------------------------------------------------
    00002
    00003 #include <vcl.h>
    00004 #pragma hdrstop
    00005
    00006 #include "MDIGradient.h"
    00007 #include "MDIEdit.h"
    00008 #include "MDIFrame.h"
    00009 //---------------------------------------------------------------------------
    00010 #pragma package(smart_init)
    00011 #pragma resource "*.dfm"
    00012 TGradientForm *GradientForm;
    00013 rgb *gradientview;
    00014
    00015 //---------------------------------------------------------------------------
    00016 __fastcall TGradientForm::TGradientForm(TComponent* Owner)
    00017         : TForm(Owner)
    00018 {
    00019 }
    00020 //---------------------------------------------------------------------------
    00021 void __fastcall TGradientForm::SetPixelFormatDescriptor()
    00022 {
    00023     PIXELFORMATDESCRIPTOR pfd = {
    00024         sizeof(PIXELFORMATDESCRIPTOR),
    00025         1,
    00026         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
    00027         PFD_TYPE_RGBA,
    00028         24,
    00029         0,0,0,0,0,0,
    00030         0,0,
    00031         0,0,0,0,0,
    00032         32,
    00033         0,
    00034         0,
    00035         PFD_MAIN_PLANE,
    00036         0,
    00037         0,0,
    00038     };
    00039     PixelFormat = ChoosePixelFormat(hdc, &pfd);
    00040     SetPixelFormat(hdc, PixelFormat, &pfd);
    00041 }
    00042
    00043 //---------------------------------------------------------------------
    00044 void __fastcall TGradientForm::FormClose(TObject *Sender, TCloseAction &Action)
    00045 {
    00046 Action=caFree;
    00047 FrameForm->GradientSlices->Checked = false;
    00048 delete [] gradientview;
    00049 memusage = memusage - 3*512*512;
    00050 FrameForm->memupdate();
    00051 wglDeleteContext(hrc);
    00052 ReleaseDC(Handle,hdc);
    00053 }
    00054 //---------------------------------------------------------------------
    00055 void __fastcall TGradientForm::FormCreate(TObject *Sender)
    00056 {
    00057     int i,a;
    00058
    00059     gradientview = new rgb[512*512];
    00060     memusage = memusage + 3*512*512;
    00061     FrameForm->memupdate();
    00062
    00063     hdc = GetDC(Handle);
    00064     SetPixelFormatDescriptor();
    00065     hrc = wglCreateContext(hdc);
    00066     if(hrc == NULL) ShowMessage("Could not create DC!");
    00067     wglMakeCurrent(hdc, hrc);
    00068
    00069     w = ClientWidth;
    00070     h = ClientHeight;
    00071
    00072     glEnable(GL_DEPTH_TEST);
    00073     glEnable(GL_CULL_FACE);
    00074
    00075     glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    00076     glOrtho (-50.0,50.0,-50.0,50.0,-50.0,50.0);
    00077
    00078     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    00079     glFlush();
    00080     SwapBuffers(hdc);
    00081     FormResize(Sender);
    00082     pagesize = sizeX*sizeY;
    00083         for(int a = 0; a<sizeY; a+=1) {
    00084         for(int i = 0; i<sizeX; i+=1) {
    00085                 gradientview[a*sizeX+i].r = gradientview[a*sizeX+i].g = gradientview[a*sizeX+i].b = absgrad[pagesize*TrackBar->Position+a*sizeX+i]/16;
    00086                 //StatusProgress->Position = 100*(float(i)/float(size))+1;
    00087         }
    00088         }
    00089     TrackBar->Max = sizeZ-1;
    00090 }
    00091 //---------------------------------------------------------------------
    00092 void __fastcall TGradientForm::FormResize(TObject *Sender)
    00093 {
    00094         tPanel->Height = ClientHeight-34;
    00095         TrackBar->Height = ClientHeight-36;
    00096         //kontrast->Height = ClientHeight-188;
    00097         //kontrastpanel->Height = ClientHeight-221;
    00098         PrepareGLWindow();
    00099         PaintGL();
    00100 }
    00101 //---------------------------------------------------------------------------
    00102 void __fastcall TGradientForm::FormPaint(TObject *Sender)
    00103 {
    00104         PaintGL();
    00105 }
    00106 //---------------------------------------------------------------------------
    00107 void __fastcall TGradientForm::PaintGL()
    00108 {
    00109         float i;
    00110
    00111         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    00112         PrepareGLWindow();
    00113         glColor3f(0.6f,0.6f,0.6f);
    00114         glLineWidth(3.0f);
    00115         glBegin(GL_LINE_STRIP);
    00116         glVertex3f(-50,-50,0);
    00117         glVertex3f(50,-50,0);
    00118         glVertex3f(50,50,0);
    00119         glVertex3f(-50,50,0);
    00120         glVertex3f(-50,-50,0);
    00121         glEnd();
    00122
    00123
    00124 //        glDrawPixels(sizeX, sizeY, GL_RGB, GL_UNSIGNED_BYTE,
    00125 //                     color+(sizeX*sizeY*TrackBar->Position));
    00126                         // we use unsigned byte (char) to meet speed issues.
    00127                         // 16 bit data (__int16) slows down drawing to much.
    00128
    00129 if (RadioButton1->Checked) glDrawPixels(sizeX, sizeY, GL_RGB, GL_UNSIGNED_BYTE, gradientview);
    00130 if (RadioButton2->Checked) glDrawPixels(sizeX, sizeZ, GL_RGB, GL_UNSIGNED_BYTE, gradientview);
    00131 if (RadioButton3->Checked) glDrawPixels(sizeY, sizeZ, GL_RGB, GL_UNSIGNED_BYTE, gradientview);
    00132
    00133         glFlush();
    00134         SwapBuffers(hdc);
    00135 }
    00136
    00137 void __fastcall TGradientForm::PrepareGLWindow()
    00138 {
    00139         wglMakeCurrent(hdc, hrc);
    00140         float yrat;
    00141         GLfloat nRange = 50.0;
    00142
    00143         w = ClientWidth-200;
    00144         h = ClientHeight;
    00145
    00146         if(h == 0) h = 1;
    00147         if(w == 0) w = 1;
    00148
    00149         glViewport(200, 0, w, h);
    00150
    00151         glMatrixMode(GL_PROJECTION);
    00152         glLoadIdentity();
    00153
    00154 if (RadioButton1->Checked) {
    00155         yrat = (float)sizeX/sizeY;
    00156         if (w <= h*yrat)
    00157         {        glOrtho (-nRange, nRange, -nRange*h*yrat/w, nRange*h*yrat/w, -nRange, nRange);
    00158                 glPixelZoom((float)w/(float)sizeX, (float)w/((float)sizeY*yrat));}
    00159          else
    00160         {        glOrtho (-nRange*w/(h*yrat), nRange*w/(h*yrat), -nRange, nRange, -nRange, nRange);
    00161                 glPixelZoom((float)h*yrat/(float)sizeX, (float)h/((float)sizeY));}
    00162 }
    00163 if (RadioButton2->Checked) {
    00164         yrat = (float)sizeX/sizeZ;
    00165         if (w <= h*yrat)
    00166         {        glOrtho (-nRange, nRange, -nRange*h*yrat/w, nRange*h*yrat/w, -nRange, nRange);
    00167                 glPixelZoom((float)w/(float)sizeX, (float)w/((float)sizeZ*yrat));}
    00168          else
    00169         {        glOrtho (-nRange*w/(h*yrat), nRange*w/(h*yrat), -nRange, nRange, -nRange, nRange);
    00170                 glPixelZoom((float)h*yrat/(float)sizeX, (float)h/(float)sizeZ);}
    00171 }
    00172 if (RadioButton3->Checked) {
    00173         yrat = (float)sizeY/sizeZ;
    00174         if (w <= h*yrat)
    00175         {        glOrtho (-nRange, nRange, -nRange*h*yrat/w, nRange*h*yrat/w, -nRange, nRange);
    00176                 glPixelZoom((float)w/(float)sizeY, (float)w/((float)sizeZ*yrat));}
    00177          else
    00178         {        glOrtho (-nRange*w/(h*yrat), nRange*w/(h*yrat), -nRange, nRange, -nRange, nRange);
    00179                 glPixelZoom((float)h*yrat/(float)sizeY, (float)h/(float)sizeZ);}
    00180 }
    00181
    00182         glRasterPos2d(-50,-50);
    00183         glMatrixMode(GL_MODELVIEW);
    00184         glLoadIdentity();
    00185 }
    00186 //---------------------------------------------------------------------------
    00187
    00188 void __fastcall TGradientForm::FormActivate(TObject *Sender)
    00189 {
    00190         wglMakeCurrent(hdc, hrc);
    00191         PaintGL();
    00192 }
    00193 //---------------------------------------------------------------------------
    00194
    00195 void __fastcall TGradientForm::TrackBarChange(TObject *Sender)
    00196 {
    00197 if (RadioButton1->Checked)
    00198 {
    00199         for(int a = 0; a<sizeY; a+=1) {
    00200         for(int i = 0; i<sizeX; i+=1) {
    00201                 gradientview[a*sizeX+i].r = gradientview[a*sizeX+i].g = gradientview[a*sizeX+i].b = absgrad[pagesize*TrackBar->Position+a*sizeX+i]/16;
    00202         }
    00203         }
    00204 }
    00205 if (RadioButton2->Checked)
    00206 {
    00207         for(int a = 0; a<sizeZ; a+=1) {
    00208         for(int i = 0; i<sizeX; i+=1) {
    00209                 gradientview[a*sizeX+i].r = gradientview[a*sizeX+i].g = gradientview[a*sizeX+i].b = absgrad[pagesize*a+TrackBar->Position*sizeX+i]/16;
    00210         }
    00211         }
    00212 }
    00213 if (RadioButton3->Checked)
    00214 {
    00215         for(int a = 0; a<sizeZ; a+=1) {
    00216         for(int i = 0; i<sizeY; i+=1) {
    00217                 gradientview[a*sizeY+i].r = gradientview[a*sizeY+i].g = gradientview[a*sizeY+i].b = absgrad[TrackBar->Position+i*sizeX+a*pagesize]/16;
    00218         }
    00219         }
    00220 }
    00221 lslice->Caption = IntToStr(TrackBar->Position) + " / " + IntToStr(TrackBar->Max);
    00222 PaintGL();
    00223
    00224 }
    00225 //---------------------------------------------------------------------------
    00226
    00227 void __fastcall TGradientForm::RadioButton1Click(TObject *Sender)
    00228 {
    00229 TrackBar->Max = sizeZ-1;
    00230 TrackBarChange(this);
    00231 }
    00232 //---------------------------------------------------------------------------
    00233
    00234 void __fastcall TGradientForm::RadioButton2Click(TObject *Sender)
    00235 {
    00236 TrackBar->Max = sizeY-1;
    00237 TrackBarChange(this);
    00238 }
    00239 //---------------------------------------------------------------------------
    00240
    00241 void __fastcall TGradientForm::RadioButton3Click(TObject *Sender)
    00242 {
    00243 TrackBar->Max = sizeX-1;
    00244 TrackBarChange(this);
    00245 }
    00246 //---------------------------------------------------------------------------
    00247