Copyright 2008-2014 Matus Chochlik. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <cmath>
#include <functional>
namespace oglplus {
class TransformProgram :
public Program
{
private:
{
const GLchar* shader_source =
"#version 330\n"
"uniform mat4 CameraMatrix, ModelMatrix;"
"uniform mat4 LightProjMatrix;"
"uniform mat2 TextureMatrix;"
"uniform vec3 CameraPosition, LightPosition;"
"in vec4 Position;"
"in vec3 Normal;"
"in vec2 TexCoord;"
"out gl_PerVertex {"
" vec4 gl_Position;"
"};"
"out float vertNoise;"
"out vec3 vertNormal;"
"out vec3 vertLightDir, vertViewDir;"
"out vec2 vertTexCoord;"
"out vec4 vertLightTexCoord;"
"void main(void)"
"{"
" gl_Position = "
" ModelMatrix *"
" Position;"
" vertNoise = noise1(Position.x+Position.y+Position.z);"
" vertLightDir = LightPosition - gl_Position.xyz;"
" vertViewDir = CameraPosition - gl_Position.xyz;"
" vertNormal = (ModelMatrix * vec4(Normal, 0.0)).xyz;"
" vertTexCoord = TextureMatrix * TexCoord;"
" vertLightTexCoord = LightProjMatrix* gl_Position;"
" gl_Position = CameraMatrix * gl_Position;"
"}";
return ShaderProgram(
shader_source,
ObjectDesc("Transform")
);
}
const Program& prog(
void)
const {
return *
this; }
public:
ProgramUniform<Mat4f> camera_matrix, model_matrix, light_proj_matrix;
ProgramUniform<Mat2f> texture_matrix;
ProgramUniform<Vec3f> camera_position, light_position;
TransformProgram(void)
, camera_matrix(prog(), "CameraMatrix")
, model_matrix(prog(), "ModelMatrix")
, light_proj_matrix(prog(), "LightProjMatrix")
, texture_matrix(prog(), "TextureMatrix")
, camera_position(prog(), "CameraPosition")
, light_position(prog(), "LightPosition")
{ }
};
class ShadowFragShader
{
public:
ShadowFragShader(void)
ObjectDesc("Shadow fragment shader"),
"#version 330\n"
"in float vertNoise;"
"in vec3 vertNormal;"
"in vec3 vertLightDir, vertViewDir;"
"in vec2 vertTexCoord;"
"in vec4 vertLightTexCoord;"
"void main(void){ }"
)
{ }
};
class ShadowProgram :
public Program
{
private:
{
Program prog(ObjectDesc(
"Shadow"));
prog.AttachShader(ShadowFragShader());
prog.MakeSeparable().Link().Use();
return prog;
}
public:
ShadowProgram(void)
{ }
};
class LineGeomShader
{
public:
LineGeomShader(void)
ObjectDesc("Line geometry shader"),
"#version 330\n"
"layout(lines) in;"
"layout(line_strip, max_vertices=4) out;"
"in gl_PerVertex {"
" vec4 gl_Position;"
"} gl_in[];"
"in float vertNoise[];"
"in vec3 vertNormal[];"
"in vec3 vertLightDir[], vertViewDir[];"
"in vec2 vertTexCoord[];"
"in vec4 vertLightTexCoord[];"
"out gl_PerVertex {"
" vec4 gl_Position;"
"};"
"out float geomOpacity;"
"void main(void)"
"{"
" vec4 p0 = gl_in[0].gl_Position;"
" vec4 p1 = gl_in[1].gl_Position;"
" vec4 n0 = vec4(vertNormal[0], 0.0);"
" vec4 n1 = vec4(vertNormal[1], 0.0);"
" float dp = pow(length(p1 - p0), 0.25);"
" float l0 = cos(vertNoise[0]*1.618)*dp;"
" float l1 = sin(vertNoise[1]*1.618)*dp;"
" vec4 v0 = p0 + n0*l0*0.01;"
" vec4 v1 = p1 + n1*l1*0.01;"
" vec4 v = v1 - v0;"
" gl_Position = v0 - v*abs(l1*0.41);"
" geomOpacity = 0.0;"
" EmitVertex();"
" gl_Position = p0;"
" geomOpacity = 1.0;"
" EmitVertex();"
" gl_Position = p1;"
" geomOpacity = 1.0;"
" EmitVertex();"
" gl_Position = v1 + v*abs(l0*0.44);"
" geomOpacity = 0.0;"
" EmitVertex();"
" EndPrimitive();"
"}"
)
{ }
};
class LineFragShader
{
public:
LineFragShader(void)
ObjectDesc("Line fragment shader"),
"#version 330\n"
"in float geomOpacity;"
"out vec4 fragColor;"
"void main(void)"
"{"
" fragColor = vec4(0.0, 0.0, 0.0, 0.1+0.4*geomOpacity);"
"}"
)
{ }
};
{
private:
{
prog.AttachShader(LineGeomShader());
prog.AttachShader(LineFragShader());
prog.MakeSeparable().Link().Use();
return prog;
}
public:
LineProgram(void)
{ }
};
class SketchFragShader
{
public:
SketchFragShader(void)
ObjectDesc("Sketch fragment shader"),
"#version 330\n"
"uniform sampler3D SketchTex;"
"uniform sampler2DShadow ShadowTex;"
"in float vertNoise;"
"in vec3 vertNormal;"
"in vec3 vertLightDir, vertViewDir;"
"in vec2 vertTexCoord;"
"in vec4 vertLightTexCoord;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float LightMult = 1.0;"
" vec3 LightProjCoord = ("
" vertLightTexCoord.xyz /"
" vertLightTexCoord.w"
" ) * 0.5 + 0.5;"
" if("
" LightProjCoord.x >= 0.0 && "
" LightProjCoord.x <= 1.0 && "
" LightProjCoord.y >= 0.0 && "
" LightProjCoord.y <= 1.0 && "
" LightProjCoord.z <= 1.0"
" )"
" {"
" float Shadow = 0.0;"
" const int sn = 12;"
" const float o = 1.0/32.0;"
" for(int s=0; s!=sn; ++s)"
" {"
" float r = float(s)/sn;"
" float a = 4.0*3.14151*r;"
" Shadow += texture("
" ShadowTex, "
" LightProjCoord+"
" vec3(cos(a)*o*r, sin(a)*o*r, 0.0)"
" );"
" }"
" LightMult *= (Shadow / sn);"
" }"
" vec3 LightRefl = reflect("
" -normalize(vertLightDir),"
" normalize(vertNormal)"
" );"
" float Specular = pow(max(dot("
" normalize(LightRefl),"
" normalize(vertViewDir)"
" )+0.02, 0.0)*1.1, 2.0);"
" float Diffuse = max(dot("
" normalize(vertNormal), "
" normalize(vertLightDir)"
" ), 0.0)*3.5;"
" float Ambient = 0.1;"
" float Light = Ambient + (Diffuse + Specular)*LightMult;"
" float Shadow = clamp(2.0 - Light, 0.0, 1.0);"
" vec3 Sample = texture(SketchTex, vec3(vertTexCoord, Shadow)).rgb;"
" fragColor = vec4(0.0, 0.0, 0.0, pow(Sample.b*(0.5 + Shadow*0.5), 0.5));"
"}"
)
{ }
};
class SketchProgram :
public Program
{
private:
{
Program prog(ObjectDesc(
"Sketch"));
prog.AttachShader(SketchFragShader());
prog.MakeSeparable().Link().Use();
return prog;
}
const Program& prog(
void)
const {
return *
this; }
public:
ProgramUniformSampler sketch_tex, shadow_tex;
SketchProgram(void)
, sketch_tex(prog(), "SketchTex")
, shadow_tex(prog(), "ShadowTex")
{ }
};
template <typename ShapeBuilder>
class Shape
{
protected:
ShapeBuilder make_shape;
shapes::DrawingInstructions shape_instr, edge_instr;
typename ShapeBuilder::IndexArray shape_indices, edge_indices;
Context gl;
Array<Buffer> vbos;
public:
Shape(
const Program& prog,
const ShapeBuilder& builder)
: make_shape(builder)
, shape_instr(make_shape.Instructions(shapes::DrawMode::Default()))
, edge_instr(make_shape.Instructions(shapes::DrawMode::Edges()))
, shape_indices(make_shape.Indices(shapes::DrawMode::Default()))
, edge_indices(make_shape.Indices(shapes::DrawMode::Edges()))
, vbos(3)
{
vao.Bind();
typename ShapeBuilder::VertexAttribs vert_attr_info;
const GLuint nva = 3;
const GLchar* vert_attr_name[nva] = {
"Position",
"Normal",
"TexCoord"
};
for(GLuint va=0; va!=nva; ++va)
{
const GLchar* name = vert_attr_name[va];
std::vector<GLfloat> data;
auto getter = vert_attr_info.VertexAttribGetter(data, name);
if(getter != nullptr)
{
vbos[va].Bind(Buffer::Target::Array);
GLuint n_per_vertex = getter(make_shape, data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, name);
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
}
}
{
vao.Bind();
gl.FrontFace(make_shape.FaceWinding());
shape_instr.Draw(shape_indices, 1);
}
void Draw(
const std::function<
bool (GLuint)>& drawing_driver)
{
vao.Bind();
gl.FrontFace(make_shape.FaceWinding());
shape_instr.Draw(shape_indices, 1, 0, drawing_driver);
}
void DrawEdges(void)
{
vao.Bind();
edge_instr.Draw(edge_indices);
}
};
class SketchExample : public Example
{
private:
Context gl;
TransformProgram transf_prog;
ShadowProgram shadow_prog;
SketchProgram sketch_prog;
LineProgram line_prog;
Shape<shapes::Plane> plane;
Shape<shapes::WickerTorus> torus;
GLuint width, height;
const GLuint sketch_tex_layers, shadow_tex_side;
public:
SketchExample(void)
: transf_prog()
, sketch_prog()
, plane(
transf_prog,
shapes::Plane(
9, 9
)
), torus(transf_prog, shapes::WickerTorus())
, sketch_tex_layers(8)
, shadow_tex_side(1024)
{
shadow_pp.Bind();
shadow_pp.UseStages(transf_prog).Vertex();
shadow_pp.UseStages(shadow_prog).Fragment();
sketch_pp.Bind();
sketch_pp.UseStages(transf_prog).Vertex();
sketch_pp.UseStages(sketch_prog).Fragment();
line_pp.Bind();
line_pp.UseStages(transf_prog).Vertex();
line_pp.UseStages(line_prog).Geometry().Fragment();
Texture::Active(0);
sketch_prog.sketch_tex.Set(0);
{
auto bound_tex = gl.Bound(Texture::Target::_3D, sketch_texture);
for(GLuint i=0; i!=sketch_tex_layers; ++i)
{
auto image = images::BrushedMetalUByte(
512, 512,
64 + i*128,
-(2+i*4), +(2+i*4),
64, 256-i*4
);
if(i == 0)
{
bound_tex.Image3D(
0,
image.Width(),
image.Height(),
sketch_tex_layers,
0,
image.Format(),
image.Type(),
nullptr
);
}
bound_tex.SubImage3D(
0,
0, 0, i,
image.Width(),
image.Height(),
1,
image.Format(),
image.Type(),
image.RawData()
);
}
bound_tex.GenerateMipmap();
}
Texture::Active(1);
sketch_prog.shadow_tex.Set(1);
gl.Bound(Texture::Target::_2D, shadow_tex)
.Image2D(
0,
shadow_tex_side, shadow_tex_side,
0,
nullptr
);
gl.Bound(Framebuffer::Target::Draw, frame_shadow_fbo)
.AttachTexture(
shadow_tex,
0
);
gl.ClearDepth(1.0f);
gl.PolygonOffset(1.0, 1.0);
gl.LineWidth(1.5);
}
void Reshape(GLuint vp_width, GLuint vp_height)
{
width = vp_width;
height = vp_height;
}
void RenderShadowMap(
const Vec3f& light_position,
const Mat4f& torus_matrix,
const Mat4f& light_proj_matrix
)
{
frame_shadow_fbo.Bind(Framebuffer::Target::Draw);
gl.Viewport(shadow_tex_side, shadow_tex_side);
gl.Clear().DepthBuffer();
transf_prog.camera_matrix.Set(light_proj_matrix);
transf_prog.camera_position.Set(light_position);
transf_prog.model_matrix.Set(torus_matrix);
shadow_pp.Bind();
torus.Draw();
}
void RenderImage(
double time,
const Mat4f& torus_matrix,
const Mat4f& light_proj_matrix
)
{
gl.ClearColor(1.0f, 0.9f, 0.8f, 0.0f);
gl.Viewport(width, height);
gl.Clear().ColorBuffer().DepthBuffer();
transf_prog.light_proj_matrix.Set(light_proj_matrix);
Degrees(60),
double(width)/height,
1, 60
);
Vec3f camera_target(0.0, 0.8, 0.0);
camera_target,
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);
shadow_pp.Bind();
gl.ColorMask(false, false, false, false);
plane.Draw();
transf_prog.model_matrix.Set(torus_matrix);
torus.Draw();
gl.ColorMask(true, true, true, true);
sketch_pp.Bind();
plane.Draw();
transf_prog.model_matrix.Set(torus_matrix);
torus.Draw([](GLuint phase) -> bool { return phase < 4; });
torus.Draw([](GLuint phase) -> bool { return phase >= 4; });
line_pp.Bind();
plane.DrawEdges();
transf_prog.model_matrix.Set(torus_matrix);
torus.DrawEdges();
}
{
const Vec3f light_position(16.0, 10.0, 9.0);
const Vec3f torus_center(0.0, 1.5, 0.0);
const Mat4f torus_matrix =
);
const Mat4f light_proj_matrix =
transf_prog.light_position.Set(light_position);
RenderShadowMap(
light_position,
torus_matrix,
light_proj_matrix
);
RenderImage(
time,
torus_matrix,
light_proj_matrix
);
}
{
return time < 90.0;
}
double ScreenshotTime(void) const
{
return 3.0;
}
};
void setupExample(ExampleParams& ){ }
std::unique_ptr<ExampleThread> makeExampleThread(
Example& ,
unsigned ,
const ExampleParams&
){ return std::unique_ptr<ExampleThread>(); }
std::unique_ptr<Example> makeExample(const ExampleParams& )
{
return std::unique_ptr<Example>(new SketchExample);
}
}