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)
namespace oglplus {
class LandscapeExample : public Example
{
private:
GLuint grid_side;
shapes::Plane make_plane;
shapes::DrawingInstructions plane_instr;
Context gl;
ProgramUniform<Mat4f> projection_matrix, camera_matrix;
ProgramUniform<GLfloat> vc_int, gc_int, fc_int;
public:
LandscapeExample(void)
: grid_side(8)
, make_plane(
grid_side, grid_side
), plane_instr(make_plane.Instructions())
, plane_indices(make_plane.Indices())
{
vs.Source(
"#version 420\n"
"uniform mat4 ProjectionMatrix, CameraMatrix;"
"layout(binding = 0, offset = 0) uniform atomic_uint vc;"
"const float mult = 1.0/128.0;"
"uniform float vc_int;"
"in vec4 Position;"
"out vec3 vertColor;"
"void main(void)"
"{"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" Position;"
" vertColor = vec3("
" fract(atomicCounterIncrement(vc)*mult),"
" 0.0,"
" 0.0 "
" )*max(vc_int, 0.0);"
"}"
);
vs.Compile();
prog.AttachShader(vs);
gs.Source(
"#version 420\n"
"layout (triangles) in;"
"layout (triangle_strip, max_vertices = 3) out;"
"layout(binding = 0, offset = 4) uniform atomic_uint gc;"
"const float mult = 1.0/128.0;"
"uniform float gc_int;"
"in vec3 vertColor[3];"
"out vec3 geomColor;"
"void main(void)"
"{"
" vec3 Color = vec3("
" 0.0,"
" fract(atomicCounterIncrement(gc)*mult),"
" 0.0 "
" )*max(gc_int, 0.0);"
" for(int v=0; v!=3; ++v)"
" {"
" gl_Position = gl_in[v].gl_Position;"
" geomColor = vertColor[v] + Color;"
" EmitVertex();"
" }"
" EndPrimitive();"
"}"
);
gs.Compile();
prog.AttachShader(gs);
fs.Source(
"#version 420\n"
"layout(binding = 0, offset = 8) uniform atomic_uint fc;"
"const float mult = 1.0/4096.0;"
"uniform float fc_int;"
"in vec3 geomColor;"
"out vec3 fragColor;"
"void main(void)"
"{"
" vec3 Color = vec3("
" 0.0,"
" 0.0,"
" sqrt(fract(atomicCounterIncrement(fc)*mult))"
" )*max(fc_int, 0.0);"
" fragColor = geomColor + Color;"
"}"
);
fs.Compile();
prog.AttachShader(fs);
prog.Link();
prog.Use();
projection_matrix = UniformLoc(prog, "ProjectionMatrix");
camera_matrix = UniformLoc(prog, "CameraMatrix");
vc_int = UniformLoc(prog, "vc_int");
gc_int = UniformLoc(prog, "gc_int");
fc_int = UniformLoc(prog, "fc_int");
plane.Bind();
positions.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_plane.Positions(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, "Position");
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
counters.Bind(Buffer::Target::AtomicCounter);
{
const GLuint tmp[3] = {0u, 0u, 0u};
Buffer::Data(
Buffer::Target::AtomicCounter,
3, tmp,
);
}
counters.BindBase(Buffer::IndexedTarget::AtomicCounter, 0);
gl.ClearColor(0.2f, 0.2f, 0.2f, 0.0f);
gl.ClearDepth(1.0f);
plane.Bind();
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
projection_matrix.Set(
Degrees(60),
double(width)/height,
1, 20
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
camera_matrix.Set(
2.8,
Degrees(85)
)
);
vc_int.Set(
SineWave(time / 5.0 - 0.25));
fc_int.Set(
SineWave(time / 7.0 + 0.25));
plane_instr.Draw(plane_indices);
const GLuint tmp[3] = {0u, 0u, 0u};
Buffer::SubData(
0, 3, tmp
);
}
{
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 LandscapeExample);
}
}