Ejemplo n.º 1
0
	void init_heights(void)
	{
		ProgramUniformSampler(display_prog, "Heights").Set(1);
		ProgramUniformSampler(shadow_prog, "Heights").Set(1);
		heights.target = Texture::Target::_2D;
		heights.BindMulti(1, Texture::Target::_2D);
		heights.Image2D(
			0,
			PixelDataInternalFormat::DepthComponent32,
			side, side,
			0,
			PixelDataFormat::DepthComponent,
			PixelDataType::Float,
			nullptr
		);
		heights.BorderColor(Vec4f(1,1,1,1));
		heights.MinFilter(TextureMinFilter::Nearest);
		heights.MagFilter(TextureMagFilter::Nearest);
		heights.WrapS(TextureWrap::ClampToBorder);
		heights.WrapT(TextureWrap::ClampToBorder);

		heights_fbo.target = Framebuffer::Target::Draw;
		heights_fbo.AttachTexture(FramebufferAttachment::Depth, heights, 0);
		heights_fbo.Complete();
	}
Ejemplo n.º 2
0
	CubeExample(void)
	 : n(16)
	 , screen_prog()
	 , draw_prog()
	 , screen(List("Position")("TexCoord").Get(), shapes::Screen(), screen_prog)
	 , cube(List("Position").Get(), shapes::Cube(), draw_prog)
	{

		draw_prog.Use();

		UniformBlock(draw_prog, "OffsetBlock").Binding(0);
		cube_pos<< BufferIndexedTarget::Uniform << 0
			<< BufferUsage::StaticDraw
			<< OffsetData(n);

		ProgramUniformSampler(screen_prog, "Palette").Set(0);
		Texture::Active(0);
		palette	<< Texture::Target::_1D
			<< TextureFilter::Nearest
			<< TextureWrap::ClampToEdge
			<< images::LinearGradient(
				16,
				Vec3f(0, 0, 0),
				std::map<GLfloat, Vec3f>({
					{  0.0/16.0, Vec3f(0.0, 0.0, 0.0)},
					{  1.0/16.0, Vec3f(0.5, 0.0, 1.0)},
					{  3.0/16.0, Vec3f(0.0, 0.0, 1.0)},
					{  6.0/16.0, Vec3f(0.0, 0.6, 0.6)},
					{  8.0/16.0, Vec3f(0.0, 1.0, 0.0)},
					{ 11.0/16.0, Vec3f(0.6, 0.6, 0.0)},
					{ 13.0/16.0, Vec3f(1.0, 0.1, 0.0)},
					{ 16.0/16.0, Vec3f(0.7, 0.0, 0.0)}
				})
			);

		ProgramUniformSampler(screen_prog, "Tex").Set(1);
		Texture::Active(1);
		tex	<< Texture::Target::Rectangle
			<< TextureMinFilter::Nearest
			<< TextureMagFilter::Nearest
			<< TextureWrap::ClampToEdge
			<< images::ImageSpec(
				64, 64,
				Format::Red,
				InternalFormat::R8,
				DataType::UnsignedByte
			);

		rbo	<< Renderbuffer::Target::Renderbuffer
			<< images::ImageSpec(64, 64, InternalFormat::DepthComponent);

		fbo	<< Framebuffer::Target::Draw
			<< FramebufferAttachment::Color << tex
			<< FramebufferAttachment::Depth << rbo
			<< FramebufferComplete();

		gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.ClearDepth(1.0f);
		gl.Enable(Capability::CullFace);
	}
Ejemplo n.º 3
0
	void init_offsets(void)
	{
		std::vector<GLfloat> offset_data(side*side*2);

		auto p = offset_data.begin();

		for(GLuint j=0; j!=side; ++j)
		{
			GLfloat v = j/GLfloat(side-1) - 0.5f;
			for(GLuint i=0; i!=side; ++i)
			{
				GLfloat u = i/GLfloat(side-1) - 0.5f;
				*p++ = u*side;
				*p++ = v*side;
			}
		}

		ProgramUniformSampler(display_prog, "Offsets").Set(0);
		ProgramUniformSampler(shadow_prog, "Offsets").Set(0);
		offsets.target = Texture::Target::_2D;
		offsets.BindMulti(0, Texture::Target::_2D);
		offsets.Image2D(
			0,
			PixelDataInternalFormat::RG32F,
			side, side,
			0,
			PixelDataFormat::RG,
			PixelDataType::Float,
			offset_data.data()
		);
		offsets.MinFilter(TextureMinFilter::Nearest);
		offsets.MagFilter(TextureMagFilter::Nearest);
		offsets.WrapS(TextureWrap::ClampToBorder);
		offsets.WrapT(TextureWrap::ClampToBorder);
	}
