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::TwistedTorus make_torus;
shapes::DrawingInstructions torus_instr;
Context gl;
Buffer positions, normals, colors;
public:
TorusExample(void)
: make_torus()
, torus_instr(make_torus.Instructions())
, torus_indices(make_torus.Indices())
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix;"
"in vec4 Position;"
"in vec3 Normal;"
"in vec3 Color;"
"out vec3 vertColor;"
"out vec3 vertNormal;"
"out vec3 vertViewDir;"
"void main(void)"
"{"
" vertColor = normalize(vec3(1,1,1)-Color);"
" vertNormal = Normal;"
" vertViewDir = ("
" vec4(0.0, 0.0, 1.0, 1.0)*"
" CameraMatrix"
" ).xyz;"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" Position;"
"}"
);
vs.Compile();
fs.Source(
"#version 330\n"
"in vec3 vertColor;"
"in vec3 vertNormal;"
"in vec3 vertViewDir;"
"out vec4 fragColor;"
"uniform vec3 LightPos[3];"
"void main(void)"
"{"
" float amb = 0.2;"
" float diff = 0.0;"
" float spec = 0.0;"
" for(int i=0;i!=3;++i)"
" {"
" diff += max("
" dot(vertNormal, LightPos[i])/"
" dot(LightPos[i], LightPos[i]),"
" 0.0"
" );"
" float k = dot(vertNormal, LightPos[i]);"
" vec3 r = 2.0*k*vertNormal - LightPos[i];"
" spec += pow(max("
" dot(normalize(r), vertViewDir),"
" 0.0"
" ), 32.0 * dot(r, r));"
" }"
" fragColor = "
" vec4(vertColor, 1.0)*(amb+diff)+"
" vec4(1.0, 1.0, 1.0, 1.0)*spec;"
"}"
);
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
prog.Use();
torus.Bind();
positions.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_torus.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_torus.Normals(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, "Normal");
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
colors.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_torus.Tangents(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, "Color");
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
Uniform<Vec3f> light_pos(prog, "LightPos");
light_pos[0].Set(
Vec3f(2.0f,-1.0f, 0.0f));
light_pos[1].Set(
Vec3f(0.0f, 3.0f,-1.0f));
light_pos[2].Set(
Vec3f(0.0f,-1.0f, 4.0f));
gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f);
gl.ClearDepth(1.0f);
gl.FrontFace(make_torus.FaceWinding());
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
prog.Use();
Uniform<Mat4f>(prog,
"ProjectionMatrix").
Set(
Degrees(60),
double(width)/height,
1, 30
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
Uniform<Mat4f>(prog,
"CameraMatrix").
Set(
5.0,
)
);
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);
}
}