2 #include <msp/gl/box.h>
3 #include <msp/gl/cylinder.h>
4 #include <msp/gl/framebuffer.h>
5 #include <msp/gl/light.h>
6 #include <msp/gl/lighting.h>
7 #include <msp/gl/material.h>
8 #include <msp/gl/mesh.h>
9 #include <msp/gl/meshbuilder.h>
10 #include <msp/gl/program.h>
11 #include <msp/gl/programdata.h>
12 #include <msp/gl/renderer.h>
13 #include <msp/gl/tests.h>
14 #include <msp/gl/texturecube.h>
15 #include <msp/graphics/simplewindow.h>
19 void create_face_texture(GL::TextureCube &texture, GL::TextureCubeFace face)
21 unsigned char *pixels = new unsigned char[128*128*3];
22 for(int y=0; y<128; ++y)
23 for(int x=0; x<128; ++x)
25 unsigned i = (x+y*128)*3;
26 GL::Vector3 v = texture.get_texel_direction(face, x, y);
29 pixels[i] = 96-48*v.y;
30 pixels[i+1] = 168-84*v.y;
35 pixels[i] = rand()%32;
36 pixels[i+1] = 128-rand()%32;
37 pixels[i+2] = rand()%32;
40 texture.image(face, 0, GL::RGB, GL::UNSIGNED_BYTE, pixels);
46 Graphics::SimpleGLWindow window(600, 400);
48 GL::TextureCube texture;
49 texture.storage(GL::RGB, 128);
50 texture.set_min_filter(GL::LINEAR);
51 texture.set_wrap(GL::CLAMP_TO_EDGE);
52 for(unsigned i=0; i<6; ++i)
53 create_face_texture(texture, texture.enumerate_faces(i));
55 GL::Mesh skybox((GL::VERTEX3, GL::TEXCOORD3));
56 GL::BoxBuilder(10, 10, 10).build(skybox);
57 for(unsigned i=0; i<skybox.get_n_vertices(); ++i)
59 float *v = skybox.modify_vertex(i);
60 for(unsigned j=0; j<3; ++j)
64 GL::Mesh box((GL::VERTEX3, GL::NORMAL3));
65 GL::BoxBuilder(2, 2, 2).build(box);
67 GL::Mesh cylinder((GL::VERTEX3, GL::NORMAL3));
68 GL::CylinderBuilder(1, 2, 32).build(cylinder);
70 GL::Lighting lighting;
71 lighting.set_ambient(GL::Color(0.3));
73 light0.set_diffuse(GL::Color(0.6));
74 light0.set_position(GL::Vector4(-1, 1, 1, 0));
75 lighting.attach(0, light0);
77 light1.set_diffuse(GL::Color(0.2));
78 light1.set_position(GL::Vector4(1, 1, 0, 0));
79 lighting.attach(1, light1);
81 GL::Material material;
82 material.set_diffuse(GL::Color(0.4));
83 material.set_ambient(GL::Color(0.4));
84 material.set_specular(GL::Color(1.0));
85 material.set_shininess(100);
87 GL::ProgramBuilder::StandardFeatures features;
88 features.lighting = true;
89 features.specular = true;
90 features.material = true;
91 features.reflection = true;
92 GL::Program shprog(features);
93 GL::ProgramData shdata;
94 shdata.uniform("environment", 0);
95 shdata.uniform("reflectivity", 0.5f);
96 float env_mat[9] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
97 shdata.uniform_matrix3("env_eye_matrix", env_mat);
99 GL::MatrixStack::projection() = GL::Matrix::frustum_centered(0.15, 0.1, 0.1, 10);
106 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
108 GL::Renderer renderer(0);
109 renderer.set_texture(&texture);
110 skybox.draw(renderer);
113 GL::Bind bind_light(lighting);
114 GL::Bind bind_depth(GL::DepthTest::lequal());
115 GL::Renderer renderer(0);
116 renderer.set_material(&material);
117 renderer.set_shader_program(&shprog, &shdata);
118 renderer.set_texture(&texture);
119 renderer.transform(GL::Matrix::translation(0, 0, -7));
121 GL::Renderer::Push _push(renderer);
122 renderer.transform(GL::Matrix::translation(-2, 0, 0));
123 renderer.transform(GL::Matrix::rotation(angle/2.3, 0, 1, 0));
124 renderer.transform(GL::Matrix::rotation(angle, 1, 0.25, 0));
128 GL::Renderer::Push _push(renderer);
129 renderer.transform(GL::Matrix::translation(2, 0, 0));
130 renderer.transform(GL::Matrix::rotation(-angle/2.3, 0, 1, 0));
131 renderer.transform(GL::Matrix::rotation(angle, 1, 0.25, 0));
132 cylinder.draw(renderer);
135 window.swap_buffers();