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>
namespace oglplus {
class ReflectionExample : public Example
{
private:
shapes::Cage make_cube;
shapes::DrawingInstructions cube_instr;
Context gl;
{
vs.Source(
"#version 330\n"
"in vec4 Position;"
"in vec3 Normal;"
"out vec3 vertColor;"
"out vec3 vertNormal;"
"out vec3 vertLight;"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"uniform vec3 LightPos;"
"void main(void)"
"{"
" gl_Position = ModelMatrix * Position;"
" vertColor = abs(normalize(Normal+vec3(1, 1, 1)));"
" vertNormal = mat3(ModelMatrix)*Normal;"
" vertLight = LightPos - gl_Position.xyz;"
" gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;"
"}"
);
vs.Compile();
fs.Source(
"#version 330\n"
"in vec3 vertColor;"
"in vec3 vertNormal;"
"in vec3 vertLight;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float l = dot(vertLight, vertLight);"
" float d = l > 0.0 ? dot(vertNormal, normalize(vertLight)) / l : 0.0;"
" float i = 0.2 + max(d*3.2, 0.0);"
" fragColor = vec4(vertColor*i, 1.0);"
"}"
);
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
return prog;
}
Uniform<Mat4f> projection_matrix, camera_matrix, model_matrix;
Buffer cube_verts, cube_normals;
Buffer plane_verts, plane_normals;
public:
ReflectionExample(void)
: make_cube(0.5,0.5,0.5, 0.1,0.1,0.1, 3,3,3)
, cube_indices(make_cube.Indices())
, cube_instr(make_cube.Instructions())
, prog(make_prog())
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
, model_matrix(prog, "ModelMatrix")
{
gl.Use(prog);
gl.Bind(cube);
gl.Bind(Buffer::Target::Array, cube_verts);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.Positions(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, "Position");
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
gl.Bind(Buffer::Target::Array, cube_normals);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.Normals(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, "Normal");
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
gl.Bind(plane);
gl.Bind(Buffer::Target::Array, plane_verts);
{
GLfloat data[4*3] = {
-2.0f, 0.0f, 2.0f,
-2.0f, 0.0f, -2.0f,
2.0f, 0.0f, 2.0f,
2.0f, 0.0f, -2.0f
};
Buffer::Data(Buffer::Target::Array, 4*3, data);
prog.Use();
VertexArrayAttrib attr(prog, "Position");
attr.Enable();
}
gl.Bind(Buffer::Target::Array, plane_normals);
{
GLfloat data[4*3] = {
-0.1f, 1.0f, 0.1f,
-0.1f, 1.0f, -0.1f,
0.1f, 1.0f, 0.1f,
0.1f, 1.0f, -0.1f
};
Buffer::Data(Buffer::Target::Array, 4*3, data);
prog.Use();
VertexArrayAttrib attr(prog, "Normal");
attr.Enable();
}
Uniform<Vec3f>(prog,
"LightPos").
Set(1.5, 2.0, 2.5);
gl.ClearColor(0.2f, 0.2f, 0.2f, 0.0f);
gl.ClearDepth(1.0f);
gl.ClearStencil(0);
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
prog.Use();
projection_matrix.Set(
Degrees(60),
double(width)/height,
1, 20
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer().StencilBuffer();
camera_matrix.Set(
5.0,
Degrees(time * 11),
Degrees(15 + (-
SineWave(0.25+time/12.5)+1.0)*0.5*75)
)
);
gl.ColorMask(false, false, false, false);
gl.Bind(plane);
model_matrix.Set(identity);
gl.ColorMask(true, true, true, true);
model_matrix.Set(reflection * model);
gl.Bind(cube);
cube_instr.Draw(cube_indices);
model_matrix.Set(model);
cube_instr.Draw(cube_indices);
gl.Bind(plane);
model_matrix.Set(identity);
}
{
return time < 60.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 ReflectionExample);
}
}