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 <oglplus/dsa/ext/texture.hpp>
#include <cmath>
namespace oglplus {
class CubeExample : public Example
{
private:
shapes::Cube make_cube;
shapes::DrawingInstructions cube_instr;
Context gl;
Lazy<Uniform<Vec3f>> light_pos;
Lazy<Uniform<Mat4f>>
projection_matrix,
tex_projection_matrix,
model_matrix;
public:
CubeExample(void)
: cube_instr(make_cube.Instructions())
, cube_indices(make_cube.Indices())
, light_pos(prog, "LightPos")
, projection_matrix(prog, "ProjectionMatrix")
, tex_projection_matrix(prog, "TexProjectionMatrix")
, model_matrix(prog, "ModelMatrix")
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"uniform mat4 TexProjectionMatrix;"
"in vec4 Position;"
"in vec3 Normal;"
"out vec3 vertNormal;"
"out vec3 vertLight;"
"out vec4 vertTexCoord;"
"uniform vec3 LightPos;"
"void main(void)"
"{"
" vertNormal = ("
" ModelMatrix *"
" vec4(-Normal, 0.0)"
" ).xyz;"
" vertLight = ("
" vec4(LightPos, 0.0)-"
" ModelMatrix * Position"
" ).xyz;"
" vertTexCoord = "
" TexProjectionMatrix *"
" ModelMatrix *"
" Position;"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" ModelMatrix *"
" Position;"
"}"
);
vs.Compile();
fs.Source(
"#version 330\n"
"uniform sampler2D TexUnit;"
"in vec3 vertNormal;"
"in vec3 vertLight;"
"in vec4 vertTexCoord;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float l = length(vertLight);"
" float d = l != 0.0 ? dot("
" vertNormal, "
" normalize(vertLight)"
" ) / l : 0.0;"
" float i = 0.1 + 4.2*max(d, 0.0);"
" vec2 coord = vertTexCoord.st/vertTexCoord.q;"
" vec4 t = texture(TexUnit, coord*0.5 + 0.5);"
" fragColor = vec4(t.rgb*i*sqrt(1.0-t.a), 1.0);"
"}"
);
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();
}
gl.DirectEXT(Texture::Target::_2D, tex)
.GenerateMipmap()
.BorderColor(
Vec4f(1.0f, 1.0f, 1.0f, 0.0f))
.Bind();
UniformSampler(prog, "TexUnit").Set(0);
Uniform<Mat4f>(prog,
"CameraMatrix").
Set(
);
gl.ClearColor(0.1f, 0.1f, 0.1f, 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(60),
double(width)/height,
1, 20
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
Vec3f lightPos(-1.0f, 2.0f, 2.0f);
lightPos *= (1.0f -
SineWave(time/5.0f)*0.4f);
light_pos.Set(lightPos);
tex_projection_matrix.Set(
);
model_matrix.Set(
);
cube.Bind();
cube_instr.Draw(cube_indices);
}
{
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);
}
}