]> git.tdb.fi Git - libs/gl.git/blobdiff - demos/texturing.cpp
Add two demo programs (with crappy code)
[libs/gl.git] / demos / texturing.cpp
diff --git a/demos/texturing.cpp b/demos/texturing.cpp
new file mode 100644 (file)
index 0000000..e618a43
--- /dev/null
@@ -0,0 +1,101 @@
+#include <cmath>
+#include <msp/graphics/simplewindow.h>
+#include <msp/gl/framebuffer.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/mesh.h>
+#include <msp/gl/meshbuilder.h>
+#include <msp/gl/projection.h>
+#include <msp/gl/texenv.h>
+#include <msp/gl/texture2d.h>
+#include <msp/gl/texturing.h>
+#include <msp/time/timestamp.h>
+#include <msp/time/utils.h>
+#include <msp/time/units.h>
+
+using namespace Msp;
+
+int main()
+{
+       Graphics::SimpleGLWindow wnd(400, 400);
+
+       GL::Texture2D tex1;
+       GL::Texture2D tex2;
+
+       char *data = new char[256*256*4];
+       for(unsigned y=0; y<256; ++y)
+               for(unsigned x=0; x<256; ++x)
+               {
+                       unsigned i = (x+y*256)*3;
+                       data[i] = 255;
+                       data[i+1] = (((x/32)+(y/32))&1 ? 255 : 0);
+                       data[i+2] = 0;
+               }
+       tex1.storage(GL::RGB, 256, 256);
+       tex1.set_min_filter(GL::LINEAR);
+       tex1.image(0, GL::RGB, GL::UNSIGNED_BYTE, data);
+
+       for(unsigned y=0; y<256; ++y)
+               for(unsigned x=0; x<256; ++x)
+               {
+                       unsigned i = (x+y*256)*4;
+                       data[i] = data[i+1] = data[i+2] = 0;
+                       data[i+3] = (((x/32)+(y/32))&1 ? 255 : 0);
+               }
+       tex2.storage(GL::RGBA, 256, 256);
+       tex2.set_min_filter(GL::LINEAR);
+       tex2.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data);
+       delete[] data;
+
+       GL::Texturing texturing;
+       texturing.attach(0, tex1);
+       GL::TexEnv texenv;
+       texenv.set_mode(GL::DECAL);
+       texturing.attach(1, tex2, texenv);
+
+       GL::MatrixStack::projection() = GL::Matrix::frustum_centered(0.2, 0.2, 0.2, 10);
+       GL::MatrixStack::modelview() = GL::Matrix::translation(0, 0, -3);
+       GL::MatrixStack::modelview() *= GL::Matrix::rotation(-45, 1, 0, 0);
+
+       GL::Mesh mesh((GL::TEXCOORD2, GL::TEXCOORD2,1, GL::VERTEX3));
+       GL::MeshBuilder bld(mesh);
+       bld.begin(GL::QUADS);
+       bld.texcoord(0, 0);
+       bld.multitexcoord(1, -0.2071, 0.5);
+       bld.vertex(-1, -1);
+       bld.texcoord(1, 0);
+       bld.multitexcoord(1, 0.5, -0.2071);
+       bld.vertex(1, -1);
+       bld.texcoord(1, 1);
+       bld.multitexcoord(1, 1.2071, 0.5);
+       bld.vertex(1, 1);
+       bld.texcoord(0, 1);
+       bld.multitexcoord(1, 0.5, 1.2071);
+       bld.vertex(-1, 1);
+       bld.end();
+
+       wnd.show();
+       float angle = 0;
+       Time::TimeStamp last;
+       while(1)
+       {
+               for(unsigned i=0; i<4; ++i)
+               {
+                       float *v = mesh.modify_vertex(i);
+                       v[2] = 0.5+sin(angle)*0.1+cos(angle+i*M_PI/2)*0.7071;
+                       v[3] = 0.5+cos(angle)*0.1+sin(angle+i*M_PI/2)*0.7071;
+               }
+               wnd.get_display().tick();
+               GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT);
+               {
+                       GL::Bind bind_tex(texturing);
+                       mesh.draw();
+               }
+               wnd.swap_buffers();
+               Time::TimeStamp t = Time::now();
+               if(last)
+                       angle += 0.5*((t-last)/Time::sec);
+               last = t;
+       }
+
+       return 0;
+}