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)
namespace oglplus {
class TessellationExample : public Example
{
private:
Context gl;
Lazy<Uniform<Mat4f>> projection_matrix, camera_matrix;
public:
TessellationExample(void)
: prog()
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
{
vert.Source(
"#version 330\n"
"uniform mat4 CameraMatrix;"
"in vec4 Position;"
"out vec4 vertPosition;"
"void main(void)"
"{"
" vertPosition = CameraMatrix * Position;"
"}"
);
vert.Compile();
prog << vert;
teco.Source(
"#version 330\n"
"#extension ARB_tessellation_shader: enable\n"
"layout(vertices = 16) out;"
"in vec4 vertPosition[];"
"patch out vec3 tecoPosition[16];"
"void main(void)"
"{"
" if(gl_InvocationID == 0)"
" {"
" int tl = 1-int(100.0 / vertPosition[gl_InvocationID].z);"
" gl_TessLevelInner[0] = tl;"
" gl_TessLevelInner[1] = tl;"
" gl_TessLevelOuter[0] = tl;"
" gl_TessLevelOuter[1] = tl;"
" gl_TessLevelOuter[2] = tl;"
" gl_TessLevelOuter[3] = tl;"
" }"
" tecoPosition[gl_InvocationID] = "
" vertPosition[gl_InvocationID].xyz;"
"}"
);
teco.Compile();
prog << teco;
teev.Source(
"#version 330\n"
"#extension ARB_tessellation_shader: enable\n"
"layout(quads, equal_spacing, ccw) in;"
"uniform mat4 ProjectionMatrix;"
"patch in vec3 tecoPosition[16];"
"const mat4 B = mat4("
" -1, 3,-3, 1,"
" 3,-6, 3, 0,"
" -3, 3, 0, 0,"
" 1, 0, 0, 0 "
");"
"mat4 Px, Py, Pz;"
"void main(void)"
"{"
" float u = gl_TessCoord.x, v = gl_TessCoord.y;"
" for(int j=0; j!=4; ++j)"
" for(int i=0; i!=4; ++i)"
" {"
" int k = j*4+i;"
" Px[j][i] = tecoPosition[k].x;"
" Py[j][i] = tecoPosition[k].y;"
" Pz[j][i] = tecoPosition[k].z;"
" }"
" mat4 Cx = B * Px * B;"
" mat4 Cy = B * Py * B;"
" mat4 Cz = B * Pz * B;"
" vec4 up = vec4(u*u*u, u*u, u, 1);"
" vec4 vp = vec4(v*v*v, v*v, v, 1);"
" vec4 tempPosition = vec4(dot(Cx * vp, up), dot(Cy * vp, up), dot(Cz * vp, up), 1.0);"
" gl_Position = ProjectionMatrix * tempPosition;"
"}"
);
teev.Compile();
prog << teev;
frag.Source(
"#version 330\n"
"out vec3 fragColor;"
"void main(void)"
"{"
" fragColor = vec3(0.1, 0.1, 0.1);"
"}"
);
frag.Compile();
prog << frag;
prog.Link();
prog.Use();
vao.Bind();
GLfloat patch_cp_pos[16*3] = {
-2.0f, 0.0f, -2.0f,
-1.0f, 0.0f, -3.0f,
1.0f, 0.0f, -5.0f,
2.0f, 0.0f, -2.0f,
-1.0f, 0.0f, -1.0f,
0.0f, 4.0f, -1.0f,
1.0f, 4.0f, -1.0f,
3.0f, 0.0f, -1.0f,
-1.0f, 0.0f, 1.0f,
-1.0f, 4.0f, 1.0f,
0.0f, 4.0f, 1.0f,
1.0f, 0.0f, 1.0f,
-2.0f, 0.0f, 2.0f,
-1.0f, 0.0f, 5.0f,
1.0f, 0.0f, 3.0f,
2.0f, 0.0f, 2.0f
};
positions.Bind(Buffer::Target::Array);
Buffer::Data(Buffer::Target::Array, 16*3, patch_cp_pos);
VertexArrayAttrib position_attr(prog, "Position");
position_attr.Setup<
Vec3f>();
position_attr.Enable();
gl.ClearColor(0.9f, 0.9f, 0.9f, 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, 100
)
);
}
{
gl.Clear().ColorBuffer().DepthBuffer();
camera_matrix.Set(
Degrees(time * 47),
)
);
}
};
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 TessellationExample);
}
}