Copyright 2008-2013 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 RectangleExample : public Example
{
private:
Context gl;
public:
RectangleExample(void)
{
vs.Source(
"#version 330\n"
"in vec2 Position;"
"out vec2 vertPos;"
"void main(void)"
"{"
" gl_Position = vec4(Position, 0.0, 1.0);"
" vertPos = gl_Position.xy;"
"}"
).Compile();
fs.Source(
"#version 330\n"
"uniform float Time;"
"uniform vec2 SunPos;"
"uniform vec3 Sun1, Sun2, Sky1, Sky2;"
"in vec2 vertPos;"
"out vec3 fragColor;"
"void main(void)"
"{"
" vec2 v = vertPos - SunPos;"
" float l = length(v);"
" float a = (sin(l)+atan(v.y, v.x))/3.1415;"
" if(l < 0.1)"
" fragColor = Sun1;"
" else if(int(18*(Time*0.1 + 1.0 + a)) % 2 == 0)"
" fragColor = mix(Sun1, Sun2, l);"
" else"
" fragColor = mix(Sky1, Sky2, l);"
"}"
).Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
prog.Use();
rectangle.Bind();
GLfloat rectangle_verts[8] = {
-1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, -1.0f,
1.0f, 1.0f
};
verts.Bind(Buffer::Target::Array);
Buffer::Data(Buffer::Target::Array, rectangle_verts);
(prog|"Position").Setup<Vec2f>().Enable();
Typechecked<Uniform<Vec3f>>(prog,
"Sun1").
Set(0.95f, 0.85f, 0.60f);
Typechecked<Uniform<Vec3f>>(prog,
"Sun2").
Set(0.90f, 0.80f, 0.20f);
Typechecked<Uniform<Vec3f>>(prog,
"Sky1").
Set(0.90f, 0.80f, 0.50f);
Typechecked<Uniform<Vec3f>>(prog,
"Sky2").
Set(0.80f, 0.60f, 0.40f);
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
}
{
gl.Clear().ColorBuffer();
Uniform<GLfloat>(prog,
"Time").
Set(GLfloat(time));
Uniform<Vec2f>(prog,
"SunPos").
Set(
-Cos(angle),
Sin(angle)
);
}
{
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 RectangleExample);
}
}