]> git.tdb.fi Git - libs/gl.git/blob - demos/cubemap.cpp
Add bounding sphere passthrough to Effect
[libs/gl.git] / demos / cubemap.cpp
1 #include <cstdlib>
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>
16
17 using namespace Msp;
18
19 void create_face_texture(GL::TextureCube &texture, GL::TextureCubeFace face)
20 {
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)
24                 {
25                         unsigned i = (x+y*128)*3;
26                         GL::Vector3 v = texture.get_texel_direction(face, x, y);
27                         if(v.y>0)
28                         {
29                                 pixels[i] = 96-48*v.y;
30                                 pixels[i+1] = 168-84*v.y;
31                                 pixels[i+2] = 255;
32                         }
33                         else
34                         {
35                                 pixels[i] = rand()%32;
36                                 pixels[i+1] = 128-rand()%32;
37                                 pixels[i+2] = rand()%32;
38                         }
39                 }
40         texture.image(face, 0, GL::RGB, GL::UNSIGNED_BYTE, pixels);
41         delete[] pixels;
42 }
43
44 int main()
45 {
46         Graphics::SimpleGLWindow window(600, 400);
47
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));
54
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)
58         {
59                 float *v = skybox.modify_vertex(i);
60                 for(unsigned j=0; j<3; ++j)
61                         v[3+j] = v[j];
62         }
63
64         GL::Mesh box((GL::VERTEX3, GL::NORMAL3));
65         GL::BoxBuilder(2, 2, 2).build(box);
66
67         GL::Mesh cylinder((GL::VERTEX3, GL::NORMAL3));
68         GL::CylinderBuilder(1, 2, 32).build(cylinder);
69
70         GL::Lighting lighting;
71         lighting.set_ambient(GL::Color(0.3));
72         GL::Light light0;
73         light0.set_diffuse(GL::Color(0.6));
74         light0.set_position(GL::Vector4(-1, 1, 1, 0));
75         lighting.attach(0, light0);
76         GL::Light light1;
77         light1.set_diffuse(GL::Color(0.2));
78         light1.set_position(GL::Vector4(1, 1, 0, 0));
79         lighting.attach(1, light1);
80
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);
86
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);
98
99         GL::MatrixStack::projection() = GL::Matrix::frustum_centered(0.15, 0.1, 0.1, 10);
100
101         window.show();
102         float angle = 0;
103         while(1)
104         {
105                 window.tick();
106                 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
107                 {
108                         GL::Renderer renderer(0);
109                         renderer.set_texture(&texture);
110                         skybox.draw(renderer);
111                 }
112                 {
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));
120                         {
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));
125                                 box.draw(renderer);
126                         }
127                         {
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);
133                         }
134                 }
135                 window.swap_buffers();
136                 angle += 0.0001;
137         }
138
139         return 0;
140 }