Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
View.cpp
1 #include "View.hh"
2 #include <algorithm>
3 
4 using namespace std;
5 using namespace BIAS;
6 using namespace BIAS;
7 
8 View::
9 View()
10 {
11  viewport_ = Vector4<int>(0, 0, 640, 480);
12  clearColor_ = Vector4<float>(0.0f, 0.0f, 0.0f, 0.0f);
13  camera_ = NULL;
14  backgroundImageScene_ = NULL;
15 }
16 
17 void View::
18 SetViewport(int x, int y, int width, int height)
19 {
20  viewport_ = Vector4<int>(x, y, width, height);
21 }
22 
23 void View::
24 SetClearColor(float red, float green, float blue, float alpha)
25 {
26  clearColor_ = Vector4<float>(red, green, blue, alpha);
27 }
28 
29 void View::
30 SetBackgroundImageScene(SceneBGImage* bg)
31 {
32  backgroundImageScene_ = bg;
33 }
34 
35 void View::
36 SetGLProjectionParametersInterface(GLProjectionParametersInterface* camera)
37 {
38  camera_ = camera;
39 
40  for (unsigned int i = 0; i < scenes_.size(); i++) {
41  scenes_[i]->SetGLProjectionParametersInterface(camera);
42  }
43 }
44 
45 void View::
46 AppendScene(SceneBase* scene)
47 {
48  BIASASSERT(scene != NULL);
49  scene->SetGLProjectionParametersInterface(camera_);
50  scenes_.push_back(scene);
51 }
52 
53 void View::
54 RemoveScene(SceneBase* scene)
55 {
56  vector<SceneBase*>::iterator itor = find(scenes_.begin(), scenes_.end(), scene);
57  if (itor == scenes_.end()) {
58  BIASERR("Scene to be removed is not contained by the view");
59  return;
60  }
61  scenes_.erase(itor);
62 }
63 
64 void View::
65 InsertSceneAt(SceneBase* scene, int index)
66 {
67  if (index <= (int)scenes_.size()) {
68  vector<SceneBase*>::iterator itor = scenes_.begin();
69  for (int i = 0; i < index; i++) {
70  itor++;
71  }
72  scenes_.insert(itor, scene);
73  } else {
74  scenes_.push_back(scene);
75  }
76  scene->SetGLProjectionParametersInterface(camera_);
77 }
78 
79 void View::
80 RemoveSceneAt(SceneBase* scene, int index)
81 {
82  if (index < 0 || index >= (int)scenes_.size()) {
83  BIASERR("Scene index is out of range");
84  return;
85  }
86 
87  vector<SceneBase*>::iterator itor = scenes_.begin();
88  for (int i = 0; i < index; i++) {
89  itor++;
90  }
91  scenes_.erase(itor);
92 }
93 
94 int View::
95 Render()
96 {
97  // get viewport to be restored after the view was rendered
98  GLint oldViewport[4];
99  glGetIntegerv(GL_VIEWPORT, oldViewport);
100 
101  // set viewport
102  glViewport(viewport_[0], viewport_[1], viewport_[2], viewport_[3]);
103 
104  // draw background quad to clear the depth buffer and color buffer in the viewport
105  glPushAttrib(GL_DEPTH_BUFFER_BIT);
106  glEnable(GL_DEPTH_TEST);
107  glDepthFunc(GL_ALWAYS);
108 
109  glMatrixMode(GL_MODELVIEW);
110  glPushMatrix();
111  glLoadIdentity();
112 
113  glMatrixMode(GL_PROJECTION);
114  glPushMatrix();
115  glLoadIdentity();
116 
117  glBegin(GL_QUADS);
118  glColor4f(clearColor_[0], clearColor_[1], clearColor_[2], clearColor_[3]);
119  glVertex3f(-1, -1, 1);
120  glVertex3f( 1, -1, 1);
121  glVertex3f( 1, 1, 1);
122  glVertex3f(-1, 1, 1);
123  glEnd();
124 
125  glMatrixMode(GL_PROJECTION);
126  glPopMatrix();
127 
128  glMatrixMode(GL_MODELVIEW);
129  glPopMatrix();
130 
131  glPopAttrib();
132 
133  // draw scenes through the GLProjection
134  int result = 0;
135  if (camera_ != NULL) {
136  result = camera_->Draw(scenes_, backgroundImageScene_);
137  /*
138  for (int i = 0; i < (int)scenes_.size(); i++) {
139  scenes_[i]->FinishedDraw();
140  }
141  */
142  }
143 
144  // restore old viewport
145  glViewport(oldViewport[0],
146  oldViewport[1],
147  oldViewport[2],
148  oldViewport[3]);
149 
150  return result;
151 }
Scene that renders a background image behind all other scenes.
Definition: SceneBGImage.hh:42
Abstract interface class to handle changes in rendering parameters by controllers and in rendering co...
Base class for all scenes.
Definition: SceneBase.hh:68
virtual void SetGLProjectionParametersInterface(GLProjectionParametersInterface *nc)
Set the camera as projectionparametersinterface, can be of type GLProjection of of any from GLProject...
Definition: SceneBase.hh:80