Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SimpleSceneGL.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /** Jan Woetzel */
26 #include "SimpleSceneGL.hh"
27 #include <Gui/GeometryGL.hh>
28 
29 #ifndef BIAS_HAVE_GLUT
30 # error Please recompile BIAS with USE_GLUT to compile this example.
31 #endif // BIAS_HAVE_GLUT
32 #define GLUT_BUILDING_LIB
33 #include <GL/glut.h>
34 
35 using namespace std;
36 using namespace BIAS;
37 
38 /// JW
39 class SimpleSceneGLData {
40 public:
41  SimpleSceneGLData() : dplist(0) {};
42  int dplist;
43 };
44 
45 
46 //
47 // pure GL implementation
48 //
49 
50 
51 void InitGeometry(int &dplist)
52 {
53  // init once:
54  static bool initDone = false;
55  if (!initDone) {
56  // do init
57  CHECK_GL_ERROR;
58  // example for an OpenGL display list
59  dplist = glGenLists(1); // identifier
60  glNewList(dplist, GL_COMPILE); // or: GL_COMPILE_AND_EXECUTE
61  {
62  // light source proeprties for enabled lighting:
63  // specified pos by w=1, direction by w=0:
64  GLfloat light_position0[] = { 10.0f, 10.0, 10.0f, 0.0f }; // w=0: direction
65  GLfloat light_maincolor0[] = { 1.0f, 1.0, 1.0f, 1.0f };
66  GLfloat light_ambientcolor0[] = { 0.4f, 0.2f, 0.2f, 1.0f };
67 
68  // GL create light 0
69  glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
70  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_maincolor0);
71  glLightfv(GL_LIGHT0, GL_SPECULAR, light_maincolor0);
72  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambientcolor0);
73 
74  glEnable(GL_LIGHT0);
75  glEnable(GL_LIGHTING); // may be switched on/off for every object
76  glEnable(GL_DEPTH_TEST); // depth buffer occlusions of objects
77 
78  // material properties for enabled lighting:
79  GLfloat paintcolor0[] = { 0.9, 0.2, 0.5, 1.0 };
80  GLfloat specularcolor0[] = { 1.0, 1.0, 1.0, 1.0 };
81  GLfloat shininess0[] = { 70. }; // 1..128, large values make smaller specular highlights
82 
83  GLfloat paintcolor1[] = { 0.3, 0.9, 0.4, 1.0 };
84  GLfloat paintcolor2[] = { 0.1, 0.2, 0.9, 1.0 };
85 
86  glMatrixMode(GL_MODELVIEW);
87  glPushMatrix();
88 
89  //// activate material properties:
90  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularcolor0);
91  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess0);
92  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
93  paintcolor0);
94  glColor3f(paintcolor0[0], paintcolor0[1], paintcolor0[2]);
95 
96  // draw geometry
97  BIAS::Vector3 < float >dtea(-0.5, -0.2, 0);
98  // position of the scene "modeling" matrix
99  glTranslatef(dtea[0], dtea[1], dtea[2]);
100  // glutteapot has strange vertex ordering for historical reasons
101  glFrontFace(GL_CW);
102  glutSolidTeapot(0.6);
103  glFrontFace(GL_CCW);
104 
105  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
106  paintcolor1);
107  glColor3f(paintcolor1[0], paintcolor1[1], paintcolor1[2]);
108 
109  glTranslatef(-2 * dtea[0], -2 * dtea[1], -2 * dtea[2]);
110  glFrontFace(GL_CW);
111  glutSolidTeapot(0.3);
112  glFrontFace(GL_CCW);
113  glTranslatef(dtea[0], dtea[1], dtea[2]);
114  // back on origin
115 
116  // activate material properties:
117  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
118  paintcolor2);
119  glColor3f(paintcolor2[0], paintcolor2[1], paintcolor2[2]);
120 
121  glTranslatef(0, 0, -2);
122  glutSolidSphere(0.8, 30, 60);
123  glTranslatef(0, 0, 2); // back to origin
124 
125  glTranslatef(1, 0, -3);
126  glutSolidSphere(0.5, 50, 100);
127  glTranslatef(-1, 0, 3); // back to origin
128 
129 
130  glTranslatef(0, 2, 0);
131  glutSolidCube(0.7);
132  glTranslatef(0, -2, 0);
133 
134  glTranslatef(0, -1, 0);
135  glutSolidIcosahedron();
136  glTranslatef(0, 1, 0);
137 
138  glTranslatef(0, -2.2, 0);
139  glutSolidTetrahedron();
140  glTranslatef(0, 2.2, 0);
141 
142  glPopMatrix();
143  }
144  glEndList();
145 
146 
147  CHECK_GL_ERROR;
148  initDone = true;
149  };
150 }
151 
152 /*
153  *
154  * this is your main entry Display routine
155  *
156  */
157 void SimpleSceneGL::Display()
158 {
159  if (this->pc == NULL)
160  return;
161 
162  BIASASSERT(this->pc != NULL);
163 
164  static SimpleSceneGLData data;
165  InitGeometry(data.dplist);
166  CHECK_GL_ERROR;
167  pc->DisplayClear();
168  pc->DisplayCameraView();
169  pc->DisplayRenderMode();
170 
171  //glutSolidCube(0.7);return;
172 
173  //pc->p_font->InitFont("Arial");
174 
175  CHECK_GL_ERROR;
176  {
177  //glPushAttrib(GL_CULL_FACE|GL_LIGHT0|GL_LIGHTING|GL_DEPTH_TEST );
178 
179  glColor3f(1.0, 0.5, 0.2);
180 
181  if (glIsList(data.dplist) == GL_TRUE) {
182  // animation: rotate the scene (not the camera) by using global time as angle:
183  // thus light and camera stay in the same place
184  {
185  const float cycletime = 10.0; // approx. sec for a full cycle
186  const float angleM = fmodf(float
187  (2.0 * 360.0 / cycletime *
188  OpenGLCanvasBase::time),
189  2.0 * 360.0);
190  if (angleM <= 360.0) {
191  glRotatef(angleM, 0.0, 1.0, 0.0);
192  } else {
193  glRotatef(angleM - 360.0, 1.0, 0.0, 0.0);
194  }
195  }
196 
197  //// interpolate between vertices
198  //glShadeModel(GL_SMOOTH);
199  glCallList(data.dplist);
200 
201  // no interpolation between vertices
202  //glTranslatef(0.3,0.2,0.2);
203  //glShadeModel(GL_FLAT);
204  //glCallList(dplist);
205 
206  } else {
207  BIASERR("invalid display list dplist=" << data.dplist);
208  }
209 
210  pc->DisplayCoordCross();
211 
212  glColor3f(1.0f, 1.0f, 0.5f);
213  //pc->p_font->PrintNormCoords("Hallo", 0.3, 0.2);
214 
215  //glPopAttrib();
216  }
217 }