34 #include "GL/freeglut.h"
53 static std::unique_ptr<GlobalState> s_global_state;
63 static const char* kNode1VertexShaderString = (
64 "uniform mat4 uProjectionMatrix;\n"
65 "uniform mat4 uModelviewMatrix;\n"
66 "uniform vec4 uTopColor;\n"
67 "uniform vec4 uBottomColor;\n"
68 "attribute vec3 aVertex;\n"
69 "attribute vec3 aNormal;\n"
70 "varying vec3 vNormal;\n"
71 "varying vec4 vColor;\n"
74 " vNormal = aNormal;\n"
75 " vColor = mix(uBottomColor, uTopColor, .5 * (1. + aVertex.y));\n"
76 " gl_Position = uProjectionMatrix * uModelviewMatrix *\n"
77 " vec4(aVertex, 1.);\n"
80 static const char* kNode1FragmentShaderString = (
82 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
83 "precision highp float;\n"
85 "precision mediump float;\n"
89 "varying vec3 vNormal;\n"
90 "varying vec4 vColor;\n"
93 " vec3 normal = normalize(vNormal);\n"
94 " vec3 dir_to_light = normalize(vec3(1., 4., 8.));\n"
95 " float intensity = min(1., abs(dot(dir_to_light, normal)));\n"
96 " gl_FragColor = intensity * vColor;\n"
99 static const char* kNode3VertexShaderString = (
100 "uniform mat4 uProjectionMatrix;\n"
101 "uniform mat4 uModelviewMatrix;\n"
102 "attribute vec3 aVertex;\n"
103 "attribute vec3 aNormal;\n"
104 "varying vec3 vNormal;\n"
106 "void main(void) {\n"
107 " vNormal = aNormal;\n"
108 " gl_Position = uProjectionMatrix * uModelviewMatrix *\n"
109 " vec4(aVertex, 1.);\n"
112 static const char* kNode3FragmentShaderString = (
114 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
115 "precision highp float;\n"
117 "precision mediump float;\n"
121 "uniform vec4 uBaseColor;\n"
122 "uniform vec3 uOpenDirection;\n"
123 "varying vec3 vNormal;\n"
125 "void main(void) {\n"
126 " vec3 normal = normalize(vNormal);\n"
127 " if (dot(vNormal, uOpenDirection) > .9)\n"
129 " vec3 dir_to_light = normalize(vec3(1., 1., 2.));\n"
130 " float intensity = min(1., abs(dot(dir_to_light, normal)));\n"
131 " gl_FragColor = intensity * uBaseColor;\n"
147 sphere_spec.
size.Set(2.f, 2.f, 2.f);
152 state_table->SetViewport(
153 ion::math::Range2i::BuildWithSize(ion::math::Point2i(0, 0),
154 ion::math::Vector2i(window_width,
156 state_table->SetClearColor(ion::math::Vector4f(0.3f, 0.3f, 0.5f, 1.0f));
157 state_table->SetClearDepthValue(1.f);
160 node1->SetStateTable(state_table);
163 reg->IncludeGlobalRegistry();
166 "Color at the top of the rectangle"));
169 "Color at the bottom of the rectangle"));
170 node1->SetShaderProgram(
172 "Node1 shader", reg, kNode1VertexShaderString,
180 ion::math::Point3f(0.f, -1.f, 0.f),
181 ion::math::Vector3f::AxisY());
185 "uTopColor", ion::math::Vector4f(1.f, .2f, .2f, 1.f)));
187 "uBottomColor", ion::math::Vector4f(.2f, 1.f, 1.f, 1.f)));
198 box_spec.
size.Set(3.f, 2.f, 1.f);
203 node2->SetStateTable(state_table);
206 "uTopColor", ion::math::Vector4f(.9f, .9f, .2f, 1.f)));
208 "uBottomColor", ion::math::Vector4f(.9f, .1f, .9f, 1.f)));
221 cylinder_spec.
height = 2.f;
225 reg->IncludeGlobalRegistry();
228 "Surface normal direction near cut-out"));
229 node3->SetShaderProgram(
231 "Node3 shader", reg, kNode3VertexShaderString,
238 "uBaseColor", ion::math::Vector4f(.9f, .9f, .7f, 1.f)));
240 "uOpenDirection", ion::math::Vector3f::AxisZ()));
247 node1->AddChild(BuildNode2(node1->GetShaderProgram()->GetRegistry()));
248 node1->AddChild(BuildNode3());
260 static void Resize(
int w,
int h) {
261 s_global_state->window_width = w;
262 s_global_state->window_height = h;
266 static void Render() {
267 if (s_global_state.get())
268 s_global_state->renderer->DrawScene(s_global_state->scene_root);
272 static void Update() {
276 static void Keyboard(
unsigned char key,
int x,
int y) {
280 static void KeyboardUp(
unsigned char key,
int x,
int y) {
283 s_global_state.reset(NULL);
300 int main(
int argc,
char* argv[]) {
301 glutInit(&argc, argv);
303 s_global_state.reset(
new GlobalState);
304 s_global_state->window_width = s_global_state->window_height = 800;
305 s_global_state->scene_root = BuildGraph(s_global_state->window_width,
306 s_global_state->window_height);
308 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
309 glutSetOption(GLUT_MULTISAMPLE, 16);
310 glutInitWindowSize(s_global_state->window_width,
311 s_global_state->window_height);
313 glutCreateWindow(
"Ion hierarchy example");
314 glutDisplayFunc(Render);
315 glutReshapeFunc(Resize);
316 glutKeyboardFunc(Keyboard);
317 glutKeyboardUpFunc(KeyboardUp);
318 glutIdleFunc(Update);
GraphicsManager manages the graphics library for an application.
int main(int argc, char *argv[])
Mainline.
const gfx::ShapePtr BuildEllipsoidShape(const EllipsoidSpec &spec)
Builds and returns a Shape representing an axis-aligned ellipsoid.
ION_API const Matrix< 4, T > LookAtMatrixFromCenter(const Point< 3, T > &eye, const Point< 3, T > ¢er, const Vector< 3, T > &up)
View matrices.
const gfx::ShapePtr BuildCylinderShape(const CylinderSpec &spec)
Builds and returns a Shape representing an axis-aligned cylinder.
The Matrix class defines a square N-dimensional matrix.
const Matrix< Dimension+1, T > TranslationMatrix(const VectorBase< Dimension, T > &t)
Affine transformation matrices.
ION_API const Matrix< 4, T > PerspectiveMatrixFromView(const Angle< T > &fovy, T aspect, T z_near, T z_far)
Returns a 4x4 perspective projection matrix based on the given parameters, which follow the conventio...
const gfx::ShapePtr BuildBoxShape(const BoxSpec &spec)
Builds and returns a Shape representing an axis-aligned box.
static const ShaderProgramPtr BuildFromStrings(const std::string &id_string, const ShaderInputRegistryPtr ®istry_ptr, const std::string &vertex_shader_string, const std::string &fragment_shader_string, const base::AllocatorPtr &allocator)
Convenience function that builds and returns a new ShaderProgram instance that uses the given ShaderI...
A StateTable represents a collection of graphical state items that affect OpenGL rendering.
A Node instance represents a node in a scene graph.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
The Renderer class handles rendering ION scene graphs using OpenGL.