33 #include "GL/freeglut.h"
52 static std::unique_ptr<GlobalState> s_global_state;
62 static const char* kVertexShaderString = (
63 "uniform mat4 uProjectionMatrix;\n"
64 "uniform mat4 uModelviewMatrix;\n"
65 "uniform vec4 uTopColor;\n"
66 "uniform vec4 uBottomColor;\n"
67 "attribute vec3 aVertex;\n"
68 "varying vec3 vPosition;\n"
69 "varying vec4 vColor;\n"
72 " vPosition = aVertex;\n"
73 " vColor = mix(uBottomColor, uTopColor, .5 * (1. + vPosition.y));\n"
74 " gl_Position = uProjectionMatrix * uModelviewMatrix *\n"
75 " vec4(aVertex, 1.);\n"
78 static const char* kFragmentShaderString = (
80 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
81 "precision highp float;\n"
83 "precision mediump float;\n"
87 "uniform float uWaveFrequency;\n"
88 "varying vec3 vPosition;\n"
89 "varying vec4 vColor;\n"
92 " float nx = sin(uWaveFrequency * radians(90.) * vPosition.x);\n"
93 " vec3 normal = normalize(vec3(nx, 0., .5));\n"
94 " vec3 dir_to_light = normalize(vec3(1., 2., 10.));\n"
95 " float intensity = max(0.0, dot(dir_to_light, normal));\n"
96 " gl_FragColor = intensity * vColor;\n"
112 rect_spec.
size.Set(2.f, 2.f);
117 state_table->SetViewport(
118 ion::math::Range2i::BuildWithSize(ion::math::Point2i(0, 0),
119 ion::math::Vector2i(window_width,
121 state_table->SetClearColor(ion::math::Vector4f(0.3f, 0.3f, 0.5f, 1.0f));
122 state_table->SetClearDepthValue(1.f);
125 root->SetStateTable(state_table);
128 reg->IncludeGlobalRegistry();
131 "Color at the top of the rectangle"));
134 "Color at the bottom of the rectangle"));
137 "Frequency of the sine wave applied to the rectangle normal"));
138 root->SetShaderProgram(
140 "Example shader", reg, kVertexShaderString,
144 0.0f, 1.732f, 0.0f, 0.0f,
145 0.0f, 0.0f, -1.905f, -13.798f,
146 0.0f, 0.0f, -1.0f, 0.0f);
148 0.0f, 1.0f, 0.0f, 0.0f,
149 0.0f, 0.0f, 1.0f, -5.0f,
150 0.0f, 0.0f, 0.0f, 1.0f);
154 "uTopColor", ion::math::Vector4f(1.f, .5f, .5f, 1.f)));
156 "uBottomColor", ion::math::Vector4f(.5f, .5f, 1.f, 1.f)));
170 static void Resize(
int w,
int h) {
171 s_global_state->window_width = w;
172 s_global_state->window_height = h;
176 static void Render() {
177 if (s_global_state.get())
178 s_global_state->renderer->DrawScene(s_global_state->scene_root);
182 static void Update() {
186 static void Keyboard(
unsigned char key,
int x,
int y) {
190 static void KeyboardUp(
unsigned char key,
int x,
int y) {
193 s_global_state.reset(NULL);
210 int main(
int argc,
char* argv[]) {
211 glutInit(&argc, argv);
213 s_global_state.reset(
new GlobalState);
214 s_global_state->window_width = s_global_state->window_height = 800;
215 s_global_state->scene_root = BuildGraph(s_global_state->window_width,
216 s_global_state->window_height);
218 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
219 glutSetOption(GLUT_MULTISAMPLE, 16);
220 glutInitWindowSize(s_global_state->window_width,
221 s_global_state->window_height);
223 glutCreateWindow(
"Ion shaders example");
224 glutDisplayFunc(Render);
225 glutReshapeFunc(Resize);
226 glutKeyboardFunc(Keyboard);
227 glutKeyboardUpFunc(KeyboardUp);
228 glutIdleFunc(Update);
GraphicsManager manages the graphics library for an application.
The Matrix class defines a square N-dimensional matrix.
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...
int main(int argc, char *argv[])
Mainline.
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...
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.