예제 #1
0
	// Returns a vector of cube offsets
	static std::vector<Mat4f> MakeCubeMatrices(GLuint count, float max_dist)
	{
		std::srand(59039);
		std::vector<Mat4f> offsets(count);
		for(GLuint i=0; i!=count; ++i)
		{
			float x = float(std::rand())/RAND_MAX;
			float y = float(std::rand())/RAND_MAX;
			float z = float(std::rand())/RAND_MAX;
			float sx = std::rand()%2 ? 1.0f: -1.0f;
			float sy = std::rand()%2 ? 1.0f: -1.0f;
			float sz = std::rand()%2 ? 1.0f: -1.0f;
			offsets[i] =
				ModelMatrixf::Translation(
					sx*(1.0f + std::pow(x, 0.9f) * max_dist),
					sy*(1.0f + std::pow(y, 1.5f) * max_dist),
					sz*(1.0f + std::pow(z, 0.7f) * max_dist)
				) *
				ModelMatrixf::RotationZ(
					RightAngles(float(std::rand())/RAND_MAX)
				) *
				ModelMatrixf::RotationY(
					RightAngles(float(std::rand())/RAND_MAX)
				) *
				ModelMatrixf::RotationX(
					RightAngles(float(std::rand())/RAND_MAX)
				);
		}
		return offsets;
	}
예제 #2
0
    void Render(ExampleClock& clock) {
        if(long(clock.Now().Seconds()) % 4 == 0) {
            status += clock.Interval().Seconds();
        } else if(status != double(long(status))) {
            if(status - double(long(status)) < 0.5)
                status = double(long(status));
            else
                status = 1.0 + double(long(status));
        }

        gl.Clear().ColorBuffer().DepthBuffer();

        point_prog.status = GLfloat(0.5 - 0.5 * CosineWave(status * 0.5));

        CamMatrixf camera = CamMatrixf::Orbiting(
          Vec3f(),
          5.5f,
          FullCircles(clock.Now().Seconds() / 19.0),
          Degrees(45 + SineWave(clock.Now().Seconds() / 15.0) * 40));
        point_prog.camera_matrix.Set(camera);

        point_prog.model_matrix.Set(
          ModelMatrixf::RotationX(RightAngles(status)));
        shape.Draw();
    }
예제 #3
0
    void make_shape_1(const Program& prog, int vbo, const GLchar* name) {
        vbos[vbo].Bind(Buffer::Target::Array);

        std::vector<GLfloat> data(point_count * 3);
        auto i = data.begin(), e = data.end();
        while(i != e) {
            auto phi = FullCircles((std::rand() % 1001) * 0.001);
            auto rho = RightAngles((std::rand() % 1001) * 0.002 - 1.0);

            *i++ = Cos(phi) * Cos(rho);
            *i++ = Sin(rho);
            *i++ = Sin(phi) * Cos(rho);
        }

        Buffer::Data(Buffer::Target::Array, data);

        VertexArrayAttrib attr(prog, name);
        attr.Setup<Vec3f>();
        attr.Enable();
    }
예제 #4
0
파일: cloud.hpp 프로젝트: GLDRorg/oglplus
	void _make_spheres(Vec3f center, GLfloat radius)
	{
		_adjust_sphere(center, radius);
		if(radius < _min_radius) return;
		if(!_apply_sphere(center, radius)) return;
		GLfloat sub_radius = radius * _sub_scale;
		GLsizei i = 0, n = (8.0f*radius*radius)/(sub_radius*sub_radius);
		while(i != n)
		{
			auto rad = radius*(1.0f + _rand_s()*_sub_variance*0.5f);
			auto rho = FullCircles(_rand_u());
			auto phi = RightAngles(_rand_s());
			_make_spheres(
				center + Vec3f(
					rad*Cos(phi)*Cos(rho),
					rad*Sin(phi),
					rad*Cos(phi)*Sin(rho)
				),
				sub_radius*(1.0f + _rand_s()*_sub_variance)
			);
			++i;
		}
	}
