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