00001
00002
00003
00004
00005
00006 #include "OpenGLView.h"
00007
00008 #ifdef HAVE_CONFIG_H
00009 #include "config.h"
00010 #else
00011 #ifdef _MSC_VER
00012 #include "msdevstudio/MSconfig.h"
00013 #endif
00014 #endif
00015
00016 #include "graphics/Color.h"
00017 #include "plotters/CompositePlotter.h"
00018 #include "plotters/PlotterBase.h"
00019 #include "graphics/FontBase.h"
00020
00021 #include "TextTTF.h"
00022 #include "OpenGL.h"
00023
00024 #include <iostream>
00025
00026 #ifdef SSTREAM_DEFECT
00027 #include <strstream>
00028 #else
00029 #include <sstream>
00030 #endif
00031
00032
00033
00034 using namespace hippodraw;
00035
00036 OpenGLView::OpenGLView ()
00037 :m_TTF(0)
00038 {
00039 m_draw_rect.setRect(0,0,100,100);
00040 m_TTF = new hippodraw::TextTTF;
00041 }
00042
00043
00044 OpenGLView::~OpenGLView()
00045 {
00046 delete m_TTF;
00047 }
00048
00049 void OpenGLView::setRect(double aX, double aY, double aW, double aH)
00050 {
00051 m_draw_rect.setRect(aX,aY,aW,aH);
00052 }
00053
00054
00055 void OpenGLView::setPlotter ( PlotterBase * plotter )
00056 {
00057 ViewBase::setPlotter ( plotter );
00058 plotter -> addObserver ( this );
00059 }
00060
00061 PlotterBase* OpenGLView::getPlotter () const
00062 {
00063 return ViewBase::getPlotter ();
00064 }
00065
00066 void OpenGLView::initPlot(double)
00067 {
00068
00069 }
00070
00071 void OpenGLView::endPlot()
00072 {
00073
00074 }
00075
00076 void OpenGLView::drawLines ( const std::vector< double > & x,
00077 const std::vector< double > & y,
00078 hippodraw::Line::Style style,
00079 const Color & color ,float)
00080 {
00081 #ifdef DEBUG_GL
00082 std::cout << " debug : drawLines " << std::endl;
00083 #endif
00084
00085 float r = (float)(color.getRed() / 255.0);
00086 float g = (float)(color.getGreen() / 255.0);
00087 float b = (float)(color.getBlue() / 255.0);
00088
00089 glColor3f(r,g,b);
00090
00091 #ifdef DEBUG_GL
00092 std::cout << "debug : drawLines : color "
00093 << " r : " << r
00094 << " g : " << g
00095 << " b : " << b
00096 << std::endl;
00097 #endif
00098
00099 switch (style)
00100 {
00101 case Line::Solid:
00102 break;
00103 case Line::Dot:
00104 break;
00105 case Line::Dash:
00106 break;
00107 case Line::DashDot:
00108 break;
00109 default:
00110 break;
00111 }
00112
00113 for ( unsigned int i = 0; i < x.size(); i = i+2 ) {
00114 glBegin (GL_LINES);
00115 glVertex3f (toViewX(x[i]),toViewY(y[i]),0 );
00116 glVertex3f (toViewX(x[i+1]),toViewY(y[i+1]),0 );
00117 glEnd ();
00118 #ifdef DEBUG_GL
00119 std::cout << "debug : " << x[i] << " " << y[i] << std::endl;
00120 std::cout << "debug : " << x[i+1] << " " << y[i+1] << std::endl;
00121 #endif
00122 }
00123 }
00124
00125 void
00126 OpenGLView::
00127 drawColorLines ( const std::vector< double > & x,
00128 const std::vector< double > & y,
00129 hippodraw::Line::Style style,
00130 const std::vector< Color > & colors,
00131 float )
00132 {
00133 #ifdef DEBUG_GL
00134 std::cout << "hippodraw::OpenGLView::drawColorLines :" << std::endl;
00135 #endif
00136
00137 for (unsigned int i = 0; i < x.size(); i+=2 ){
00138
00139 const Color & color = colors[i];
00140 float r = color.getRed () / 255.0;
00141 float g = color.getGreen () / 255.0;
00142 float b = color.getBlue () / 255.0;
00143
00144 glColor3f(r,g,b);
00145
00146
00147
00148 switch (style) {
00149 case Line::Solid:
00150 break;
00151 case Line::Dot:
00152 break;
00153 case Line::Dash:
00154 break;
00155 case Line::DashDot:
00156 break;
00157 default:
00158 break;
00159 }
00160
00161 glBegin (GL_LINES);
00162 glVertex3f (toViewX(x[i]),toViewY(y[i]),0 );
00163 glVertex3f (toViewX(x[i+1]),toViewY(y[i+1]),0 );
00164 glEnd ();
00165
00166 }
00167 }
00168
00169 void OpenGLView::drawViewLines (
00170 const std::vector< double > & x
00171 ,const std::vector< double > & y
00172 ,hippodraw::Line::Style style
00173 ,bool
00174 ,float
00175 )
00176 {
00177 #ifdef DEBUG_GL
00178 std::cout << " debug : drawViewLines" << std::endl;
00179 #endif
00180 glColor3f(0,0,0);
00181
00182 switch (style) {
00183 case Line::Solid:
00184 break;
00185 case Line::Dot:
00186 break;
00187 case Line::Dash:
00188 break;
00189 case Line::DashDot:
00190 break;
00191 default:
00192 break;
00193 }
00194
00195 for ( unsigned int i = 0; i < x.size(); i = i+2 ) {
00196 glBegin (GL_LINES);
00197 glVertex3f (toX(x[i]),toY(y[i]),0 );
00198 glVertex3f (toX(x[i+1]),toY(y[i+1]),0 );
00199 glEnd ();
00200
00201
00202 }
00203 }
00204
00205 void OpenGLView::drawViewLines ( const std::vector< double > & x,
00206 const std::vector< double > & y,
00207 hippodraw::Line::Style style,
00208 const Color & color ,float)
00209 {
00210 #ifdef DEBUG_GL
00211 std::cout << " debug : drawViewLines 2" << std::endl;
00212 #endif
00213
00214 float r = (float)(color.getRed() / 255.0);
00215 float g = (float)(color.getGreen() / 255.0);
00216 float b = (float)(color.getBlue() / 255.0);
00217
00218 glColor3f(r,g,b);
00219
00220 switch (style) {
00221 case Line::Solid:
00222 break;
00223 case Line::Dot:
00224 break;
00225 case Line::Dash:
00226 break;
00227 case Line::DashDot:
00228 break;
00229 default:
00230 break;
00231 }
00232
00233 for ( unsigned int i = 0; i < x.size(); i = i+2 ) {
00234 glBegin (GL_LINES);
00235 glVertex3d (toX(x[i]),toY(y[i]),0 );
00236 glVertex3d (toX(x[i+1]),toY(y[i+1]),0 );
00237 glEnd ();
00238 }
00239 }
00240
00241 void OpenGLView::drawPolyLine ( const std::vector< double > & xpoints,
00242 const std::vector< double > & ypoints,
00243 hippodraw::Line::Style style,
00244 const Color & color , float)
00245 {
00246 #ifdef DEBUG_GL
00247 std::cout << " debug : drawPolyLine" << std::endl;
00248 #endif
00249
00250 float r = (float)(color.getRed() / 255.0);
00251 float g = (float)(color.getGreen() / 255.0);
00252 float b = (float)(color.getBlue() / 255.0);
00253
00254 glColor3f(r,g,b);
00255
00256 switch (style) {
00257 case Line::Solid:
00258 break;
00259 case Line::Dot:
00260 break;
00261 case Line::Dash:
00262 break;
00263 case Line::DashDot:
00264 break;
00265 default:
00266 break;
00267 }
00268
00269 glBegin (GL_LINE_STRIP);
00270 for ( unsigned int i = 0; i < xpoints.size(); i++ ) {
00271 glVertex3f (toViewX(xpoints[i]),toViewY(ypoints[i]),0 );
00272 #ifdef DEBUG_GL
00273
00274 #endif
00275 }
00276 glEnd ();
00277 }
00278
00279 void OpenGLView::drawSquare ( double x1, double y1, double x2, double y2,
00280 int red, int green, int blue )
00281 {
00282 #ifdef DEBUG_GL
00283 std::cout << " debug : drawSquare " << std::endl;
00284 #endif
00285
00286 float r = (float)(red / 255.0);
00287 float g = (float)(green / 255.0);
00288 float b = (float)(blue / 255.0);
00289
00290 glColor3f(r,g,b);
00291
00292 glBegin(GL_POLYGON);
00293 glVertex3f(toViewX(x1),toViewY(y1),0);
00294 glVertex3f(toViewX(x2),toViewY(y1),0);
00295 glVertex3f(toViewX(x2),toViewY(y2),0);
00296 glVertex3f(toViewX(x1),toViewY(y2),0);
00297 glEnd();
00298 }
00299
00300 void OpenGLView::drawViewSquare ( float x1, float y1, float x2, float y2,
00301 int red, int green, int blue ){
00302 #ifdef DEBUG_GL
00303 std::cout << "hippodraw::OpenGLView::drawViewSquare : " << std::endl;
00304 #endif
00305
00306 float r = (float)(red / 255.0);
00307 float g = (float)(green / 255.0);
00308 float b = (float)(blue / 255.0);
00309
00310 glColor3f(r,g,b);
00311
00312 glBegin(GL_POLYGON);
00313 glVertex3f(toX(x1),toY(y1),0);
00314 glVertex3f(toX(x2),toY(y1),0);
00315 glVertex3f(toX(x2),toY(y2),0);
00316 glVertex3f(toX(x1),toY(y2),0);
00317 glEnd();
00318
00319 }
00320
00321 void OpenGLView::drawPoints ( const std::vector<double> & x,
00322 const std::vector<double> & y,
00323 hippodraw::Symbol::Type type,
00324 float sym_size,
00325 const Color & color )
00326 {
00327 #ifdef DEBUG_GL
00328 std::cout << " debug : drawPoints " << std::endl;
00329 #endif
00330
00331
00332
00333 float r = (float)(color.getRed() / 255.0);
00334 float g = (float)(color.getGreen() / 255.0);
00335 float b = (float)(color.getBlue() / 255.0);
00336
00337 glColor3f(r,g,b);
00338
00339 sym_size *= 0.2;
00340
00341 for (unsigned int i = 0; i < x.size(); i++) {
00342 switch(type) {
00343 case hippodraw::Symbol::SQUARE:
00344 glBegin(GL_LINE_STRIP);
00345 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00346 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00347 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])+(sym_size/2),0);
00348 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])+(sym_size/2),0);
00349 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00350 glEnd ();
00351 break;
00352 case hippodraw::Symbol::SOLIDSQUARE:
00353 glBegin(GL_POLYGON);
00354 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00355 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00356 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])+(sym_size/2),0);
00357 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])+(sym_size/2),0);
00358 glEnd ();
00359 break;
00360 case hippodraw::Symbol::TRIANGLE:
00361 glBegin(GL_LINE_STRIP);
00362 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00363 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00364 glVertex3f(toViewX(x[i]),toViewY(y[i])+(sym_size/2),0);
00365 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00366 glEnd ();
00367 break;
00368 case hippodraw::Symbol::FILLED_TRIANGLE:
00369 glBegin(GL_POLYGON);
00370 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00371 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00372 glVertex3f(toViewX(x[i]),toViewY(y[i])+(sym_size/2),0);
00373 glEnd ();
00374 break;
00375 case hippodraw::Symbol::CIRCLE:
00376 case hippodraw::Symbol::FILLED_CIRCLE:
00377 std::cout << " debug : drawPoints : circle : dummy." << std::endl;
00378 glBegin(GL_LINE_STRIP);
00379
00380
00381
00382
00383
00384
00385
00386
00387 glEnd ();
00388 break;
00389 case hippodraw::Symbol::PLUS:
00390 glBegin(GL_LINES);
00391 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY (y[i]),0);
00392 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY (y[i]),0);
00393 glVertex3f(toViewX(x[i]),toViewY(y[i])-(sym_size/2),0);
00394 glVertex3f(toViewX(x[i]),toViewY(y[i])+(sym_size/2),0);
00395 glEnd ();
00396 break;
00397 case hippodraw::Symbol::TIMES:
00398 glBegin(GL_LINES);
00399 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00400 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])+(sym_size/2),0);
00401 glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
00402 glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])+(sym_size/2),0);
00403 glEnd ();
00404 break;
00405 default:
00406 break;
00407 }
00408 }
00409 }
00410
00411 void OpenGLView::drawPoints (
00412 const std::vector< double > &
00413 ,const std::vector< double > &
00414 ,const std::vector< Color > &
00415 ,hippodraw::Symbol::Type
00416 ,float
00417 )
00418 {
00419 #ifdef DEBUG_GL
00420 std::cout << " debug : drawPoints 2" << std::endl;
00421 #endif
00422 std::cout << " debug : drawPoints 2 : dummy" << std::endl;
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 }
00509
00510 void OpenGLView::drawMag ( float x, float y, int mag, float fontsize )
00511 {
00512 #ifdef SSTREAM_DEFECT
00513 std::ostrstream ost;
00514 #else
00515 std::ostringstream ost;
00516 #endif
00517
00518 ost << mag << std::ends;
00519 const std::string s(ost.str());
00520 #ifdef DEBUG_GL
00521 std::cout << " debug : drawMag " << s
00522 << " x " << x << " y " << y
00523 << " fontsize " << fontsize
00524 << std::endl;
00525 #endif
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540 draw_Text ( std::string("x10"), toX(x), toY(y), fontsize, 0, 'l', 't' );
00541
00542 draw_Text ( s, toX(x+2*fontsize), toY(y+fontsize*0.2), fontsize, 0, 'l', 't' );
00543 }
00544
00545 void OpenGLView::drawUserText ( const std::string &s, float x, float y,
00546 float fontsize, float angle,
00547 char xp, char yp )
00548 {
00549 #ifdef DEBUG_GL
00550 std::cout << " debug : drawUserText " << s << std::endl;
00551 #endif
00552 draw_Text ( s, toViewX (x), toViewY (y), fontsize, angle, xp , yp);
00553 }
00554
00555 void OpenGLView::drawText ( const std::string &s, float x, float y,
00556 float fontsize, float angle,
00557 char xp, char yp , bool)
00558 {
00559 #ifdef DEBUG_GL
00560 std::cout << " debug : drawText " << s << std::endl;
00561 #endif
00562 draw_Text ( s, toX(x), toY(y), fontsize, angle, xp, yp );
00563 }
00564
00565 void OpenGLView::drawText ( const std::string &s, float x, float y,
00566 float fontsize, float angle,
00567 char xp, char yp , bool,
00568 const FontBase * font)
00569 {
00570 #ifdef DEBUG_GL
00571 std::cout << " debug : drawText2 " << s << std::endl;
00572 #endif
00573 draw_Text ( s, toX(x), toY(y), fontsize, angle, xp, yp, font );
00574 }
00575
00576 void OpenGLView:: update (
00577 const Observable *
00578 )
00579 {
00580
00581 }
00582
00583 float OpenGLView::userToDrawX ( double x ) const
00584 {
00585 return m_plotter->userToMarginX( x );
00586 }
00587
00588 float OpenGLView::userToDrawY ( double y ) const
00589 {
00590 return m_plotter->userToInvertedMarginY( y );
00591 }
00592
00593 float OpenGLView::userToDrawColor ( double c ) const
00594 {
00595 return m_plotter->userToMarginColor( c );
00596 }
00597
00598 HippoRectangle OpenGLView::getDrawRect() const
00599 {
00600 return m_draw_rect;
00601 }
00602 void OpenGLView::setDrawRect (
00603 float
00604 ,float
00605 ,float
00606 ,float
00607 ) {
00608 std::cout << "hippodraw::OpenGLView::setDrawRect : dummy " << std::endl;
00609 }
00610
00614 void OpenGLView::draw_Text (
00615 const std::string &s
00616 ,float x
00617 ,float y
00618 ,float fontsize
00619 ,float angle
00620 ,char xp
00621 ,char yp
00622 ,const FontBase* font
00623 )
00624 {
00625 #ifdef DEBUG_GL
00626 #endif
00627 std::cout << " debug : draw_Text \"" << s
00628 << "\" : x " << x
00629 << " y " << y
00630 << " fontsize " << fontsize
00631 << " angle " << angle
00632 << " xp " << xp
00633 << " yp " << yp
00634 << " font " << font
00635 << std::endl;
00636 if(font)
00637 std::cout << " debug : draw_Text \"" << font->family()
00638 << "\" : pointSize " << font->pointSize()
00639 << std::endl;
00640 if(angle==90)
00641 glRasterPos2f(x+12,y);
00642 else
00643 glRasterPos2f(x,y);
00644
00645 hippodraw::TextTTF::Justification hJ;
00646 switch (xp) {
00647 case 'c':
00648 case 'C':
00649 hJ = hippodraw::TextTTF::CENTER;
00650 break;
00651 case 'r':
00652 case 'R':
00653 hJ = hippodraw::TextTTF::RIGHT;
00654 break;
00655 default:
00656 hJ = hippodraw::TextTTF::LEFT;
00657 break;
00658 }
00659
00660 hippodraw::TextTTF::Justification vJ;
00661 switch (yp){
00662 case 'c':
00663 case 'C':
00664 vJ = hippodraw::TextTTF::MIDDLE;
00665 break;
00666 case 't':
00667 case 'T':
00668 vJ = hippodraw::TextTTF::TOP;
00669 break;
00670 default:
00671 vJ = hippodraw::TextTTF::BOTTOM;
00672 break;
00673 }
00674
00675
00676
00678
00679
00680
00681
00682
00683 float pointSize = fontsize * 0.8;
00684
00685 m_TTF->setString(s);
00686 m_TTF->setSize(pointSize);
00687 m_TTF->setJustification(hJ,vJ);
00688 m_TTF->setRotated(angle==90?true:false);
00689 m_TTF->render();
00690 }
00691
00692 float OpenGLView::toViewX ( double datX ) const
00693 {
00694
00695 return ( m_draw_rect.getX() + m_plotter->userToMarginX ( datX ) );
00696 }
00697
00698 float OpenGLView::toViewY ( double datY ) const
00699 {
00700 HippoRectangle m_boundingRect(m_draw_rect.getX(),
00701 m_draw_rect.getY(),
00702 m_draw_rect.getWidth(),
00703 m_draw_rect.getHeight());
00704
00705 float s1 = 2 * m_boundingRect.getY() + m_boundingRect.getHeight();
00706
00707 float s2 = 2 * m_draw_rect.getY() + m_draw_rect.getHeight();
00708
00709 return ( s1 - ( s2 - ( ( m_draw_rect.getY()
00710 + m_plotter->userToMarginY ( datY ) ) -
00711 ( 2 * m_plotter->getMarginRect().getY() ) +
00712 ( m_draw_rect.getHeight() ) -
00713 ( m_plotter->getMarginRect().getHeight() )
00714 )
00715 )
00716 );
00717 }
00718
00719 float OpenGLView::toCanvasX ( double dx ) const
00720 {
00721 return static_cast < float > ( dx );
00722 }
00723
00724 float OpenGLView::toCanvasY ( double dy ) const
00725 {
00726 return static_cast < float > ( dy );
00727
00728 }
00729
00730 float OpenGLView::toX ( double x ) const
00731 {
00732 return static_cast<float> ( m_draw_rect.getX() + x );
00733 }
00734 float OpenGLView::toY ( double y ) const
00735 {
00736 return static_cast<float>(m_draw_rect.getY()+m_draw_rect.getHeight()-y);
00737 }