]> git.tdb.fi Git - libs/gl.git/blob - demos/shaders.cpp
Set both min and mag filters when exporting textures
[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/timedelta.h>
20 #include <msp/time/utils.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::TANGENT3, GL::BINORMAL3));
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).tbn().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         for(unsigned i=0; i<12; ++i)
69         {
70                 GL::ProgramBuilder::StandardFeatures feat;
71                 feat.material = i/4>0;
72                 feat.texture = i/4>1;
73                 feat.lighting = i%4>0;
74                 feat.normal_map = i%4>1;
75                 feat.specular = i%4>2;
76                 programs.push_back(new GL::Program(feat));
77         }
78
79         GL::ProgramData progdata;
80         progdata.uniform("texture", 0);
81         progdata.uniform("normalmap", 1);
82
83         GL::Lighting lighting;
84         GL::Light light;
85         light.set_position(GL::Vector4(0, -0.781, 0.625, 0));
86         lighting.attach(0, light);
87
88         GL::Texturing texturing;
89         texturing.attach(0, tex1);
90         texturing.attach(1, tex2);
91
92         GL::MatrixStack::projection() = GL::Matrix::frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 20);
93         GL::MatrixStack::modelview() = GL::Matrix::translation(0, 0, -10);
94         GL::MatrixStack::modelview() *= GL::Matrix::rotation_deg(85, -1, 0, 0);
95
96         wnd.show();
97         float angle = 0.0;
98         Time::TimeStamp last;
99         while(1)
100         {
101                 wnd.get_display().tick();
102                 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
103                 {
104                         GL::Bind bind_depth(GL::DepthTest::lequal());
105                         GL::Renderer renderer(0);
106                         renderer.set_lighting(&lighting);
107                         renderer.set_material(&mat);
108                         renderer.set_texturing(&texturing);
109                         renderer.add_shader_data(progdata);
110                         for(unsigned i=0; i<12; ++i)
111                         {
112                                 GL::Renderer::Push push(renderer);
113                                 renderer.set_shader_program(programs[i]);
114                                 renderer.transform(GL::Matrix::translation(-3.3+(i%4)*2.2, 0, -3.5+(i/4)*3.0));
115                                 renderer.transform(GL::Matrix::rotation(angle, 0, 0, 1));
116                                 mesh.draw(renderer);
117                         }
118                 }
119                 wnd.swap_buffers();
120                 Time::TimeStamp t = Time::now();
121                 if(last)
122                         angle += 0.5*((t-last)/Time::sec);
123                 last = t;
124         }
125
126         return 0;
127 }