]> git.tdb.fi Git - libs/gl.git/commitdiff
Make the cubemap demo more interesting
authorMikko Rasa <tdb@tdb.fi>
Mon, 13 Aug 2012 21:26:40 +0000 (00:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 13 Aug 2012 21:31:44 +0000 (00:31 +0300)
It now displays two different shapes, which have reflections through an
environment map.  The same environment map is used to render a skybox.

Also uses the new direction lookup functions to create the contents for
the cube map texture.

demos/cubemap.cpp

index b3de5a1225f21d76381421121cba69d4b34f157b..4f663055b8fc4ff68dce2c11b5c00bff484154cf 100644 (file)
@@ -1,10 +1,14 @@
+#include <cstdlib>
 #include <msp/gl/box.h>
+#include <msp/gl/cylinder.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/program.h>
+#include <msp/gl/programdata.h>
 #include <msp/gl/renderer.h>
 #include <msp/gl/tests.h>
 #include <msp/gl/texturecube.h>
@@ -15,13 +19,23 @@ 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)
+       for(int y=0; y<128; ++y)
+               for(int 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);
+                       GL::Vector3 v = texture.get_texel_direction(face, x, y);
+                       if(v.y>0)
+                       {
+                               pixels[i] = 96-48*v.y;
+                               pixels[i+1] = 168-84*v.y;
+                               pixels[i+2] = 255;
+                       }
+                       else
+                       {
+                               pixels[i] = rand()%32;
+                               pixels[i+1] = 128-rand()%32;
+                               pixels[i+2] = rand()%32;
+                       }
                }
        texture.image(face, 0, GL::RGB, GL::UNSIGNED_BYTE, pixels);
        delete[] pixels;
@@ -29,29 +43,30 @@ void create_face_texture(GL::TextureCube &texture, GL::TextureCubeFace face)
 
 int main()
 {
-       Graphics::SimpleGLWindow window(400, 400);
+       Graphics::SimpleGLWindow window(600, 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);
+       for(unsigned i=0; i<6; ++i)
+               create_face_texture(texture, texture.enumerate_faces(i));
 
-       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)
+       GL::Mesh skybox((GL::VERTEX3, GL::TEXCOORD3));
+       GL::BoxBuilder(10, 10, 10).build(skybox);
+       for(unsigned i=0; i<skybox.get_n_vertices(); ++i)
        {
-               float *v = box.modify_vertex(i);
+               float *v = skybox.modify_vertex(i);
                for(unsigned j=0; j<3; ++j)
                        v[3+j] = v[j];
        }
 
+       GL::Mesh box((GL::VERTEX3, GL::NORMAL3));
+       GL::BoxBuilder(2, 2, 2).build(box);
+
+       GL::Mesh cylinder((GL::VERTEX3, GL::NORMAL3));
+       GL::CylinderBuilder(1, 2, 32).build(cylinder);
+
        GL::Lighting lighting;
        lighting.set_ambient(GL::Color(0.3));
        GL::Light light0;
@@ -64,13 +79,22 @@ int main()
        lighting.attach(1, light1);
 
        GL::Material material;
-       material.set_diffuse(GL::Color(1.0));
-       material.set_ambient(GL::Color(1.0));
+       material.set_diffuse(GL::Color(0.4));
+       material.set_ambient(GL::Color(0.4));
+       material.set_specular(GL::Color(1.0));
+       material.set_shininess(100);
 
-       GL::Bind bind_light(lighting);
-       GL::Bind bind_depth(GL::DepthTest::lequal());
+       GL::Program::StandardFeatures features;
+       features.lighting = true;
+       features.specular = true;
+       features.material = true;
+       features.reflection = true;
+       GL::Program shprog(features);
+       GL::ProgramData shdata(shprog);
+       shdata.uniform("environment", 0);
+       shdata.uniform("reflectivity", 0.5f);
 
-       GL::MatrixStack::projection() = GL::Matrix::frustum(-0.05, 0.05, -0.05, 0.05, 0.1, 10);
+       GL::MatrixStack::projection() = GL::Matrix::frustum_centered(0.15, 0.1, 0.1, 10);
 
        window.show();
        float angle = 0;
@@ -78,13 +102,34 @@ int main()
        {
                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);
+               {
+                       GL::Renderer renderer(0);
+                       renderer.set_texture(&texture);
+                       skybox.draw(renderer);
+               }
+               {
+                       GL::Bind bind_light(lighting);
+                       GL::Bind bind_depth(GL::DepthTest::lequal());
+                       GL::Renderer renderer(0);
+                       renderer.set_material(&material);
+                       renderer.set_shader(&shprog, &shdata);
+                       renderer.set_texture(&texture);
+                       renderer.matrix_stack() *= GL::Matrix::translation(0, 0, -7);
+                       {
+                               GL::Renderer::Push _push(renderer);
+                               renderer.matrix_stack() *= GL::Matrix::translation(-2, 0, 0);
+                               renderer.matrix_stack() *= GL::Matrix::rotation(angle/2.3, 0, 1, 0);
+                               renderer.matrix_stack() *= GL::Matrix::rotation(angle, 1, 0.25, 0);
+                               box.draw(renderer);
+                       }
+                       {
+                               GL::Renderer::Push _push(renderer);
+                               renderer.matrix_stack() *= GL::Matrix::translation(2, 0, 0);
+                               renderer.matrix_stack() *= GL::Matrix::rotation(-angle/2.3, 0, 1, 0);
+                               renderer.matrix_stack() *= GL::Matrix::rotation(angle, 1, 0.25, 0);
+                               cylinder.draw(renderer);
+                       }
+               }
                window.swap_buffers();
                angle += 0.0001;
        }