| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
2.1.3 Drawing in memory
The last way of MathGL using is the drawing in memory. Class mglGraphZB allows one to create a bitmap picture in memory. Further this picture can be displayed in window by some window libraries (like wxWidgets, FLTK, Windows GDI and so on). For example, the code for drawing in wxWidget library looks like:
void MyForm::OnPaint(wxPaintEvent& event)
{
int w,h,x,y;
GetClientSize(&w,&h); // size of the picture
mglGraphZB gr(w,h);
gr.Alpha(true); // draws something using MathGL
gr.Light(true); gr.Light(0,mglPoint(1,0,-1));
sample(&gr,NULL);
wxImage img(w,h,gr.GetBits(),true);
ToolBar->GetSize(&x,&y); // gets a height of the toolbar if any
wxPaintDC dc(this); // and draws it
dc.DrawBitmap(wxBitmap(img),0,y);
}
The drawing in other libraries is most the same.
For example, FLTK code will look like
void Fl_MyWidget::draw()
{
mglGraphZB gr(w(),h());
gr.Alpha(true); // draws something using MathGL
gr.Light(true); gr.Light(0,mglPoint(1,0,-1));
sample(&gr,NULL);
fl_draw_image(gr.GetBits(), x(), y(), gr.GetWidth(), gr.GetHeight(), 3);
}
Qt code will look like
void MyWidget::paintEvent(QPaintEvent *)
{
mglGraphZB gr(w(),h());
gr.Alpha(true); // draws something using MathGL
gr.Light(true); gr.Light(0,mglPoint(1,0,-1));
sample(&gr,NULL);
// Qt don't support RGB format as is. So, let convert it to BGRN.
const uchar *bb = gr.GetBits();
register long i, w=gr.GetWidth(), h=gr.GetHeight();
*buf = new uchar[4*w*h];
for(i=0;i<w*h;i++)
{
(*buf)[4*i] = bb[3*i+2];
(*buf)[4*i+1] = bb[3*i+1];
(*buf)[4*i+2] = bb[3*i];
(*buf)[4*i+3] = 255;
}
QPixmap pic = QPixmap::fromImage(QImage(*buf, w, h, QImage::Format_RGB32));
QPainter paint;
paint.begin(this); paint.drawPixmap(0,0,pic); paint.end();
delete []buf;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
