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;
Buffer verts, normals, texcoords;
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")
{
namespace se = oglplus::smart_enums;
vs.Source(
"#version 120\n"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"attribute vec4 Position;"
"attribute vec3 Normal;"
"attribute vec2 TexCoord;"
"varying vec3 vertNormal;"
"varying vec3 vertLight;"
"varying vec2 vertTexCoord;"
"uniform vec3 LightPos;"
"void main(void)"
"{"
" vertNormal = mat3(ModelMatrix)*Normal;"
" gl_Position = ModelMatrix * Position;"
" vertLight = LightPos - gl_Position.xyz;"
" vertTexCoord = TexCoord;"
" gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;"
"}"
).Compile();
fs.Source(
"#version 120\n"
"uniform sampler2D TexUnit;"
"varying vec3 vertNormal;"
"varying vec3 vertLight;"
"varying vec2 vertTexCoord;"
"void main(void)"
"{"
" float l = length(vertLight);"
" float d = l > 0 ? dot(vertNormal, normalize(vertLight)) / l : 0.0;"
" float i = 0.3 + 2.0*max(d, 0.0);"
" vec4 t = texture2D(TexUnit, vertTexCoord);"
" gl_FragColor = vec4(t.rgb*i, 1.0);"
"}"
).Compile();
prog.AttachShader(vs).AttachShader(fs).Link().Use();
cube.Bind();
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.Positions(data);
(prog|"Position").Setup<GLfloat>(n_per_vertex).Enable();
}
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.Normals(data);
(prog|"Normal").Setup<GLfloat>(n_per_vertex).Enable();
}
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.TexCoordinates(data);
(prog|"TexCoord").Setup<GLfloat>(n_per_vertex).Enable();
}
{
auto image = images::NewtonFractal(
512, 512,
images::NewtonFractal::X4Minus1(),
[](double x) -> double
{
}
);
.Image2D(image)
.GenerateMipmap();
}
(prog/"TexUnit") = 0;
(prog/
"LightPos") =
Vec3f(1.0f, 2.0f, 3.0f);
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),
GLfloat(width)/height,
0.1, 30
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
camera_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);
}
}