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 Particle
{
private:
shapes::Sphere make_sphere;
shapes::DrawingInstructions sphere_instr;
Uniform<Mat4f> projection_matrix, camera_matrix, model_matrix;
Uniform<Vec3f> light_pos;
public:
: sphere_instr(make_sphere.Instructions())
, sphere_indices(make_sphere.Indices())
, fs(std::forward<FragmentShader>(frag))
{
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
prog.Use();
projection_matrix = (prog/"ProjectionMatrix");
camera_matrix = (prog/"CameraMatrix");
model_matrix = (prog/"ModelMatrix");
light_pos = (prog/"LightPos");
sphere.Bind();
const GLuint n_attr = 2;
typedef GLuint (shapes::Sphere::*Func)(std::vector<GLfloat>&) const;
Func func[n_attr] = {
};
Reference<Buffer> vbo[n_attr] = {verts, normals};
const GLchar* ident[n_attr] = {"Position", "Normal"};
for(GLuint i=0; i!=n_attr; ++i)
{
vbo[i].Bind(Buffer::Target::Array);
std::vector<GLfloat> data;
GLuint n_per_vertex = (make_sphere.*func[i])(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, ident[i]);
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
}
void SetProjection(
const Mat4f& projection)
{
prog.Use();
projection_matrix.Set(projection);
}
void SetLightAndCamera(
const Vec3f& light,
const Mat4f& camera)
{
prog.Use();
light_pos.Set(light);
camera_matrix.Set(camera);
}
void Render(
const Mat4f& model)
{
prog.Use();
model_matrix.Set(model);
sphere.Bind();
sphere_instr.Draw(sphere_indices);
}
};
class SphereExample : public Example
{
private:
Context gl;
{
shader.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"in vec4 Position;"
"in vec3 Normal;"
"out vec3 vertNormal;"
"out vec3 vertLight;"
"out vec3 vertViewNormal;"
"uniform vec3 LightPos;"
"void main(void)"
"{"
" gl_Position = ModelMatrix * Position;"
" vertNormal = mat3(ModelMatrix)*Normal;"
" vertViewNormal = mat3(CameraMatrix)*vertNormal;"
" vertLight = LightPos - gl_Position.xyz;"
" gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;"
"}"
);
shader.Compile();
return std::move(shader);
}
{
"#version 330\n"
"in vec3 vertNormal;"
"in vec3 vertLight;"
"in vec3 vertViewNormal;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float lighting = dot("
" vertNormal, "
" normalize(vertLight)"
" );"
" float intensity = clamp("
" 0.4 + lighting * 1.0,"
" 0.0,"
" 1.0"
" );");
}
{
" fragColor = sig?"
" vec4(1.0, 1.0, 1.0, 1.0):"
" vec4(color * intensity, 1.0);"
"}");
}
{
" bool sig = ("
" abs(vertViewNormal.x) < 0.5 &&"
" abs(vertViewNormal.y) < 0.2 "
" ) || ("
" abs(vertViewNormal.y) < 0.5 &&"
" abs(vertViewNormal.x) < 0.2 "
" );"
" vec3 color = vec3(1.0, 0.0, 0.0);");
}
{
" bool sig = false;"
" vec3 color = vec3(0.5, 0.5, 0.5);");
}
{
" bool sig = ("
" abs(vertViewNormal.x) < 0.5 &&"
" abs(vertViewNormal.y) < 0.2"
" );"
" vec3 color = vec3(0.0, 0.0, 1.0);");
}
{
StrCRef source[3] = {fs_prologue(), color_fs, fs_epilogue()};
shader.Source(source);
shader.Compile();
return shader;
}
Particle proton;
Particle neutron;
Particle electron;
public:
SphereExample(void)
: vs(make_vs())
, proton(vs,make_fs(fs_proton()))
, neutron(vs,make_fs(fs_neutron()))
, electron(vs,make_fs(fs_electron()))
{
gl.ClearColor(0.3f, 0.3f, 0.3f, 0.0f);
gl.ClearDepth(1.0f);
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
Degrees(60),
double(width)/height,
1, 50
);
proton.SetProjection(projection);
neutron.SetProjection(projection);
electron.SetProjection(projection);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
auto light =
Vec3f(8.0f, 8.0f, 8.0f);
21.0,
Degrees(time * 15),
);
);
proton.SetLightAndCamera(light, camera);
neutron.SetLightAndCamera(light, camera);
electron.SetLightAndCamera(light, camera);
electron.Render(
);
electron.Render(
);
}
{
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 SphereExample);
}
}