38 #include "GL/freeglut.h"
57 static std::unique_ptr<GlobalState> s_global_state;
67 static const char* kVertexShaderString = (
68 "uniform mat4 uProjectionMatrix;\n"
69 "uniform mat4 uModelviewMatrix;\n"
70 "uniform mat4 uTextureMatrix;\n"
71 "attribute vec3 aVertex;\n"
72 "attribute vec2 aTexCoords;\n"
73 "varying vec3 vPosition;\n"
74 "varying vec2 vTexCoords;\n"
77 " vTexCoords = (uTextureMatrix * vec4(aTexCoords, 0., 1.)).st;\n"
78 " vPosition = aVertex;\n"
79 " gl_Position = uProjectionMatrix * uModelviewMatrix *\n"
80 " vec4(aVertex, 1.);\n"
83 static const char* kFragmentShaderString = (
85 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
86 "precision highp float;\n"
88 "precision mediump float;\n"
92 "uniform sampler2D uSampler;\n"
93 "uniform float uWaveFrequency;\n"
94 "varying vec3 vPosition;\n"
95 "varying vec2 vTexCoords;\n"
98 " float nx = sin(uWaveFrequency * radians(90.) * vPosition.x);\n"
99 " vec3 normal = normalize(vec3(nx, 0., .5));\n"
100 " vec3 dir_to_light = normalize(vec3(1., 2., 10.));\n"
101 " float intensity = max(0.0, dot(dir_to_light, normal));\n"
102 " gl_FragColor = intensity * texture2D(uSampler, vTexCoords);\n"
117 ion::math::Vector3f::AxisZ(),
118 ion::math::Anglef::FromDegrees(degrees)) *
124 static const int kWidth = 2;
125 static const int kHeight = 2;
126 static const int kRowSize = kWidth * 3;
127 static const uint8 pixels[kHeight * kRowSize] = {
128 0xee, 0x22, 0xee, 0x00, 0x55, 0xdd,
129 0x00, 0xdd, 0xaa, 0xdd, 0xcc, 0x33,
134 ion::base::DataContainer::CreateAndCopy<uint8>(
155 rect_spec.
size.Set(2.f, 2.f);
160 state_table->SetViewport(
161 ion::math::Range2i::BuildWithSize(ion::math::Point2i(0, 0),
162 ion::math::Vector2i(window_width,
164 state_table->SetClearColor(ion::math::Vector4f(0.3f, 0.3f, 0.5f, 1.0f));
165 state_table->SetClearDepthValue(1.f);
168 root->SetStateTable(state_table);
171 reg->IncludeGlobalRegistry();
174 "Matrix applied to texture coordinates"));
180 "Frequency of the sine wave applied to the rectangle normal"));
181 root->SetShaderProgram(
183 "Example shader", reg, kVertexShaderString,
187 0.0f, 1.732f, 0.0f, 0.0f,
188 0.0f, 0.0f, -1.905f, -13.798f,
189 0.0f, 0.0f, -1.0f, 0.0f);
191 0.0f, 1.0f, 0.0f, 0.0f,
192 0.0f, 0.0f, 1.0f, -5.0f,
193 0.0f, 0.0f, 0.0f, 1.0f);
213 static void Resize(
int w,
int h) {
214 s_global_state->window_width = w;
215 s_global_state->window_height = h;
219 static void Render() {
220 if (s_global_state.get())
221 s_global_state->renderer->DrawScene(s_global_state->scene_root);
225 static void Update() {
229 static void Keyboard(
unsigned char key,
int x,
int y) {
233 static void KeyboardUp(
unsigned char key,
int x,
int y) {
236 s_global_state.reset(NULL);
253 int main(
int argc,
char* argv[]) {
254 glutInit(&argc, argv);
256 s_global_state.reset(
new GlobalState);
257 s_global_state->window_width = s_global_state->window_height = 800;
258 s_global_state->scene_root = BuildGraph(s_global_state->window_width,
259 s_global_state->window_height);
261 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
262 glutSetOption(GLUT_MULTISAMPLE, 16);
263 glutInitWindowSize(s_global_state->window_width,
264 s_global_state->window_height);
266 glutCreateWindow(
"Ion texture example");
267 glutDisplayFunc(Render);
268 glutReshapeFunc(Resize);
269 glutKeyboardFunc(Keyboard);
270 glutKeyboardUpFunc(KeyboardUp);
271 glutIdleFunc(Update);
GraphicsManager manages the graphics library for an application.
A Texture object represents the image data and mipmaps associated with a single texture.
const Matrix< 4, T > RotationMatrixAxisAngleH(const Vector< 3, T > &axis, const Angle< T > &angle)
Returns a 4x4 Matrix representing a 3D rotation specified as axis and angle.
An Image represents 2D image data that can be used in a texture supplied to a shader.
The Matrix class defines a square N-dimensional matrix.
const Matrix< Dimension+1, T > TranslationMatrix(const VectorBase< Dimension, T > &t)
Affine transformation matrices.
const Grid & image
The original monochrome image data, as doubles (0 - 1).
TexturePtr texture
The Texture to add sub-image data to.
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.
A Sampler object represents texture parameters that control how texture data is accessed in shaders...