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 CubeMapExample : public Example
{
private:
shapes::SpiralSphere make_shape;
shapes::DrawingInstructions shape_instr;
Context gl;
Lazy<Uniform<Mat4f>> projection_matrix, camera_matrix, model_matrix;
public:
CubeMapExample(void)
: shape_instr(make_shape.Instructions())
, shape_indices(make_shape.Indices())
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
, model_matrix(prog, "ModelMatrix")
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"in vec4 Position;"
"in vec3 Normal;"
"in vec2 TexCoord;"
"out vec3 vertNormal;"
"out vec3 vertLightDir;"
"out vec3 vertLightRefl;"
"out vec3 vertViewDir;"
"out vec3 vertViewRefl;"
"uniform vec3 LightPos;"
"void main(void)"
"{"
" gl_Position = ModelMatrix * Position;"
" vertNormal = mat3(ModelMatrix)*Normal;"
" vertLightDir = LightPos - gl_Position.xyz;"
" vertLightRefl = reflect("
" -normalize(vertLightDir),"
" normalize(vertNormal)"
" );"
" vertViewDir = ("
" vec4(0.0, 0.0, 1.0, 1.0)*"
" CameraMatrix"
" ).xyz;"
" vertViewRefl = reflect("
" normalize(vertViewDir),"
" normalize(vertNormal)"
" );"
" gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;"
"}"
);
vs.Compile();
fs.Source(
"#version 330\n"
"uniform samplerCube TexUnit;"
"in vec3 vertNormal;"
"in vec3 vertLightDir;"
"in vec3 vertLightRefl;"
"in vec3 vertViewDir;"
"in vec3 vertViewRefl;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float l = length(vertLightDir);"
" float d = dot("
" normalize(vertNormal), "
" normalize(vertLightDir)"
" ) / l;"
" float s = dot("
" normalize(vertLightRefl),"
" normalize(vertViewDir)"
" );"
" vec3 lt = vec3(1.0, 1.0, 1.0);"
" vec3 env = texture(TexUnit, vertViewRefl).rgb;"
" fragColor = vec4("
" env * 0.4 + "
" (lt + env) * 1.5 * max(d, 0.0) + "
" lt * pow(max(s, 0.0), 64), "
" 1.0"
" );"
"}"
);
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
prog.Use();
shape.Bind();
verts.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_shape.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_shape.Normals(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, "Normal");
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
{
GLuint tex_side = 256;
auto image = images::NewtonFractal(
tex_side, tex_side,
images::NewtonFractal::X4Minus1(),
images::NewtonFractal::DefaultMixer()
);
auto tex_target = Texture::Target::CubeMap;
tex << tex_target;
for(int i=0; i!=6; ++i)
{
Texture::CubeMapFace(i) << image;
}
}
Typechecked<Uniform<SLtoCpp<SLDataType::SamplerCube>>>(prog,
"TexUnit").
Set(0);
Uniform<Vec3f>(prog,
"LightPos").
Set(
Vec3f(3.0f, 5.0f, 4.0f));
gl.ClearColor(0.2f, 0.05f, 0.1f, 0.0f);
gl.ClearDepth(1.0f);
gl.FrontFace(make_shape.FaceWinding());
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
prog.Use();
projection_matrix.Set(
Degrees(60),
double(width)/height,
1, 100
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
camera_matrix.Set(
)
);
model_matrix.Set(
)
);
shape.Bind();
shape_instr.Draw(shape_indices);
}
{
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 CubeMapExample);
}
}