问题描述
我正在尝试创建一个具有顶点、几何体和片段着色器的着色器。我开始时只有一个顶点和片段着色器,一切正常,但是一旦我尝试附加几何着色器,链接就不再成功。
这是我的几何着色器代码:
//Create geometry shader for exploding
const GLchar* geometryShader =
"#version 410\n " \
"layout (triangles) in;" \
"layout (triangle_strip,max_vertices = 3) out;" \
"in VS_OUT {" \
"vec2 texCoords;" \
"} gs_in[];" \
"out vec2 TexCoords; " \
"uniform float time;" \
"vec4 explode(vec4 position,vec3 normal)" \
"{" \
"float magnitude = 2.0;" \
"vec3 direction = normal * ((sin(time) + 1.0) / 2.0) * magnitude; " \
"return position + vec4(direction,0.0);" \
"}" \
"vec3 Getnormal()" \
"{" \
"vec3 a = vec3(gl_in[0].gl_Position) - vec3(gl_in[1].gl_Position);" \
"vec3 b = vec3(gl_in[2].gl_Position) - vec3(gl_in[1].gl_Position);" \
"return normalize(cross(a,b));" \
"}" \
"void main() { " \
"vec3 normal = Getnormal();" \
"gl_Position = explode(gl_in[0].gl_Position,normal);" \
"TexCoords = gs_in[0].texCoords;" \
"EmitVertex();" \
"gl_Position = explode(gl_in[1].gl_Position,normal);" \
"TexCoords = gs_in[1].texCoords;" \
"EmitVertex();" \
"gl_Position = explode(gl_in[2].gl_Position,normal);" \
"TexCoords = gs_in[2].texCoords;" \
"EmitVertex();" \
"EndPrimitive();" \
"}" \
"" \
"" \
"" \
"";
以及加载我的着色器的代码:
gluint LoadShader(const GLchar* vert,const GLchar* geo,const GLchar* frag)
{
//Create a new vertex shader,attach source code,compile it and check for errors
gluint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderId,1,&vert,NULL);
glCompileShader(vertexShaderId);
GLint success = 0;
glGetShaderiv(vertexShaderId,GL_COMPILE_STATUS,&success);
//display any errors that occur in the vertex shader
if (!success)
{
GLint maxLength = 0;
glGetShaderiv(vertexShaderId,GL_INFO_LOG_LENGTH,&maxLength);
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(vertexShaderId,maxLength,&maxLength,&errorLog.at(0));
std::cout << &errorLog.at(0) << std::endl;
throw std::exception();
}
//Create a new geometry shader,compile it and check for errors
gluint geometryShaderId = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometryShaderId,&geo,NULL);
glCompileShader(geometryShaderId);
glGetShaderiv(geometryShaderId,&success);
//display any errors that occur in the vertex shader
if (!success)
{
GLint maxLength = 0;
glGetShaderiv(geometryShaderId,&maxLength);
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(geometryShaderId,&errorLog.at(0));
std::cout << &errorLog.at(0) << std::endl;
throw std::exception();
}
//Create a new fragment shader,compile it and check for errors
gluint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaderId,&frag,NULL);
glCompileShader(fragmentShaderId);
glGetShaderiv(fragmentShaderId,&success);
//display any errors that occur in the fragment shader
if (!success)
{
GLint maxLength = 0;
glGetShaderiv(fragmentShaderId,&maxLength);
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(fragmentShaderId,&errorLog.at(0));
std::cout << &errorLog.at(0) << std::endl;
throw std::exception();
}
//Create new shader program and attach our shader objects
gluint programId = glCreateProgram();
glAttachShader(programId,vertexShaderId);
glAttachShader(programId,geometryShaderId);
glAttachShader(programId,fragmentShaderId);
//Ensure the VAO "position" attribute stream gets set as the first position during the link.
glBindAttribLocation(programId,"a_Position");
glBindAttribLocation(programId,"a_Texcord");
glBindAttribLocation(programId,2,"in_normal");
//Perform the link and check for failure
glLinkProgram(programId);
glGetProgramiv(programId,GL_LINK_STATUS,&success);
if (!success)
{
throw std::exception();
}
//Detach and destroy the shader objects. These are no longer needed because we Now have a complete shader program
glDetachShader(programId,vertexShaderId);
glDeleteShader(vertexShaderId);
glDetachShader(programId,geometryShaderId);
glDeleteShader(geometryShaderId);
glDetachShader(programId,fragmentShaderId);
glDeleteShader(fragmentShaderId);
//Find uniform locations
GLint colorloc = glGetUniformlocation(programId,"u_Texcord");
GLint modelLoc = glGetUniformlocation(programId,"u_Model");
GLint viewLoc = glGetUniformlocation(programId,"u_View");
GLint projectionLoc = glGetUniformlocation(programId,"u_Projection");
if (modelLoc == -1)
{
throw std::exception();
}
if (projectionLoc == -1)
{
throw std::exception();
}
return programId;
}
在这部分抛出异常(如果我注释掉这部分,它会被其他两个异常捕获,但我认为这是因为链接不成功,尽管我可能是错误的):
//Perform the link and check for failure
glLinkProgram(programId);
glGetProgramiv(programId,&success);
if (!success)
{
throw std::exception();
}
我在网上查看了为什么会发生这种情况的原因有很多,但是由于这没有显示错误消息,所以我不太确定出了什么问题。任何建议将不胜感激,并提前致谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)