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); }
/// Returns the swizzle value for the specified component/coord TextureSwizzle Swizzle(TextureSwizzleCoord coord) const { switch(GLenum(coord)) { case GL_TEXTURE_SWIZZLE_R: return SwizzleR(); case GL_TEXTURE_SWIZZLE_G: return SwizzleG(); case GL_TEXTURE_SWIZZLE_B: return SwizzleB(); case GL_TEXTURE_SWIZZLE_A: return SwizzleA(); } return TextureSwizzle(); }
/// Sets the swizzle value for the specified component/coord TextureSwizzleTuple& Swizzle( TextureSwizzleCoord coord, TextureSwizzle mode ) { switch(GLenum(coord)) { case GL_TEXTURE_SWIZZLE_R: SwizzleR(mode); break; case GL_TEXTURE_SWIZZLE_G: SwizzleG(mode); break; case GL_TEXTURE_SWIZZLE_B: SwizzleB(mode); break; case GL_TEXTURE_SWIZZLE_A: SwizzleA(mode); break; } return *this; }
WavesExample(void) : make_plane( Vec3f( 0.0f, 0.0f, 0.0f), Vec3f(100.0f, 0.0f, 0.0f), Vec3f( 0.0f, 0.0f,-100.0f), 50, 50 ), plane_instr(make_plane.PatchInstructions()) , plane_indices(make_plane.PatchIndices()) , camera_matrix(prog, "CameraMatrix") , camera_position(prog, "CameraPosition") , anim_time(prog, "Time") , prev_period(-1) { VertexShader vs(ObjectDesc("Vertex")); vs.Source(StrLit( "#version 410\n" "uniform vec3 CameraPosition;" "in vec3 Position;" "out vec3 vertPosition;" "out float vertDistance;" "void main(void)" "{" " vertPosition = Position;" " vertDistance = distance(CameraPosition, Position);" "}" )); vs.Compile(); prog.AttachShader(vs); TessControlShader cs(ObjectDesc("TessControl")); cs.Source( "#version 410\n" "layout(vertices = 3) out;" "in vec3 vertPosition[];" "in float vertDistance[];" "out vec3 tecoPosition[];" "int tessLevel(float dist)" "{" " return clamp(int(150.0 / (dist+0.1)), 1, 10);" "}" "void main(void)" "{" " tecoPosition[gl_InvocationID] =" " vertPosition[gl_InvocationID];" " if(gl_InvocationID == 0)" " {" " gl_TessLevelInner[0] = tessLevel((" " vertDistance[0]+" " vertDistance[1]+" " vertDistance[2] " " )*0.333);" " gl_TessLevelOuter[0] = tessLevel((" " vertDistance[1]+" " vertDistance[2] " " )*0.5);" " gl_TessLevelOuter[1] = tessLevel((" " vertDistance[2]+" " vertDistance[0] " " )*0.5);" " gl_TessLevelOuter[2] = tessLevel((" " vertDistance[0]+" " vertDistance[1] " " )*0.5);" " }" "}" ); cs.Compile(); prog.AttachShader(cs); TessEvaluationShader es(ObjectDesc("TessEvaluation")); es.Source( "#version 410\n" "#define MaxWaves 5\n" "layout(triangles, equal_spacing, ccw) in;" "uniform mat4 ProjectionMatrix, CameraMatrix;" "uniform vec3 LightPosition;" "uniform vec3 CameraPosition;" "uniform float Time;" "uniform int WaveCount;" "uniform vec3 WaveDirections[MaxWaves];" "uniform vec3 WaveDimensions[MaxWaves];" "in vec3 tecoPosition[];" "out vec3 teevNormal;" "out vec3 teevLightDir;" "out vec3 teevViewDir;" "out float teevDistance;" "void main(void)" "{" " const vec3 Up = vec3(0.0, 1.0, 0.0);" " vec3 Position =" " gl_TessCoord.x * tecoPosition[0]+" " gl_TessCoord.y * tecoPosition[1]+" " gl_TessCoord.z * tecoPosition[2];" " vec3 Pos = Position;" " vec3 Nml = Up;" " for(int w=0; w!=WaveCount; ++w)" " {" " vec3 Dir = WaveDirections[w];" " vec3 Dim = WaveDimensions[w];" " float Dist = dot(Position, Dir);" " float u = Dim.y*sin(Dist/Dim.x + Time*Dim.z);" " Pos += Up * u;" " float w = (Dim.y/Dim.x)*cos(Dist/Dim.x + Time*Dim.z);" " Nml -= Dir * w;" " float d = -0.125*Dim.x*sin(2.0*Dist/Dim.x + Time*Dim.z);" " Pos += Dir * d;" " }" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " vec4(Pos, 1.0);" " teevNormal = normalize(Nml);" " teevLightDir = normalize(LightPosition - Pos);" " teevViewDir = normalize(CameraPosition - Pos);" " teevDistance = distance(CameraPosition, Pos);" "}" ); es.Compile(); prog.AttachShader(es); FragmentShader fs(ObjectDesc("Fragment")); fs.Source(StrLit( "#version 410\n" "uniform samplerCube EnvMap;" "in vec3 teevNormal;" "in vec3 teevLightDir;" "in vec3 teevViewDir;" "in float teevDistance;" "out vec3 fragColor;" "void main(void)" "{" " float Dim = clamp(30.0/teevDistance, 0.0, 1.0);" " float LightRefl = dot(reflect(-teevLightDir, teevNormal), teevViewDir);" " float LightHit = dot(teevNormal, teevLightDir);" " float Diffuse = clamp(LightHit+0.1, 0.0, 1.0);" " float Specular = pow(clamp(LightRefl, 0.0, 0.91), 32);" " vec3 Environ=texture(EnvMap,reflect(-teevViewDir, teevNormal)).rgb;" " vec3 WaterColor = vec3(0.4, 0.5, 0.5);" " vec3 LightColor = vec3(1.0, 1.0, 1.0);" " vec3 FogColor = vec3(0.9, 0.9, 0.9);" " vec3 WaveColor =" " LightColor*Specular+" " WaterColor*Diffuse+" " Environ*0.02;" " fragColor = mix(WaveColor, FogColor, 1.0-Dim);" "}" )); fs.Compile(); prog.AttachShader(fs); prog.Link(); prog.Use(); plane.Bind(); verts.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_plane.Positions(data); Buffer::Data(Buffer::Target::Array, data); VertexAttribArray attr(prog, "Position"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } Uniform<Vec3f>(prog, "LightPosition").Set(-100.0, 100.0, 20.0); Uniform<Vec3f> wave_directions(prog, "WaveDirections"); Uniform<Vec3f> wave_dimensions(prog, "WaveDimensions"); Uniform<GLint>(prog, "WaveCount").Set(5); wave_directions[0] = Normalized(Vec3f(1.0f, 0.0f, 1.0f)); wave_dimensions[0] = Vec3f(5.0f, 1.5f, 1.2f); wave_directions[1] = Normalized(Vec3f(1.0f, 0.0f, 0.5f)); wave_dimensions[1] = Vec3f(4.0f, 0.8f, 1.2f); wave_directions[2] = Normalized(Vec3f(1.0f, 0.0f, 0.1f)); wave_dimensions[2] = Vec3f(2.0f, 0.5f, 2.4f); wave_directions[3] = Normalized(Vec3f(1.0f, 0.0f,-0.1f)); wave_dimensions[3] = Vec3f(1.5f, 0.2f, 3.7f); wave_directions[4] = Normalized(Vec3f(1.0f, 0.0f, 0.4f)); wave_dimensions[4] = Vec3f(1.1f, 0.2f, 4.7f); Texture::Active(0); { auto image = images::Squares(512, 512, 0.9f, 16, 16); auto bound_tex = Bind(env_map, Texture::Target::CubeMap); for(int i=0; i!=6; ++i) Texture::Image2D(Texture::CubeMapFace(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); } UniformSampler(prog, "EnvMap").Set(0); gl.ClearColor(0.9f, 0.9f, 0.9f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); }
/// Synonym for SwizzleB TextureSwizzle Blue(void) const { return SwizzleB(); }
/// Synonym for SwizzleB TextureSwizzleTuple& Blue(TextureSwizzle mode) { return SwizzleB(mode); }