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 CubeExample : public Example
{
private:
shapes::Cube make_cube;
shapes::DrawingInstructions cube_instr;
Context gl;
Lazy<Uniform<Mat4f>> projection_matrix, camera_matrix, model_matrix;
Lazy<Uniform<GLint>> front_facing;
int inst_count;
public:
CubeExample(void)
: cube_instr(make_cube.Instructions())
, cube_indices(make_cube.Indices())
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
, model_matrix(prog, "ModelMatrix")
, front_facing(prog, "FrontFacing")
, inst_count(32)
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"uniform vec3 LightPos;"
"uniform int InstCount;"
"uniform int FrontFacing;"
"in vec4 Position;"
"in vec3 Normal;"
"out float vertMult;"
"out vec3 vertColor;"
"out vec3 vertWrapNormal;"
"out vec3 vertNormal;"
"out vec3 vertLight;"
"void main(void)"
"{"
" int inst = (FrontFacing != 0) ? "
" (InstCount - gl_InstanceID - 1):"
" gl_InstanceID;"
" vertMult = float(inst) / float(InstCount-1);"
" float sca = 1.0 - 0.3 * pow(vertMult, 2);"
" mat4 ScaleMatrix = mat4("
" sca, 0.0, 0.0, 0.0,"
" 0.0, sca, 0.0, 0.0,"
" 0.0, 0.0, sca, 0.0,"
" 0.0, 0.0, 0.0, 1.0 "
" );"
" gl_Position = ModelMatrix * Position;"
" vertColor = Normal;"
" vec3 wrap = Position.xyz - Normal;"
" vertWrapNormal = "
" mat3(ModelMatrix)*"
" normalize(mix("
" Normal,"
" wrap,"
" mix(0.5, 1.0, vertMult)"
" ));"
" vertNormal = mat3(ModelMatrix)*Normal;"
" vertLight = LightPos-gl_Position.xyz;"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" ScaleMatrix *"
" gl_Position;"
"}"
);
vs.Compile();
fs.Source(
"#version 330\n"
"in float vertMult;"
"in vec3 vertColor;"
"in vec3 vertWrapNormal;"
"in vec3 vertNormal;"
"in vec3 vertLight;"
"out vec4 fragColor;"
"uniform int InstCount;"
"void main(void)"
"{"
" float l = dot(vertLight, vertLight);"
" float d = l > 0.0 ? dot("
" vertNormal, "
" normalize(vertLight)"
" ) / l : 0.0;"
" float s = max("
" dot(vertWrapNormal, vertLight)/l,"
" 0.0"
" );"
" float intensity = clamp("
" 0.2 + d * 3.0 + s * 5.5,"
" 0.0,"
" 1.0"
" );"
" fragColor = vec4("
" abs(vertColor) * intensity,"
" (2.5 + 1.5*d + 1.5*s) / InstCount"
" );"
"}"
);
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
prog.Use();
cube.Bind();
verts.Bind(Buffer::Target::Array);
{
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();
}
normals.Bind(Buffer::Target::Array);
{
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();
}
Uniform<Vec3f>(prog,
"LightPos").
Set(
Vec3f(-3.0f, -2.0f, -3.0f));
Uniform<GLint>(prog,
"InstCount").
Set(inst_count);
gl.ClearColor(0.5f, 0.6f, 0.5f, 0.0f);
gl.ClearDepth(1.0f);
gl.FrontFace(make_cube.FaceWinding());
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
prog.Use();
projection_matrix.Set(
Degrees(70),
double(width)/height,
1, 20
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
camera_matrix.Set(
3.0f,
Degrees(time * 50),
)
);
model_matrix.Set(
);
front_facing.Set(0);
cube_instr.Draw(cube_indices, inst_count);
front_facing.Set(1);
cube_instr.Draw(cube_indices, inst_count);
}
{
return time < 30.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 CubeExample);
}
}