Shows the basic usage of OGLplus by drawing a simple transformed triangle.
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)
namespace oglplus {
class TriangleExample : public Example
{
private:
Context gl;
Array<Buffer> buffs;
public:
TriangleExample(void)
: buffs(2)
{
vs.Source(" \
#version 330\n \
in vec3 Position; \
in vec3 Color; \
uniform mat4 Matrix; \
out vec4 vertColor; \
void main(void) \
{ \
gl_Position = Matrix * vec4(Position, 1.0); \
vertColor = vec4(Color, 1.0); \
} \
");
vs.Compile();
fs.Source(" \
#version 330\n \
in vec4 vertColor; \
out vec4 fragColor; \
void main(void) \
{ \
fragColor = vertColor; \
} \
");
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link().Use();
triangle.Bind();
GLfloat triangle_verts[9] = {
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
buffs[0].Bind(Buffer::Target::Array);
Buffer::Data(Buffer::Target::Array, triangle_verts);
VertexArrayAttrib vert_attr(prog, "Position");
vert_attr.Setup<
Vec3f>();
vert_attr.Enable();
GLfloat triangle_colors[9] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
buffs[1].Bind(Buffer::Target::Array);
Buffer::Data(Buffer::Target::Array, triangle_colors);
VertexArrayAttrib color_attr(prog, "Color");
color_attr.Setup<
Vec3f>();
color_attr.Enable();
Uniform<Mat4f> u1(prog, "Matrix");
Vec4f(2.0f, 0.0f, 0.0f, -1.0f),
Vec4f(0.0f, 2.0f, 0.0f, -1.0f),
Vec4f(0.0f, 0.0f, 2.0f, 0.0f),
Vec4f(0.0f, 0.0f, 0.0f, 1.0f)
));
gl.ClearColor(1.0f, 1.0f, 1.0f, 0.0f);
}
void Reshape(GLuint width, GLuint height)
{
gl.Viewport(width, height);
}
{
gl.Clear().ColorBuffer();
}
};
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 TriangleExample);
}
}