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;
LazyUniform<Mat4f> projection_matrix, camera_matrix, model_matrix;
LazyUniform<Vec4f> light_pos_cam;
LazyUniform<Vec3f> front_color, back_color;
public:
TorusExample(void)
: make_torus(1.0, 0.5, 12, 12)
, torus_instr(make_torus.Instructions())
, torus_indices(make_torus.Indices())
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
, model_matrix(prog, "ModelMatrix")
, light_pos_cam(prog, "LightPosCam")
, front_color(prog, "FrontColor")
, back_color(prog, "BackColor")
{
vs.Source(
"#version 330\n"
"uniform mat4 ModelMatrix, CameraMatrix;"
"in vec4 Position;"
"void main(void)"
"{"
" gl_Position = CameraMatrix *"
" ModelMatrix * Position;"
"}"
);
vs.Compile();
gs.Source(
"#version 330\n"
"layout(triangles) in;"
"layout(triangle_strip, max_vertices = 8) out;"
"uniform mat4 ProjectionMatrix;"
"uniform vec4 LightPosCam;"
"out vec3 geomLightDir;"
"out float geomOpacity;"
"void main(void)"
"{"
" vec4 c = vec4(("
" gl_in[0].gl_Position.xyz+"
" gl_in[1].gl_Position.xyz+"
" gl_in[2].gl_Position.xyz "
" ) * 0.333333, 1.0);"
" for(int v = 0; v != 4; ++v)"
" {"
" vec4 b = gl_in[v%3].gl_Position;"
" vec4 a = vec4("
" b.xyz + (c.xyz - b.xyz)*0.3,"
" 1.0"
" );"
" gl_Position = ProjectionMatrix * a;"
" geomLightDir = (LightPosCam - a).xyz;"
" geomOpacity = 1.0;"
" EmitVertex();"
" gl_Position = ProjectionMatrix * b;"
" geomLightDir = (LightPosCam - b).xyz;"
" geomOpacity = 0.0;"
" EmitVertex();"
" }"
" EndPrimitive();"
"}"
);
gs.Compile();
fs.Source(
"#version 330\n"
"in vec3 geomLightDir;"
"in float geomOpacity;"
"uniform vec3 FrontColor, BackColor;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float l = length(geomLightDir);"
" vec3 color = gl_FrontFacing?"
" FrontColor:"
" BackColor;"
" fragColor = vec4(color*(4.0/l), geomOpacity);"
"}"
);
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(gs);
prog.AttachShader(fs);
prog.Link();
torus.Bind();
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_torus.Positions(data);
prog.Use();
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
gl.ClearColor(0.8f, 0.7f, 0.6f, 0.0f);
gl.ClearDepth(1.0f);
gl.FrontFace(make_torus.FaceWinding());
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
Degrees(60),
double(width)/height,
1, 20
);
prog.Use();
projection_matrix.Set(projection);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
4.5,
Degrees(time * 35),
);
auto model =
Vec4f lightPos(4.0f, 4.0f, -8.0f, 1.0f);
prog.Use();
camera_matrix.Set(camera);
model_matrix.Set(model);
light_pos_cam.Set(camera * lightPos);
front_color.Set(
Vec3f(0.3f, 0.2f, 0.0f));
back_color.Set(
Vec3f(0.2f, 0.1f, 0.0f));
torus_instr.Draw(torus_indices);
front_color.Set(
Vec3f(0.9f, 0.8f, 0.1f));
back_color.Set(
Vec3f(1.0f, 0.9f, 0.8f));
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);
}
}