Ion: doc/usersguide/examples/rectangle.cc Source File

1 

18 #include <memory>

19 

30 

32 #include "GL/freeglut.h"

33 

34 namespace {

35 

37 

42 

43 

44 struct GlobalState {

45  int window_width;

46  int window_height;

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);

68 

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);

79  root->SetStateTable(state_table);

80 

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);

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);

92  "uProjectionMatrix", proj));

94  "uModelviewMatrix", view));

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:

132  s_global_state.reset(NULL);

133  glutLeaveMainLoop();

134  break;

135  }

136  glutPostRedisplay();

137 }

138 

139 }

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.

A Uniform instance represents a uniform shader argument.

The Matrix class defines a square N-dimensional matrix.

A StateTable represents a collection of graphical state items that affect OpenGL rendering.

A Node instance represents a node in a scene graph.

const gfx::ShapePtr BuildRectangleShape(const RectangleSpec &spec)

Builds and returns a Shape representing a rectangle in one of the principal Cartesian planes...

The Renderer class handles rendering ION scene graphs using OpenGL.