void Render(double time) { const double day_duration = 67.0; auto sun = Vec3f(0.000, 1.000, 0.000) * 1e10 * SineWave(time / day_duration) + Vec3f(0.000, 0.000, -1.000) * 1e10 * CosineWave(time / day_duration); auto camera = CamMatrixf::Orbiting( Vec3f(), 5.0, FullCircles(-0.10 - time / 27.0), Degrees(-20 - SineWave(time / 17.0) * 30)); auto model = ModelMatrixf::RotationA( Vec3f(1.0, 1.0, 1.0), FullCircles(time / 13.0)); gl.Clear().ColorBuffer().DepthBuffer(); sky_box.Use(); sky_box_prog.Use(); sky_box_camera_matrix.Set(camera); sky_box_sun_position.Set(sun); sky_box.Draw(); shape.Use(); shape_prog.Use(); shape_model_matrix.Set(model); shape_camera_matrix.Set(camera); shape_camera_position.Set(camera.Position()); shape_sun_position.Set(sun); shape.Draw(); }
void RenderOffscreen(double time) { fbo.Bind(); gl.Clear().ColorBuffer().DepthBuffer(); gl.BlendFunc(BlendFn::One, BlendFn::One); gl.Enable(Capability::Blend); gl.Enable(Capability::DepthTest); draw_prog.Use(); draw_prog.camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(), 27.0, Degrees(time * 23), Degrees(SineWave(time / 23.0) * 80) ) ); cube.Use(); cube.Draw(n*n*n); gl.Disable(Capability::DepthTest); gl.Disable(Capability::Blend); fbo.Unbind(); }
LightRayExample(void) : gl() , mesh_loader( mesh_input.stream, shapes::ObjMesh::LoadingOptions(false).Normals() ), meshes(List("Position")("Normal").Get(), mesh_loader) , fan_index(mesh_loader.GetMeshIndex("Fan")) , light_position(0.0, 0.0, -100.0) , vert_shader() , mask_prog(vert_shader) , mask_vao(meshes.VAOForProgram(mask_prog)) , draw_prog(vert_shader) , draw_vao(meshes.VAOForProgram(draw_prog)) , shadow_tex_unit(0) , light_tex_unit(1) { mesh_input.stream.close(); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); RenderShadowMap(512); SetupLightMask(); mask_prog.Use(); mask_prog.light_position.Set(light_position); draw_prog.Use(); draw_prog.light_position.Set(light_position); gl.ClearColor(1.0f, 1.0f, 1.0f, 0.0f); }
void Update(double time) { gl.Viewport(size, size); fbo.Bind(Framebuffer::Target::Draw); Framebuffer::AttachColorTexture( Framebuffer::Target::Draw, 1, holder.CurrentHeightMap(), 0 ); Context::ColorBuffer draw_buffs[2] = { FramebufferColorAttachment::_0, FramebufferColorAttachment::_1 }; gl.DrawBuffers(draw_buffs); prog.Use(); prog.hmap_1.Set(holder.HMapUnit1()); prog.hmap_2.Set(holder.HMapUnit2()); prog.time.Set(time); screen.Use(); gl.Disable(Capability::DepthTest); screen.Draw(); gl.Enable(Capability::DepthTest); holder.Swap(); }
void Render(double time) { auto camera = CamMatrixf::Roll(Degrees(SineWave(time / 11.0)*7+SineWave(time/13.0)*5))* CamMatrixf::Orbiting( Vec3f(), 40.0f, Degrees(SineWave(time / 11.0)*10+CosineWave(time/19.0)*10-90), Degrees(SineWave(time / 17.0)*10+SineWave(time/13.0)*10) ); auto mm_identity = ModelMatrixf(); auto mm_rotation = ModelMatrixf::RotationZ(FullCircles(time / 7.0)); Uniform<Mat4f>* model_matrix = nullptr; GLuint drawing_fan = fan_index; auto drawing_driver = [ &model_matrix, &mm_identity, &mm_rotation, &drawing_fan ](GLuint phase) -> bool { if(phase == drawing_fan) model_matrix->Set(mm_rotation); else model_matrix->Set(mm_identity); return true; }; // render the light mask light_fbo.Bind(Framebuffer::Target::Draw); gl.Clear().ColorBuffer().DepthBuffer(); mask_vao.Bind(); mask_prog.Use(); mask_prog.camera_matrix.Set(camera); model_matrix = &mask_prog.model_matrix; meshes.Draw(drawing_driver); // render the final image DefaultFramebuffer().Bind(Framebuffer::Target::Draw); gl.Clear().ColorBuffer().DepthBuffer(); draw_vao.Bind(); draw_prog.Use(); Vec4f lsp = projection * camera * Vec4f(light_position, 1.0); draw_prog.light_screen_pos = lsp.xyz()/lsp.w(); draw_prog.camera_matrix.Set(camera); model_matrix = &draw_prog.model_matrix; meshes.Draw(drawing_driver); }
void Render(double time) { flow.Update(time); Framebuffer::BindDefault(Framebuffer::Target::Draw); gl.DrawBuffer(ColorBuffer::BackLeft); gl.Viewport(width, height); gl.Clear().ColorBuffer().DepthBuffer(); screen_prog.Use(); screen.Use(); screen.Draw(); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(), 4.0, Degrees(time * 25), Degrees(SineWave(time / 30.0) * 90) ) ); model_matrix.Set( ModelMatrixf::RotationA( Vec3f(1.0f, 1.0f, 1.0f), FullCircles(time * 0.5) ) ); shape.Draw(); thread_ready.Signal(); parent_ready.Wait(); }
ObjMeshExample(void) : gl() , objects(load_objects()) , depth_prog() , draw_prog() , depth_vao(objects.VAOForProgram(depth_prog)) , draw_vao(objects.VAOForProgram(draw_prog)) { UniformSampler(draw_prog, "DepthTex").Set(0); Texture::Active(0); depth_tex.Bind(Texture::Target::Rectangle); gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::CullFace); }
void Draw(double time, double fade) { // Shadow map shadows_fbo.Bind(FramebufferTarget::Draw); gl.Viewport(shadow_size, shadow_size); gl.Clear().DepthBuffer(); auto light = CamMatrixf::Orbiting( Vec3f(0, side*0.25, 0), side*1.5, Degrees(-time * 27), Degrees(SineWave(time / 19.0)*25 + 45) ); shadow_prog.fade.Set(fade); shadow_prog.camera_matrix.Set(light); shadow_prog.Use(); shadow_vao.Bind(); gl.Enable(Capability::PolygonOffsetFill); cube.Draw(side*side); gl.Disable(Capability::PolygonOffsetFill); gl.Finish(); // On-screen default_fb.Bind(Framebuffer::Target::Draw); gl.Viewport(width, height); gl.Clear().ColorBuffer().DepthBuffer(); auto camera = CamMatrixf::Orbiting( Vec3f(), side*1.1, Degrees(time * 19), Degrees(SineWave(time / 20.0) * 39 + 50) ); display_prog.fade.Set(fade); display_prog.light_pos.Set(light.Position()); display_prog.camera_pos.Set(camera.Position()); display_prog.light_matrix.Set(light); display_prog.camera_matrix.Set(camera); display_prog.Use(); display_vao.Bind(); cube.Draw(side*side); }
void Render(double time) { gl.ClearDepth(0.0f); gl.Clear().DepthBuffer(); auto camera = CamMatrixf::Orbiting( objects.BoundingSphere().Center(), objects.BoundingSphere().Radius()*2.8, FullCircles(time / 19.0), Degrees(SineWave(time / 17.0) * 90) ); depth_prog.Use(); gl.DepthFunc(CompareFn::Greater); gl.CullFace(Face::Front); depth_prog.camera_matrix.Set(camera); depth_prog.model_matrix.Set(Mat4f()); objects.Draw(); Texture::CopyImage2D( Texture::Target::Rectangle, 0, PixelDataInternalFormat::DepthComponent, 0, 0, width, height, 0 ); gl.ClearDepth(1.0f); gl.Clear().ColorBuffer().DepthBuffer(); draw_prog.Use(); gl.DepthFunc(CompareFn::Less); gl.CullFace(Face::Back); draw_prog.camera_matrix.Set(camera); draw_prog.model_matrix.Set(Mat4f()); objects.Draw(); }
ParallaxExample() : prog(make_prog()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , camera_position(prog, "CameraPosition") , light_position(prog, "LightPosition") , shape( List("Position")("TexCoord").Get(), shapes::Plane( Vec3f(), Vec3f(1.0f, 0.0f, 0.0f), Vec3f(0.0f, 0.0f, -1.0f), 32, 32)) { shape.UseInProgram(prog); auto tex_image = images::LoadTexture("stones_color_hmap"); Texture::Active(0); try { UniformSampler(prog, "ColorMap").Set(0); gl.Bound(Texture::Target::_2D, color_tex) .MinFilter(TextureMinFilter::LinearMipmapLinear) .MagFilter(TextureMagFilter::Linear) .WrapS(TextureWrap::Repeat) .WrapT(TextureWrap::Repeat) .Image2D(tex_image) .GenerateMipmap(); } catch(Error&) { } Texture::Active(1); try { UniformSampler(prog, "BumpMap").Set(1); gl.Bound(Texture::Target::_2D, bump_tex) .MinFilter(TextureMinFilter::LinearMipmapLinear) .MagFilter(TextureMagFilter::Linear) .WrapS(TextureWrap::Repeat) .WrapT(TextureWrap::Repeat) .Image2D( images::NormalMap(tex_image, images::NormalMap::FromAlpha())) .GenerateMipmap(); } catch(Error&) { } gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); gl.ClearDepth(1.0f); (Capability::DepthTest) << true; (Capability::CullFace) << false; (Functionality::ClipDistance | 0) << true; (Functionality::ClipDistance | 1) << true; (Functionality::ClipDistance | 2) << true; }
void Render(double time) { float o = 2; float s = 24; prog.offset.Set( GLfloat(CosineWave(time/59.0)*o), GLfloat(SineWave(time/61.0)*o) ); prog.scale.Set(GLfloat(s + 1 + SineWave(time / 19.0)*s)); screen.Draw(); }
BarDisplay(void) : gl() , side(128) , shadow_size(512) , cube( List("Position")("Normal").Get(), shapes::Cube(0.95, 1.0, 0.95, 0.0, 0.5, 0.0) ), display_vao(cube.VAOForProgram(display_prog)) , shadow_vao(cube.VAOForProgram(shadow_prog)) { init_shadows(); init_heights(); init_offsets(); auto light_proj = CamMatrixf::PerspectiveX( Degrees(74), 1.0, 1, 3*side ); display_prog.light_proj_matrix.Set(light_proj); shadow_prog.projection_matrix.Set(light_proj); gl.PolygonOffset(4.0, 4.0); }
ParallaxMapExample() : prog(make()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , model_matrix(prog, "ModelMatrix") , camera_position(prog, "CameraPosition") , light_position(prog, "LightPosition") , shape( List("Position")("Normal")("Tangent")("TexCoord").Get(), shapes::Torus(1.0f, 0.5, 72, 48)) { shape.UseInProgram(prog); auto tex_image = images::LoadTexture("bricks_color_hmap"); Texture::Active(0); try { UniformSampler(prog, "ColorMap").Set(0); gl.Bound(Texture::Target::_2D, color_tex) .MinFilter(TextureMinFilter::LinearMipmapLinear) .MagFilter(TextureMagFilter::Linear) .WrapS(TextureWrap::Repeat) .WrapT(TextureWrap::Repeat) .Image2D(tex_image) .GenerateMipmap(); } catch(Error&) { } Texture::Active(1); try { UniformSampler(prog, "BumpMap").Set(1); gl.Bound(Texture::Target::_2D, bump_tex) .MinFilter(TextureMinFilter::LinearMipmapLinear) .MagFilter(TextureMagFilter::Linear) .WrapS(TextureWrap::Repeat) .WrapT(TextureWrap::Repeat) .Image2D( images::NormalMap(tex_image, images::NormalMap::FromAlpha())) .GenerateMipmap(); } catch(Error&) { } gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Disable(Capability::CullFace); gl.Enable(Functionality::ClipDistance, 0); gl.Enable(Functionality::ClipDistance, 1); gl.Enable(Functionality::ClipDistance, 2); }
void Render(ExampleClock& clock) { gl.Clear().ColorBuffer().DepthBuffer(); double time = clock.Now().Seconds(); auto langle = FullCircles(time / 23.0); light_position.Set( GLfloat(Cos(langle) * 20.0), GLfloat((1.2 + Sin(langle)) * 15.0), GLfloat(Sin(langle) * 20.0)); double x = SineWave(time / 13.0); if(x + 0.93 < 0.0) clock.Pace(0.2); else clock.Pace(1.0); auto camera = CamMatrixf::Orbiting( Vec3f(), GLfloat(9.5 + x * 5.1), FullCircles(time / 17.0), Degrees(SineWave(time / 20.0) * 89)); camera_matrix.Set(camera); camera_position.Set(camera.Position()); model_matrix.Set( ModelMatrixf::TranslationX(+2.0f) * ModelMatrixf::RotationX(FullCircles(time / 13.0))); shape.Draw(); model_matrix.Set( ModelMatrixf::TranslationX(-2.0f) * ModelMatrixf::RotationZ(FullCircles(time / 11.0))); shape.Draw(); }
void Use(void) { gl.ClearDepth(1.0f); gl.ClearColor(0.8f, 0.8f, 0.8f, 0.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::CullFace); gl.CullFace(Face::Back); dfb.Bind(Framebuffer::Target::Draw); gl.Viewport(width, height); prog.Use(); cube.Use(); SetProjection(); }
void Use(void) { gl.ClearDepth(1.0f); gl.ClearColor(0.9f, 0.4f, 0.4f, 1.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::CullFace); gl.CullFace(Face::Back); fbo.Bind(Framebuffer::Target::Draw); gl.Viewport(tex_side, tex_side); prog.Use(); shape.Use(); projection_matrix.Set(CamMatrixf::PerspectiveX(Degrees(48), 1.0, 1, 100)); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); auto camera = CamMatrixf::Orbiting( Vec3f(), 2.9f, FullCircles(time / 17.0), Degrees(45 + SineWave(time / 20.0) * 40)); camera_matrix.Set(camera); camera_position.Set(camera.Position()); auto langle = FullCircles(time / 31.0); light_position.Set( GLfloat(Cos(langle) * 20.0f), GLfloat((1.2 + Sin(langle)) * 15.0f), GLfloat(Sin(langle) * 20.0f)); shape.Draw(); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(), 3.0, Degrees(time * 35), Degrees(SineWave(time / 20.0) * 60) ) ); model_matrix.Set(ModelMatrixf::RotationX(FullCircles(time * 0.25))); assert(thread_ready); thread_ready->Wait(); cube.Draw(); parent_ready.Signal(); }
void Render(double time) { GLfloat bs_rad = shape.BoundingSphere().Radius()*1.2f; auto light = CamMatrixf::Orbiting( shape.BoundingSphere().Center(), shape.BoundingSphere().Radius()*10.0f, FullCircles(time / 23.0), Degrees(-CosineWave(time / 31.0) * 80) ); auto camera = CamMatrixf::Orbiting( shape.BoundingSphere().Center(), shape.BoundingSphere().Radius()* GLfloat(3.2+SineWave(time / 23.0)*0.8), FullCircles(time / 19.0), Degrees(SineWave(time / 21.0) * 80) ); GLfloat cam_tgt_dist = Distance( shape.BoundingSphere().Center(), camera.Position() ); auto cam_proj = CamMatrixf::PerspectiveX( Degrees(45), width, height, cam_tgt_dist-bs_rad, cam_tgt_dist+bs_rad ); auto model = ModelMatrixf::RotationZ(Degrees(SineWave(time / 21.0)*25))* ModelMatrixf::Translation(0.0f,-bs_rad*0.25f, 0.0f); data_prog.Use(); data_prog.camera_matrix.Set(cam_proj*camera); data_prog.model_matrix.Set(model); data_prog.camera_position.Set(camera.Position()); data_prog.light_position.Set(light.Position()); data_buffer.Bind(); gl.Enable(Capability::DepthTest); gl.Clear().ColorBuffer().DepthBuffer(); shape.Use(); shape.Draw(); draw_prog.Use(); draw_prog.slider.Set(GLfloat(CosineWave01(time / 11.0)*width)); DefaultFramebuffer().Bind(Framebuffer::Target::Draw); gl.Disable(Capability::DepthTest); gl.Clear().ColorBuffer(); screen.Use(); screen.Draw(); }
SkyBoxExample() : sky_box(List("Position").Get(), shapes::SkyBox()) , shape(List("Position")("Normal").Get(), shapes::WickerTorus()) , sky_box_prog() , sky_box_projection_matrix(sky_box_prog, "ProjectionMatrix") , sky_box_camera_matrix(sky_box_prog, "CameraMatrix") , shape_projection_matrix(shape_prog, "ProjectionMatrix") , shape_camera_matrix(shape_prog, "CameraMatrix") , shape_model_matrix(shape_prog, "ModelMatrix") , sky_box_sun_position(sky_box_prog, "SunPosition") , shape_sun_position(shape_prog, "SunPosition") , shape_camera_position(shape_prog, "CameraPosition") { VertexShader sky_box_vs; sky_box_vs.Source( "#version 140\n" "uniform mat4 ProjectionMatrix, CameraMatrix;" "mat4 Matrix = ProjectionMatrix*CameraMatrix;" "in vec3 Position;" "out vec3 vertTexCoord;" "void main()" "{" " gl_Position = Matrix * vec4(Position * 100.0, 1.0);" " vertTexCoord = Position;" "}"); sky_box_vs.Compile(); sky_box_prog.AttachShader(sky_box_vs); VertexShader shape_vs; shape_vs.Source( "#version 140\n" "uniform vec3 SunPosition;" "uniform vec3 CameraPosition;" "uniform mat4 ProjectionMatrix,CameraMatrix,ModelMatrix;" "in vec4 Position;" "in vec3 Normal;" "out vec3 vertNormal, vertViewRefl, vertLightDir;" "void main()" "{" " gl_Position = ModelMatrix * Position;" " vertNormal = mat3(ModelMatrix)*Normal;" " vertViewRefl = reflect(" " gl_Position.xyz - CameraPosition," " vertNormal" " );" " vertLightDir = SunPosition - gl_Position.xyz;" " gl_Position = ProjectionMatrix*CameraMatrix*gl_Position;" "}"); shape_vs.Compile(); shape_prog.AttachShader(shape_vs); FragmentShader sky_box_fs; sky_box_fs.Source( "#version 140\n" "in vec3 vertTexCoord;" "out vec3 fragColor;" "vec3 sky_color(vec3 vd);" "void main()" "{" " fragColor = sky_color(normalize(vertTexCoord));" "}"); sky_box_fs.Compile(); sky_box_prog.AttachShader(sky_box_fs); FragmentShader shape_fs; shape_fs.Source( "#version 140\n" "in vec3 vertNormal, vertViewRefl, vertLightDir;" "out vec3 fragColor;" "vec3 sky_color(vec3 vd);" "void main()" "{" " float l = max(dot(normalize(vertNormal), " "normalize(vertLightDir))+0.1, 0.0);" " float a = 0.1;" " fragColor = " " 0.1*vec3(1.0, 1.0, 1.0)*(a+l)+" " 0.9*sky_color(normalize(vertViewRefl));" "}"); shape_fs.Compile(); shape_prog.AttachShader(shape_fs); FragmentShader sky_fs; sky_fs.Source( "#version 140\n" "const float WorldRadius = 6371000;" "const float AtmThickness = 50000;" "const vec3 AirColor = vec3(0.32, 0.36, 0.45);" "const vec3 LightColor = vec3(1.0, 1.0, 1.0);" "uniform vec3 SunPosition;" "uniform samplerCube EnvMap;" "float atm_intersection(vec3 v)" "{" " const vec3 c = vec3(0.0, -WorldRadius, 0.0);" " const float r = WorldRadius + AtmThickness;" " const float c_c = dot(-c, -c);" " float v_c = dot( v, -c);" " return (-v_c + sqrt(v_c*v_c - c_c + r*r))/AtmThickness;" "}" "vec3 sky_color(vec3 vd)" "{" " vec3 up = vec3(0.0, 1.0, 0.0);" " vec3 ld = normalize(SunPosition);" " vec4 cl = texture(EnvMap, vd);" " float ai = atm_intersection(vd);" " float al = max(dot(ld, up) + 0.12, 0.0);" " float vl = max(dot(vd, ld), 0.0);" " float ct = (1.0-cl.a)*cl.b;" " vec3 ac = max(LightColor-AirColor*pow(ai, 0.33), vec3(0.0, 0.0, " "0.0));" " vec3 Sun = " " ac*(vl>0.995+0.004*al ? 1.0:0.0);" " vec3 Air = " " min(AirColor*sqrt(pow(al,0.25)*ai), vec3(al, al, al)*1.5)+" " ac*pow(min(vl+0.001*ai, 1.0), 1024.0/pow(ai, 2.0))+" " ac*(vl/(1.0+pow(3.0*al, 8.0)))*pow(ai, 0.6)*0.5;" " vec3 Clouds =" " ac*pow(min(vl*(cl.g+cl.b), 1.015), 64.0)*pow(ct, 2.0)+" " ac*pow(min(vl*cl.g+cl.b, 1.020), 32.0)*ct+" " ac*pow(min(vl*cl.g*cl.b, 1.010), 16.0)*pow(ct, 0.5)+" " ac*0.7*min(cl.g + cl.b*0.5, 1.0)*al+" " ac*(cl.g*(1.0-cl.b*0.2)*5.0)*pow(1.0-al, 2.0)*(al)+" " LightColor*0.5*min(al + cl.g*0.4+cl.b*0.1, 1.0)*sqrt(al);" " return mix(Air, Clouds, cl.a*(1.0-cl.r*0.8))+Sun*(1.0-cl.a);" "}"); sky_fs.Compile(); sky_box_prog.AttachShader(sky_fs); shape_prog.AttachShader(sky_fs); sky_box_prog.Link(); sky_box.UseInProgram(sky_box_prog); shape_prog.Link(); shape.UseInProgram(shape_prog); { ProgramUniformSampler(sky_box_prog, "EnvMap").Set(0); ProgramUniformSampler(shape_prog, "EnvMap").Set(0); Texture::Active(0); gl.Bound(Texture::Target::CubeMap, env_map) .MinFilter(TextureMinFilter::Linear) .MagFilter(TextureMagFilter::Linear) .WrapS(TextureWrap::ClampToEdge) .WrapT(TextureWrap::ClampToEdge) .WrapR(TextureWrap::ClampToEdge); Texture::ImageCM( 0, images::LoadTexture("clouds01-cm_0", false, false)); Texture::ImageCM( 1, images::LoadTexture("clouds01-cm_1", false, false)); Texture::ImageCM( 2, images::LoadTexture("clouds01-cm_2", false, false)); Texture::ImageCM( 3, images::LoadTexture("clouds01-cm_3", false, false)); Texture::ImageCM( 4, images::LoadTexture("clouds01-cm_4", false, false)); Texture::ImageCM( 5, images::LoadTexture("clouds01-cm_5", false, false)); } gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); // auto camera = CamMatrixf::Orbiting( Vec3f(0.0, 1.5, 0.0), 7.0 + SineWave(time / 11.0)*1.5, FullCircles(time / 19.0), Degrees(SineWave(time / 20.0) * 30 + 35) ); cube_prog.camera_matrix = camera; cube_prog.camera_position = camera.Position(); // shiny gray/blue checkered cube cube_prog.frag_subroutines.Apply(cube_prog.frag_shiny_checker); cube_prog.specular_factor = 32; cube_prog.color_1 = Vec3f(0.9, 0.8, 0.7); cube_prog.color_2 = Vec3f(0.3, 0.4, 0.5); cube_prog.tex_scale = Vec2f(4, 4); cube_prog.model_matrix = ModelMatrixf::RotationY(FullCircles(time / 7.0))* ModelMatrixf::Translation( 2.0, 0.0, 0.0)* ModelMatrixf::RotationX(Degrees(25 * time)); cube.Draw(); // shiny textured cube cube_prog.frag_subroutines.Apply(cube_prog.frag_shiny_texture); cube_prog.specular_factor = 16; cube_prog.tex_scale = Vec2f(1, 1); cube_prog.model_matrix = ModelMatrixf::RotationY(FullCircles(time / 7.0))* ModelMatrixf::Translation(-2.0, 0.0, 0.0)* ModelMatrixf::RotationX(Degrees(-17 * time)); cube.Draw(); // shiny yellow/black striped cube cube_prog.frag_subroutines.Apply(cube_prog.frag_shiny_strips); cube_prog.specular_factor = 32; cube_prog.color_1 = Vec3f(0.9, 0.9, 0.1); cube_prog.color_2 = Vec3f(0.1, 0.1, 0.1); cube_prog.tex_scale = Vec2f(16, 16); cube_prog.model_matrix = ModelMatrixf::RotationY(FullCircles(time / 7.0))* ModelMatrixf::Translation( 0.0, 2.0, 0.0)* ModelMatrixf::RotationY(Degrees(37 * time)); cube.Draw(); // shiny gray/green spiral cube cube_prog.frag_subroutines.Apply(cube_prog.frag_shiny_spiral); cube_prog.specular_factor = 24; cube_prog.color_1 = Vec3f(0.9, 0.9, 0.9); cube_prog.color_2 = Vec3f(0.4, 0.9, 0.4); cube_prog.tex_scale = Vec2f(1, 1); cube_prog.model_matrix = ModelMatrixf::RotationY(FullCircles(time / 7.0))* ModelMatrixf::Translation( 0.0,-2.0, 0.0)* ModelMatrixf::RotationY(Degrees(-13 * time)); cube.Draw(); // dull white/red striped cube cube_prog.frag_subroutines .Assign( cube_prog.pixel_light_func, cube_prog.dull ).Assign( cube_prog.pixel_color_func, cube_prog.strips ).Apply(); cube_prog.specular_factor = 32; cube_prog.color_2 = Vec3f(1.0, 1.0, 1.0); cube_prog.color_1 = Vec3f(0.9, 0.2, 0.2); cube_prog.tex_scale = Vec2f(8, 6); cube_prog.model_matrix = ModelMatrixf::RotationY(FullCircles(time / 7.0))* ModelMatrixf::Translation( 0.0, 0.0, 2.0)* ModelMatrixf::RotationZ(Degrees(27 * time)); cube.Draw(); // dull textured cube cube_prog.frag_subroutines .Assign( cube_prog.pixel_color_func, cube_prog.texture_bgr ).Apply(); cube_prog.tex_scale = Vec2f(1, 1); cube_prog.model_matrix = ModelMatrixf::RotationY(FullCircles(time / 7.0))* ModelMatrixf::Translation( 0.0, 0.0,-2.0)* ModelMatrixf::RotationZ(Degrees(-23 * time)); cube.Draw(); }
TriangleExample(void) : projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , camera_position(prog, "CameraPosition") , light_position(prog, "LightPosition") , shape( List("Position")("TexCoord").Get(), shapes::Plane( Vec3f(), Vec3f(1.0f, 0.0f, 0.0f), Vec3f(0.0f, 0.0f,-1.0f), 32, 32 ) ) { VertexShader vs; vs.Source(StrLit( "#version 330\n" "in vec3 Position;" "in vec2 TexCoord;" "out vec2 vertTexCoord;" "void main(void)" "{" " gl_Position = vec4(Position, 1.0);" " vertTexCoord = TexCoord;" "}" )).Compile(); prog.AttachShader(vs); GeometryShader gs; gs.Source(StrLit( "#version 330\n" "#extension GL_ARB_gpu_shader5 : enable\n" "layout(triangles, invocations = 7) in;" "layout(triangle_strip, max_vertices = 21) out;" "uniform mat4 ProjectionMatrix, CameraMatrix;" "mat4 Matrix = ProjectionMatrix * CameraMatrix;" "uniform vec3 CameraPosition;" "in vec2 vertTexCoord[3];" "out gl_PerVertex {" " vec4 gl_Position;" " float gl_ClipDistance[3];" "};" "flat out mat3 geomPositionFront;" "flat out mat3 geomTexCoordFront;" "flat out vec3 geomWFront;" "noperspective out vec3 geomBarycentric;" "out vec3 geomPosition;" "out vec3 geomTexCoord;" "void main(void)" "{" " vec4 world_pos[8*3];" " vec3 tex_coord[8*3];" " vec4 view_pos[8*3];" " vec3 screen_pos[8*3];" " bool front_facing[8];" " int ft = gl_InvocationID+1;" " for(int pass=0; pass!=2; ++pass)" " {" " bool first = pass == 0;" " if(((ft == 0) && first) || (((ft != 0) && !first)))" " {" " for(int v=0; v!=3; ++v)" " {" " int w = 2-v;" " world_pos[0+v] = gl_in[w].gl_Position;" " tex_coord[0+v] = vec3(vertTexCoord[w], 0.0);" " }" " }" " vec4 n = vec4(-0.15 * normalize(cross(" " gl_in[1].gl_Position.xyz-gl_in[0].gl_Position.xyz," " gl_in[2].gl_Position.xyz-gl_in[0].gl_Position.xyz " " )), 0.0);" " if(((ft == 1) && first) || (((ft != 1) && !first)))" " {" " for(int v=0; v!=3; ++v)" " {" " world_pos[3+v] = gl_in[v].gl_Position + n;" " tex_coord[3+v] = vec3(vertTexCoord[v], 1.0);" " }" " }" " for(int v=0; v!=3; ++v)" " {" " int w = (v+1)%3;" " int k = 2+2*v;" " if(((ft == k) && first) || (((ft != k) && !first)))" " {" " world_pos[6+0+v*6] = gl_in[v].gl_Position;" " tex_coord[6+0+v*6] = vec3(vertTexCoord[v], 0.0);" " world_pos[6+1+v*6] = gl_in[w].gl_Position;" " tex_coord[6+1+v*6] = vec3(vertTexCoord[w], 0.0);" " world_pos[6+2+v*6] = gl_in[v].gl_Position + n;" " tex_coord[6+2+v*6] = vec3(vertTexCoord[v], 1.0);" " }" " k = 3+2*v;" " if(((ft == k) && first) || (((ft != k) && !first)))" " {" " world_pos[6+3+v*6] = gl_in[w].gl_Position;" " tex_coord[6+3+v*6] = vec3(vertTexCoord[w], 0.0);" " world_pos[6+4+v*6] = gl_in[w].gl_Position + n;" " tex_coord[6+4+v*6] = vec3(vertTexCoord[w], 1.0);" " world_pos[6+5+v*6] = gl_in[v].gl_Position + n;" " tex_coord[6+5+v*6] = vec3(vertTexCoord[v], 1.0);" " }" " }" " for(int t=first?ft:0; t!=8; ++t)" " {" " if(!first && (t == ft)) continue;" " int o = t*3;" " for(int v=0; v!=3; ++v)" " {" " int w = o+v;" " view_pos[w] = Matrix * world_pos[w];" " screen_pos[w] = view_pos[w].xyz/view_pos[w].w;" " }" " front_facing[t] = cross(" " screen_pos[o+1]-screen_pos[o+0]," " screen_pos[o+2]-screen_pos[o+0] " " ).z < 0.0;" " if(first) break;" " }" " if(first && !front_facing[ft]) return;" " }" " int o = ft*3;" " vec4 clip_plane[3];" " for(int v=0; v!=3; ++v)" " {" " int w = (v+1)%3;" " vec3 p0 = world_pos[o+v].xyz;" " vec3 p1 = world_pos[o+w].xyz;" " vec3 p2 = CameraPosition;" " vec3 pv = normalize(cross(p1-p0, p2-p0));" " clip_plane[v] = vec4(pv, -dot(pv, p0));" " }" " vec3 lo = CameraPosition;" " vec3 p0 = world_pos[o+0].xyz;" " vec3 pu = world_pos[o+1].xyz-p0;" " vec3 pv = world_pos[o+2].xyz-p0;" " vec3 lp = lo-p0;" " float w0 = view_pos[o+0].w;" " float wu = view_pos[o+1].w-w0;" " float wv = view_pos[o+2].w-w0;" " vec3 t0 = tex_coord[o+0];" " vec3 tu = tex_coord[o+1]-t0;" " vec3 tv = tex_coord[o+2]-t0;" " for(int bt=0; bt!=8; ++bt)" " {" " int k = bt*3;" " if((ft != bt) && !front_facing[bt])" " {" " for(int v=0; v!=3; ++v)" " {" " vec3 lt = world_pos[k+v].xyz;" " mat3 im = mat3(lo-lt, pu, pv);" " vec3 ic = inverse(im)*lp;" " float s = ic.y;" " float t = ic.z;" " geomPositionFront[v] = p0+pu*s+pv*t;" " geomTexCoordFront[v] = t0+tu*s+tv*t;" " geomWFront[v] = w0+wu*s+wv*t;" " }" " for(int v=0; v!=3; ++v)" " {" " int w = k+v;" " gl_Position = view_pos[w];" " for(int c=0; c!=3; ++c)" " {" " gl_ClipDistance[c] = dot(" " clip_plane[c]," " world_pos[w]" " );" " }" " geomPosition = world_pos[w].xyz;" " geomTexCoord = tex_coord[w];" " geomBarycentric = vec3(0.0);" " geomBarycentric[v] = 1.0;" " EmitVertex();" " }" " EndPrimitive();" " }" " }" "}" )).Compile(); prog.AttachShader(gs); FragmentShader fs; fs.Source(StrLit( "#version 330\n" "uniform float Time;" "uniform sampler2D ColorMap;" "uniform sampler2D BumpMap;" "uniform vec3 LightPosition;" "flat in mat3 geomPositionFront;" "flat in mat3 geomTexCoordFront;" "flat in vec3 geomWFront;" "noperspective in vec3 geomBarycentric;" "in vec3 geomPosition;" "in vec3 geomTexCoord;" "out vec3 fragColor;" "vec3 vcdiv(vec3 a, vec3 b)" "{" " return vec3(a.x/b.x, a.y/b.y, a.z/b.z);" "}" "void main(void)" "{" " const vec3 one = vec3(1.0, 1.0, 1.0);" " vec3 bzfv = vcdiv(geomBarycentric,geomWFront);" " vec3 p0 = geomPosition;" " vec3 p1 = (geomPositionFront*bzfv)/dot(one,bzfv);" " vec3 tc0 = geomTexCoord;" " vec3 tc1 = (geomTexCoordFront*bzfv)/dot(one,bzfv);" " ivec2 ts = textureSize(BumpMap, 0);" " int mts = max(ts.x, ts.y);" " vec2 dtc = tc1.xy - tc0.xy;" " float mdtc = max(abs(dtc.x), abs(dtc.y));" " int nsam = max(min(int(mdtc*mts), mts/2), 1);" " float step = 1.0 / nsam;" " for(int s=0; s<=nsam; ++s)" " {" " vec3 tc = mix(tc1, tc0, s*step);" " vec4 bm = texture(BumpMap, tc.xy);" " if(tc.z <= bm.w)" " {" " vec3 p = mix(p1, p0, s*step);" " vec3 ldir = normalize(LightPosition - p);" " float l = max(dot(ldir, bm.xzy), 0.0)*1.3;" " fragColor = texture(ColorMap, tc.xy).rgb*l;" " return;" " }" " }" " discard;" "}" )).Compile(); prog.AttachShader(fs); prog.Link(); prog.Use(); shape.UseInProgram(prog); auto tex_image = images::LoadTexture("stones_color_hmap"); Texture::Active(0); try { UniformSampler(prog, "ColorMap").Set(0); auto bound_tex = Bind(color_tex, Texture::Target::_2D); bound_tex.Image2D(tex_image); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::Repeat); bound_tex.WrapT(TextureWrap::Repeat); } catch(Error&){ } Texture::Active(1); try { UniformSampler(prog, "BumpMap").Set(1); auto bound_tex = Bind(bump_tex, Texture::Target::_2D); bound_tex.Image2D( images::NormalMap( tex_image, images::NormalMap::FromAlpha() ) ); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::Repeat); bound_tex.WrapT(TextureWrap::Repeat); } catch(Error&){ } gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Disable(Capability::CullFace); gl.Enable(Functionality::ClipDistance, 0); gl.Enable(Functionality::ClipDistance, 1); gl.Enable(Functionality::ClipDistance, 2); }
void RenderShadowMap(GLuint size) { // matrices auto lt_proj= CamMatrixf::PerspectiveX(Degrees(12), 1.0, 85.0, 110.0); auto light = CamMatrixf::LookingAt(light_position, Vec3f()); // setup the texture Texture::Active(shadow_tex_unit); mask_prog.Use(); Uniform<Mat4f>(mask_prog, "LightMatrix").Set(lt_proj*light); UniformSampler(mask_prog, "ShadowMap").Set(GLuint(shadow_tex_unit)); draw_prog.Use(); Uniform<Mat4f>(draw_prog, "LightMatrix").Set(lt_proj*light); UniformSampler(draw_prog, "ShadowMap").Set(GLuint(shadow_tex_unit)); Texture::Target tex_tgt = Texture::Target::_2D; shadow_map.Bind(tex_tgt); Texture::MinFilter(tex_tgt, TextureMinFilter::Linear); Texture::MagFilter(tex_tgt, TextureMagFilter::Linear); Texture::WrapS(tex_tgt, TextureWrap::ClampToEdge); Texture::WrapT(tex_tgt, TextureWrap::ClampToEdge); Texture::CompareMode(tex_tgt, TextureCompareMode::CompareRefToTexture); Texture::Image2D( tex_tgt, 0, PixelDataInternalFormat::DepthComponent32, size, size, 0, PixelDataFormat::DepthComponent, PixelDataType::Float, nullptr ); // create shadow program ShadowProgram shadow_prog(vert_shader); // VAO for the meshes in shadow program VertexArray vao = meshes.VAOForProgram(shadow_prog); vao.Bind(); // FBO for offscreen rendering of the shadow map Framebuffer::Target fbo_tgt = Framebuffer::Target::Draw; Framebuffer fbo; fbo.Bind(fbo_tgt); Framebuffer::AttachTexture(fbo_tgt, FramebufferAttachment::Depth, shadow_map, 0); // RBO for offscreen rendering Renderbuffer::Target rbo_tgt = Renderbuffer::Target::Renderbuffer; Renderbuffer rbo; rbo.Bind(rbo_tgt); Renderbuffer::Storage(rbo_tgt, PixelDataInternalFormat::RGBA, size, size); Framebuffer::AttachRenderbuffer(fbo_tgt, FramebufferAttachment::Color, rbo); // setup the matrices shadow_prog.projection_matrix.Set(lt_proj); shadow_prog.camera_matrix.Set(light); // setup and clear the viewport gl.Viewport(size, size); gl.Clear().DepthBuffer(); // draw the meshes gl.PolygonOffset(1.0, 1.0); gl.Enable(Capability::PolygonOffsetFill); meshes.Draw(); gl.Disable(Capability::PolygonOffsetFill); gl.Finish(); // bind the default framebuffer DefaultFramebuffer().Bind(Framebuffer::Target::Draw); }
void RenderOnscreen(double) { screen_prog.Use(); screen.Use(); screen.Draw(); }
void operator()(void) { gl.Use(prog); mesh.Use(); mesh.Draw(); }