Exemplo n.º 1
0
	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);
	}
Exemplo n.º 2
0
	void Render(double time)
	{
		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
		camera_matrix.Set(
			CamMatrixf::Orbiting(
				Vec3f(),
				5.0,
				Degrees(time * 11),
				Degrees(15 + (-SineWave(0.25+time/12.5)+1.0)*0.5*75)
			)
		);
		ModelMatrixf identity;
		// make the model transformation matrix
		ModelMatrixf model =
			ModelMatrixf::Translation(0.0f, 1.5f, 0.0) *
			ModelMatrixf::RotationZ(Degrees(time * 43))*
			ModelMatrixf::RotationY(Degrees(time * 63))*
			ModelMatrixf::RotationX(Degrees(time * 79));
		// make the reflection matrix
		auto reflection = ModelMatrixf::Reflection(false, true, false);
		//
		gl.Disable(Capability::Blend);
		gl.Disable(Capability::DepthTest);
		gl.Enable(Capability::StencilTest);
		gl.ColorMask(false, false, false, false);
		gl.StencilFunc(CompareFunction::Always, 1, 1);
		gl.StencilOp(StencilOp::Keep, StencilOp::Keep, StencilOp::Replace);

		gl.Bind(plane);
		model_matrix.Set(identity);
		gl.DrawArrays(PrimitiveType::TriangleStrip, 0, 4);

		gl.ColorMask(true, true, true, true);
		gl.Enable(Capability::DepthTest);
		gl.StencilFunc(CompareFunction::Equal, 1, 1);
		gl.StencilOp(StencilOp::Keep, StencilOp::Keep, StencilOp::Keep);

		// draw the cube using the reflection program
		model_matrix.Set(reflection * model);
		gl.Bind(cube);
		cube_instr.Draw(cube_indices);

		gl.Disable(Capability::StencilTest);

		// draw the cube using the normal object program
		model_matrix.Set(model);
		cube_instr.Draw(cube_indices);

		// blend-in the plane
		gl.Enable(Capability::Blend);
		gl.BlendEquation(BlendEquation::Max);
		gl.Bind(plane);
		model_matrix.Set(identity);
		gl.DrawArrays(PrimitiveType::TriangleStrip, 0, 4);
	}
void GIDeferredRenderer::Render()
{
    using namespace oglplus;
    static Context gl;
    static auto &camera = Camera::Active();
    static auto &scene = Scene::Active();
    static auto &info = Window().Info();

    if (!camera || !scene || !scene->IsLoaded() || VoxelizerRenderer::ShowVoxels)
    {
        return;
    }

    SetAsActive();
    // bind g buffer for writing
    geometryBuffer.Bind(FramebufferTarget::Draw);
    gl.ColorMask(true, true, true, true);
    gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.Viewport(info.framebufferWidth, info.framebufferHeight);
    gl.Clear().ColorBuffer().DepthBuffer();
    // activate geometry pass shader program
    CurrentProgram<GeometryProgram>(GeometryPass());
    // rendering and GL flags
    gl.ClearDepth(1.0f);
    gl.Enable(Capability::DepthTest);
    gl.Disable(Capability::Blend);
    gl.Enable(Capability::CullFace);
    gl.FrontFace(FaceOrientation::CCW);
    gl.CullFace(Face::Back);
    camera->DoFrustumCulling(true);
    // draw whole scene tree from root node
    scene->rootNode->DrawList();
    // start light pass
    DefaultFramebuffer().Bind(FramebufferTarget::Draw);
    gl.ColorMask(true, true, true, true);
    gl.Viewport(info.framebufferWidth, info.framebufferHeight);
    gl.Clear().ColorBuffer().DepthBuffer();
    CurrentProgram<LightingProgram>(LightingPass());
    // pass light info and texture locations for final light pass
    SetLightPassUniforms();
    // draw the result onto a fullscreen quad
    fsQuad.DrawElements();
}
Exemplo n.º 4
0
    void Render(double time) {
        gl.Clear().ColorBuffer().DepthBuffer().StencilBuffer();

        auto camera = CamMatrixf::Orbiting(
          Vec3f(),
          9.0,
          FullCircles(time * 0.1),
          Degrees(15 + (-SineWave(0.25 + time / 12.5) + 1.0) * 0.5 * 75));
        ModelMatrixf identity;
        ModelMatrixf model =
          ModelMatrixf::Translation(0.0f, 2.5f, 0.0) *
          ModelMatrixf::RotationA(
            Vec3f(1.0f, 1.0f, 1.0f), FullCircles(time * 0.2));

        gl.CullFace(Face::Back);
        gl.ColorMask(true, true, true, true);
        gl.DepthMask(true);
        gl.Disable(Capability::StencilTest);

        object_prog.Use();
        object_camera_matrix.Set(camera);
        object_light_mult.Set(0.2f);

        object_model_matrix.Set(identity);
        plane.Bind();
        gl.DrawArrays(PrimitiveType::TriangleStrip, 0, 4);

        object_model_matrix.Set(model);
        torus.Bind();
        torus_instr.Draw(torus_indices);

        gl.ColorMask(false, false, false, false);
        gl.DepthMask(false);
        gl.Enable(Capability::StencilTest);
        gl.StencilFunc(CompareFunction::Always, 0);
        gl.StencilOpSeparate(
          Face::Front, StencilOp::Keep, StencilOp::Keep, StencilOp::Incr);
        gl.StencilOpSeparate(
          Face::Back, StencilOp::Keep, StencilOp::Keep, StencilOp::Decr);

        shadow_prog.Use();
        shadow_camera_matrix.Set(camera);
        shadow_model_matrix.Set(model);

        gl.CullFace(Face::Back);
        torus_instr.Draw(torus_indices);
        gl.CullFace(Face::Front);
        torus_instr.Draw(torus_indices);
        gl.CullFace(Face::Back);

        gl.ColorMask(true, true, true, true);
        gl.DepthMask(true);
        gl.Clear().DepthBuffer();
        gl.StencilFunc(CompareFunction::Equal, 0);
        gl.StencilOp(StencilOp::Keep, StencilOp::Keep, StencilOp::Keep);

        object_prog.Use();
        object_light_mult.Set(2.5);

        object_model_matrix.Set(identity);
        object_color.Set(0.8f, 0.7f, 0.4f);
        plane.Bind();
        gl.DrawArrays(PrimitiveType::TriangleStrip, 0, 4);

        object_model_matrix.Set(model);
        object_color.Set(0.9f, 0.8f, 0.1f);
        torus.Bind();
        torus_instr.Draw(torus_indices);
    }
