概念
插值(Interpolation)表示寻找一系列已知点之间的值的过程。
穿过A点和B点的直线的公式如下:
P = A + tD
其中,D=(B-A)
因此该公式可写成
P = A + t(B - A)或 P = (1-t)A + tB
GLSL中有mix()函数可以进行插值计算
bezier曲线
vertex shader
#version 450 core
void main(){
vec4 vertices[3] = vec4[3](vec4(-0.5, 0.0, 0.5, 1.0),
vec4(0.0, 0.5, 0.5, 1.0),
vec4(0.5, 0.0, 0.5, 1.0));
gl_Position = vertices[gl_VertexID];
}
tesc shader
#version 450 core
layout(vertices = 3) out;
void main(){
if(gl_InvocationID == 0)
{
//将细分因子写入这两个变量中,后面会详细讲什么意思
gl_TessLevelInner[0] = 32.0;
gl_TessLevelOuter[0] = 32.0;
gl_TessLevelOuter[1] = 32.0;
gl_TessLevelOuter[2] = 32.0;
}
//将输入的数据作为输出数据
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}
tese shader
#version 450 core
layout(isolines, equal_spacing, cw) in;
vec4 quardratic_bezier(vec4 A, vec4 B, vec4 C, float t);
void main(){
float u = gl_TessCoord.x;
vec4 A = gl_in[0].gl_Position;
vec4 B = gl_in[1].gl_Position;
vec4 C = gl_in[2].gl_Position;
vec4 P = quardratic_bezier(A, B, C, u);
gl_Position = P;
}
vec4 quardratic_bezier(vec4 A, vec4 B, vec4 C, float t){
vec4 D = mix(A, B, t);
vec4 E = mix(B, C, t);
vec4 P = mix(D, E, t);
return P;
}
fragment shader
#version 450 core
out vec4 color;
void main(){
color = vec4(1.0, 1.0, 1.0, 1.0);
}
Shader类在前面的文章中有,这里不在提
源代码
#include "sb7.h"
#include "Shader.h"
class my_application : public sb7::application
{
public:
void startup() {
shader = new Shader("vs.vert", "tec.tesc", "tee.tese", "fs.frag");
//创建vao对象
glCreateVertexArrays(1, &vertex_arrays_object);
glBindVertexArray(vertex_arrays_object);
}
void render(double currentTime) {
shader->use();
//抗锯齿
glEnable(GL_MULTISAMPLE);
glDrawArrays(GL_PATCHES, 0, 3);
}
void shutdown(){
}
private:
//vao,shader,必不可少
GLuint vertex_arrays_object;
Shader* shader;
};
DECLARE_MAIN(my_application);