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)
#include <cmath>
namespace oglplus {
struct MeshInputFile
{
std::ifstream stream;
MeshInputFile(void)
{
OpenResourceFile(stream, "models", "suzanne", ".obj");
}
};
class MeshExample : public Example
{
private:
MeshInputFile mesh_input;
shapes::ObjMesh load_mesh;
shapes::DrawingInstructions mesh_instr;
Context gl;
Uniform<Mat4f> camera_matrix, projection_matrix, stretch_matrix;
public:
MeshExample(void)
: load_mesh(mesh_input.stream)
, mesh_instr(load_mesh.Instructions())
, mesh_indices(load_mesh.Indices())
, camera_matrix(prog)
, projection_matrix(prog)
, stretch_matrix(prog)
{
load_mesh.BoundingSphere(mesh_bs);
vs.Source(
"#version 330\n"
"uniform mat4 StretchMatrix, ProjectionMatrix, CameraMatrix;"
"in vec4 Position;"
"in vec3 Normal;"
"out vec3 vertNormal;"
"void main(void)"
"{"
" vertNormal = Normal;"
" gl_Position = "
" StretchMatrix *"
" ProjectionMatrix *"
" CameraMatrix *"
" Position;"
"}"
).Compile();
fs.Source(
"#version 330\n"
"in vec3 vertNormal;"
"out vec3 fragColor;"
"void main(void)"
"{"
" vec3 c = normalize(vec3(1, 1, 1) - vertNormal);"
" fragColor = c;"
"}"
).Compile();
prog.AttachShader(vs).AttachShader(fs);
prog.Link().Use();
camera_matrix.BindTo("CameraMatrix");
projection_matrix.BindTo("ProjectionMatrix");
stretch_matrix.BindTo("StretchMatrix");
mesh.Bind();
positions.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = load_mesh.Positions(data);
Buffer::Data(Buffer::Target::Array, data);
(prog|"Position").Setup<GLfloat>(n_per_vertex).Enable();
}
normals.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = load_mesh.Normals(data);
Buffer::Data(Buffer::Target::Array, data);
(prog|"Normal").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);
projection_matrix.Set(
Degrees(70),
double(width)/height,
1, 20
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
int x=0, y=0, nx=1, ny=1;
switch(int(time / 3.0) % 5)
{
case 1: x=1; y=1; nx=4; ny=4; break;
case 2: x=2; y=1; nx=4; ny=4; break;
case 3: x=1; y=2; nx=4; ny=4; break;
case 4: x=2; y=2; nx=4; ny=4; break;
default:;
}
camera_matrix.Set(
mesh_bs.Center(),
mesh_bs.Radius()*1.4+1.0,
)
);
mesh_instr.Draw(mesh_indices);
}
{
return time < 30.0;
}
double ScreenshotTime(void) const
{
return 2.5;
}
};
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 MeshExample);
}
}