예제 #5
0
	void Render(double time)
	{
		gl.Clear().ColorBuffer().StencilBuffer();

		int border = 20;
		std::stringstream text;

		// shape colors
		GLfloat shape_color_gen_coeffs[9] = {
			 0.0f, 0.0f, 0.2f,
			 0.2f, 0.0f, 0.2f,
			 0.0f, 0.0f, 0.2f
		};
		npr.ColorGen(
			PathNVColor::Primary,
			PathNVGenMode::ObjectBoundingBox,
			PathNVColorFormat::RGB,
			shape_color_gen_coeffs
		);

		// Shape
		dsa.ModelviewMatrix()
			.LoadIdentity()
			.Translate(64, 64, 0)
			.Translate(192, 192, 0)
			.Rotate(RightAngles(time), Vec3f::Unit(2))
			.Translate(-192, -192, 0);
		shape.StencilFill(PathNVFillMode::CountUp, 0xFF);
		shape.CoverFill(PathNVFillCoverMode::BoundingBox);

		// text colors
		GLfloat text_color_gen_coeffs[9] = {
			-0.3f, 0.0f, 0.3f,
			 0.0f, 0.2f, 0.6f,
			 0.3f, 0.0f, 0.0f
		};
		npr.ColorGen(
			PathNVColor::Primary,
			PathNVGenMode::ObjectBoundingBox,
			PathNVColorFormat::RGB,
			text_color_gen_coeffs
		);

		// Time
		dsa.ModelviewMatrix()
			.LoadIdentity()
			.Translate(border, border, 0);
		text	<< "Time: "
			<< std::setw(7)
			<< std::setprecision(2)
			<< std::fixed
			<< time << "[s]";
		RenderText(text.str());
		text.str(std::string());

		// Frame no
		dsa.ModelviewMatrix()
			.LoadIdentity()
			.Translate(tex_side-border, border, 0)
			.Rotate(RightAngle(), Vec3f::Unit(2));
		text	<< "Frame: "
			<< std::setw(7)
			<< std::fixed
			<< frame_no;
		RenderText(text.str());
		text.str(std::string());

		// FPS
		dsa.ModelviewMatrix()
			.LoadIdentity()
			.Translate(tex_side-border, tex_side-border, 0)
			.Rotate(RightAngles(2), Vec3f::Unit(2));
		text	<< "FPS: ";
		if(time < 1) text << "<N/A>";
		else text << std::setw(7) << std::fixed << frame_no / time;
		RenderText(text.str());
		text.str(std::string());

		// title
		dsa.ModelviewMatrix()
			.LoadIdentity()
			.Translate(border, tex_side-border, 0)
			.Rotate(RightAngles(3), Vec3f::Unit(2));
		RenderText("OpenGL");

		// sync
		thread_ready.Signal();
		parent_ready.Wait();

		++frame_no;
	}
