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