Ejemplo n.º 4
0
	LiquidExample(const ExampleParams& params)
	 : liquid_prog()
	 , grid(liquid_prog, params.quality)
	 , grid_repeat(int(1 + params.quality*2))
	{
		Texture::Active(1);
		{
			auto image = images::Squares(512, 512, 0.9f, 8, 8);
			auto bound_tex = gl.Bound(Texture::Target::CubeMap, env_map);
			for(int i=0; i!=6; ++i)
				Texture::ImageCM(i, image);
			bound_tex.GenerateMipmap();
			bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear);
			bound_tex.MagFilter(TextureMagFilter::Linear);
			bound_tex.WrapS(TextureWrap::ClampToEdge);
			bound_tex.WrapT(TextureWrap::ClampToEdge);
			bound_tex.WrapR(TextureWrap::ClampToEdge);
			bound_tex.SwizzleG(TextureSwizzle::Red);
			bound_tex.SwizzleB(TextureSwizzle::Red);
		}
		ProgramUniformSampler(liquid_prog, "EnvMap").Set(1);

		const Vec3f light_position(12.0, 1.0, 8.0);
		liquid_prog.light_position.Set(light_position);

		gl.ClearColor(0.7f, 0.65f, 0.55f, 0.0f);
		gl.ClearDepth(1.0f);
		gl.Enable(Capability::DepthTest);

		gl.Enable(Capability::CullFace);
		gl.FrontFace(FaceOrientation::CW);
		gl.CullFace(Face::Back);
	}
Ejemplo n.º 5
0
	void init_shadows(void)
	{
		ProgramUniformSampler(display_prog, "Shadows").Set(2);
		shadows.target = Texture::Target::_2D;
		shadows.BindMulti(2, Texture::Target::_2D);
		shadows.Image2D(
			0,
			PixelDataInternalFormat::DepthComponent32,
			shadow_size, shadow_size,
			0,
			PixelDataFormat::DepthComponent,
			PixelDataType::Float,
			nullptr
		);
		shadows.BorderColor(Vec4f(1,1,1,1));
		shadows.MinFilter(TextureMinFilter::Linear);
		shadows.MagFilter(TextureMagFilter::Linear);
		shadows.WrapS(TextureWrap::ClampToBorder);
		shadows.WrapT(TextureWrap::ClampToBorder);
		shadows.CompareMode(TextureCompareMode::CompareRefToTexture);

		shadows_fbo.target = Framebuffer::Target::Draw;
		shadows_fbo.AttachTexture(FramebufferAttachment::Depth, shadows, 0);
		shadows_fbo.Complete();
	}
Ejemplo n.º 6
0
	AmbiOcclMaps(const images::Image& smaps, const Program& prog)
	{

		ProgramUniformSampler(prog, "AmbiOcclMaps").Set(0);
		this->BindMulti(0, Texture::Target::_2DArray);
		this->Image3D(smaps);
		this->MinFilter(TextureMinFilter::Linear);
		this->MagFilter(TextureMagFilter::Linear);
		this->WrapS(TextureWrap::ClampToEdge);
		this->WrapT(TextureWrap::ClampToEdge);
	}
