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 TorusExample : public Example
{
private:
shapes::Torus make_torus;
shapes::DrawingInstructions torus_instr;
Context gl;
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"in vec4 Position;"
"in vec3 Normal;"
"out vec3 vertNormal;"
"void main(void)"
"{"
" vertNormal = mat3(ModelMatrix)*Normal;"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" ModelMatrix *"
" Position;"
"}"
).Compile();
fs.Source(
"#version 330\n"
"in vec3 vertNormal;"
"out vec4 fragColor;"
"uniform vec3 LightPos;"
"void main(void)"
"{"
" float intensity = 2.0 * max("
" dot(vertNormal, LightPos)/"
" length(LightPos),"
" 0.0"
" );"
" if(!gl_FrontFacing)"
" fragColor = vec4(0.0, 0.0, 0.0, 1.0);"
" else if(intensity > 0.9)"
" fragColor = vec4(1.0, 0.9, 0.8, 1.0);"
" else if(intensity > 0.1)"
" fragColor = vec4(0.7, 0.6, 0.4, 1.0);"
" else"
" fragColor = vec4(0.3, 0.2, 0.1, 1.0);"
"}"
).Compile();
prog << vs << fs;
prog.Link().Use();
return prog;
}
Uniform<Mat4f> projection_matrix, camera_matrix, model_matrix;
public:
TorusExample(void)
: make_torus(1.0, 0.5, 72, 48)
, torus_instr(make_torus.Instructions())
, torus_indices(make_torus.Indices())
, prog(make_prog())
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
, model_matrix(prog, "ModelMatrix")
{
torus.Bind();
verts.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_torus.Positions(data);
Buffer::Data(Buffer::Target::Array, data);
(prog|"Position").Setup<GLfloat>(n_per_vertex).Enable();
}
normals.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_torus.Normals(data);
Buffer::Data(Buffer::Target::Array, data);
(prog|"Normal").Setup<GLfloat>(n_per_vertex).Enable();
}
(prog/
"LightPos").
Set(
Vec3f(4.0f, 4.0f, -8.0f));
gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f);
gl.ClearDepth(1.0f);
gl.FrontFace(make_torus.FaceWinding());
glLineWidth(4.0f);
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
prog.Use();
projection_matrix.Set(
Degrees(75),
double(width)/height,
1, 30
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
camera_matrix.Set(
3.5,
Degrees(time * 35),
)
);
model_matrix.Set(
);
torus_instr.Draw(torus_indices);
torus_instr.Draw(torus_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 TorusExample);
}
}