]> git.tdb.fi Git - libs/gl.git/commitdiff
A simple program to test cubemap texture functionality
authorMikko Rasa <tdb@tdb.fi>
Thu, 9 Aug 2012 19:35:39 +0000 (22:35 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 9 Aug 2012 19:35:39 +0000 (22:35 +0300)
Build
demos/cubemap.cpp [new file with mode: 0644]

diff --git a/Build b/Build
index 24e767ac690546d31bf2547777b5dfb850027363..4ea0aa793964f6e0141c196db46337b871e49f5b 100644 (file)
--- a/Build
+++ b/Build
@@ -54,6 +54,15 @@ package "mspgl"
                };
        };
 
+       program "cubemap"
+       {
+               source "demos/cubemap.cpp";
+               build_info
+               {
+                       library "mspgl";
+               };
+       };
+
        source_tarball
        {
                source "License.txt";
diff --git a/demos/cubemap.cpp b/demos/cubemap.cpp
new file mode 100644 (file)
index 0000000..b3de5a1
--- /dev/null
@@ -0,0 +1,93 @@
+#include <msp/gl/box.h>
+#include <msp/gl/framebuffer.h>
+#include <msp/gl/light.h>
+#include <msp/gl/lighting.h>
+#include <msp/gl/material.h>
+#include <msp/gl/mesh.h>
+#include <msp/gl/meshbuilder.h>
+#include <msp/gl/renderer.h>
+#include <msp/gl/tests.h>
+#include <msp/gl/texturecube.h>
+#include <msp/graphics/simplewindow.h>
+
+using namespace Msp;
+
+void create_face_texture(GL::TextureCube &texture, GL::TextureCubeFace face)
+{
+       unsigned char *pixels = new unsigned char[128*128*3];
+       for(unsigned y=0; y<128; ++y)
+               for(unsigned x=0; x<128; ++x)
+               {
+                       unsigned i = (x+y*128)*3;
+                       pixels[i] = (face==GL::NEGATIVE_X ? 0 : face==GL::POSITIVE_X ? 255 : face==GL::NEGATIVE_Z ? 255-x*2 : x*2);
+                       pixels[i+1] = (face==GL::NEGATIVE_Y ? 0 : face==GL::POSITIVE_Y ? 255 : 255-y*2);
+                       pixels[i+2] = (face==GL::NEGATIVE_Z ? 0 : face==GL::POSITIVE_Z ? 255 : face==GL::POSITIVE_X ? 255-x*2 : face==GL::NEGATIVE_X ? x*2 : face==GL::NEGATIVE_Y ? 255-y*2 : y*2);
+               }
+       texture.image(face, 0, GL::RGB, GL::UNSIGNED_BYTE, pixels);
+       delete[] pixels;
+}
+
+int main()
+{
+       Graphics::SimpleGLWindow window(400, 400);
+
+       GL::TextureCube texture;
+       texture.storage(GL::RGB, 128);
+       texture.set_min_filter(GL::LINEAR);
+       texture.set_wrap(GL::CLAMP_TO_EDGE);
+       create_face_texture(texture, GL::NEGATIVE_Z);
+       create_face_texture(texture, GL::POSITIVE_Z);
+       create_face_texture(texture, GL::NEGATIVE_Y);
+       create_face_texture(texture, GL::POSITIVE_Y);
+       create_face_texture(texture, GL::NEGATIVE_X);
+       create_face_texture(texture, GL::POSITIVE_X);
+
+       GL::Mesh box((GL::VERTEX3, GL::TEXCOORD3, GL::NORMAL3));
+       GL::MeshBuilder builder(box);
+       GL::BoxBuilder(2, 2, 2).build(builder);
+       for(unsigned i=0; i<box.get_n_vertices(); ++i)
+       {
+               float *v = box.modify_vertex(i);
+               for(unsigned j=0; j<3; ++j)
+                       v[3+j] = v[j];
+       }
+
+       GL::Lighting lighting;
+       lighting.set_ambient(GL::Color(0.3));
+       GL::Light light0;
+       light0.set_diffuse(GL::Color(0.6));
+       light0.set_position(GL::Vector4(-1, 1, 1, 0));
+       lighting.attach(0, light0);
+       GL::Light light1;
+       light1.set_diffuse(GL::Color(0.2));
+       light1.set_position(GL::Vector4(1, 1, 0, 0));
+       lighting.attach(1, light1);
+
+       GL::Material material;
+       material.set_diffuse(GL::Color(1.0));
+       material.set_ambient(GL::Color(1.0));
+
+       GL::Bind bind_light(lighting);
+       GL::Bind bind_depth(GL::DepthTest::lequal());
+
+       GL::MatrixStack::projection() = GL::Matrix::frustum(-0.05, 0.05, -0.05, 0.05, 0.1, 10);
+
+       window.show();
+       float angle = 0;
+       while(1)
+       {
+               window.tick();
+               GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
+               GL::Renderer renderer(0);
+               renderer.matrix_stack() *= GL::Matrix::translation(0, 0, -5);
+               renderer.matrix_stack() *= GL::Matrix::rotation(angle/2.3, 0, 1, 0);
+               renderer.matrix_stack() *= GL::Matrix::rotation(angle, 1, 0.25, 0);
+               renderer.set_material(&material);
+               renderer.set_texture(&texture);
+               box.draw(renderer);
+               window.swap_buffers();
+               angle += 0.0001;
+       }
+
+       return 0;
+}