Ejemplo n.º 7
0
	LiquidExample(const ExampleParams& params)
	 : liquid_prog()
	 , grid(liquid_prog, params.quality)
	 , grid_repeat(1 + params.quality*2)
	{
		Texture::Active(0);
		{
			auto image = images::NewtonFractal(
				256, 256,
				Vec3f(0.1f, 0.1f, 0.1f),
				Vec3f(1.0f, 1.0f, 1.0f),
				Vec2f(-1.0f, -1.0f),
				Vec2f( 1.0f,  1.0f),
				images::NewtonFractal::X4Minus1(),
				images::NewtonFractal::DefaultMixer()
			);
			auto bound_tex = oglplus::Context::Bound(
				Texture::Target::CubeMap,
				env_map
			);
			for(int i=0; i!=6; ++i)
				Texture::ImageCM(i, image);
			bound_tex.GenerateMipmap();
			bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear);
			bound_tex.MagFilter(TextureMagFilter::Linear);
			bound_tex.WrapS(TextureWrap::ClampToEdge);
			bound_tex.WrapT(TextureWrap::ClampToEdge);
			bound_tex.WrapR(TextureWrap::ClampToEdge);
		}
		ProgramUniformSampler(liquid_prog, "EnvMap").Set(0);

		const Vec3f light_position(12.0, 1.0, 8.0);
		liquid_prog.light_position.Set(light_position);

		gl.ClearColor(0.7f, 0.65f, 0.55f, 0.0f);
		gl.ClearDepth(1.0f);
		gl.Enable(Capability::DepthTest);

		gl.Enable(Capability::CullFace);
		gl.FrontFace(FaceOrientation::CW);
		gl.CullFace(Face::Back);
	}
Ejemplo n.º 8
0
	DataBuffer(const Program& prog, GLuint tex_unit, GLuint width, GLuint height)
	{
		Texture::Active(tex_unit);
		ProgramUniformSampler(prog, "DataMap").Set(tex_unit);

		tex	<< Texture::Target::Rectangle
			<< TextureFilter::Linear
			<< TextureWrap::ClampToEdge
			<< images::ImageSpec(
				width, height,
				Format::RGBA,
				InternalFormat::RGBA32F,
				PixelDataType::Float
			);

		rbo	<< Renderbuffer::Target::Renderbuffer
			<< images::ImageSpec(width, height, InternalFormat::DepthComponent32);

		fbo	<< Framebuffer::Target::Draw
			<< FramebufferAttachment::Color << tex
			<< FramebufferAttachment::Depth << rbo
			<< FramebufferComplete();
	}
Ejemplo n.º 9
0
	DepthBuffer(const Program& prog, GLuint tex_unit, GLuint tex_side)
	 : side(tex_side)
	{
		Texture::Active(tex_unit);
		ProgramUniformSampler(prog, "DepthMap").Set(tex_unit);

		tex	<< Texture::Target::_2D
			<< TextureFilter::Nearest
			<< TextureWrap::ClampToEdge
			<< images::ImageSpec(
				side, side,
				Format::DepthComponent,
				InternalFormat::DepthComponent32,
				PixelDataType::Float
			);

		rbo	<< Renderbuffer::Target::Renderbuffer
			<< images::ImageSpec(side, side, InternalFormat::R8);

		fbo	<< Framebuffer::Target::Draw
			<< FramebufferAttachment::Color << rbo
			<< FramebufferAttachment::Depth << tex
			<< FramebufferComplete();
	}