Exemplo n.º 5
0
	void RenderImage(
		double time,
		const Mat4f& torus_matrix,
		const Mat4f& light_proj_matrix
	)
	{
		// this is going into the on-screen framebuffer
		DefaultFramebuffer().Bind(Framebuffer::Target::Draw);

		gl.ClearColor(1.0f, 0.9f, 0.8f, 0.0f);
		gl.Viewport(width, height);
		gl.Clear().ColorBuffer().DepthBuffer();
		gl.CullFace(Face::Back);
		//
		transf_prog.light_proj_matrix.Set(light_proj_matrix);

		Mat4f perspective = CamMatrixf::PerspectiveX(
			Degrees(60),
			float(width)/height,
			1, 60
		);

		// setup the camera
		Vec3f camera_target(0.0f, 0.8f, 0.0f);
		auto camera = CamMatrixf::Orbiting(
			camera_target,
			GLfloat(7.0 - SineWave(time / 14.0)*3.0),
			FullCircles(time / 26.0),
			Degrees(45 + SineWave(time / 17.0) * 40)
		);
		Vec3f camera_position = camera.Position();
		transf_prog.camera_matrix.Set(perspective*camera);
		transf_prog.camera_position.Set(camera_position);

		// render into the depth buffer
		shadow_pp.Bind();

		gl.Enable(Capability::PolygonOffsetFill);
		gl.ColorMask(false, false, false, false);

		transf_prog.model_matrix = ModelMatrixf();
		plane.Draw();

		transf_prog.model_matrix.Set(torus_matrix);
		torus.Draw();

		gl.ColorMask(true, true, true, true);
		gl.Disable(Capability::PolygonOffsetFill);

		gl.Enable(Capability::Blend);
		gl.Disable(Capability::Blend);

		// render into the color buffer
		sketch_pp.Bind();

		gl.Enable(Capability::Blend);

		transf_prog.model_matrix = ModelMatrixf();
		transf_prog.texture_matrix.Set(Mat2f(Vec2f(3.0, 0.0), Vec2f(0.0, 3.0)));
		plane.Draw();

		transf_prog.model_matrix.Set(torus_matrix);
		transf_prog.texture_matrix.Set(Mat2f(Vec2f(8.0, 0.0), Vec2f(0.0, 2.0)));
		torus.Draw([](GLuint phase) -> bool { return phase <  4; });
		transf_prog.texture_matrix.Set(Mat2f(Vec2f(0.0, 2.0), Vec2f(8.0, 0.0)));
		torus.Draw([](GLuint phase) -> bool { return phase >= 4; });

		// render the edges
		line_pp.Bind();

		transf_prog.model_matrix = ModelMatrixf();
		plane.DrawEdges();

		transf_prog.model_matrix.Set(torus_matrix);
		torus.DrawEdges();

		gl.Disable(Capability::Blend);
	}