예제 #6
0
	ShadowVolExample(const ExampleParams& params)
	 : shape_instr(make_shape.Instructions())
	 , shape_indices(make_shape.Indices())
	 , shape_vs(ShaderType::Vertex, ObjectDesc("Shape vertex"))
	 , depth_vs(ShaderType::Vertex, ObjectDesc("Depth vertex"))
	 , light_vs(ShaderType::Vertex, ObjectDesc("Light vertex"))
	 , depth_gs(ShaderType::Geometry, ObjectDesc("Depth geometry"))
	 , light_gs(ShaderType::Geometry, ObjectDesc("Light geometry"))
	 , shape_fs(ShaderType::Fragment, ObjectDesc("Shape fragment"))
	 , depth_fs(ShaderType::Fragment, ObjectDesc("Depthfragment"))
	 , light_fs(ShaderType::Fragment, ObjectDesc("Light fragment"))
	 , shape_prog(ObjectDesc("Shape"))
	 , depth_prog(ObjectDesc("Depth"))
	 , light_prog(ObjectDesc("Light"))
	 , tex_side(128)
	 , sample_count(params.HighQuality()?1024:128)
	{
		shape_vs.Source(
			"#version 330\n"
			"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
			"in vec4 Position;"
			"in vec3 Normal;"
			"in vec2 TexCoord;"
			"out vec3 vertNormal;"
			"out vec3 vertLightDir;"
			"out vec3 vertLightRefl;"
			"out vec3 vertViewDir;"
			"out vec3 vertViewRefl;"
			"uniform vec3 LightPos;"
			"void main(void)"
			"{"
			"	gl_Position = ModelMatrix * Position;"
			"	vertNormal = mat3(ModelMatrix)*Normal;"
			"	vertLightDir = LightPos - gl_Position.xyz;"
			"	vertLightRefl = reflect("
			"		-normalize(vertLightDir),"
			"		normalize(vertNormal)"
			"	);"
			"	vertViewDir = (vec4(0.0, 0.0, 1.0, 1.0)* CameraMatrix).xyz;"
			"	vertViewRefl = reflect("
			"		normalize(vertViewDir),"
			"		normalize(vertNormal)"
			"	);"
			"	gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;"
			"}"
		);
		shape_vs.Compile();

		shape_fs.Source(
			"#version 330\n"
			"in vec3 vertNormal;"
			"in vec3 vertLightDir;"
			"in vec3 vertLightRefl;"
			"in vec3 vertViewDir;"
			"in vec3 vertViewRefl;"
			"out vec4 fragColor;"
			"void main(void)"
			"{"
			"	float l = length(vertLightDir);"
			"	float d = dot("
			"		normalize(vertNormal), "
			"		normalize(vertLightDir)"
			"	) / l;"
			"	float s = dot("
			"		normalize(vertLightRefl),"
			"		normalize(vertViewDir)"
			"	);"
			"	vec3 ambi = vec3(0.6, 0.3, 0.5);"
			"	vec3 diff = vec3(0.9, 0.7, 0.8);"
			"	vec3 spec = vec3(1.0, 0.9, 0.95);"
			"	fragColor = vec4("
			"		ambi * 0.3 + "
			"		diff * 0.7 * max(d, 0.0) + "
			"		spec * pow(max(s, 0.0), 64), "
			"		1.0"
			"	);"
			"}"
		);
		shape_fs.Compile();

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

		depth_vs.Source(
			"#version 330\n"
			"uniform mat4 ModelMatrix;"
			"uniform vec3 LightPos;"
			"in vec4 Position;"
			"void main(void)"
			"{"
			"	gl_Position = "
			"		mat4("
			"			1.0, 0.0, 0.0, -LightPos.x,"
			"			0.0, 1.0, 0.0, -LightPos.y,"
			"			0.0, 0.0, 1.0, -LightPos.z,"
			"			0.0, 0.0, 0.0,  1.0"
			"		)*"
			"		ModelMatrix *"
			"		mat4("
			"			10.0,  0.0,  0.0,  0.0,"
			"			 0.0, 10.0,  0.0,  0.0,"
			"			 0.0,  0.0, 10.0,  0.0,"
			"			 0.0,  0.0,  0.0,  1.0 "
			"		)*"
			"		Position;"
			"}"
		);
		depth_vs.Compile();

		depth_gs.Source(
			"#version 330\n"
			"layout(triangles) in;"
			"layout(triangle_strip, max_vertices = 18) out;"

			"uniform mat4 ProjectionMatrix;"

			"const mat4 CubeFaceMatrix[6] = mat4[6]("
			"	mat4("
			"		 0.0,  0.0, -1.0,  0.0,"
			"		 0.0, -1.0,  0.0,  0.0,"
			"		-1.0,  0.0,  0.0,  0.0,"
			"		 0.0,  0.0,  0.0,  1.0 "
			"	), mat4("
			"		 0.0,  0.0,  1.0,  0.0,"
			"		 0.0, -1.0,  0.0,  0.0,"
			"		 1.0,  0.0,  0.0,  0.0,"
			"		 0.0,  0.0,  0.0,  1.0 "
			"	), mat4("
			"		 1.0,  0.0,  0.0,  0.0,"
			"		 0.0,  0.0, -1.0,  0.0,"
			"		 0.0,  1.0,  0.0,  0.0,"
			"		 0.0,  0.0,  0.0,  1.0 "
			"	), mat4("
			"		 1.0,  0.0,  0.0,  0.0,"
			"		 0.0,  0.0,  1.0,  0.0,"
			"		 0.0, -1.0,  0.0,  0.0,"
			"		 0.0,  0.0,  0.0,  1.0 "
			"	), mat4("
			"		 1.0,  0.0,  0.0,  0.0,"
			"		 0.0, -1.0,  0.0,  0.0,"
			"		 0.0,  0.0, -1.0,  0.0,"
			"		 0.0,  0.0,  0.0,  1.0 "
			"	), mat4("
			"		-1.0,  0.0,  0.0,  0.0,"
			"		 0.0, -1.0,  0.0,  0.0,"
			"		 0.0,  0.0,  1.0,  0.0,"
			"		 0.0,  0.0,  0.0,  1.0 "
			"	)"
			");"

			"void main(void)"
			"{"
			"	for(gl_Layer=0; gl_Layer!=6; ++gl_Layer)"
			"	{"
			"		for(int i=0; i!=3; ++i)"
			"		{"
			"			gl_Position = "
			"				ProjectionMatrix *"
			"				CubeFaceMatrix[gl_Layer]*"
			"				gl_in[i].gl_Position;"
			"			EmitVertex();"
			"		}"
			"		EndPrimitive();"
			"	}"
			"}"
		);
		depth_gs.Compile();

		depth_fs.Source(
			"#version 330\n"
			"void main(void)"
			"{"
			"	gl_FragDepth = gl_FragCoord.z;"
			"}"
		);
		depth_fs.Compile();

		depth_prog.AttachShader(depth_vs);
		depth_prog.AttachShader(depth_gs);
		depth_prog.AttachShader(depth_fs);
		depth_prog.Link();
		depth_prog.Use();

		Uniform<Mat4f>(depth_prog, "ProjectionMatrix").Set(
			CamMatrixf::PerspectiveX(
				RightAngles(1.0),
				1.0,
				0.1,
				10.0
			)
		);

		// bind the VAO for the shape
		shape.Bind();

		shape_positions.Bind(Buffer::Target::Array);
		{
			std::vector<GLfloat> data;
			GLuint n_per_vertex = make_shape.Positions(data);
			Buffer::Data(Buffer::Target::Array, data);

			VertexAttribSlot location;
			if(VertexAttribArray::QueryCommonLocation(
				"Position",
				location,
				shape_prog,
				depth_prog
			))
			{
				VertexAttribArray shape_attr(location);
				shape_attr.Setup(n_per_vertex, DataType::Float);
				shape_attr.Enable();
			}
			else assert(!"Inconsistent 'Position' location");
		}

		shape_normals.Bind(Buffer::Target::Array);
		{
			std::vector<GLfloat> data;
			GLuint n_per_vertex = make_shape.Normals(data);
			Buffer::Data(Buffer::Target::Array, data);

			shape_prog.Use();
			VertexAttribArray attr(shape_prog, "Normal");
			attr.Setup(n_per_vertex, DataType::Float);
			attr.Enable();
		}

		light_vs.Source(
			"#version 330\n"
			"in vec3 Position;"
			"out float vertZOffs;"
			"uniform vec3 LightPos;"
			"uniform int SampleCount;"
			"void main(void)"
			"{"
			"	float hp = (SampleCount-1) * 0.5;"
			"	vertZOffs = (gl_InstanceID - hp)/hp;"
			"	gl_Position = vec4(Position + LightPos, 1.0);"
			"}"
		);
		light_vs.Compile();

		light_gs.Source(
			"#version 330\n"
			"layout(points) in;"
			"layout(triangle_strip, max_vertices = 4) out;"
			"in float vertZOffs[];"
			"out vec4 geomPosition;"
			"uniform mat4 CameraMatrix, ProjectionMatrix;"
			"uniform vec3 ViewX, ViewY, ViewZ;"
			"uniform float LightVolSize;"
			"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)"
			"	{"
			"		geomPosition = vec4("
			"			gl_in[0].gl_Position.xyz+"
			"			ViewX * xo[i] * LightVolSize+"
			"			ViewY * yo[j] * LightVolSize+"
			"			ViewZ * zo    * LightVolSize,"
			"			1.0"
			"		);"
			"		gl_Position = "
			"			ProjectionMatrix *"
			"			CameraMatrix *"
			"			geomPosition;"
			"		EmitVertex();"
			"	}"
			"	EndPrimitive();"
			"}"
		);
		light_gs.Compile();

		light_fs.Source(
			"#version 330\n"
			"in vec4 geomPosition;"
			"out vec4 fragColor;"
			"uniform samplerCubeShadow ShadowMap;"
			"uniform int SampleCount;"
			"uniform vec3 LightPos;"
			"void main(void)"
			"{"
			"	vec3 LightDir = geomPosition.xyz - LightPos;"
			"	vec4 ShadowCoord = vec4("
			"		normalize(LightDir),"
			"		length(LightDir)"
			"	);"
			"	float s = texture(ShadowMap, ShadowCoord);"
			"	float alpha = s / (SampleCount * pow(length(LightDir), 2));"
			"	fragColor = vec4(1.0, 1.0, 1.0, alpha);"
			"}"
		);
		light_fs.Compile();

		light_prog.AttachShader(light_vs);
		light_prog.AttachShader(light_gs);
		light_prog.AttachShader(light_fs);
		light_prog.Link();
		light_prog.Use();

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

		// bind the VBO for the light volume plane positions
		light_positions.Bind(Buffer::Target::Array);
		{
			GLfloat position[3] = {0.0, 0.0, 0.0};
			Buffer::Data(Buffer::Target::Array, 3, position);
			VertexAttribArray attr(light_prog, "Position");
			attr.Setup(3, DataType::Float);
			attr.Enable();
		}

		Uniform<GLint>(light_prog, "SampleCount").Set(sample_count);
		Uniform<GLfloat>(light_prog, "LightVolSize").Set(4);
		UniformSampler(light_prog, "ShadowMap").Set(0);

		// Setup the texture and the offscreen FBO
		Texture::Active(0);
		{
			auto bound_tex = Bind(depth_tex, Texture::Target::CubeMap);
			bound_tex.MinFilter(TextureMinFilter::Linear);
			bound_tex.MagFilter(TextureMagFilter::Linear);
			bound_tex.WrapS(TextureWrap::ClampToEdge);
			bound_tex.WrapT(TextureWrap::ClampToEdge);
			bound_tex.WrapR(TextureWrap::ClampToEdge);
			bound_tex.CompareFunc(CompareFunction::LEqual);
			bound_tex.CompareMode(
				TextureCompareMode::CompareRefToTexture
			);

			for(int i=0; i!=6; ++i)
			{
				Texture::Image2D(
					Texture::CubeMapFace(i),
					0,
					PixelDataInternalFormat::DepthComponent,
					tex_side, tex_side,
					0,
					PixelDataFormat::DepthComponent,
					PixelDataType::Float,
					nullptr
				);
			}

			auto bound_fbo = Bind(
				depth_fbo,
				Framebuffer::Target::Draw
			);
			bound_fbo.AttachTexture(
				FramebufferAttachment::Depth,
				depth_tex,
				0
			);
		}
		//
		gl.ClearColor(0.2f, 0.05f, 0.1f, 0.0f);
		gl.ClearDepth(1.0f);
		gl.Enable(Capability::DepthTest);

		gl.Enable(Capability::CullFace);
		gl.FrontFace(make_shape.FaceWinding());
		gl.CullFace(Face::Back);

		gl.BlendFunc(BlendFunction::SrcAlpha, BlendFunction::One);
	}