Ejemplo n.º 10
0
    ReflectionExample()
      : make_plane(
          Vec3f(), Vec3f(3.0f, 0.0f, 0.0f), Vec3f(0.0f, 0.0f, -3.0f), 15, 15)
      , plane_instr(make_plane.Instructions())
      , plane_indices(make_plane.Indices())
      , make_shape()
      , shape_instr(make_shape.Instructions())
      , shape_indices(make_shape.Indices())
      , plane_vs(ObjectDesc("Plane vertex"))
      , shape_vs(ObjectDesc("Shape vertex"))
      , plane_fs(ObjectDesc("Plane fragment"))
      , shape_fs(ObjectDesc("Shape fragment"))
      , plane_projection_matrix(plane_prog)
      , plane_camera_matrix(plane_prog)
      , plane_model_matrix(plane_prog)
      , shape_projection_matrix(shape_prog)
      , shape_camera_matrix(shape_prog)
      , shape_model_matrix(shape_prog)
      , width(800)
      , height(600)
      , tex_size_div(2) {
        plane_vs.Source(
          "#version 140\n"
          "uniform vec3 LightPosition;"
          "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
          "in vec4 Position;"
          "out vec3 vertLightDir;"
          "out vec4 vertTexCoord;"
          "void main()"
          "{"
          "	gl_Position = ModelMatrix*Position;"
          "	vertLightDir = LightPosition - gl_Position.xyz;"
          "	gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;"
          "	vertTexCoord = gl_Position;"
          "}");
        plane_vs.Compile();

        plane_fs.Source(
          "#version 140\n"
          "uniform sampler2DRect ReflectTex;"
          "uniform vec3 Normal;"
          "in vec3 vertLightDir;"
          "in vec4 vertTexCoord;"
          "out vec3 fragColor;"
          "const int n = 5;"
          "const int ns = (n*n);"
          "const float blur = 0.15/n;"
          "void main()"
          "{"
          "	float d = dot(Normal, normalize(vertLightDir));"
          "	float intensity = 0.5 + pow(1.4*d, 2.0);"
          "	vec3 color = vec3(0.0, 0.0, 0.0);"
          "	int n = 2;"
          "	float pct = 0.5/vertTexCoord.w;"
          "	for(int y=-n; y!=(n+1); ++y)"
          "	for(int x=-n; x!=(n+1); ++x)"
          "	{"
          "		vec2 coord = vertTexCoord.xy;"
          "		coord += vec2(blur*x, blur*y);"
          "		coord *= pct;"
          "		coord += vec2(0.5, 0.5);"
          "		coord *= textureSize(ReflectTex);"
          "		color += texture(ReflectTex, coord).rgb/ns;"
          "	}"
          "	fragColor = color*intensity;"
          "}");
        plane_fs.Compile();

        plane_prog.AttachShader(plane_vs);
        plane_prog.AttachShader(plane_fs);
        plane_prog.Link();
        plane_prog.Use();

        plane_projection_matrix.BindTo("ProjectionMatrix");
        plane_camera_matrix.BindTo("CameraMatrix");
        plane_model_matrix.BindTo("ModelMatrix");

        Vec3f lightPos(3.0f, 0.5f, 2.0f);
        Uniform<Vec3f>(plane_prog, "LightPosition").Set(lightPos);
        Uniform<Vec3f>(plane_prog, "Normal").Set(make_plane.Normal());

        plane.Bind();

        plane_verts.Bind(Buffer::Target::Array);
        {
            std::vector<GLfloat> data;
            GLuint n_per_vertex = make_plane.Positions(data);
            Buffer::Data(Buffer::Target::Array, data);
            VertexArrayAttrib attr(plane_prog, "Position");
            attr.Setup<GLfloat>(n_per_vertex);
            attr.Enable();
        }
        //
        Texture::Active(1);
        gl.Bound(Texture::Target::Rectangle, depth_tex)
          .MinFilter(TextureMinFilter::Linear)
          .MagFilter(TextureMagFilter::Linear)
          .WrapS(TextureWrap::ClampToEdge)
          .WrapT(TextureWrap::ClampToEdge);

        Texture::Active(0);
        ProgramUniformSampler(plane_prog, "ReflectTex").Set(0);
        gl.Bound(Texture::Target::Rectangle, reflect_tex)
          .MinFilter(TextureMinFilter::Linear)
          .MagFilter(TextureMagFilter::Linear)
          .WrapS(TextureWrap::ClampToEdge)
          .WrapT(TextureWrap::ClampToEdge);

        gl.Bound(Framebuffer::Target::Draw, fbo)
          .AttachTexture(FramebufferAttachment::Color, reflect_tex, 0)
          .AttachTexture(FramebufferAttachment::Depth, depth_tex, 0);

        shape_vs.Source(
          "#version 140\n"
          "uniform vec3 LightPosition;"
          "uniform mat4 ProjectionMatrix, ModelMatrix, CameraMatrix;"
          "in vec4 Position;"
          "in vec3 Normal;"
          "out vec3 vertNormal;"
          "out vec3 vertLightDir;"
          "out vec3 vertLightRefl;"
          "out vec3 vertViewDir;"
          "out vec3 vertColor;"
          "void main()"
          "{"
          "	gl_Position = ModelMatrix * Position;"
          "	vertLightDir = LightPosition - gl_Position.xyz;"
          "	vertNormal = mat3(ModelMatrix)*Normal;"
          "	vertLightRefl = reflect("
          "		-normalize(vertLightDir),"
          "		normalize(vertNormal)"
          "	);"
          "	vertViewDir = (vec4(0.0, 0.0, 1.0, 1.0)*CameraMatrix).xyz;"
          "	vertColor = vec3(1, 1, 1) - vertNormal;"
          "	gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;"
          "}");
        shape_vs.Compile();

        shape_fs.Source(
          "#version 140\n"
          "in vec3 vertNormal;"
          "in vec3 vertLightDir;"
          "in vec3 vertLightRefl;"
          "in vec3 vertViewDir;"
          "in vec3 vertColor;"
          "out vec3 fragColor;"

          "void main()"
          "{"
          "	float l = length(vertLightDir);"
          "	float d = dot("
          "		normalize(vertNormal), "
          "		normalize(vertLightDir)"
          "	) / l;"
          "	float s = dot("
          "		normalize(vertLightRefl),"
          "		normalize(vertViewDir)"
          "	);"
          "	vec3 lt = vec3(1.0, 1.0, 1.0);"
          "	fragColor = "
          "		vertColor * 0.4 + "
          "		(lt + vertColor)*pow(max(2.5*d, 0.0), 3) + "
          "		lt * pow(max(s, 0.0), 64);"
          "}");
        shape_fs.Compile();

        shape_prog.AttachShader(shape_vs);
        shape_prog.AttachShader(shape_fs);
        shape_prog.Link();
        shape_prog.Use();

        shape_projection_matrix.BindTo("ProjectionMatrix");
        shape_camera_matrix.BindTo("CameraMatrix");
        shape_model_matrix.BindTo("ModelMatrix");

        Uniform<Vec3f>(shape_prog, "LightPosition").Set(lightPos);

        shape.Bind();

        shape_verts.Bind(Buffer::Target::Array);
        {
            std::vector<GLfloat> data;
            GLuint n_per_vertex = make_shape.Positions(data);
            Buffer::Data(Buffer::Target::Array, data);
            VertexArrayAttrib attr(shape_prog, "Position");
            attr.Setup<GLfloat>(n_per_vertex);
            attr.Enable();
        }

        shape_normals.Bind(Buffer::Target::Array);
        {
            std::vector<GLfloat> data;
            GLuint n_per_vertex = make_shape.Normals(data);
            Buffer::Data(Buffer::Target::Array, data);
            VertexArrayAttrib attr(shape_prog, "Normal");
            attr.Setup<GLfloat>(n_per_vertex);
            attr.Enable();
        }
        //
        gl.ClearColor(0.5f, 0.5f, 0.4f, 0.0f);
        gl.ClearDepth(1.0f);
        gl.Enable(Capability::DepthTest);
        gl.Enable(Capability::CullFace);
    }
