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 SphereExample : public Example
{
private:
shapes::Sphere make_sphere;
shapes::DrawingInstructions sphere_instr;
Context gl;
Lazy<Uniform<Mat4f>> projection_matrix;
Lazy<Uniform<Mat4f>> camera_matrix;
public:
SphereExample(void)
: sphere_instr(make_sphere.Instructions())
, sphere_indices(make_sphere.Indices())
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix;"
"layout(location = 0) in vec4 Position;"
"layout(location = 1) in vec2 TexCoord;"
"out vec2 vertTexCoord;"
"void main(void)"
"{"
" vertTexCoord = TexCoord;"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" Position;"
"}"
).Compile();
fs.Source(
"#version 330\n"
"in vec2 vertTexCoord;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float i = ("
" int(vertTexCoord.x*18) % 2+"
" int(vertTexCoord.y*14) % 2"
" ) % 2;"
" fragColor = vec4(1-i/2, 1-i/2, 1-i/2, 1.0);"
"}"
).Compile();
prog.AttachShader(vs).AttachShader(fs);
prog.Link().Use();
sphere.Bind();
verts.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_sphere.Positions(data);
Buffer::Data(Buffer::Target::Array, data);
(prog|0).Setup<GLfloat>(n_per_vertex).Enable();
}
texcoords.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_sphere.TexCoordinates(data);
Buffer::Data(Buffer::Target::Array, data);
(prog|1).Setup<GLfloat>(n_per_vertex).Enable();
}
gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f);
gl.ClearDepth(1.0f);
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
prog.Use();
projection_matrix.Set(
Degrees(60),
double(width)/height,
1, 20
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
camera_matrix.Set(
3.5,
Degrees(time * 135),
)
);
sphere_instr.Draw(sphere_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 SphereExample);
}
}