我最近开始学习镶嵌,今天我试图在镶嵌后绘制一个三角形,以便可以使用看到所有镶嵌的较小三角形。但是由于某种原因,输出只是彩色背景,其中没有任何三角形。
对于细分,我制作了
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
和control shader
,然后将它们链接到evaluation shader
。(下面的代码)
program
然后我在// Source code for Tesselation Control Shader
static const GLchar * tesselation_control_shader[] =
{
"#version 450 core \n"
" \n"
"layout(vertices = 3) out; \n"
" \n"
"void main(void) \n"
"{ \n"
" //Only if I am invocation 0 \n"
" if (gl_InvocationID == 0) \n"
" { \n"
" gl_TessLevelInner[0] = 5.0; \n"
" gl_TessLevelOuter[0] = 5.0; \n"
" gl_TessLevelOuter[1] = 5.0; \n"
" gl_TessLevelOuter[2] = 5.0; \n"
" } \n"
" \n"
" // Everybody copies their input to their input \n"
" gl_out[gl_InvocationID].gl_Position = \n"
" gl_in[gl_InvocationID].gl_Position; \n"
"} \n"
};
// Source code for tesselation evaluation shader
static const GLchar * tesselation_evaluation_shader[] =
{
"#version 450 core \n"
" \n"
"layout(triangles, equal_spacing, cw) in; \n"
" \n"
"void main(void) \n"
"{ \n"
" gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position + \n"
" gl_TessCoord.y * gl_in[1].gl_Position + \n"
" gl_TessCoord.z * gl_in[2].gl_Position); \n"
"} \n"
};
函数中调用glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
,然后使用render
绘制三角形。
最初我以为glDrawArrays(GL_TRIANGLE, 0, 3)
的默认设置为glPolygonMode
,但我认为这不是问题,因为我只是在看一本书(OpenGL Superbible 7th Edition)。
我该如何解决?
修改:
我在下面添加了整个程序的代码:
GL_FILL
答案 0 :(得分:3)
如果使用镶嵌着色器,则必须绘制补丁。您必须通过glPatchParameteri( GL_PATCH_VERTICES, ...)
设置补丁的大小,原始类型必须为GL_PATCHES
。
如果补丁中的顶点数为3,则必须这样做:
glPatchParameteri(GL_PATCH_VERTICES, 3);
glDrawArrays(GL_PATCHES, 0, 3)
请参见OpenGL 4.6 API Core Profile Specification; 10.1.15 Separate Patches; page 342:
使用模式
PATCHES
指定了单独的补丁。面片是用于原始细分的顶点的有序集合(第11.2节)。包含面片的顶点没有隐含的几何顺序。曲面细分着色器和固定功能曲面细分器使用面片的顶点来生成新的点,线或三角形图元。void PatchParameteri( enum pname, int value );
pname设置为
PATCH_VERTICES
您的着色器程序甚至都没有链接,因为片段着色器试图从输入接口块读取,
未声明为上一个着色器阶段的输出。
您必须通过镶嵌控制和评估着色器将顶点属性传递给片段着色器:
镶嵌控制着色器:
#version 450 core
layout(vertices = 3) out;
in VS_OUT
{
vec4 color;
} tesc_in[];
out TESC_OUT
{
vec4 color;
} tesc_out[];
void main(void)
{
if (gl_InvocationID == 0)
{
gl_TessLevelInner[0] = 5.0;
gl_TessLevelOuter[0] = 5.0;
gl_TessLevelOuter[1] = 5.0;
gl_TessLevelOuter[2] = 5.0;
}
tesc_out[gl_InvocationID].color = tesc_in[gl_InvocationID].color;
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}
镶嵌评估着色器:
#version 450 core
layout(triangles, equal_spacing, cw) in;
in TESC_OUT
{
vec4 color;
} tese_in[];
out TESE_OUT
{
vec4 color;
} tese_out;
void main(void)
{
tese_out.color = ( gl_TessCoord.x * tese_in[0].color +
gl_TessCoord.y * tese_in[1].color +
gl_TessCoord.z * tese_in[2].color ) / 3.0;
gl_Position = ( gl_TessCoord.x * gl_in[0].gl_Position +
gl_TessCoord.y * gl_in[1].gl_Position +
gl_TessCoord.z * gl_in[2].gl_Position ) / 3.0;
}
片段着色器:
#version 450 core
in TESE_OUT
{
vec4 color;
} fs_in;
out vec4 color;
void main(void)
{
color = fs_in.color;
}
此外,我建议检查着色器对象是否已成功编译:
GLuint shaderObj = .... ;
glCompileShader( shaderObj );
GLint status = GL_TRUE;
glGetShaderiv( shaderObj, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
GLint logLen;
glGetShaderiv( shaderObj, GL_INFO_LOG_LENGTH, &logLen );
std::vector< char >log( logLen );
GLsizei written;
glGetShaderInfoLog( shaderObj, logLen, &written, log.data() );
std::cout << "compile error:" << std::endl << log.data() << std::endl;
}
和着色器程序对象已成功链接:
GLuint progObj = ....;
glLinkProgram( progObj );
GLint status = GL_TRUE;
glGetProgramiv( progObj, GL_LINK_STATUS, &status );
if ( status == GL_FALSE )
{
GLint logLen;
glGetProgramiv( progObj, GL_INFO_LOG_LENGTH, &logLen );
std::vector< char >log( logLen );
GLsizei written;
glGetProgramInfoLog( progObj, logLen, &written, log.data() );
std::cout << "link error:" << std::endl << log.data() << std::endl;
}
顺便阅读一下Raw string literals,它简化了着色器源代码字符串的声明:
例如
std::string fragment_shader_source = R"(
#version 450 core
in TESE_OUT
{
vec4 color;
} fs_in;
out vec4 color;
void main(void)
{
color = fs_in.color;
}
)";
另外,offset
可能会将三角形移出视口。在属性初始化中更改offset
的值:
GLfloat attrib[] = { 0.0, 0.0, 0.0, 0.0 };
或出于调试原因而放弃顶点着色器中的offest
gl_Position = vertices[gl_VertexID];
您还必须确保同时设置了color属性:
GLfloat attrib1[] = { 1.0, 1.0, 0.0, 1.0 };
glVertexAttrib4fv(1, attrib1);
结果可能看起来像这样:
答案 1 :(得分:1)
因此,在检查了许多资源和Superbible的代码存储库后,我意识到我有很多不必要的代码(例如,着色器中的interface blocks
),甚至还有很多错误(例如,我有两个错误) program
变量。
但是,修复所有产生所需输出(细分三角形)的代码后,是:
/**
Program to draw a triangle with tesselation.
**/
#include <sb7.h>
class TesselatedTriangle : public sb7::application
{
void init()
{
static const char title[] = "Tessellated Triangle";
sb7::application::init();
memcpy(info.title, title, sizeof(title));
}
virtual void startup()
{
static const char * vertex_shader_source[] =
{
"#version 450 core \n"
" \n"
"void main(void) \n"
"{ \n"
" const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0), \n"
" vec4(-0.25, -0.25, 0.5, 1.0), \n"
" vec4( 0.25, 0.25, 0.5, 1.0)); \n"
" \n"
" gl_Position = vertices[gl_VertexID]; \n"
"} \n"
};
static const char * tesselation_control_shader_source[] =
{
"#version 450 core \n"
" \n"
"layout (vertices = 3) out; \n"
" \n"
"void main(void) \n"
"{ \n"
" if (gl_InvocationID == 0) \n"
" { \n"
" gl_TessLevelInner[0] = 5.0; \n"
" gl_TessLevelOuter[0] = 5.0; \n"
" gl_TessLevelOuter[1] = 5.0; \n"
" gl_TessLevelOuter[2] = 5.0; \n"
" } \n"
" gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; \n"
"} \n"
};
static const char * tesselation_evaluation_shader_source[] =
{
"#version 450 core \n"
" \n"
"layout (triangles, equal_spacing, cw) in; \n"
" \n"
"void main(void) \n"
"{ \n"
" gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) + \n"
" (gl_TessCoord.y * gl_in[1].gl_Position) + \n"
" (gl_TessCoord.z * gl_in[2].gl_Position); \n"
"} \n"
};
static const char * fragment_shader_source[] =
{
"#version 450 core \n"
" \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(0.0, 0.8, 1.0, 1.0); \n"
"} \n"
};
rendering_program = glCreateProgram();
// Compile shaders
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, vertex_shader_source, NULL);
glCompileShader(vs);
GLuint tcs = glCreateShader(GL_TESS_CONTROL_SHADER);
glShaderSource(tcs, 1, tesselation_control_shader_source, NULL);
glCompileShader(tcs);
GLuint tes = glCreateShader(GL_TESS_EVALUATION_SHADER);
glShaderSource(tes, 1, tesselation_evaluation_shader_source, NULL);
glCompileShader(tes);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, fragment_shader_source, NULL);
glCompileShader(fs);
// Attach shaders to the program
glAttachShader(rendering_program, vs);
glAttachShader(rendering_program, tcs);
glAttachShader(rendering_program, tes);
glAttachShader(rendering_program, fs);
// Link the program
glLinkProgram(rendering_program);
// Generate vertex arrays
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
// Declare the drawing mode for the polygons
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
virtual void render(double currentTime)
{
static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, green);
glUseProgram(rendering_program);
glDrawArrays(GL_PATCHES, 0, 3);
}
virtual void shutdown()
{
glDeleteVertexArrays(1, &vertex_array_object);
glDeleteProgram(rendering_program);
}
private:
GLuint rendering_program;
GLuint vertex_array_object;
};
// One and only instance of DECLARE_MAIN
DECLARE_MAIN(TesselatedTriangle)
希望这可以帮助其他遇到相同问题的人。