Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rectangle.cc
Go to the documentation of this file.
1 
18 #include <memory>
19 
20 #include "ion/gfx/node.h"
21 #include "ion/gfx/renderer.h"
23 #include "ion/gfx/shape.h"
24 #include "ion/gfx/statetable.h"
25 #include "ion/gfx/uniform.h"
27 #include "ion/math/matrix.h"
28 #include "ion/math/range.h"
29 #include "ion/math/vector.h"
30 
32 #include "GL/freeglut.h"
33 
34 namespace {
35 
37 
42 
43 
44 struct GlobalState {
45  int window_width;
46  int window_height;
47  ion::gfx::NodePtr scene_root;
48  ion::gfx::RendererPtr renderer;
49 };
50 
51 static std::unique_ptr<GlobalState> s_global_state;
52 
54 
59 
60 
61 static const ion::gfx::NodePtr BuildGraph(int window_width, int window_height) {
63 
66  rect_spec.size.Set(2.f, 2.f);
67  root->AddShape(ion::gfxutils::BuildRectangleShape(rect_spec));
68 
69  ion::gfx::StateTablePtr state_table(
70  new ion::gfx::StateTable(window_width, window_height));
71  state_table->SetViewport(
72  ion::math::Range2i::BuildWithSize(ion::math::Point2i(0, 0),
73  ion::math::Vector2i(window_width,
74  window_height)));
75  state_table->SetClearColor(ion::math::Vector4f(0.3f, 0.3f, 0.5f, 1.0f));
76  state_table->SetClearDepthValue(1.f);
77  state_table->Enable(ion::gfx::StateTable::kDepthTest, true);
78  state_table->Enable(ion::gfx::StateTable::kCullFace, true);
79  root->SetStateTable(state_table);
80 
81  const ion::gfx::ShaderInputRegistryPtr& global_reg =
83  const ion::math::Matrix4f proj(1.732f, 0.0f, 0.0f, 0.0f,
84  0.0f, 1.732f, 0.0f, 0.0f,
85  0.0f, 0.0f, -1.905f, -13.798f,
86  0.0f, 0.0f, -1.0f, 0.0f);
87  const ion::math::Matrix4f view(1.0f, 0.0f, 0.0f, 0.0f,
88  0.0f, 1.0f, 0.0f, 0.0f,
89  0.0f, 0.0f, 1.0f, -5.0f,
90  0.0f, 0.0f, 0.0f, 1.0f);
91  root->AddUniform(global_reg->Create<ion::gfx::Uniform>(
92  "uProjectionMatrix", proj));
93  root->AddUniform(global_reg->Create<ion::gfx::Uniform>(
94  "uModelviewMatrix", view));
95  root->AddUniform(global_reg->Create<ion::gfx::Uniform>(
96  "uBaseColor", ion::math::Vector4f(1.f, 1.f, 0.f, 1.f)));
97 
98  return root;
99 }
100 
102 
107 
108 
109 static void Resize(int w, int h) {
110  s_global_state->window_width = w;
111  s_global_state->window_height = h;
112  glutPostRedisplay();
113 }
114 
115 static void Render() {
116  if (s_global_state.get())
117  s_global_state->renderer->DrawScene(s_global_state->scene_root);
118  glutSwapBuffers();
119 }
120 
121 static void Update() {
122  glutPostRedisplay();
123 }
124 
125 static void Keyboard(unsigned char key, int x, int y) {
126  glutPostRedisplay();
127 }
128 
129 static void KeyboardUp(unsigned char key, int x, int y) {
130  switch (key) {
131  case 27: // Escape.
132  s_global_state.reset(NULL);
133  glutLeaveMainLoop();
134  break;
135  }
136  glutPostRedisplay();
137 }
138 
139 } // anonymous namespace
140 
142 
147 
148 
149 int main(int argc, char* argv[]) {
150  glutInit(&argc, argv);
151 
152  s_global_state.reset(new GlobalState);
153  s_global_state->window_width = s_global_state->window_height = 800;
154  s_global_state->scene_root = BuildGraph(s_global_state->window_width,
155  s_global_state->window_height);
156 
157  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
158  glutSetOption(GLUT_MULTISAMPLE, 16);
159  glutInitWindowSize(s_global_state->window_width,
160  s_global_state->window_height);
161 
162  glutCreateWindow("Ion rectangle example");
163  glutDisplayFunc(Render);
164  glutReshapeFunc(Resize);
165  glutKeyboardFunc(Keyboard);
166  glutKeyboardUpFunc(KeyboardUp);
167  glutIdleFunc(Update);
168 
171  s_global_state->renderer.Reset(new ion::gfx::Renderer(graphics_manager));
172 
173  glutMainLoop();
174 }
GraphicsManager manages the graphics library for an application.
static const ShaderInputRegistryPtr & GetGlobalRegistry()
Returns the ShaderInputRegistry instance representing all supported global uniforms and attributes...
int main(int argc, char *argv[])
Mainline.
Definition: rectangle.cc:149
A Uniform instance represents a uniform shader argument.
Definition: uniform.h:76
The Matrix class defines a square N-dimensional matrix.
Definition: matrix.h:35
A StateTable represents a collection of graphical state items that affect OpenGL rendering.
Definition: statetable.h:48
A Node instance represents a node in a scene graph.
Definition: node.h:45
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
const gfx::ShapePtr BuildRectangleShape(const RectangleSpec &spec)
Builds and returns a Shape representing a rectangle in one of the principal Cartesian planes...
Definition: shapeutils.cc:1290
The Renderer class handles rendering ION scene graphs using OpenGL.
Definition: renderer.h:50