]> git.tdb.fi Git - libs/gl.git/blob - demos/texturing.cpp
Adjust the desert pillars demo to recent changes
[libs/gl.git] / demos / texturing.cpp
1 #include <cmath>
2 #include <msp/graphics/simplewindow.h>
3 #include <msp/gl/framebuffer.h>
4 #include <msp/gl/matrix.h>
5 #include <msp/gl/mesh.h>
6 #include <msp/gl/meshbuilder.h>
7 #include <msp/gl/texenv.h>
8 #include <msp/gl/texture2d.h>
9 #include <msp/gl/texturing.h>
10 #include <msp/time/timestamp.h>
11 #include <msp/time/utils.h>
12 #include <msp/time/units.h>
13
14 using namespace Msp;
15
16 int main()
17 {
18         Graphics::SimpleGLWindow wnd(400, 400);
19
20         GL::Texture2D tex1;
21         GL::Texture2D tex2;
22
23         unsigned char *data = new unsigned char[256*256*4];
24         for(unsigned y=0; y<256; ++y)
25                 for(unsigned x=0; x<256; ++x)
26                 {
27                         unsigned i = (x+y*256)*3;
28                         data[i] = 255;
29                         data[i+1] = (((x/32)+(y/32))&1 ? 255 : 0);
30                         data[i+2] = 0;
31                 }
32         tex1.storage(GL::RGB, 256, 256);
33         tex1.set_min_filter(GL::LINEAR);
34         tex1.image(0, GL::RGB, GL::UNSIGNED_BYTE, data);
35
36         for(unsigned y=0; y<256; ++y)
37                 for(unsigned x=0; x<256; ++x)
38                 {
39                         unsigned i = (x+y*256)*4;
40                         data[i] = data[i+1] = data[i+2] = 0;
41                         data[i+3] = (((x/32)+(y/32))&1 ? 255 : 0);
42                 }
43         tex2.storage(GL::RGBA, 256, 256);
44         tex2.set_min_filter(GL::LINEAR);
45         tex2.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data);
46         delete[] data;
47
48         GL::Texturing texturing;
49         texturing.attach(0, tex1);
50         GL::TexEnv texenv;
51         texenv.set_mode(GL::DECAL);
52         texturing.attach(1, tex2, texenv);
53
54         GL::MatrixStack::projection() = GL::Matrix::frustum_centered(0.2, 0.2, 0.2, 10);
55         GL::MatrixStack::modelview() = GL::Matrix::translation(0, 0, -3);
56         GL::MatrixStack::modelview() *= GL::Matrix::rotation(-45, 1, 0, 0);
57
58         GL::Mesh mesh((GL::TEXCOORD2, GL::TEXCOORD2,1, GL::VERTEX3));
59         GL::MeshBuilder bld(mesh);
60         bld.begin(GL::QUADS);
61         bld.texcoord(0, 0);
62         bld.multitexcoord(1, -0.2071, 0.5);
63         bld.vertex(-1, -1);
64         bld.texcoord(1, 0);
65         bld.multitexcoord(1, 0.5, -0.2071);
66         bld.vertex(1, -1);
67         bld.texcoord(1, 1);
68         bld.multitexcoord(1, 1.2071, 0.5);
69         bld.vertex(1, 1);
70         bld.texcoord(0, 1);
71         bld.multitexcoord(1, 0.5, 1.2071);
72         bld.vertex(-1, 1);
73         bld.end();
74
75         wnd.show();
76         float angle = 0;
77         Time::TimeStamp last;
78         while(1)
79         {
80                 for(unsigned i=0; i<4; ++i)
81                 {
82                         float *v = mesh.modify_vertex(i);
83                         v[2] = 0.5+sin(angle)*0.1+cos(angle+i*M_PI/2)*0.7071;
84                         v[3] = 0.5+cos(angle)*0.1+sin(angle+i*M_PI/2)*0.7071;
85                 }
86                 wnd.get_display().tick();
87                 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT);
88                 {
89                         GL::Bind bind_tex(texturing);
90                         mesh.draw();
91                 }
92                 wnd.swap_buffers();
93                 Time::TimeStamp t = Time::now();
94                 if(last)
95                         angle += 0.5*((t-last)/Time::sec);
96                 last = t;
97         }
98
99         return 0;
100 }