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 TorusExample : public Example
{
private:
shapes::Torus make_torus;
shapes::DrawingInstructions torus_instr;
Context gl;
Lazy<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())
, 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;"
"out vec3 vertNormal;"
"void main(void)"
"{"
" vertNormal = mat3(CameraMatrix)*mat3(ModelMatrix)*Normal;"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" ModelMatrix *"
" Position;"
"}"
).Compile();
fs.Source(
"#version 330\n"
"uniform int ColorCount;"
"uniform vec4 Color[8];"
"in vec3 vertNormal;"
"vec3 ViewDir = vec3(0.0, 0.0, 1.0);"
"vec3 TopDir = vec3(0.0, 1.0, 0.0);"
"out vec4 fragColor;"
"void main(void)"
"{"
" float k = dot(vertNormal, ViewDir);"
" vec3 reflDir = 2.0*k*vertNormal - ViewDir;"
" float a = dot(reflDir, TopDir);"
" vec3 reflColor;"
" for(int i = 0; i != (ColorCount - 1); ++i)"
" {"
" if(a<Color[i].a && a>=Color[i+1].a)"
" {"
" float m = "
" (a - Color[i].a)/"
" (Color[i+1].a-Color[i].a);"
" reflColor = mix("
" Color[i].rgb,"
" Color[i+1].rgb,"
" m"
" );"
" break;"
" }"
" }"
" float i = max(dot(vertNormal, TopDir), 0.0);"
" vec3 diffColor = vec3(i, i, i);"
" fragColor = vec4("
" mix(reflColor, diffColor, 0.3 + i*0.7),"
" 1.0"
" );"
"}"
).Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link().Use();
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);
VertexArrayAttrib attr(prog, "Position");
attr.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);
VertexArrayAttrib attr(prog, "Normal");
attr.Setup<GLfloat>(n_per_vertex).
Enable();
}
Uniform<GLint>(prog,
"ColorCount").
Set(8);
Uniform<Vec4f> color(prog, "Color");
color[0].Set(1.0f, 1.0f, 0.9f, 1.00f);
color[1].Set(1.0f, 0.9f, 0.8f, 0.97f);
color[2].Set(0.9f, 0.7f, 0.5f, 0.95f);
color[3].Set(0.5f, 0.5f, 1.0f, 0.95f);
color[4].Set(0.2f, 0.2f, 0.7f, 0.00f);
color[5].Set(0.1f, 0.1f, 0.1f, 0.00f);
color[6].Set(0.2f, 0.2f, 0.2f,-0.10f);
color[7].Set(0.5f, 0.5f, 0.5f,-1.00f);
gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f);
gl.ClearDepth(1.0f);
gl.FrontFace(make_torus.FaceWinding());
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
prog.Use();
projection_matrix.Set(
Degrees(80),
double(width)/height,
1, 20
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
camera_matrix.Set(
3.5,
Degrees(time * 35),
)
);
model_matrix.Set(
);
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);
}
}