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 CubeExample : public Example
{
private:
shapes::Cube make_cube;
shapes::DrawingInstructions cube_instr;
Context gl;
Uniform<Mat4f> projection_matrix, camera_matrix, model_matrix;
public:
CubeExample(void)
: cube_instr(make_cube.Instructions())
, cube_indices(make_cube.Indices())
, projection_matrix(prog)
, camera_matrix(prog)
, model_matrix(prog)
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"in vec4 Position;"
"in vec2 TexCoord;"
"out vec2 vertTexCoord;"
"void main(void)"
"{"
" vertTexCoord = TexCoord;"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" ModelMatrix *"
" Position;"
"}"
).Compile();
fs.Source(
"#version 330\n"
"in vec2 vertTexCoord;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float i = int(("
" vertTexCoord.x+"
" vertTexCoord.y "
" )*8) % 2;"
" fragColor = mix("
" vec4(0, 0, 0, 1),"
" vec4(1, 1, 0, 1),"
" i"
" );"
"}"
).Compile();
prog.AttachShader(vs).AttachShader(fs);
prog.Link().Use();
projection_matrix.BindTo("ProjectionMatrix");
camera_matrix.BindTo("CameraMatrix");
model_matrix.BindTo("ModelMatrix");
cube.Bind();
verts.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.Positions(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, "Position");
attr.Setup<GLfloat>(n_per_vertex).
Enable();
}
texcoords.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.TexCoordinates(data);
Buffer::Data(Buffer::Target::Array, data);
VertexArrayAttrib attr(prog, "TexCoord");
attr.Setup<GLfloat>(n_per_vertex).
Enable();
}
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
prog.Use();
projection_matrix.Set(
Degrees(60),
double(width)/height,
1, 30
)
);
}
{
const GLfloat clear_color[4] = {0.8f, 0.8f, 0.7f, 0.0f};
gl.ClearColorBuffer(0, clear_color);
gl.ClearDepthBuffer(1.0f);
camera_matrix.Set(
3.5,
Degrees(time * 15),
)
);
model_matrix.Set(
);
cube_instr.Draw(cube_indices);
model_matrix.Set(
);
cube_instr.Draw(cube_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 CubeExample);
}
}