]> git.tdb.fi Git - libs/gl.git/blob - demos/shaders.cpp
Restructure the exporter to make room for new stuff
[libs/gl.git] / demos / shaders.cpp
1 #include <vector>
2 #include <cmath>
3 #include <msp/graphics/simplewindow.h>
4 #include <msp/gl/capsule.h>
5 #include <msp/gl/framebuffer.h>
6 #include <msp/gl/light.h>
7 #include <msp/gl/lighting.h>
8 #include <msp/gl/material.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/mesh.h>
11 #include <msp/gl/meshbuilder.h>
12 #include <msp/gl/program.h>
13 #include <msp/gl/programdata.h>
14 #include <msp/gl/renderer.h>
15 #include <msp/gl/tests.h>
16 #include <msp/gl/texture2d.h>
17 #include <msp/gl/texturing.h>
18 #include <msp/time/timestamp.h>
19 #include <msp/time/utils.h>
20 #include <msp/time/units.h>
21
22 using namespace std;
23 using namespace Msp;
24
25 int main()
26 {
27         Graphics::SimpleGLWindow wnd(800, 800);
28
29         GL::Texture2D tex1;
30         GL::Texture2D tex2;
31
32         char *data = new char[256*256*3];
33         for(unsigned y=0; y<256; ++y)
34                 for(unsigned x=0; x<256; ++x)
35                 {
36                         unsigned i = x+y*256;
37                         data[i] = ((x/16+y/16)&1 ? 255 : 128);
38                 }
39         tex1.storage(GL::LUMINANCE, 256, 256);
40         tex1.set_min_filter(GL::LINEAR);
41         tex1.image(0, GL::LUMINANCE, GL::UNSIGNED_BYTE, data);
42
43         for(unsigned y=0; y<256; ++y)
44                 for(unsigned x=0; x<256; ++x)
45                 {
46                         float yf = (y%16)/40.0-0.2;
47                         float xf = (x%16)/40.0-0.2;
48                         unsigned i = (x+y*256)*3;
49                         float l = sqrt(xf*xf+yf*yf+1);
50                         data[i] = static_cast<unsigned>((1+xf/l)*127);
51                         data[i+1] = static_cast<unsigned>((1+yf/l)*127);
52                         data[i+2] = static_cast<unsigned>((1+1/l)*127);
53                 }
54         tex2.storage(GL::RGB, 256, 256);
55         tex2.set_min_filter(GL::LINEAR);
56         tex2.image(0, GL::RGB, GL::UNSIGNED_BYTE, data);
57         delete[] data;
58
59         GL::Mesh mesh((GL::VERTEX3, GL::NORMAL3, GL::TEXCOORD2, GL::COLOR4_UBYTE, GL::ATTRIB3,4, GL::ATTRIB3,5));
60         GL::MeshBuilder bld(mesh);
61         bld.color(0.5f, 1.0f, 0.0f);
62         GL::CapsuleBuilder(1, 0.72498, 32, 17).texture_fit(GL::GeometryBuilder::WRAP).tangent(4).binormal(5).build(bld);
63         GL::Material mat;
64         mat.set_diffuse(GL::Color(0.5, 1.0, 0.0));
65         mat.set_specular(GL::Color(0.45, 0.5, 0.4));
66         mat.set_shininess(50);
67         vector<GL::Program *> programs;
68         vector<GL::ProgramData *> progdata;
69         for(unsigned i=0; i<12; ++i)
70         {
71                 GL::Program::StandardFeatures feat;
72                 feat.material = i/4>0;
73                 feat.texture = i/4>1;
74                 feat.lighting = i%4>0;
75                 feat.normalmap = i%4>1;
76                 feat.specular = i%4>2;
77                 programs.push_back(new GL::Program(feat));
78                 if(feat.normalmap)
79                 {
80                         programs.back()->bind_attribute(4, "tangent");
81                         programs.back()->bind_attribute(5, "binormal");
82                         programs.back()->link();
83                         progdata.push_back(new GL::ProgramData(*programs.back()));
84                         progdata.back()->uniform("normalmap", 1);
85                 }
86                 else
87                         progdata.push_back(0);
88         }
89
90         GL::Lighting lighting;
91         GL::Light light;
92         light.set_position(GL::Vector4(0, 2, 3, 0));
93         lighting.attach(0, light);
94
95         GL::Bind bind_light(lighting);
96
97         GL::Texturing texturing;
98         texturing.attach(0, tex1);
99         texturing.attach(1, tex2);
100
101         GL::MatrixStack::projection() = GL::Matrix::frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 20);
102         GL::MatrixStack::modelview() = GL::Matrix::translation(0, 0, -10);
103         GL::MatrixStack::modelview() *= GL::Matrix::rotation_deg(85, -1, 0, 0);
104
105         wnd.show();
106         float angle = 0.0;
107         Time::TimeStamp last;
108         while(1)
109         {
110                 wnd.get_display().tick();
111                 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
112                 {
113                         GL::Bind bind_depth(GL::DepthTest::lequal());
114                         GL::Renderer renderer(0);
115                         renderer.set_material(&mat);
116                         renderer.set_texturing(&texturing);
117                         for(unsigned i=0; i<12; ++i)
118                         {
119                                 GL::MatrixStack::Push push(renderer.matrix_stack());
120                                 renderer.set_shader(programs[i], progdata[i]);
121                                 renderer.matrix_stack() *= GL::Matrix::translation(-3.3+(i%4)*2.2, 0, -3.5+(i/4)*3.0);
122                                 renderer.matrix_stack() *= GL::Matrix::rotation(angle, 0, 0, 1);
123                                 mesh.draw(renderer);
124                         }
125                 }
126                 wnd.swap_buffers();
127                 Time::TimeStamp t = Time::now();
128                 if(last)
129                         angle += 0.5*((t-last)/Time::sec);
130                 last = t;
131         }
132
133         return 0;
134 }