Ejemplo n.º 11
0
	Field(const images::Image& map, const Program& prog)
	 : gl()
	{
		const GLuint w = map.Width();
		const GLuint h = map.Height();
		const GLuint d = map.Depth();
		const GLuint c = 3;

		radius = std::sqrt(GLfloat(w*w+h*h+d*d))*0.5f;

		vert_count = w*h*d;

		std::vector<GLfloat> pos_data(w*h*d*c);
		std::vector<GLuint>  tec_data(w*h*d*c);

		GLfloat xo = w * 0.5f;
		GLfloat yo = h * 0.5f;
		GLfloat zo = d * 0.5f;

		for(GLuint z=0; z!=d; ++z)
		{
			for(GLuint y=0; y!=h; ++y)
			{
				for(GLuint x=0; x!=w; ++x)
				{
					GLuint k = z*w*h*c+y*w*c+x*c;
					pos_data[k+0] = x-xo;
					pos_data[k+1] = y-yo;
					pos_data[k+2] = z-zo;

					tec_data[k+0] = x;
					tec_data[k+1] = y;
					tec_data[k+2] = z;
				}
			}
		}

		positions.Data(pos_data);
		texcoords.Data(tec_data);

		DSAVertexArrayAttribEXT(vao, prog, "Position")
			.Setup<Vector<GLfloat, 3>>(positions)
			.Enable();

		DSAVertexArrayAttribEXT(vao, prog, "TexCoord")
			.Setup<Vector<GLuint, 3>>(texcoords)
			.Enable();


		ProgramUniformSampler(prog, "Pattern").Set(1);
		pattern.BindMulti(1, Texture::Target::_3D);
		pattern.Image3D(map);
		pattern.MinFilter(TextureMinFilter::Nearest);
		pattern.MagFilter(TextureMagFilter::Nearest);
		pattern.WrapS(TextureWrap::ClampToBorder);
		pattern.WrapT(TextureWrap::ClampToBorder);
		pattern.WrapR(TextureWrap::ClampToBorder);
		pattern.BorderColor(Vec4f(0,0,0,1));


		ProgramUniformSampler(prog, "FadeMap").Set(2);
		fademap.BindMulti(2, Texture::Target::_3D);
		fademap.Image3D(images::RandomRedUByte(
			map.Width(),
			map.Height(),
			map.Depth()
		));
		fademap.MinFilter(TextureMinFilter::Nearest);
		fademap.MagFilter(TextureMagFilter::Nearest);
		fademap.WrapS(TextureWrap::ClampToBorder);
		fademap.WrapT(TextureWrap::ClampToBorder);
		fademap.WrapR(TextureWrap::ClampToBorder);
		fademap.BorderColor(Vec4f(0,0,0,1));
	}
