Shows interoperability with GLM matrix and vector classesCopyright 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 "glut_glew_example.hpp"
#define GLM_FORCE_RADIANS
#include <oglplus/interop/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
class GLMBoxesExample
: public oglplus::StandaloneExample
{
private:
oglplus::Lazy<oglplus::Uniform<float>> frame_time;
public:
GLMBoxesExample(int, const char**)
: cube_instr(make_cube.Instructions())
, cube_indices(make_cube.Indices())
, frame_time(prog, "Time")
{
vs.Source(
"#version 330\n"
"uniform mat4 CameraMatrix, ScaleMatrix;"
"uniform vec3 LightPos;"
"uniform float Time;"
"in vec4 Position;"
"in vec3 Normal;"
"out vec3 vertColor;"
"void main(void)"
"{"
" float angle = gl_InstanceID * 10 * 2 * 3.14159 / 360.0;"
" float ct = cos(angle+Time);"
" float st = sin(angle+Time);"
" mat4 ModelMatrix = mat4("
" ct, 0.0, st, 0.0,"
" 0.0, 1.0, 0.0, 0.0,"
" -st, 0.0, ct, 0.0,"
" 0.0, 0.0, 0.0, 1.0 "
" ) * mat4("
" 1.0, 0.0, 0.0, 0.0,"
" 0.0, 1.0, 0.0, 0.0,"
" 0.0, 0.0, 1.0, 0.0,"
" 12.0, 0.0, 0.0, 1.0 "
" ) * mat4("
" ct, -st, 0.0, 0.0,"
" st, ct, 0.0, 0.0,"
" 0.0, 0.0, 1.0, 0.0,"
" 0.0, 0.0, 0.0, 1.0 "
" );"
" gl_Position = "
" ModelMatrix *"
" ScaleMatrix *"
" Position;"
" vec3 vertLightDir = normalize(LightPos - gl_Position.xyz);"
" vec3 vertNormal = normalize(("
" ModelMatrix *"
" vec4(Normal, 0.0)"
" ).xyz);"
" gl_Position = CameraMatrix * gl_Position;"
" vertColor = abs(normalize("
" Normal -"
" vec3(1.0, 1.0, 1.0) +"
" Position.xyz*0.2"
" )) * (0.6 + 0.5*max(dot(vertNormal, vertLightDir), 0.0));"
"}"
);
vs.Compile();
fs.Source(
"#version 330\n"
"in vec3 vertColor;"
"out vec3 fragColor;"
"void main(void)"
"{"
" fragColor = vertColor;"
"}"
);
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
prog.Use();
cube.Bind();
positions.Bind(oglplus::Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.Positions(data);
oglplus::Buffer::Data(oglplus::Buffer::Target::Array, data);
oglplus::VertexArrayAttrib attr(prog, "Position");
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
normals.Bind(oglplus::Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_cube.Normals(data);
oglplus::Buffer::Data(oglplus::Buffer::Target::Array, data);
oglplus::VertexArrayAttrib attr(prog, "Normal");
attr.Setup<GLfloat>(n_per_vertex);
attr.Enable();
}
gl.ClearColor(0.8f, 0.8f, 0.8f, 0.0f);
gl.ClearDepth(1.0f);
oglplus::Typechecked<oglplus::Uniform<glm::vec3>>(prog, "LightPos").Set(
glm::vec3(7.0, 3.0, -1.0)
);
oglplus::Typechecked<oglplus::Uniform<glm::mat4x4>>(prog, "ScaleMatrix").Set(
glm::scale(glm::mat4(1.0), glm::vec3(1.0, 0.3, 1.7))
);
}
void Reshape(void)
{
auto camera = glm::perspective(
53.0f*3.1415f/180.f,
GLfloat(Aspect()),
1.0f, 100.0f
) * glm::lookAt(
glm::vec3(21.0f, 7.0f, 0.0f),
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 0.0f, 1.0f, 0.0f)
);
oglplus::Uniform<glm::mat4x4>(prog, "CameraMatrix").Set(camera);
}
void Render(void)
{
gl.Clear().ColorBuffer().DepthBuffer();
frame_time.Set(FrameTime());
cube_instr.Draw(cube_indices, 36);
}
};
int main(int argc, char* argv[])
{
return oglplus::GlutGlewMain<GLMBoxesExample>(
"Example usage with GLM matrix and vector classes",
argc, argv
);
}