From: Mikko Rasa Date: Wed, 7 Sep 2011 16:23:03 +0000 (+0300) Subject: Add two demo programs (with crappy code) X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=a5bf02196c301faea88d54933d0586da0aadb614 Add two demo programs (with crappy code) --- diff --git a/Build b/Build index 83ff24df..59a63b4f 100644 --- a/Build +++ b/Build @@ -37,6 +37,24 @@ package "mspgl" }; }; + program "shaders" + { + source "demos/shaders.cpp"; + build_info + { + library "mspgl"; + }; + }; + + program "texturing" + { + source "demos/texturing.cpp"; + build_info + { + library "mspgl"; + }; + }; + tarball "@src" { source "License.txt"; diff --git a/demos/shaders.cpp b/demos/shaders.cpp new file mode 100644 index 00000000..b7f2a196 --- /dev/null +++ b/demos/shaders.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +int main() +{ + Graphics::SimpleGLWindow wnd(800, 800); + + GL::Texture2D tex1; + GL::Texture2D tex2; + + char *data = new char[256*256*3]; + for(unsigned y=0; y<256; ++y) + for(unsigned x=0; x<256; ++x) + { + unsigned i = x+y*256; + data[i] = ((x/16+y/16)&1 ? 255 : 128); + } + tex1.storage(GL::LUMINANCE, 256, 256); + tex1.set_min_filter(GL::LINEAR); + tex1.image(0, GL::LUMINANCE, GL::UNSIGNED_BYTE, data); + + for(unsigned y=0; y<256; ++y) + for(unsigned x=0; x<256; ++x) + { + float yf = (y%16)/40.0-0.2; + float xf = (x%16)/40.0-0.2; + unsigned i = (x+y*256)*3; + float l = sqrt(xf*xf+yf*yf+1); + data[i] = static_cast((1+xf/l)*127); + data[i+1] = static_cast((1+yf/l)*127); + data[i+2] = static_cast((1+1/l)*127); + } + tex2.storage(GL::RGB, 256, 256); + tex2.set_min_filter(GL::LINEAR); + tex2.image(0, GL::RGB, GL::UNSIGNED_BYTE, data); + delete[] data; + + GL::Mesh mesh((GL::VERTEX3, GL::NORMAL3, GL::TEXCOORD2, GL::COLOR4_UBYTE, GL::ATTRIB3,4, GL::ATTRIB3,5)); + GL::MeshBuilder bld(mesh); + bld.color(0.5f, 1.0f, 0.0f); + GL::CapsuleBuilder(1, 0.72498, 32, 17).texture_fit(GL::GeometryBuilder::WRAP).tangent(4).binormal(5).build(bld); + GL::Material mat; + mat.set_diffuse(GL::Color(0.5, 1.0, 0.0)); + mat.set_specular(GL::Color(0.45, 0.5, 0.4)); + mat.set_shininess(50); + vector programs; + vector progdata; + for(unsigned i=0; i<12; ++i) + { + GL::Program::StandardFeatures feat; + feat.material = i/4>0; + feat.texture = i/4>1; + feat.lighting = i%4>0; + feat.normalmap = i%4>1; + feat.specular = i%4>2; + programs.push_back(new GL::Program(feat)); + if(feat.normalmap) + { + programs.back()->bind_attribute(4, "tangent"); + programs.back()->bind_attribute(5, "binormal"); + programs.back()->link(); + progdata.push_back(new GL::ProgramData(*programs.back())); + progdata.back()->uniform("normalmap", 1); + } + else + progdata.push_back(0); + } + + GL::Lighting lighting; + GL::Light light; + light.set_position(GL::Vector4(0, 2, 3, 0)); + lighting.attach(0, light); + + GL::Bind bind_light(lighting); + + GL::Texturing texturing; + texturing.attach(0, tex1); + texturing.attach(1, tex2); + + GL::MatrixStack::projection() = GL::Matrix::frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 20); + GL::MatrixStack::modelview() = GL::Matrix::translation(0, 0, -10); + GL::MatrixStack::modelview() *= GL::Matrix::rotation_deg(85, -1, 0, 0); + + wnd.show(); + float angle = 0.0; + Time::TimeStamp last; + while(1) + { + wnd.get_display().tick(); + GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); + { + GL::Bind bind_depth(GL::DepthTest::lequal()); + GL::Renderer renderer(0); + renderer.set_material(&mat); + renderer.set_texturing(&texturing); + for(unsigned i=0; i<12; ++i) + { + GL::MatrixStack::Push push(renderer.matrix_stack()); + renderer.set_shader(programs[i], progdata[i]); + renderer.matrix_stack() *= GL::Matrix::translation(-3.3+(i%4)*2.2, 0, -3.5+(i/4)*3.0); + renderer.matrix_stack() *= GL::Matrix::rotation(angle, 0, 0, 1); + mesh.draw(renderer); + } + } + wnd.swap_buffers(); + Time::TimeStamp t = Time::now(); + if(last) + angle += 0.5*((t-last)/Time::sec); + last = t; + } + + return 0; +} diff --git a/demos/texturing.cpp b/demos/texturing.cpp new file mode 100644 index 00000000..e618a43c --- /dev/null +++ b/demos/texturing.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +}