Пример #1
0
    FlareGeomShader()
      : GeometryShader(
          ObjectDesc("Flare geometry shader"),
          StrCRef("#version 150\n"
                  "layout(points) in;"
                  "layout(triangle_strip, max_vertices = 72) out;"

                  "uniform mat4 ProjectionMatrix;"
                  "uniform int Samples;"

                  "out vec2 geomTexCoord;"

                  "void main()"
                  "{"
                  "	for(int l=0; l!=3; ++l)"
                  "	{"
                  "		int i = 0, n = 8 + l*4;"
                  "		float step = (2.0 * 3.1415)/float(n-1);"
                  "		float a = length(gl_in[0].gl_Position)*(0.3+l*0.4);"
                  "		float Radius = "
                  "			sqrt(Samples)*0.01 + "
                  "			Samples*sqrt(float(l))*0.001;"
                  "		while(i != n)"
                  "		{"
                  "			vec4 Offs = vec4(cos(a)*(1.0+l*0.2),sin(a),0,0);"
                  "			gl_Position = "
                  "				ProjectionMatrix * "
                  "				gl_in[0].gl_Position;"
                  "			geomTexCoord = vec2(float(i), 0.0);"
                  "			EmitVertex();"
                  "			gl_Position = "
                  "				ProjectionMatrix * "
                  "				(gl_in[0].gl_Position + Offs*Radius);"
                  "			geomTexCoord = vec2(float(i), 1.05-l*0.05);"
                  "			EmitVertex();"
                  "			++i;"
                  "			a += step;"
                  "		}"
                  "		EndPrimitive();"
                  "	}"
                  "}")) {}
Пример #2
0
	LiquidFragShader(void)
	 : FragmentShader(
		ObjectDesc("Liquid fragment shader"),
		StrCRef("#version 330\n"

		"uniform samplerCube EnvMap;"

		"in vec3 geomNormal, geomLightDir, geomViewDir;"

		"out vec3 fragColor;"

		"void main(void)"
		"{"
		"	vec3 Normal = normalize(geomNormal);"
		"	vec3 LightDir = normalize(geomLightDir);"
		"	vec3 ViewDir = normalize(geomViewDir);"

		"	float LightRefl = dot(reflect(-LightDir, Normal), ViewDir);"
		"	float LightHit = dot(Normal, LightDir);"

		"	float Specular = pow(clamp(LightRefl+0.1, 0.0, 1.0), 32);"

		"	float Diffuse1 = pow(max(LightHit*0.6+0.4, 0.0), 2.0);"
		"	float Diffuse2 = sqrt(max(LightHit+0.2, 0.0));"
		"	float Diffuse = Diffuse1 * 0.8 + Diffuse2 * 0.2;"

		"	float ViewLight = max(0.3-dot(ViewDir, LightDir), 0.0);"

		"	float Ambient = 0.9;"

		"	vec3 Environ = texture(EnvMap, reflect(-ViewDir, Normal)).rgb;"
		"	fragColor = "
		"		Environ * Ambient +"
		"		vec3(0.4, 0.8, 0.4) * Diffuse+"
		"		vec3(0.2, 0.2, 0.2) * Specular;"
		"}")
	)
	{ }
Пример #3
0
	static Program make_prog(void)
	{
		Program prog;
		VertexShader vs;
		vs.Source(StrCRef(
			"#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(StrCRef(
			"#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(StrCRef(
			"#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();

		return prog;
	}
Пример #4
0
	LiquidGeomShader(void)
	 : GeometryShader(
		ObjectDesc("Liquid geometry shader"),
		StrCRef("#version 330\n"
		"layout(triangles_adjacency) in;"
		"layout(triangle_strip, max_vertices = 4) out;"

		"uniform mat4 CameraMatrix;"
		"uniform vec3 CameraPosition, LightPosition;"

		"in vec3 vertNormal[];"
		"in float vertValue[];"

		"out vec3 geomNormal, geomLightDir, geomViewDir;"

		"void do_nothing(void){ }"

		"float find_t(int i1, int i2)"
		"{"
		"	float d = vertValue[i2] - vertValue[i1];"
		"	return -vertValue[i1]/d;"
		"}"

		"void make_vertex(int i1, int i2)"
		"{"
		"	float t = find_t(i1, i2);"
		"	gl_Position = mix("
		"		gl_in[i1].gl_Position,"
		"		gl_in[i2].gl_Position,"
		"		t"
		"	);"
		"	geomNormal = mix("
		"		vertNormal[i1],"
		"		vertNormal[i2],"
		"		t"
		"	);"
		"	geomLightDir = LightPosition - gl_Position.xyz;"
		"	geomViewDir = CameraPosition - gl_Position.xyz;"
		"	gl_Position = CameraMatrix * gl_Position;"
		"	EmitVertex();"
		"}"

		"void make_triangle(int a1, int a2, int b1, int b2, int c1, int c2)"
		"{"
		"	make_vertex(a1, a2);"
		"	make_vertex(b1, b2);"
		"	make_vertex(c1, c2);"
		"	EndPrimitive();"
		"}"

		"void make_quad(int a1,int a2,int b1,int b2,int c1,int c2,int d1,int d2)"
		"{"
		"	make_vertex(a1, a2);"
		"	make_vertex(b1, b2);"
		"	make_vertex(c1, c2);"
		"	make_vertex(d1, d2);"
		"	EndPrimitive();"
		"}"

		"void process_tetrahedron(int a, int b, int c, int d)"
		"{"
		"	if(vertValue[a] >= 0.0)"
		"	{"
		"		if(vertValue[b] >= 0.0)"
		"		{"
		"			if(vertValue[c] >= 0.0)"
		"			{"
		"				if(vertValue[d] >= 0.0)"
		"					do_nothing();"
		"				else make_triangle(d,a, d,b, d,c);"
		"			}"
		"			else"
		"			{"
		"				if(vertValue[d] >= 0.0)"
		"					make_triangle(c,a, c,d, c,b);"
		"				else make_quad(c,a, d,a, c,b, d,b);"
		"			}"
		"		}"
		"		else"
		"		{"
		"			if(vertValue[c] >= 0.0)"
		"			{"
		"				if(vertValue[d] >= 0.0)"
		"					make_triangle(b,c, b,d, b,a);"
		"				else make_quad(b,c, d,c, b,a, d,a);"
		"			}"
		"			else"
		"			{"
		"				if(vertValue[d] >= 0.0)"
		"					make_quad(c,a, c,d, b,a, b,d);"
		"				else make_triangle(c,a, d,a, b,a);"
		"			}"
		"		}"
		"	}"
		"	else"
		"	{"
		"		if(vertValue[b] >= 0.0)"
		"		{"
		"			if(vertValue[c] >= 0.0)"
		"			{"
		"				if(vertValue[d] >= 0.0)"
		"					make_triangle(a,b, a,d, a,c);"
		"				else make_quad(a,c, a,b, d,c, d,b);"
		"			}"
		"			else"
		"			{"
		"				if(vertValue[d] >= 0.0)"
		"					make_quad(a,b, a,d, c,b, c,d);"
		"				else make_triangle(a,b, d,b, c,b);"
		"			}"
		"		}"
		"		else"
		"		{"
		"			if(vertValue[c] >= 0.0)"
		"			{"
		"				if(vertValue[d] >= 0.0)"
		"					make_quad(b,c, b,d, a,c, a,d);"
		"				else make_triangle(b,c, d,c, a,c);"
		"			}"
		"			else"
		"			{"
		"				if(vertValue[d] >= 0.0)"
		"					make_triangle(a,d, c,d, b,d);"
		"				else do_nothing();"
		"			}"
		"		}"
		"	}"
		"}"

		"void main(void)"
		"{"
		"	process_tetrahedron(0, 2, 4, 1);"
		"}")
	)
	{ }
Пример #5
0
	ShapeFragShader(void)
	 : FragmentShader(
		ObjectDesc("Shape fragment shader"),
		StrCRef("#version 330\n"

		"const int LightCount = 32;"

		"uniform vec3 Color1, Color2;"
		"uniform vec3 LightPosition[LightCount];"
		"uniform sampler2D MetalTex;"

		"in vec3 vertPosition;"
		"in vec3 vertViewDir;"
		"in vec3 vertNormal;"
		"in vec3 vertTangent;"
		"in vec3 vertBitangent;"
		"in vec2 vertTexCoord;"

		"out vec3 fragColor;"

		"const vec3 LightColor = vec3(1.0, 1.0, 1.0);"

		"void main(void)"
		"{"
		"	vec4 Sample = texture(MetalTex, vertTexCoord);"

		"	vec3 fragNormal = normalize("
		"		(Sample.b + 0.5)*vertNormal + "
		"		(Sample.r - 0.5)*vertTangent + "
		"		(Sample.g - 0.5)*vertBitangent"
		"	);"

		"	float Specular = 0.0, Diffuse = 0.0;"
		"	for(int l=0; l!=LightCount; ++l)"
		"	{"
		"		vec3 LightDir = normalize(LightPosition[l]-vertPosition);"
		"		vec3 LightRefl = reflect("
		"			-LightDir,"
		"			fragNormal"
		"		);"

		"		Specular += pow(max(dot("
		"			normalize(LightRefl),"
		"			normalize(vertViewDir)"
		"		)+0.04, 0.0), 32+Sample.b*32)*pow(0.4+Sample.b*1.6, 4.0);"

		"		Diffuse += pow(max(dot("
		"			LightDir, "
		"			normalize(vertNormal*2.0 + fragNormal)"
		"		), 0.0), 2.0);"
		"	}"

		"	float Ambient = 0.1;"
		"	Diffuse /= LightCount;"
		"	Specular /= sqrt(float(LightCount));"

		"	vec3 Color = mix(Color1, Color2, Sample.b);"

		"	fragColor = "
		"		Color * Ambient +"
		"		Color * Diffuse +"
		"		LightColor * Specular;"
		"}")
	)
	{ }
Пример #6
0
	LiquidGeomShader(void)
	 : GeometryShader(
		ObjectDesc("Liquid geometry shader"),
		StrCRef("#version 330\n"
		"layout(triangles_adjacency) in;"
		"layout(triangle_strip, max_vertices = 4) out;"

		"uniform isampler1D Configurations;"
		"uniform mat4 CameraMatrix;"
		"uniform vec3 CameraPosition, LightPosition;"

		"in vec3 vertNormal[];"
		"in float vertValue[];"
		"flat in int vertSign[];"

		"out vec3 geomNormal, geomLightDir, geomViewDir;"

		"void make_vertex(int i1, int i2)"
		"{"
		"	float t = vertValue[i1]/(vertValue[i1] - vertValue[i2]);"
		"	gl_Position = mix("
		"		gl_in[i1].gl_Position,"
		"		gl_in[i2].gl_Position,"
		"		t"
		"	);"
		"	geomNormal = mix("
		"		vertNormal[i1],"
		"		vertNormal[i2],"
		"		t"
		"	);"
		"	geomLightDir = LightPosition - gl_Position.xyz;"
		"	geomViewDir = CameraPosition - gl_Position.xyz;"
		"	gl_Position = CameraMatrix * gl_Position;"
		"	EmitVertex();"
		"}"

		"void make_triangle(const ivec4 v1, const ivec4 v2)"
		"{"
		"	make_vertex(v1.x, v2.x);"
		"	make_vertex(v1.y, v2.y);"
		"	make_vertex(v1.z, v2.z);"
		"	EndPrimitive();"
		"}"

		"void make_quad(const ivec4 v1, const ivec4 v2)"
		"{"
		"	make_vertex(v1.x, v2.x);"
		"	make_vertex(v1.y, v2.y);"
		"	make_vertex(v1.z, v2.z);"
		"	make_vertex(v1.w, v2.w);"
		"	EndPrimitive();"
		"}"

		"void process_tetrahedron(int a, int b, int c, int d)"
		"{"
		"	ivec4 i = ivec4(vertSign[a],vertSign[b],vertSign[c],vertSign[d]);"
		"	int si = int(dot(i, ivec4(1, 1, 1, 1))) % 4;"
		"	if(si != 0)"
		"	{"
		"		int iv = int(dot(i, ivec4(16, 8, 4, 2)));"
		"		ivec4 v1 = texelFetch(Configurations, iv+0, 0);"
		"		ivec4 v2 = texelFetch(Configurations, iv+1, 0);"
		"		if(si % 2 == 0) make_quad(v1, v2);"
		"		else make_triangle(v1, v2);"
		"	}"
		"}"

		"void main(void)"
		"{"
		"	process_tetrahedron(0, 2, 4, 1);"
		"}")
	)
	{
		Texture::Active(0);
		{
			const GLubyte a = 0x0, b = 0x2, c = 0x4, d = 0x1, x = 0xFF;
			const GLubyte  tex_data[] = {
				x, x, x, x,   x, x, x, x,
				a, c, b, x,   d, d, d, x,
				b, d, a, x,   c, c, c, x,
				b, b, a, a,   c, d, c, d,
				a, d, c, x,   b, b, b, x,
				a, a, c, c,   b, d, b, d,
				a, a, d, d,   c, b, c, b,
				a, a, a, x,   b, d, c, x,
				c, d, b, x,   a, a, a, x,
				c, c, b, b,   a, d, a, d,
				b, d, b, d,   c, c, a, a,
				b, b, b, x,   c, d, a, x,
				c, d, c, d,   a, a, b, b,
				c, c, c, x,   a, d, b, x,
				d, d, d, x,   a, b, c, x,
				x, x, x, x,   x, x, x, x
			};
			oglplus::Context::Bound(Texture::Target::_1D, _configurations)
				.Image1D(
					0,
					PixelDataInternalFormat::RGBA8UI,
					sizeof(tex_data),
					0,
					PixelDataFormat::RGBAInteger,
					PixelDataType::UnsignedByte,
					tex_data
				)
				.MinFilter(TextureMinFilter::Nearest)
				.MagFilter(TextureMagFilter::Nearest)
				.WrapS(TextureWrap::ClampToEdge);
		}
	}
Пример #7
0
inline StrCRef ValueName_(GLenum*, GLenum)
{
	return StrCRef();
}
Пример #8
0
	// The part calculating the color for the neutrons
	static StrCRef fs_neutron(void)
	{
		return StrCRef(
		"	bool sig = false;"
		"	vec3 color = vec3(0.5, 0.5, 0.5);");
	}