Ejemplo n.º 12
0
	SortProg(void)
	 : Program(make())
	 , pass(self(), "Pass")
	{
		ProgramUniformSampler(self(), "SortNW").Set(0);
	}
Ejemplo n.º 13
0
	VolLightExample(void)
	 : volume_vs(ShaderType::Vertex, ObjectDesc("Volume vertex"))
	 , plane_vs(ShaderType::Vertex, ObjectDesc("Plane vertex"))
	 , volume_gs(ShaderType::Geometry, ObjectDesc("Volume geometry"))
	 , volume_fs(ShaderType::Fragment, ObjectDesc("Volume fragment"))
	 , plane_fs(ShaderType::Fragment, ObjectDesc("Plane fragment"))
	 , volume_prog(ObjectDesc("Volume"))
	 , plane_prog(ObjectDesc("Plane"))
	 , samples(150)
	{
		volume_vs.Source(
			"#version 330\n"
			"in vec4 Position;"
			"out float vertZOffs;"
			"uniform int SampleCount;"
			"uniform mat4 CameraMatrix;"
			"uniform vec3 ViewZ;"
			"uniform float Size;"
			"void main(void)"
			"{"
			"	float hp = (SampleCount-1) * 0.5;"
			"	vertZOffs = (gl_InstanceID - hp)/hp;"
			"	gl_Position = vec4("
			"		Position.xyz +"
			"		ViewZ*vertZOffs*Size*0.5,"
			"		1.0"
			"	);"
			"}"
		);
		volume_vs.Compile();

		volume_gs.Source(
			"#version 330\n"
			"layout(points) in;"
			"layout(triangle_strip, max_vertices = 4) out;"
			"uniform mat4 CameraMatrix, ProjectionMatrix;"
			"uniform mat4 TexProjectionMatrix;"
			"uniform vec3 ViewX, ViewY;"
			"uniform float Size;"
			"in float vertZOffs[];"
			"out vec4 geomTexCoord;"
			"void main(void)"
			"{"
			"	float zo = vertZOffs[0];"
			"	float yo[2] = float[2](-1.0, 1.0);"
			"	float xo[2] = float[2](-1.0, 1.0);"
			"	for(int j=0;j!=2;++j)"
			"	for(int i=0;i!=2;++i)"
			"	{"
			"		vec4 v = vec4("
			"			gl_in[0].gl_Position.xyz+"
			"			ViewX * xo[i] * Size+"
			"			ViewY * yo[j] * Size,"
			"			1.0"
			"		);"
			"		geomTexCoord = "
			"			TexProjectionMatrix *"
			"			v;"
			"		gl_Position = "
			"			ProjectionMatrix *"
			"			CameraMatrix * v;"
			"		EmitVertex();"
			"	}"
			"	EndPrimitive();"
			"}"
		);
		volume_gs.Compile();

		volume_fs.Source(
			"#version 330\n"
			"uniform sampler2D LightTex;"
			"uniform int SampleCount;"
			"in vec4 geomTexCoord;"
			"out vec4 fragColor;"
			"void main(void)"
			"{"
			"	vec2 coord = geomTexCoord.st/geomTexCoord.q;"
			"	float depth = geomTexCoord.z;"
			"	if(depth < 0.0) discard;"
			"	vec4 t  = texture(LightTex, coord*0.5 + 0.5);"
			"	if(t.a == 0.0) discard;"
			"	float alpha = 10.0*(1.0-t.a)/SampleCount;"
			"	alpha *= (t.r+t.g+t.b)*0.3333;"
			"	alpha /= sqrt(depth);"
			"	fragColor = vec4(t.rgb, alpha);"
			"}"
		);
		volume_fs.Compile();

		volume_prog.AttachShader(volume_vs);
		volume_prog.AttachShader(volume_gs);
		volume_prog.AttachShader(volume_fs);
		volume_prog.Link();
		volume_prog.Use();

		// bind the VAO for the volumes
		volume.Bind();

		// bind the VBO for the volume positions
		volume_pos.Bind(Buffer::Target::Array);
		{
			GLfloat position[3] = {0.0, 0.0, 0.0};
			Buffer::Data(Buffer::Target::Array, 3, position);
			VertexArrayAttrib attr(volume_prog, "Position");
			attr.Setup<Vec3f>();
			attr.Enable();
		}

		Vec3f lightPos(2.0f, 4.0f, -3.0f);
		auto texProjMat =
			CamMatrixf::PerspectiveX(Degrees(30), 1.0, 0.3, 20.0) *
			CamMatrixf::LookingAt(lightPos, Vec3f(0, 0, 0));

		Uniform<GLint>(volume_prog, "SampleCount").Set(samples);
		Uniform<GLfloat>(volume_prog, "Size").Set(Length(lightPos));
		Uniform<Mat4f>(volume_prog, "TexProjectionMatrix").Set(texProjMat);

		plane_vs.Source(
			"#version 330\n"
			"in vec4 Position;"
			"uniform mat4 CameraMatrix, ProjectionMatrix;"
			"uniform mat4 TexProjectionMatrix;"
			"out vec2 vertChecker;"
			"out vec4 vertTexCoord;"
			"void main(void)"
			"{"
			"	gl_Position = "
			"		ProjectionMatrix *"
			"		CameraMatrix *"
			"		Position;"
			"	vertTexCoord = "
			"		TexProjectionMatrix *"
			"		Position;"
			"	vertChecker = Position.xz;"
			"}"
		);
		plane_vs.Compile();

		plane_fs.Source(
			"#version 330\n"
			"uniform sampler2D LightTex;"
			"in vec4 vertTexCoord;"
			"in vec2 vertChecker;"
			"out vec4 fragColor;"
			"void main(void)"
			"{"
			"	vec2 coord = vertTexCoord.st/vertTexCoord.q;"
			"	vec4 t  = texture(LightTex, coord*0.5 + 0.5);"
			"	float i = ("
			"		1 +"
			"		int(vertChecker.x+10) % 2+"
			"		int(vertChecker.y+10) % 2"
			"	) % 2;"
			"	vec3 color = vec3(0.1, 0.1, 0.1);"
			"	color += t.rgb * (1.0 - t.a);"
			"	color *= mix("
			"		vec3(0.9, 0.9, 1.0), "
			"		vec3(0.4, 0.4, 0.9), "
			"		i"
			"	);"
			"	fragColor = vec4(color, 1.0);"
			"}"
		);
		plane_fs.Compile();

		plane_prog.AttachShader(plane_vs);
		plane_prog.AttachShader(plane_fs);
		plane_prog.Link();
		plane_prog.Use();

		Uniform<Mat4f>(plane_prog, "TexProjectionMatrix").Set(texProjMat);

		plane.Bind();

		// bind the VBO for the plane vertices
		plane_pos.Bind(Buffer::Target::Array);
		{
			GLfloat data[4*3] = {
				-9.0f, -4.0f,  9.0f,
				-9.0f, -4.0f, -9.0f,
				 9.0f, -4.0f,  9.0f,
				 9.0f, -4.0f, -9.0f
			};
			Buffer::Data(Buffer::Target::Array, 4*3, data);
			plane_prog.Use();
			VertexArrayAttrib attr(plane_prog, "Position");
			attr.Setup<Vec3f>();
			attr.Enable();
		}

		Texture::Active(0);
		ProgramUniformSampler(volume_prog, "LightTex").Set(0);

		light_tex
			<< Texture::Target::_2D
			<< TextureMinFilter::LinearMipmapLinear
			<< TextureMagFilter::Linear
			<< TextureWrap::ClampToBorder
			<< Vec4f(0.0f, 0.0f, 0.0f, 0.0f)
			<< images::LoadTexture("flower_glass")
			<< TextureMipmap();

		gl.ClearColor(0.0f, 0.05f, 0.1f, 0.0f);
		gl.ClearDepth(1.0f);
		gl.BlendFunc(BlendFn::SrcAlpha, BlendFn::One);
	}
Ejemplo n.º 14
0
    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);
    }