void Render(double time) { namespace se = oglplus::smart_enums; gl.Clear().ColorBuffer().DepthBuffer().StencilBuffer(); // make the camera matrix orbiting around the origin // at radius of 3.5 with elevation between 15 and 90 degrees Mat4f camera = CamMatrixf::Orbiting( Vec3f(), 6.5, Degrees(time * 135), Degrees(15 + (-SineWave(0.25+time/12.5)+1.0)*0.5*75) ); ModelMatrixf model = ModelMatrixf::Translation(0.0f, 1.5f, 0.0); ModelMatrixf identity; // SetProgramUniform(prog_norm, "CameraMatrix", camera); SetProgramUniform(prog_refl, "CameraMatrix", camera); // draw the plane into the stencil buffer prog_norm.Use(); gl.Disable(se::Blend()); gl.Disable(se::DepthTest()); gl.Enable(se::StencilTest()); gl.ColorMask(false, false, false, false); gl.StencilFunc(se::Always(), 1, 1); gl.StencilOp(se::Keep(), se::Keep(), se::Replace()); Uniform<Mat4f> model_matrix_norm(prog_norm, "ModelMatrix"); model_matrix_norm.Set(identity); plane.Bind(); gl.DrawArrays(se::TriangleStrip(), 0, 4); gl.ColorMask(true, true, true, true); gl.Enable(se::DepthTest()); gl.StencilFunc(se::Equal(), 1, 1); gl.StencilOp(se::Keep(), se::Keep(), se::Keep()); // draw the torus using the reflection program prog_refl.Use(); Uniform<Mat4f>(prog_refl, "ModelMatrix").Set(model); torus.Bind(); torus_instr.Draw(torus_indices); gl.Disable(se::StencilTest()); prog_norm.Use(); // draw the torus using the normal object program model_matrix_norm.Set(model); torus_instr.Draw(torus_indices); // blend-in the plane gl.Enable(se::Blend()); gl.BlendEquation(se::Max()); model_matrix_norm.Set(identity); plane.Bind(); gl.DrawArrays(se::TriangleStrip(), 0, 4); }
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) { gl.Clear().ColorBuffer().DepthBuffer(); // // camera matrix auto camera = CamMatrixf::Orbiting( Vec3f(), 4.5f, Degrees(time * 50), Degrees(SineWave(time / 15.0) * 70) ); // model matrix auto model = ModelMatrixf::Translation( 0.0f, sqrt(1.0f+SineWave(time / 2.0)), 0.0f ) * ModelMatrixf::RotationX(FullCircles(time)); // // use transform feedback to get transformed hole vertices prog_tfb.Use(); Uniform<Mat4f>(prog_tfb, "CameraMatrix").Set(camera); Uniform<Mat4f>(prog_tfb, "ModelMatrix").Set(model); holes.Bind(); { TransformFeedback::Activator activates_tfb( TransformFeedbackPrimitiveType::Points ); gl.DrawArrays(PrimitiveType::Points, 0, hole_count); } prog.Use(); // Uniform<Mat4f>(prog, "CameraMatrix").Set(camera); Uniform<Mat4f>(prog, "ModelMatrix").Set(model); // map the transform feedback buffer BufferTypedMap<GLfloat> transf_hole_verts_map( Buffer::Target::TransformFeedback, BufferMapAccess::Read ); // use the values stored in the buffer as the input // for the fragment shader, that will use them to // calculate the bump map Uniform<GLfloat>(prog, "TransfHole").SetVectors<3>( transf_hole_verts_map.Count(), transf_hole_verts_map.Data() ); // bind the VAO for the spere and render it sphere.Bind(); sphere_instr.Draw(sphere_indices); }
void Render(double time) { static const ModelMatrixf reflection = ModelMatrixf::Translation(0.0f, -1.0f, 0.0f) * ModelMatrixf::Reflection(false, true, false); auto camera = CamMatrixf::Orbiting( Vec3f(), 4.5, FullCircles(time / 10.0), Degrees(45.0 - SineWave(time / 7.0)*35.0) ); shape_prog.Use(); shape.Bind(); gl.FrontFace(make_shape.FaceWinding()); shape_model_matrix = ModelMatrixf::Translation(0.0f, 1.1f, 0.0f) * ModelMatrixf::RotationX(FullCircles(time / 12.0)); // render into the off-screen framebuffer fbo.Bind(Framebuffer::Target::Draw); gl.Viewport(width/tex_size_div, height/tex_size_div); gl.Clear().ColorBuffer().DepthBuffer(); shape_camera_matrix = camera * reflection; gl.FrontFace(Inverted(make_shape.FaceWinding())); shape_instr.Draw(shape_indices); // render into the on-screen framebuffer dfb.Bind(Framebuffer::Target::Draw); gl.Viewport(width, height); gl.Clear().ColorBuffer().DepthBuffer(); shape_camera_matrix = camera; gl.FrontFace(make_shape.FaceWinding()); shape_instr.Draw(shape_indices); // Render the plane plane_prog.Use(); plane.Bind(); gl.FrontFace(make_plane.FaceWinding()); plane_camera_matrix = camera; plane_model_matrix = ModelMatrixf::Translation(0.0f, -0.5f, 0.0f); plane_instr.Draw(plane_indices); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); // auto lightAzimuth = FullCircles(time * -0.5); light_pos.Set( Vec3f( Cos(lightAzimuth), 1.0f, Sin(lightAzimuth) ) * 2.0f ); // camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(), 3.0f, Degrees(-45), Degrees(SineWave(time / 30.0) * 70) ) ); // set the model matrix model_matrix.Set( ModelMatrixf::RotationY(FullCircles(time * 0.05)) ); cube.Bind(); gl.CullFace(Face::Back); cube_instr.Draw(cube_indices); }
void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const { shader.Bind(); ib.Bind(); va.Bind(); GLCall(glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, 0)); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); // // set the matrix for camera orbiting the origin camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(), 4.0 - SineWave(time / 6.0) * 2.0, FullCircles(time * 0.4), Degrees(SineWave(time / 30.0) * 90) ) ); // set the model matrix model_matrix.Set( ModelMatrixf::RotationZ(FullCircles(time * 0.1)) ); cube.Bind(); gl.CullFace(Face::Front); cube_instr.Draw(cube_indices); gl.CullFace(Face::Back); cube_instr.Draw(cube_indices); }
VertexArray VAOForProgram(const ProgramOps& prog) const { VertexArray vao; vao.Bind(); prog.Use(); size_t i=0, n = _names.size(); while(i != n) { if(_npvs[i] != 0) { try { _vbos[i].Bind(Buffer::Target::Array); VertexAttribArray attr(prog, _names[i]); attr.Setup(_npvs[i], DataType::Float); attr.Enable(); } catch(Error&){ } } ++i; } assert((i+1) == _npvs.size()); if(_npvs[i] != 0) { assert((i+1) == _vbos.size()); _vbos[i].Bind(Buffer::Target::ElementArray); } return std::move(vao); }
Shape(const Program& prog, const ShapeBuilder& builder) : make_shape(builder) , shape_instr(make_shape.Instructions()) , shape_indices(make_shape.Indices()) , vbos(4) { // bind the VAO for the shape vao.Bind(); typename ShapeBuilder::VertexAttribs vert_attr_info; const GLchar* vert_attr_name[] = { "Position", "Normal", "Tangent", "TexCoord"}; for(int va = 0; va != 4; ++va) { const GLchar* name = vert_attr_name[va]; std::vector<GLfloat> data; auto getter = vert_attr_info.VertexAttribGetter(data, name); if(getter != nullptr) try { // bind the VBO for the vertex attribute vbos[va].Bind(Buffer::Target::Array); GLuint npv = getter(make_shape, data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array VertexArrayAttrib attr(prog, name); attr.Setup<GLfloat>(npv); attr.Enable(); } catch(Error&) { } } }
void Update(void) { 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.tex_1.Set(holder.TexUnit1()); prog.tex_2.Set(holder.TexUnit2()); prog.new_drop.Set(std::rand()%size, std::rand()%size); vao.Bind(); gl.DrawArrays(PrimitiveType::Points, 0, 1); holder.Swap(); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); // Uniform<Mat4f>(prog, "cameraMatrix").Set( CamMatrixf::Orbiting( Vec3f(), 3.0 + std::sin(time)*1.5, FullCircles(time * 0.5), Degrees(std::sin(time * 0.5) * 70) ) ); Uniform<Mat4f>(prog, "modelMatrix").Set( ModelMatrixf::RotationA( Vec3f(1.0f, 1.0f, 1.0f), FullCircles(time * 0.4) ) ); Uniform<Vec3f>(prog, "lightPos").Set(Vec3f(1.0f, 2.0f, 3.0f*std::sin(time * 2.9))); vao.Bind(); // This is not very effective shape.Instructions().Draw(shape.Indices()); }
Grid(const Program& prog, float quality) : make_grid(1.0, 16 + quality*quality*64) , grid_instr(make_grid.Instructions()) , grid_indices(make_grid.Indices()) { // bind the VAO for the shape vao.Bind(); std::vector<GLfloat> data; // bind the VBO for the positions positions.Bind(Buffer::Target::Array); GLuint n_per_vertex = make_grid.Positions(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array VertexArrayAttrib attr(prog, "Position"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); // bind the VBO for the indices indices.Bind(Buffer::Target::ElementArray); // upload them Buffer::Data(Buffer::Target::ElementArray, grid_indices); // clear the indexs buffer grid_indices.clear(); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); // // set the matrix for camera orbiting the origin camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(), 4.5, Degrees(time * 35), Degrees(SineWave(time / 20.0) * 60) ) ); // set the model matrix model_matrix.Set( ModelMatrixf::RotationX(FullCircles(time * 0.25)) ); torus.Bind(); gl.PolygonMode(PolygonMode::Line); gl.CullFace(Face::Front); torus_instr.Draw(torus_indices); // gl.PolygonMode(PolygonMode::Fill); gl.CullFace(Face::Back); torus_instr.Draw(torus_indices); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); // // set the matrix for camera orbiting the origin camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(), 4.5 - SineWave(time / 16.0) * 2.0, FullCircles(time / 12.0), Degrees(SineWave(time / 30.0) * 90) ) ); // set the model matrix model_matrix.Set( ModelMatrixf::RotationA( Vec3f(1.0f, 1.0f, 1.0f), FullCircles(time / 10.0) ) ); shape.Bind(); shape_instr.Draw(shape_indices); }
RectangleExample(void) { prog << "#version 330\n" "in vec2 Position;" "in vec3 Color;" "out vec3 vertColor;" "void main(void)" "{" " vertColor = Color;" " gl_Position = vec4(Position, 0.0, 1.0);" "}"_glsl_vs; prog << "#version 330\n" "in vec3 vertColor;" "out vec4 fragColor;" "void main(void)" "{" " fragColor = vec4(vertColor, 1.0);" "}"_glsl_fs; prog.Link(); prog.Use(); // bind the VAO for the rectangle rectangle.Bind(); GLfloat rectangle_verts[8] = { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }; // bind the VBO for the rectangle vertices verts.Bind(Buffer::Target::Array); // upload the data Buffer::Data(Buffer::Target::Array, 8, rectangle_verts); // setup the vertex attribs array for the vertices VertexAttribArray vert_attr(prog, "Position"); vert_attr.Setup(2, DataType::Float); vert_attr.Enable(); GLfloat rectangle_colors[12] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, }; // bind the VBO for the rectangle colors colors.Bind(Buffer::Target::Array); // upload the data Buffer::Data(Buffer::Target::Array, 12, rectangle_colors); // setup the vertex attribs array for the vertices VertexAttribArray color_attr(prog, "Color"); color_attr.Setup(3, DataType::Float); color_attr.Enable(); // gl.ClearDepth(1.0f); }
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) { fbo.Bind(Framebuffer::Target::Draw); gl.Clear().ColorBuffer().DepthBuffer(); main_prog.Use(); cube.Bind(); camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(), 20.5, FullCircles(time / 20.0), Degrees(SineWave(time / 25.0) * 30) ) ); auto i = cube_matrices.begin(), e = cube_matrices.end(); while(i != e) { model_matrix.Set(*i); ambient_color.Set(0.7f, 0.6f, 0.2f); diffuse_color.Set(1.0f, 0.8f, 0.3f); face_instr.Draw(face_indices); ambient_color.Set(0.1f, 0.1f, 0.1f); diffuse_color.Set(0.3f, 0.3f, 0.3f); edge_instr.Draw(edge_indices); ++i; } dfb.Bind(Framebuffer::Target::Draw); gl.Clear().ColorBuffer().DepthBuffer(); dof_prog.Use(); screen.Bind(); focus_depth.Set(0.6 + SineWave(time / 9.0)*0.3); gl.Enable(Capability::Blend); gl.DrawArrays(PrimitiveType::TriangleStrip, 0, 4); gl.Disable(Capability::Blend); }
void Render(double /*time*/) { gl.Clear().ColorBuffer(); // draw the lines between control points color = Vec3f(0.9f, 0.9f, 0.2f); control.Bind(); gl.DrawArrays(PrimitiveType::LineStrip, 0, ctrl_n); // draw the curve color = Vec3f(0.1f, 0.1f, 0.1f); curve.Bind(); gl.DrawArrays(PrimitiveType::LineStrip, 0, curve_n); // draw the control points color = Vec3f(0.9f, 0.0f, 0.0f); control.Bind(); gl.PointSize(8.0); gl.DrawArrays(PrimitiveType::Points, 0, ctrl_n); gl.PointSize(1.0); }
void Render(const Mat4f& model) { prog.Use(); model_matrix.Set(model); // bind the VAO sphere.Bind(); // use the instructions to draw the sphere // (this basically calls glDrawArrays* or glDrawElements*) sphere_instr.Draw(sphere_indices); }
Shape(const Program& prog) : vbos(4) , point_count(4096) { // bind the VAO for the shape vao.Bind(); make_shape_1(prog, 0, "Position1"); make_shape_2(prog, 1, "Position2"); make_radiance(prog, 2, "Radiance1"); make_radiance(prog, 3, "Radiance2"); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); auto camera = CamMatrixf::Orbiting( Vec3f(), 8.5, FullCircles(time / 5.0), Degrees(15 + (-SineWave(time/10.0)+1.0)* 0.5 * 75) ); ModelMatrixf model = ModelMatrixf::Translation(0.0f, 2.5f, 0.0) * ModelMatrixf::RotationA( Vec3f(1.0f, 1.0f, 1.0f), FullCircles(time / 7.0) ); plane_prog.Use(); plane_camera_matrix.Set(camera); plane.Bind(); gl.DrawArrays(PrimitiveType::TriangleStrip, 0, 4); shape_prog.Use(); shape_camera_matrix.Set(camera); shape_model_matrix.Set(model); shape.Bind(); shape_instr.Draw(shape_indices); halo_prog.Use(); halo_camera_matrix.Set(camera); halo_model_matrix.Set(model); gl.DepthMask(false); gl.Enable(Capability::Blend); shape_instr.Draw(shape_indices); gl.Disable(Capability::Blend); gl.DepthMask(true); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); auto cameraMatrix = CamMatrixf::Orbiting( Vec3f(0.0f, 3.0f, 0.0f), 8.0f, FullCircles(time / 12.0), Degrees(SineWave(time / 20.0) * 80) ); plane.Bind(); plane_prog.Use(); Uniform<Mat4f>(plane_prog, "CameraMatrix").Set(cameraMatrix); gl.DrawArrays(PrimitiveType::TriangleStrip, 0, 4); gl.Enable(Capability::Blend); volume.Bind(); volume_prog.Use(); Uniform<Mat4f>(volume_prog, "CameraMatrix").Set(cameraMatrix); Uniform<Vec3f>(volume_prog, "ViewX").Set( cameraMatrix.Row(0).xyz() ); Uniform<Vec3f>(volume_prog, "ViewY").Set( cameraMatrix.Row(1).xyz() ); Uniform<Vec3f>(volume_prog, "ViewZ").Set( cameraMatrix.Row(2).xyz() ); gl.DrawArraysInstanced( PrimitiveType::Points, 0, 1, samples ); gl.Disable(Capability::Blend); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); auto lightPos = light_path.Position(time * 0.05); auto cameraMatrix = CamMatrixf::Orbiting( Vec3f(), 4.5f, Degrees(0), Degrees(SineWave(time / 20.0) * 80) ); light.Bind(); light_prog.Use(); Uniform<Vec3f>(light_prog, "LightPos").Set(lightPos); Uniform<Mat4f>(light_prog, "CameraMatrix").Set(cameraMatrix); sphere_instr.Draw(sphere_indices); clouds.Bind(); cloud_prog.Use(); Uniform<Vec3f>(cloud_prog, "LightPos").Set(lightPos); Uniform<Mat4f>(cloud_prog, "CameraMatrix").Set(cameraMatrix); Uniform<Vec4f>(cloud_prog, "ViewX").Set(cameraMatrix.Row(0)); Uniform<Vec4f>(cloud_prog, "ViewY").Set(cameraMatrix.Row(1)); Uniform<Vec4f>(cloud_prog, "ViewZ").Set(cameraMatrix.Row(2)); for(std::size_t i=0, n=positions.size(); i!=n; ++i) { cloud_tex[i].Bind(Texture::Target::_3D); gl.DrawArraysInstanced( PrimitiveType::Points, i, 1, samples ); } }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); CamMatrixf camera = CamMatrixf::Orbiting( Vec3f(), 4.5f + float(SineWave(time / 25.0)), FullCircles(time / 30.0), Degrees(SineWave(time / 19.0) * 20) ); light_prog.camera_matrix.Set(camera); flare_prog.camera_matrix.Set(camera); shape_prog.camera_matrix.Set(camera); shape_prog.camera_position.Set(camera.Position()); shape_prog.model_matrix.Set( ModelMatrixf::RotationX(FullCircles(time / 30.0)) ); shape_prog.Use(); shape.Draw(); NoProgram().Use(); lights.Bind(); light_prog.Use(); for(GLuint l=0; l!=n_flares; ++l) { queries[l].Begin(Query::Target::SamplesPassed); gl.DrawArrays(PrimitiveType::Points, l, 1); queries[l].End(Query::Target::SamplesPassed); } gl.Enable(Capability::Blend); gl.Disable(Capability::DepthTest); flare_prog.Use(); for(GLuint l=0; l!=n_flares; ++l) { GLint samples = 0; queries[l].WaitForResult(samples); if(samples != 0) { flare_prog.samples = samples; gl.DrawArrays(PrimitiveType::Points, l, 1); } } gl.Enable(Capability::DepthTest); gl.Disable(Capability::Blend); }
RippleGenerator(GLuint first_tex_unit) : size(1024) , prog(size) , holder(size, first_tex_unit) { fbo.Bind(Framebuffer::Target::Draw); Framebuffer::AttachColorTexture( Framebuffer::Target::Draw, 0, holder.BumpMap(), 0 ); vao.Bind(); }
Particle(const VertexShader& vs, FragmentShader&& frag) : sphere_instr(make_sphere.Instructions()) , sphere_indices(make_sphere.Indices()) , fs(std::forward<FragmentShader>(frag)) { // attach the shaders to the program prog.AttachShader(vs); prog.AttachShader(fs); // link and use it prog.Link(); prog.Use(); projection_matrix = (prog/"ProjectionMatrix"); camera_matrix = (prog/"CameraMatrix"); model_matrix = (prog/"ModelMatrix"); light_pos = (prog/"LightPos"); // bind the VAO for the sphere sphere.Bind(); const GLuint n_attr = 2; // pointers to the vertex attribute data build functions typedef GLuint (shapes::Sphere::*Func)(std::vector<GLfloat>&) const; Func func[n_attr] = { &shapes::Sphere::Positions, &shapes::Sphere::Normals, }; // managed references to the VBOs Reference<Buffer> vbo[n_attr] = {verts, normals}; // vertex attribute identifiers from the shaders const GLchar* ident[n_attr] = {"Position", "Normal"}; for(GLuint i=0; i!=n_attr; ++i) { // bind the VBO vbo[i].Bind(Buffer::Target::Array); // make the data std::vector<GLfloat> data; GLuint n_per_vertex = (make_sphere.*func[i])(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attrib VertexArrayAttrib attr(prog, ident[i]); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } }
TorusExample(void) : make_torus(1.0, 0.5, 36, 24) , torus_instr(make_torus.Instructions()) , torus_indices(make_torus.Indices()) , prog(make_prog()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , model_matrix(prog, "ModelMatrix") { Uniform<Vec4f>(prog, "ClipPlane").Set(0.f, 0.f, 1.f, 0.f); // bind the VAO for the torus torus.Bind(); // bind the VBO for the torus vertices verts.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_torus.Positions(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices VertexArrayAttrib attr(prog, "Position"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VBO for the torus UV-coordinates texcoords.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_torus.TexCoordinates(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices VertexArrayAttrib attr(prog, "TexCoord"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f); gl.ClearDepth(1.0f); gl.FrontFace(make_torus.FaceWinding()); gl.Enable(Capability::DepthTest); gl.Enable(Functionality::ClipDistance, 0); }
void Render( const Vec3f& light, const Mat4f& camera, const Mat4f& model ) { // use the shading program prog.Use(); // set the uniforms light_pos.Set(light); camera_matrix.Set(camera); model_matrix.Set(model); // bind the VAO shape.Bind(); // use the instructions to draw the shape // (this basically calls glDrawArrays* or glDrawElements*) shape_instr.Draw(shape_indices); }
void Render(double time) { gl.Clear().ColorBuffer().DepthBuffer(); // // set the matrix for camera orbiting the origin camera_matrix.Set( CamMatrixf::Orbiting( Vec3f(0.0f, 0.5f, 0.0f), 6.5, Degrees(time * 35), Degrees(55 - SineWave(time / 20.0) * 30) ) ); light_pos.Set(light_path.Position(time / 10.0)); plane.Bind(); plane_instr.Draw(plane_indices); }
Shape(const Shader& vs, const Shader& fs) : shape_instr(make_shape.Instructions()) , shape_indices(make_shape.Indices()) , prog(make(vs, fs)) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , model_matrix(prog, "ModelMatrix") , light_pos(prog, "LightPos") { // bind the VAO for the shape shape.Bind(); const GLuint n_attr = 3; // pointers to the vertex attribute data build functions typedef GLuint (ShapeMaker::*Func)(std::vector<GLfloat>&) const; Func func[n_attr] = { &ShapeMaker::Positions, &ShapeMaker::Normals, &ShapeMaker::TexCoordinates }; // managed references to the VBOs Reference<Buffer> vbo[n_attr] = {verts, normals, texcoords}; // vertex attribute identifiers from the shaders const GLchar* ident[n_attr] = {"Position", "Normal", "TexCoord"}; for(GLuint i=0; i!=n_attr; ++i) { // bind the VBO vbo[i].Bind(Buffer::Target::Array); // make the data std::vector<GLfloat> data; GLuint n_per_vertex = (make_shape.*func[i])(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attrib VertexArrayAttrib attr(prog, ident[i]); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } }