]> git.tdb.fi Git - libs/gl.git/blob - demos/cubemap.cpp
A simple program to test cubemap texture functionality
[libs/gl.git] / demos / cubemap.cpp
1 #include <msp/gl/box.h>
2 #include <msp/gl/framebuffer.h>
3 #include <msp/gl/light.h>
4 #include <msp/gl/lighting.h>
5 #include <msp/gl/material.h>
6 #include <msp/gl/mesh.h>
7 #include <msp/gl/meshbuilder.h>
8 #include <msp/gl/renderer.h>
9 #include <msp/gl/tests.h>
10 #include <msp/gl/texturecube.h>
11 #include <msp/graphics/simplewindow.h>
12
13 using namespace Msp;
14
15 void create_face_texture(GL::TextureCube &texture, GL::TextureCubeFace face)
16 {
17         unsigned char *pixels = new unsigned char[128*128*3];
18         for(unsigned y=0; y<128; ++y)
19                 for(unsigned x=0; x<128; ++x)
20                 {
21                         unsigned i = (x+y*128)*3;
22                         pixels[i] = (face==GL::NEGATIVE_X ? 0 : face==GL::POSITIVE_X ? 255 : face==GL::NEGATIVE_Z ? 255-x*2 : x*2);
23                         pixels[i+1] = (face==GL::NEGATIVE_Y ? 0 : face==GL::POSITIVE_Y ? 255 : 255-y*2);
24                         pixels[i+2] = (face==GL::NEGATIVE_Z ? 0 : face==GL::POSITIVE_Z ? 255 : face==GL::POSITIVE_X ? 255-x*2 : face==GL::NEGATIVE_X ? x*2 : face==GL::NEGATIVE_Y ? 255-y*2 : y*2);
25                 }
26         texture.image(face, 0, GL::RGB, GL::UNSIGNED_BYTE, pixels);
27         delete[] pixels;
28 }
29
30 int main()
31 {
32         Graphics::SimpleGLWindow window(400, 400);
33
34         GL::TextureCube texture;
35         texture.storage(GL::RGB, 128);
36         texture.set_min_filter(GL::LINEAR);
37         texture.set_wrap(GL::CLAMP_TO_EDGE);
38         create_face_texture(texture, GL::NEGATIVE_Z);
39         create_face_texture(texture, GL::POSITIVE_Z);
40         create_face_texture(texture, GL::NEGATIVE_Y);
41         create_face_texture(texture, GL::POSITIVE_Y);
42         create_face_texture(texture, GL::NEGATIVE_X);
43         create_face_texture(texture, GL::POSITIVE_X);
44
45         GL::Mesh box((GL::VERTEX3, GL::TEXCOORD3, GL::NORMAL3));
46         GL::MeshBuilder builder(box);
47         GL::BoxBuilder(2, 2, 2).build(builder);
48         for(unsigned i=0; i<box.get_n_vertices(); ++i)
49         {
50                 float *v = box.modify_vertex(i);
51                 for(unsigned j=0; j<3; ++j)
52                         v[3+j] = v[j];
53         }
54
55         GL::Lighting lighting;
56         lighting.set_ambient(GL::Color(0.3));
57         GL::Light light0;
58         light0.set_diffuse(GL::Color(0.6));
59         light0.set_position(GL::Vector4(-1, 1, 1, 0));
60         lighting.attach(0, light0);
61         GL::Light light1;
62         light1.set_diffuse(GL::Color(0.2));
63         light1.set_position(GL::Vector4(1, 1, 0, 0));
64         lighting.attach(1, light1);
65
66         GL::Material material;
67         material.set_diffuse(GL::Color(1.0));
68         material.set_ambient(GL::Color(1.0));
69
70         GL::Bind bind_light(lighting);
71         GL::Bind bind_depth(GL::DepthTest::lequal());
72
73         GL::MatrixStack::projection() = GL::Matrix::frustum(-0.05, 0.05, -0.05, 0.05, 0.1, 10);
74
75         window.show();
76         float angle = 0;
77         while(1)
78         {
79                 window.tick();
80                 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
81                 GL::Renderer renderer(0);
82                 renderer.matrix_stack() *= GL::Matrix::translation(0, 0, -5);
83                 renderer.matrix_stack() *= GL::Matrix::rotation(angle/2.3, 0, 1, 0);
84                 renderer.matrix_stack() *= GL::Matrix::rotation(angle, 1, 0.25, 0);
85                 renderer.set_material(&material);
86                 renderer.set_texture(&texture);
87                 box.draw(renderer);
88                 window.swap_buffers();
89                 angle += 0.0001;
90         }
91
92         return 0;
93 }