Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleGLShared.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Vision N GmbH
5  Schauenburgerstr. 116
6  24118 Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
21 
22 #include <bias_config.h>
23 
24 //#include <Base/Common/LeakChecking.h>
25 #include "ExampleGLShared.hh"
26 
27 // BIAS
28 #include <Gui/ConsoleRedirectIO.hh>
29 #include <Gui/GeometryGL.hh>
30 
31 using namespace std;
32 
33 // required for wx RTTI dynamic construction:
34 IMPLEMENT_APP(ExampleGLSharedApp)
35 
36 // --------------------------------------------------------
37 #ifdef BIAS_HAVE_GLUT
38 # define GLUT_BUILDING_LIB
39 # include <GL/glut.h>
40 #endif // BIAS_HAVE_GLUT
41 
42 
43 ///
44 /// your own implementation of the scenes GL calls:
45 ///
46 
47 MyGLCanvas::MyGLCanvas(wxFrame* parent)
48 :
49 BIAS::OpenGLCanvas( parent, -1)
50 ,dplist(0)
51 {}
52 
53 
54 void MyGLCanvas::InitGeometry()
55 {
56  // init once:
57  static bool initDone=false;
58  if(!initDone)
59  {
60  // do init
61  CHECK_GL_ERROR;
62  // example for an OpenGL display list
63  dplist = glGenLists(1); // identifier
64  glNewList(dplist, GL_COMPILE ); // or: GL_COMPILE_AND_EXECUTE
65  {
66  // light source proeprties for enabled lighting:
67  // specified pos by w=1, direction by w=0:
68  GLfloat light_position0[] = {10.0f, 10.0, 10.0f, 0.0f}; // w=0: direction
69  GLfloat light_maincolor0[] = {1.0f, 1.0, 1.0f, 1.0f};
70  GLfloat light_ambientcolor0[] = {0.4f, 0.2f, 0.2f, 1.0f};
71 
72  // GL create light 0
73  glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
74  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_maincolor0);
75  glLightfv(GL_LIGHT0, GL_SPECULAR, light_maincolor0);
76  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambientcolor0);
77 
78  glEnable(GL_LIGHT0);
79  glEnable(GL_LIGHTING); // may be switched on/off for every object
80  glEnable(GL_DEPTH_TEST); // depth buffer occlusions of objects
81 
82  // material properties for enabled lighting:
83  GLfloat paintcolor0[] = { 0.9f, 0.2f, 0.5f, 1.0f};
84  GLfloat specularcolor0[] = { 1.0f, 1.0f, 1.0f, 1.0f};
85  GLfloat shininess0[] = { 70.0f }; // 1..128, large values make smaller specular highlights
86 
87  GLfloat paintcolor1[] = { 0.3f, 0.9f, 0.4f, 1.0f};
88  GLfloat paintcolor2[] = { 0.1f, 0.2f, 0.9f, 1.0f};
89 
90  glMatrixMode(GL_MODELVIEW);
91  glPushMatrix();
92 
93  //// activate material properties:
94  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specularcolor0);
95  glMaterialfv( GL_FRONT_AND_BACK, GL_SHININESS, shininess0);
96  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
97  paintcolor0);
98  glColor3f(
99  paintcolor0[0],
100  paintcolor0[1],
101  paintcolor0[2] );
102 
103  // draw geometry
104  BIAS::Vector3<float> dtea(-0.5f, -0.2f, 0.0f);
105  // position of the scene "modeling" matrix
106  glTranslatef( dtea[0], dtea[1], dtea[2] );
107  // glutteapot has strange vertex ordering for historical reasons
108  glFrontFace(GL_CW);
109 #ifdef BIAS_HAVE_GLUT
110  glutSolidTeapot(0.6);
111 #endif //BIAS_HAVE_GLUT
112  glFrontFace(GL_CCW);
113 
114  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
115  paintcolor1 );
116  glColor3f(
117  paintcolor1[0],
118  paintcolor1[1],
119  paintcolor1[2] );
120 
121  glTranslatef( -2*dtea[0], -2*dtea[1], -2*dtea[2] );
122  glFrontFace(GL_CW);
123 #ifdef BIAS_HAVE_GLUT
124  glutSolidTeapot(0.3);
125 #endif // BIAS_HAVE_GLUT
126  glFrontFace(GL_CCW);
127  glTranslatef( dtea[0], dtea[1], dtea[2] );
128  // back on origin
129 
130  // activate material properties:
131  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, paintcolor2);
132  glColor3f(paintcolor2[0], paintcolor2[1], paintcolor2[2]);
133 
134 #ifdef BIAS_HAVE_GLUT
135  glTranslatef(0,0,-2);
136  glutSolidSphere(0.8, 30, 60);
137  glTranslatef(0,0, 2); // back to origin
138 
139  glTranslatef( 1,0,-3);
140  glutSolidSphere(0.5, 50, 100);
141  glTranslatef(-1,0, 3);// back to origin
142 
143  glTranslatef(0,2,0);
144  glutSolidCube(0.7);
145  glTranslatef(0,-2,0);
146 
147  glTranslatef(0,-1,0);
148  glutSolidIcosahedron();
149  glTranslatef(0, 1,0);
150 
151  glTranslatef(0,-2.2,0);
152  glutSolidTetrahedron();
153  glTranslatef(0, 2.2,0);
154 #endif // BIAS_HAVE_GLUT
155 
156  glPopMatrix();
157  }
158  glEndList();
159 
160  #ifdef BIAS_HAVE_FONTGL
161  // font:
162 #ifdef WIN32
163  font.InitFont( GetDC(HWND(GetHandle())), "Arial" );
164 #else // WIN32
165  font.InitFont( GetHandle(), "Arial" );
166 #endif // WIN32
167 
168  #endif // BIAS_HAVE_FONTGL
169 
170  CHECK_GL_ERROR;
171  initDone=true;
172  };
173 }
174 
175 
176 void MyGLCanvas::DisplayCalls()
177 {
178  //CALLINFO;
179  CHECK_GL_ERROR;
180  InitGeometry();
181 
182  DisplayClear();
183  DisplayCameraView();
184 
185  CHECK_GL_ERROR;
186 
187  {
188  //glPushAttrib(GL_CULL_FACE|GL_LIGHT0|GL_LIGHTING|GL_DEPTH_TEST );
189  //glPopAttrib();
190 
191  glColor3d(1.0, 0.5, 0.2);
192 
193  // animation: rotate the scene (not the camera) by using global time as angle:
194  // thus light and camera stay in the same place
195  {
196  const float cycletime=10.0; // approx. sec for a full cycle
197  const float angleM = fmodf( float(2.0*360.0/cycletime*OpenGLCanvasBase::time), 2.0*360.0);
198  if (angleM<=360.0){
199  glRotatef( angleM, 0.0, 1.0, 0.0 );
200  } else {
201  glRotatef( angleM-360.0, 1.0, 0.0, 0.0 );
202  }
203  }
204 
205 #if 1
206  if(glIsList(dplist) == GL_TRUE)
207  {
208  glCallList(dplist);
209  } else {
210  BIASERR("invalid display list dplist="<<dplist);
211  BIASBREAK;
212  }
213 #endif // 0/1
214 
215  DisplayCoordCross();
216 
217 #ifdef BIAS_HAVE_FONTGL
218  if (font.IsInitialized())
219  {
220  glColor3f(1.0f, 1.0f, 0.5f);
221  font.PrintWinCoords("a0", 0, 0 );
222  font.PrintWinCoords("a100x0y", 100, 0 );
223  font.PrintWinCoords("a100x50y", 100, 50 ); // OK.
224 
225  glColor3f(1.0f, 0.5f, 1.0f);
226  font.DrawTextGL::PrintNormCoords("n0", 0.0, 0.0);
227  font.DrawTextGL::PrintNormCoords("n0.5x0y", 0.5, 0.0);
228  font.DrawTextGL::PrintNormCoords("n0.5x0.25y", 0.5, 0.25);
229 
231  }
232 #endif // BIAS_HAVE_FONTGL
233  }
234 }
235 
236 
237 // --------------------------------------------------------
238 
239 // 2nd sharing with 1st:
240 MyGLCanvas2::MyGLCanvas2(wxFrame* parent)
241 :
242 BIAS::OpenGLCanvas( parent, -1)
243 ,dplist(0)
244 {}
245 
246 
247 // ctor with shared context
248 MyGLCanvas2::MyGLCanvas2(wxFrame* parent
249  ,wxGLContext* sharedContext
250  ,wxWindowID id
251  ,const wxPoint& pos
252  ,const wxSize& size
253  ,long style
254  ,const wxString& name
255  ,int* attribList
256  ,const wxPalette& palette )
257  :
258 BIAS::OpenGLCanvas(parent,sharedContext, id,pos,size,style,name,attribList,palette)
259 ,dplist(0)
260 {}
261 
262 void MyGLCanvas2::DisplayCalls()
263 {
264  CHECK_GL_ERROR;
265  DisplayClear();
266  DisplayCameraView();
267  //OpenGLCanvas::DisplayCalls();
268 
269  if(glIsList(dplist) == GL_TRUE)
270  {
271  {
272  const float cycletime=3.0; // approx. sec for a full cycle
273  const float angleM = fmodf( float(2.0*360.0/cycletime*OpenGLCanvasBase::time), 2.0*360.0);
274  if (angleM<=360.0){
275  glRotatef( -angleM, 0.0, 1.0, 0.0 );
276  } else {
277  glRotatef( -(angleM-360.0), 1.0, 0.0, 0.0 );
278  }
279  }
280 
281  glCallList(dplist);
282  } else {
283  BIASERR("dplist invalid");
284  BIASBREAK;
285  }
286 
288 
289  CHECK_GL_ERROR;
290 }
291 
292 
293 // --------------------------------------------------------
294 
295 bool ExampleGLSharedApp::OnInit()
296 {
297  // utility: create a Redirection console for cout, cerr etc.
299  // testing output:
300  cout<<"Hello cout"<<endl;
301  cout<<"Hello cerr"<<endl;
302 
303  MyFrame *frame = new MyFrame( wxT("ExampleOpenGLCanvas"), wxPoint(0,0), wxSize(640,480) );
304  frame->Show(TRUE);
305  SetTopWindow(frame);
306 
307  return TRUE;
308 }
309 
310 
311 // --------------------------------------------------------
312 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
313 EVT_MENU(ID_QUIT, MyFrame::OnQuit)
314 EVT_MENU(ID_ABOUT, MyFrame::OnAbout)
315 END_EVENT_TABLE()
316 // --------------------------------------------------------
317 
318 
319 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
320 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
321 ,p_canvas(NULL)
322 ,p_2nd_canvas(NULL)
323 {
324  // Create a simple top Menu
325  wxMenu *menuFile = new wxMenu;
326  menuFile->Append( ID_ABOUT, wxT("&About...") );
327  menuFile->AppendSeparator();
328  menuFile->Append( ID_QUIT, wxT("E&xit") );
329  wxMenuBar *menuBar = new wxMenuBar;
330  menuBar->Append(menuFile, wxT("&File"));
331  SetMenuBar(menuBar);
332 
333  // Create a simple bottom status bar
334  CreateStatusBar(1);
335 
336  // Canvas to test:
337  p_canvas=new MyGLCanvas(this);
338 
339  // share the context:
340  if ( p_canvas->GetContext() == NULL)
341  {
342  BIASERR("context of p_canvas is NULL."<<endl
343  <<"This means sharing OpenGL contexts may not be supported."<<endl
344  <<"Could not share context. aborting");
345  BIASBREAK;
346  exit(-1);
347  }
348  p_2nd_canvas = new MyGLCanvas2(this, p_canvas->GetContext(), -1);
349  // distribuet the id of the dplist:
350  p_2nd_canvas->dplist = 1; // HACK there is only one dplist
351 
352 
353  // put them into sizers:
354  wxBoxSizer * sizer = new wxBoxSizer(wxHORIZONTAL);
355  sizer->Add( p_canvas ,1,wxEXPAND|wxALL);
356 #if wxCHECK_VERSION(2, 6, 2)
357  sizer->AddSpacer(4); // available in 2.6.2 but not 2.4.2 (JW)
358 #endif
359  sizer->Add( p_2nd_canvas ,1,wxEXPAND|wxALL);
360 
361  // limit min. size:
362  sizer->SetItemMinSize(p_canvas, 500,400);
363  sizer->SetItemMinSize(p_2nd_canvas, 300,200);
364  sizer->SetSizeHints(this);
365 
366  // set sizer and triggers Layout()
367  SetSizerAndFit(sizer);
368 }
369 
370 
371 void MyFrame::OnQuit(wxCommandEvent& ){
372  Close(TRUE);
373 }
374 
375 
376 void MyFrame::OnAbout(wxCommandEvent& )
377 {
378  wxString title(wxT("About"));
379  wxString msg; // message
380  msg<<wxT("About - ")<<AsciiToWx(FUNCNAME)<<wxT("\n");
381  msg<<wxT("author: Jan Woetzel (c) 2003-2005\n");
382  msg<<wxT("build: ")<<AsciiToWx(__DATE__)<<wxT(" ")<<AsciiToWx(__TIME__)<<wxT("\n");
383  msg<<wxT("from ")<<AsciiToWx(__FILE__)<<wxT("\n");
384  msg<<wxT("GUI using: ")<<wxVERSION_STRING<<wxT("\n");
385  msg<<wxT("\n");
386  COUT(msg);
387  wxMessageBox(msg, title, wxICON_INFORMATION | wxOK );
388 }
389 
void BIASGui_EXPORT ConsoleRedirectIO(std::string name=std::string(""))
Extra Console for WIN32 WinMain gui applications.
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
static void DisplayCoordCross(const float &size=1.0f, const float &linewidth=1.0f)
draw a coord cross as three lines in r,g,b
Definition: GeometryGL.cpp:598
OpenGL canvases with additinal event handling (JW)
Definition: OpenGLCanvas.hh:68
static void DisplayCoordCrossAxesLabel(const float &size, const BIAS::DrawTextGL &font, const std::string &LabelX=std::string("x"), const std::string &LabelY=std::string("y"), const std::string &LabelZ=std::string("z"))
draw only the axes labels for a coord cross as text in r,g,b JW
Definition: GeometryGL.cpp:568