41 #include "GL/freeglut.h"
60 static std::unique_ptr<GlobalState> s_global_state;
70 static const char* kVertexShaderString = (
71 "uniform mat4 uProjectionMatrix;\n"
72 "uniform mat4 uModelviewMatrix;\n"
73 "uniform mat4 uTextureMatrix;\n"
74 "attribute vec3 aVertex;\n"
75 "attribute vec2 aTexCoords;\n"
76 "attribute vec3 aNormal;\n"
77 "attribute float aOffsetAlongNormal;\n"
78 "varying vec3 vPosition;\n"
79 "varying vec2 vTexCoords;\n"
80 "varying vec3 vNormal;\n"
83 " vTexCoords = (uTextureMatrix * vec4(aTexCoords, 0., 1.)).st;\n"
84 " vPosition = aVertex + aOffsetAlongNormal * aNormal;\n"
85 " vNormal = aNormal;\n"
86 " gl_Position = uProjectionMatrix * uModelviewMatrix *\n"
87 " vec4(vPosition, 1.);\n"
90 static const char* kFragmentShaderString = (
92 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
93 "precision highp float;\n"
95 "precision mediump float;\n"
99 "uniform sampler2D uSampler;\n"
100 "varying vec3 vPosition;\n"
101 "varying vec2 vTexCoords;\n"
102 "varying vec3 vNormal;\n"
104 "void main(void) {\n"
105 " vec3 dir_to_light = normalize(vec3(6., 3., 10.));\n"
106 " float intensity = .3 * abs(dot(dir_to_light, vNormal));\n"
107 " gl_FragColor = intensity * texture2D(uSampler, vTexCoords);\n"
121 ion::math::Vector3f
normal;
122 float offset_along_normal;
126 const ion::math::Point3f apex(0.f, 1.f, 0.f);
127 const ion::math::Point3f back_left(-1.f, -1.f, -1.f);
128 const ion::math::Point3f back_right(1.f, -1.f, -1.f);
129 const ion::math::Point3f front_left(-1.f, -1.f, 1.f);
130 const ion::math::Point3f front_right(1.f, -1.f, 1.f);
132 static const size_t kVertexCount = 12U;
133 Vertex vertices[kVertexCount];
136 vertices[0].position = front_left;
137 vertices[1].position = front_right;
138 vertices[2].position = apex;
139 vertices[3].position = front_right;
140 vertices[4].position = back_right;
141 vertices[5].position = apex;
142 vertices[6].position = back_right;
143 vertices[7].position = back_left;
144 vertices[8].position = apex;
145 vertices[9].position = back_left;
146 vertices[10].position = front_left;
147 vertices[11].position = apex;
150 for (
int face = 0; face < 4; ++face) {
151 Vertex& v0 = vertices[3 * face + 0];
152 Vertex& v1 = vertices[3 * face + 1];
153 Vertex& v2 = vertices[3 * face + 2];
155 v1.position - v0.position, v2.position - v0.position);
156 v0.texture_coords.Set(0.f, 0.f);
157 v1.texture_coords.Set(1.f, 0.f);
158 v2.texture_coords.Set(.5f, 1.f);
162 for (
size_t v = 0; v < kVertexCount; ++v)
163 vertices[v].offset_along_normal = .05f * static_cast<float>(1U + v % 2U);
166 ion::base::DataContainer::CreateAndCopy<Vertex>(
169 buffer_object->SetData(data_container,
sizeof(vertices[0]), kVertexCount,
171 return buffer_object;
181 .Bind(v.position,
"aVertex")
182 .
Bind(v.texture_coords,
"aTexCoords")
183 .
Bind(v.normal,
"aNormal")
184 .
Bind(v.offset_along_normal,
"aOffsetAlongNormal")
185 .
Apply(reg, attribute_array, buffer_object);
186 return attribute_array;
192 static const size_t kIndexCount = 12U;
193 uint16 indices[kIndexCount];
194 for (
size_t i = 0; i < kIndexCount; ++i)
195 indices[i] = static_cast<uint16>(i);
198 ion::base::DataContainer::CreateAndCopy<uint16>(
202 index_buffer->SetData(data_container,
sizeof(indices[0]), kIndexCount,
211 shape->SetLabel(
"Pyramid");
213 shape->SetAttributeArray(BuildPyramidAttributeArray(reg));
214 shape->SetIndexBuffer(BuildPyramidIndexBuffer());
222 ion::math::Vector3f::AxisZ(),
223 ion::math::Anglef::FromDegrees(degrees)) *
229 static const int kWidth = 2;
230 static const int kHeight = 2;
231 static const int kRowSize = kWidth * 3;
232 static const uint8 pixels[kHeight * kRowSize] = {
233 0xee, 0x22, 0xee, 0x00, 0x55, 0xdd,
234 0x00, 0xdd, 0xaa, 0xdd, 0xcc, 0x33,
239 ion::base::DataContainer::CreateAndCopy<uint8>(
260 state_table->SetViewport(
261 ion::math::Range2i::BuildWithSize(ion::math::Point2i(0, 0),
262 ion::math::Vector2i(window_width,
264 state_table->SetClearColor(ion::math::Vector4f(0.3f, 0.3f, 0.5f, 1.0f));
265 state_table->SetClearDepthValue(1.f);
268 root->SetStateTable(state_table);
271 reg->IncludeGlobalRegistry();
274 "Offset of each vertex along its surface normal vector"));
277 "Matrix applied to texture coordinates"));
281 root->SetShaderProgram(
283 "Example shader", reg, kVertexShaderString,
286 root->AddShape(BuildPyramidShape(reg));
293 ion::math::Point3f::Zero(),
294 ion::math::Vector3f::AxisY());
313 static void Resize(
int w,
int h) {
314 s_global_state->window_width = w;
315 s_global_state->window_height = h;
319 static void Render() {
320 if (s_global_state.get())
321 s_global_state->renderer->DrawScene(s_global_state->scene_root);
325 static void Update() {
329 static void Keyboard(
unsigned char key,
int x,
int y) {
333 static void KeyboardUp(
unsigned char key,
int x,
int y) {
336 s_global_state.reset(NULL);
353 int main(
int argc,
char* argv[]) {
354 glutInit(&argc, argv);
356 s_global_state.reset(
new GlobalState);
357 s_global_state->window_width = s_global_state->window_height = 800;
358 s_global_state->scene_root = BuildGraph(s_global_state->window_width,
359 s_global_state->window_height);
361 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
362 glutSetOption(GLUT_MULTISAMPLE, 16);
363 glutInitWindowSize(s_global_state->window_width,
364 s_global_state->window_height);
366 glutCreateWindow(
"Ion shape example");
367 glutDisplayFunc(Render);
368 glutReshapeFunc(Resize);
369 glutKeyboardFunc(Keyboard);
370 glutKeyboardUpFunc(KeyboardUp);
371 glutIdleFunc(Update);
GraphicsManager manages the graphics library for an application.
A Texture object represents the image data and mipmaps associated with a single texture.
An IndexBuffer is a type of BufferObject that contains the element indices of an array, e.g., a vertex index array.
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.
SharedPtr< Allocator > AllocatorPtr
ION_API const Matrix< 4, T > LookAtMatrixFromCenter(const Point< 3, T > &eye, const Point< 3, T > ¢er, const Vector< 3, T > &up)
View matrices.
An Image represents 2D image data that can be used in a texture supplied to a shader.
A BufferObject describes a generic array of data used, for example, to describe the vertices in a Sha...
The Matrix class defines a square N-dimensional matrix.
A Shape object represents a shape (vertices + indices) to draw.
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...
An AttributeArray represents a collection of Attributes used to describe the vertices of a Shape...
const Grid & image
The original monochrome image data, as doubles (0 - 1).
BufferToAttributeBinder is a simple interface to insert a set of Attributes containing BufferObjectEl...
BufferToAttributeBinder & Bind(const FieldType &field, const std::string &attribute_name)
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.
Vector< 3, T > Cross(const Vector< 3, T > &v0, const Vector< 3, T > &v1)
Returns the 3-dimensional cross product of 2 Vectors.
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.
A Sampler object represents texture parameters that control how texture data is accessed in shaders...
void Apply(const gfx::ShaderInputRegistryPtr ®, const gfx::AttributeArrayPtr &aa, const gfx::BufferObjectPtr &bo)