OpenGLView.cxx
Go to the documentation of this file.
1 /* Hippo OpenGLView implementation
2  *
3  */
4 
5 // this :
6 #include "OpenGLView.h"
7 
8 #ifdef HAVE_CONFIG_H
9 #include "hippoconfig.h"
10 #else
11 #ifdef _MSC_VER
12 #include "msdevstudio/MSconfig.h"
13 #endif
14 #endif
15 
16 #include "graphics/Color.h"
18 #include "plotters/PlotterBase.h"
19 #include "graphics/FontBase.h"
20 
21 #include "TextTTF.h"
22 #include "OpenGL.h"
23 
24 #include <iostream>
25 
26 #ifdef SSTREAM_DEFECT
27 #include <strstream>
28 #else
29 #include <sstream>
30 #endif
31 
32 //#define DEBUG_GL
33 
34 using namespace hippodraw;
35 
37 :m_TTF(0)
38 {
39  m_draw_rect.setRect(0,0,100,100);
41 }
42 
43 
45 {
46  delete m_TTF;
47 }
48 
49 void OpenGLView::setRect(double aX, double aY, double aW, double aH)
50 {
51  m_draw_rect.setRect(aX,aY,aW,aH);
52 }
53 
54 
56 {
57  ViewBase::setPlotter ( plotter );
58  plotter -> addObserver ( this );
59 }
60 
62 {
63  return ViewBase::getPlotter ();
64 }
65 
66 
67 void OpenGLView::drawLines ( const std::vector< double > & x,
68  const std::vector< double > & y,
70  const Color & color ,float)
71 {
72 #ifdef DEBUG_GL
73  std::cout << " debug : drawLines " << std::endl;
74 #endif
75 
76  float r = (float)(color.getRed() / 255.0);
77  float g = (float)(color.getGreen() / 255.0);
78  float b = (float)(color.getBlue() / 255.0);
79 
80  glColor3f(r,g,b);
81 
82 #ifdef DEBUG_GL
83  std::cout << "debug : drawLines : color "
84  << " r : " << r
85  << " g : " << g
86  << " b : " << b
87  << std::endl;
88 #endif
89 
90  switch (style)
91  {
92  case Line::Solid:
93  break;
94  case Line::Dot:
95  break;
96  case Line::Dash:
97  break;
98  case Line::DashDot:
99  break;
100  default:
101  break;
102  }
103 
104  for ( unsigned int i = 0; i < x.size(); i = i+2 ) {
105  glBegin (GL_LINES);
106  glVertex3f (toViewX(x[i]),toViewY(y[i]),0 );
107  glVertex3f (toViewX(x[i+1]),toViewY(y[i+1]),0 );
108  glEnd ();
109 #ifdef DEBUG_GL
110  std::cout << "debug : " << x[i] << " " << y[i] << std::endl;
111  std::cout << "debug : " << x[i+1] << " " << y[i+1] << std::endl;
112 #endif
113  }
114 }
115 
116 void
118 drawColorLines ( const std::vector< double > & x,
119  const std::vector< double > & y,
121  const std::vector< Color > & colors,
122  float )
123 {
124 #ifdef DEBUG_GL
125  std::cout << "hippodraw::OpenGLView::drawColorLines :" << std::endl;
126 #endif
127 
128  for (unsigned int i = 0; i < x.size(); i+=2 ){
129 
130  const Color & color = colors[i];
131  float r = color.getRed () / 255.0;
132  float g = color.getGreen () / 255.0;
133  float b = color.getBlue () / 255.0;
134 
135  glColor3f(r,g,b);
136 
137  //FIXME m_outfile << size << " setlinewidth" << endl;
138 
139  switch (style) { //FIXME
140  case Line::Solid:
141  break;
142  case Line::Dot:
143  break;
144  case Line::Dash:
145  break;
146  case Line::DashDot:
147  break;
148  default:
149  break;
150  }
151 
152  glBegin (GL_LINES);
153  glVertex3f (toViewX(x[i]),toViewY(y[i]),0 );
154  glVertex3f (toViewX(x[i+1]),toViewY(y[i+1]),0 );
155  glEnd ();
156 
157  }
158 }
159 
161  const std::vector< double > & x
162 ,const std::vector< double > & y
164 ,bool //color
165 ,float
166 )
167 {
168 #ifdef DEBUG_GL
169  std::cout << " debug : drawViewLines" << std::endl;
170 #endif
171  glColor3f(0,0,0);
172 
173  switch (style) { //FIXME
174  case Line::Solid:
175  break;
176  case Line::Dot:
177  break;
178  case Line::Dash:
179  break;
180  case Line::DashDot:
181  break;
182  default:
183  break;
184  }
185 
186  for ( unsigned int i = 0; i < x.size(); i = i+2 ) {
187  glBegin (GL_LINES);
188  glVertex3f (toX(x[i]),toY(y[i]),0 );
189  glVertex3f (toX(x[i+1]),toY(y[i+1]),0 );
190  glEnd ();
191  //std::cout << "debug : " << toX(x[i]) << " " << toY(y[i]) << std::endl;
192  //std::cout << "debug : " << toX(x[i+1]) << " " << toY(y[i+1]) << std::endl;
193  }
194 }
195 
196 void OpenGLView::drawViewLines ( const std::vector< double > & x,
197  const std::vector< double > & y,
199  const Color & color, float)
200 {
201 #ifdef DEBUG_GL
202  std::cout << " debug : drawViewLines 2" << std::endl;
203 #endif
204 
205  float r = (float)(color.getRed() / 255.0);
206  float g = (float)(color.getGreen() / 255.0);
207  float b = (float)(color.getBlue() / 255.0);
208 
209  glColor3f(r,g,b);
210 
211  switch (style) { //FIXME
212  case Line::Solid:
213  break;
214  case Line::Dot:
215  break;
216  case Line::Dash:
217  break;
218  case Line::DashDot:
219  break;
220  default:
221  break;
222  }
223 
224  for ( unsigned int i = 0; i < x.size(); i = i+2 ) {
225  glBegin (GL_LINES);
226  glVertex3d (toX(x[i]),toY(y[i]),0 );
227  glVertex3d (toX(x[i+1]),toY(y[i+1]),0 );
228  glEnd ();
229  }
230 }
231 
232 void OpenGLView::drawPolyLine ( const std::vector< double > & xpoints,
233  const std::vector< double > & ypoints,
235  const Color & color, float)
236 {
237 #ifdef DEBUG_GL
238  std::cout << " debug : drawPolyLine" << std::endl;
239 #endif
240 
241  float r = (float)(color.getRed() / 255.0);
242  float g = (float)(color.getGreen() / 255.0);
243  float b = (float)(color.getBlue() / 255.0);
244 
245  glColor3f(r,g,b);
246 
247  switch (style) {
248  case Line::Solid:
249  break;
250  case Line::Dot:
251  break;
252  case Line::Dash:
253  break;
254  case Line::DashDot:
255  break;
256  default:
257  break;
258  }
259 
260  glBegin (GL_LINE_STRIP);
261  for ( unsigned int i = 0; i < xpoints.size(); i++ ) {
262  glVertex3f (toViewX(xpoints[i]),toViewY(ypoints[i]),0 );
263 #ifdef DEBUG_GL
264  //std::cout << "debug : pol " << i << " " << toViewX(xpoints[i]) << " " << toViewY(ypoints[i]) << std::endl;
265 #endif
266  }
267  glEnd ();
268 }
269 
270 void OpenGLView::drawSquare ( double x1, double y1, double x2, double y2,
271  int red, int green, int blue )
272 {
273 #ifdef DEBUG_GL
274  std::cout << " debug : drawSquare " << std::endl;
275 #endif
276 
277  float r = (float)(red / 255.0);
278  float g = (float)(green / 255.0);
279  float b = (float)(blue / 255.0);
280 
281  glColor3f(r,g,b);
282 
283  glBegin(GL_POLYGON);
284  glVertex3f(toViewX(x1),toViewY(y1),0);
285  glVertex3f(toViewX(x2),toViewY(y1),0);
286  glVertex3f(toViewX(x2),toViewY(y2),0);
287  glVertex3f(toViewX(x1),toViewY(y2),0);
288  glEnd();
289 }
290 
291 void OpenGLView::drawViewSquare ( float x1, float y1, float x2, float y2,
292  int red, int green, int blue ){
293 #ifdef DEBUG_GL
294  std::cout << "hippodraw::OpenGLView::drawViewSquare : " << std::endl;
295 #endif
296 
297  float r = (float)(red / 255.0);
298  float g = (float)(green / 255.0);
299  float b = (float)(blue / 255.0);
300 
301  glColor3f(r,g,b);
302 
303  glBegin(GL_POLYGON);
304  glVertex3f(toX(x1),toY(y1),0);
305  glVertex3f(toX(x2),toY(y1),0);
306  glVertex3f(toX(x2),toY(y2),0);
307  glVertex3f(toX(x1),toY(y2),0);
308  glEnd();
309 
310 }
311 
312 void OpenGLView::drawPoints ( const std::vector<double> & x,
313  const std::vector<double> & y,
315  float sym_size,
316  const Color & color )
317 {
318 #ifdef DEBUG_GL
319  std::cout << " debug : drawPoints " << std::endl;
320 #endif
321 
322  //printf("debug : type %d %g\n",type,sym_size);
323 
324  float r = (float)(color.getRed() / 255.0);
325  float g = (float)(color.getGreen() / 255.0);
326  float b = (float)(color.getBlue() / 255.0);
327 
328  glColor3f(r,g,b);
329 
330  sym_size *= 0.2;
331 
332  for (unsigned int i = 0; i < x.size(); i++) {
333  switch(type) {
335  glBegin(GL_LINE_STRIP);
336  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
337  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
338  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])+(sym_size/2),0);
339  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])+(sym_size/2),0);
340  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
341  glEnd ();
342  break;
344  glBegin(GL_POLYGON);
345  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
346  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
347  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])+(sym_size/2),0);
348  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])+(sym_size/2),0);
349  glEnd ();
350  break;
352  glBegin(GL_LINE_STRIP);
353  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
354  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
355  glVertex3f(toViewX(x[i]),toViewY(y[i])+(sym_size/2),0);
356  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
357  glEnd ();
358  break;
360  glBegin(GL_POLYGON);
361  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
362  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
363  glVertex3f(toViewX(x[i]),toViewY(y[i])+(sym_size/2),0);
364  glEnd ();
365  break;
368  std::cout << " debug : drawPoints : circle : dummy." << std::endl;
369  glBegin(GL_LINE_STRIP);
370 /*
371  s_outfile << toViewX (x[i]) + (sym_size/2) << " "
372  << toViewY (y[i]) << " moveto" << endl;
373  s_outfile << toViewX (x[i]) << " "
374  << toViewY (y[i]) << " "
375  << sym_size/2 << " "
376  << "0.0 360 arc" << endl;
377 */
378  glEnd ();
379  break;
381  glBegin(GL_LINES);
382  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY (y[i]),0);
383  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY (y[i]),0);
384  glVertex3f(toViewX(x[i]),toViewY(y[i])-(sym_size/2),0);
385  glVertex3f(toViewX(x[i]),toViewY(y[i])+(sym_size/2),0);
386  glEnd ();
387  break;
389  glBegin(GL_LINES);
390  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])-(sym_size/2),0);
391  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])+(sym_size/2),0);
392  glVertex3f(toViewX(x[i])+(sym_size/2),toViewY(y[i])-(sym_size/2),0);
393  glVertex3f(toViewX(x[i])-(sym_size/2),toViewY(y[i])+(sym_size/2),0);
394  glEnd ();
395  break;
396  default:
397  break;
398  }
399  }
400 }
401 
403  const std::vector< double > & //x
404 ,const std::vector< double > & //y
405 ,const std::vector< Color > & //colors
407 ,float //sym_size
408 )
409 {
410 #ifdef DEBUG_GL
411  std::cout << " debug : drawPoints 2" << std::endl;
412 #endif
413  std::cout << " debug : drawPoints 2 : dummy" << std::endl;
414  /*
415  for (unsigned int i = 0; i < x.size(); i++)
416  {
417 
418  s_outfile << "gsave" << endl;
419  s_outfile << hue[i] << " 1.0 1.0 sethsbcolor" << endl;
420  s_outfile << "newpath systemdict begin" << endl;
421 
422  switch ( type )
423  {
424  case hippodraw::Symbol::SQUARE:
425  case hippodraw::Symbol::SOLIDSQUARE:
426 
427  s_outfile << toViewX (x[i])-(sym_size/2) << " "
428  << toViewY (y[i])-(sym_size/2) << " moveto" << endl;
429  s_outfile << toViewX (x[i])+(sym_size/2) << " "
430  << toViewY (y[i])-(sym_size/2) << " lineto" << endl;
431  s_outfile << toViewX (x[i])+(sym_size/2) << " "
432  << toViewY (y[i])+(sym_size/2) << " lineto" << endl;
433  s_outfile << toViewX (x[i])-(sym_size/2) << " "
434  << toViewY (y[i])+(sym_size/2) << " lineto" << endl;
435  s_outfile << "closepath" << endl;
436 
437  break;
438 
439  case hippodraw::Symbol::TRIANGLE:
440  case hippodraw::Symbol::FILLED_TRIANGLE:
441  s_outfile << toViewX (x[i]) << " "
442  << toViewY (y[i]) + (sym_size/2) << " moveto" << endl;
443  s_outfile << toViewX (x[i]) + (sym_size/2) << " "
444  << toViewY (y[i])-(sym_size/2) << " lineto" << endl;
445  s_outfile << toViewX (x[i]) - (sym_size/2) << " "
446  << toViewY (y[i])-(sym_size/2) << " lineto" << endl;
447  s_outfile << "closepath" << endl;
448  break;
449 
450  case hippodraw::Symbol::CIRCLE:
451  case hippodraw::Symbol::FILLED_CIRCLE:
452  s_outfile << toViewX (x[i]) + (sym_size/2) << " "
453  << toViewY (y[i]) << " moveto" << endl;
454  s_outfile << toViewX (x[i]) << " "
455  << toViewY (y[i]) << " "
456  << sym_size/2 << " "
457  << "0.0 360 arc" << endl;
458  break;
459 
460  case hippodraw::Symbol::PLUS:
461  s_outfile << toViewX (x[i])-(sym_size/2) << " "
462  << toViewY (y[i]) << " moveto" << endl;
463  s_outfile << toViewX (x[i])+(sym_size/2) << " "
464  << toViewY (y[i]) << " lineto" << endl;
465  s_outfile << toViewX (x[i]) << " "
466  << toViewY (y[i])-(sym_size/2) << " moveto" << endl;
467  s_outfile << toViewX (x[i]) << " "
468  << toViewY (y[i])+(sym_size/2) << " lineto" << endl;
469  break;
470 
471  case hippodraw::Symbol::TIMES:
472  s_outfile << toViewX (x[i])-(sym_size/2) << " "
473  << toViewY (y[i])-(sym_size/2) << " moveto" << endl;
474  s_outfile << toViewX (x[i])+(sym_size/2) << " "
475  << toViewY (y[i])+(sym_size/2) << " lineto" << endl;
476  s_outfile << toViewX (x[i])+(sym_size/2) << " "
477  << toViewY (y[i])-(sym_size/2) << " moveto" << endl;
478  s_outfile << toViewX (x[i])-(sym_size/2) << " "
479  << toViewY (y[i])+(sym_size/2) << " lineto" << endl;
480  break;
481 
482  default:
483  break;
484 
485  }
486 
487  s_outfile << "end" << endl;
488 
489  if(filled)
490  {
491  s_outfile << "fill grestore" << endl;
492  }
493  else
494  {
495  s_outfile << "stroke grestore" << endl;
496  }
497  }
498  */
499 }
500 
501 void OpenGLView::drawMag ( float x, float y, int mag, float fontsize )
502 {
503 #ifdef SSTREAM_DEFECT
504  std::ostrstream ost;
505 #else
506  std::ostringstream ost;
507 #endif
508 
509  ost << mag << std::ends;
510  const std::string s(ost.str());
511 #ifdef DEBUG_GL
512  std::cout << " debug : drawMag " << s
513  << " x " << x << " y " << y
514  << " fontsize " << fontsize
515  << std::endl;
516 #endif
517  // AxisRep2D::drawXTickLabels do not give good y position
518  // when having an y upstream. We try to reverse what is done
519  // here (yp=t) and correct.
520 
521  // Reverse :
522 /*
523  float font_size = fontsize / 0.75;
524  y -= m_draw_rect.getY();
525  y -= (font_size * 1.2);
526  // Correct :
527  y -= (font_size * 1.2);
528  y += m_draw_rect.getY();
529 */
530 
531  draw_Text ( std::string("x10"), toX(x), toY(y), fontsize, 0, 'l', 't' );
532 
533  draw_Text ( s, toX(x+2*fontsize), toY(y+fontsize*0.2), fontsize, 0, 'l', 't' );
534 }
535 
536 // void OpenGLView::drawUserText ( const std::string &s, float x, float y,
537 // float fontsize, float angle,
538 // char xp, char yp )
539 // {
540 // #ifdef DEBUG_GL
541 // std::cout << " debug : drawUserText " << s << std::endl;
542 // #endif
543 // draw_Text ( s, toViewX (x), toViewY (y), fontsize, angle, xp , yp);
544 // }
545 
546 void OpenGLView::drawText ( const std::string &s, float x, float y,
547  float fontsize, float angle,
548  char xp, char yp , bool)
549 {
550 #ifdef DEBUG_GL
551  std::cout << " debug : drawText " << s << std::endl;
552 #endif
553  draw_Text ( s, toX(x), toY(y), fontsize, angle, xp, yp );
554 }
555 
556 void OpenGLView::drawText ( const std::string &s, float x, float y,
557  float fontsize, float angle,
558  char xp, char yp, bool,
559  const FontBase * font,
560  const Color * color )
561 {
562 #ifdef DEBUG_GL
563  std::cout << " debug : drawText2 " << s << std::endl;
564 #endif
565  draw_Text ( s, toX(x), toY(y), fontsize, angle, xp, yp, font );
566 }
567 
569  const Observable * //display
570 )
571 {
572  //std::cout << " debug : update : dummy." << std::endl;
573 }
574 
575 float OpenGLView::userToDrawX ( double x ) const
576 {
577 // return m_plotter->userToMarginX( x );
578  return userToMarginX( x );
579 }
580 
581 float OpenGLView::userToDrawY ( double y ) const
582 {
583 // return m_plotter->userToInvertedMarginY( y );
584  return userToInvertedMarginY( y );
585 }
586 
587 float OpenGLView::userToDrawColor ( double c ) const
588 {
589  // return m_plotter->userToMarginColor( c );
590 // }
591 return userToMarginColor( c );
592 }
593 
595 {
596  return m_draw_rect;
597 }
599  float //x
600 ,float //y
601 ,float //w
602 ,float //h
603 ) {
604  std::cout << "hippodraw::OpenGLView::setDrawRect : dummy " << std::endl;
605 }
606 
611  const std::string &s
612 ,float x
613 ,float y
614 ,float fontsize
615 ,float angle
616 ,char xp
617 ,char yp
618 ,const FontBase* font
619 )
620 {
621 #ifdef DEBUG_GL
622 #endif
623  std::cout << " debug : draw_Text \"" << s
624  << "\" : x " << x
625  << " y " << y
626  << " fontsize " << fontsize
627  << " angle " << angle
628  << " xp " << xp
629  << " yp " << yp
630  << " font " << font
631  << std::endl;
632  if(font)
633  std::cout << " debug : draw_Text \"" << font->family()
634  << "\" : pointSize " << font->pointSize()
635  << std::endl;
636  if(angle==90)
637  glRasterPos2f(x+12,y);
638  else
639  glRasterPos2f(x,y);
640 
642  switch (xp) {
643  case 'c':
644  case 'C':
646  break;
647  case 'r':
648  case 'R':
650  break;
651  default:
653  break;
654  }
655 
657  switch (yp){
658  case 'c':
659  case 'C':
661  break;
662  case 't':
663  case 'T':
665  break;
666  default:
668  break;
669  }
670 
671  // fontsize 4 -> TTF pointSize 10
672  // fontsize 5 -> TTF pointSize 25 (CERN Personnel)
674  // 25 = a * 5 + b;
675  // 10 = a * 4 + b;
676  //float a = (25-10)/(5-4);
677  //float b = 25 - a * 5;
678  //float pointSize = a * fontsize + b;
679  float pointSize = fontsize * 0.8;
680 
681  m_TTF->setString(s);
682  m_TTF->setSize(pointSize);
683  m_TTF->setJustification(hJ,vJ);
684  m_TTF->setRotated(angle==90?true:false);
685  m_TTF->render();
686 }
687 
688 float OpenGLView::toViewX ( double datX ) const
689 {
690  //return toX(m_plotter->userToMarginX ( datX ));
691 // return ( m_draw_rect.getX() + m_plotter->userToMarginX ( datX ) );
692  return ( m_draw_rect.getX() + userToMarginX ( datX ) );
693 }
694 
695 float OpenGLView::toViewY ( double datY ) const
696 {
697  Rect m_boundingRect(m_draw_rect.getX(),
698  m_draw_rect.getY(),
701  //From EpsView::toViewY.
702  float s1 = 2 * m_boundingRect.getY() + m_boundingRect.getHeight();
703 
704  float s2 = 2 * m_draw_rect.getY() + m_draw_rect.getHeight();
705 
706 // return ( s1 - ( s2 - ( ( m_draw_rect.getY()
707 // + m_plotter->userToMarginY ( datY ) ) -
708 // ( 2 * m_plotter->getMarginRect().getY() ) +
709 // ( m_draw_rect.getHeight() ) -
710 // ( m_plotter->getMarginRect().getHeight() )
711  return ( s1 - ( s2 - ( ( m_draw_rect.getY()
712  + userToMarginY ( datY ) ) -
713  ( 2 * getMarginRect().getY() ) +
714  ( m_draw_rect.getHeight() ) -
715  ( getMarginRect().getHeight() )
716  )
717  )
718  );
719 }
720 
721 float OpenGLView::toCanvasX ( double dx ) const
722 {
723  return static_cast < float > ( dx );
724 }
725 
726 float OpenGLView::toCanvasY ( double dy ) const
727 {
728  return static_cast < float > ( dy );
729  //return toY(dy);
730 }
731 
732 float OpenGLView::toX ( double x ) const
733 {
734  return static_cast<float> ( m_draw_rect.getX() + x );
735 }
736 float OpenGLView::toY ( double y ) const
737 {
738  return static_cast<float>(m_draw_rect.getY()+m_draw_rect.getHeight()-y);
739 }

Generated for HippoDraw Class Library by doxygen