几何着色器错误地解释为顶点着色器

时间:2014-03-12 20:06:14

标签: glsl shader opengl-3 geometry-shader

我正在努力让我的简单几何着色器工作。但我有一些奇怪的错误信息。似乎opengl认为我的几何着色器是我的顶点着色器 这是错误消息:

  

0℃; 10 -10 :错误C7575:OpenGL不允许顶点着色器中的输入块

但是我的顶点着色器没有输入块。

这是我的顶点着色器

#version 430 

in vec4 s_vPosition;
in vec4 s_vColor;
uniform mat4 mF; //final matrix = mP*mV*mDneu*mM

out VS_OUT
{
    vec4 color;     //sent color out to next stage
} vs_out;

void main (void) {
    vs_out.color = s_vColor;
    gl_Position = mF*s_vPosition;
}

几何着色器

#version 430 

layout(triangles) in;
layout(triangle_strip) out;
layout(max_vertices = 3) out;

in VS_OUT
{
    vec4 color;
} gs_in[];

out GS_OUT 
{
    vec4 color;
} gs_out;

 void main(void)
{
   for(int i = 0; i < gl_in.length(); i++)
  {
    gl_Position = gl_in[i].gl_Position;

    gs_out.color = gs_in[i].color;
    EmitVertex();
  }
  EndPrimitive();
}

fragmentShader

#version 430                                                  

in GS_OUT                                             
{                                                     
    vec4 color;                                       
} fs_in;                                              

out vec4 fColor;

void main(void)                                       
{                                                    
    fColor.r = abs(fs_in.color.r);
    fColor.g = abs(fs_in.color.g);
    fColor.b = abs(fs_in.color.b);
    fColor.a = 1.0f;                              
}

当我运行程序时,几何着色器无法编译。只有当我摆脱输入块时,它才会编译。

我也认识到,功能......

// Find the position of the variables in the shader
positionID = glGetAttribLocation(shaderProgramID, "s_vPosition");
colorID = glGetAttribLocation(shaderProgramID, "s_vColor");
cout <<"positionID: "<< (int)positionID << endl;
cout <<"colorID   : "<< (int)colorID << endl;

...当未包含几何着色器时返回0和1(对于positionID和colorID),并且当它包含在着色器程序中时返回0和-1。 所以我觉得有些事情出了问题,但我不知道它会是什么 请帮助:)

我不知道它是否有用,但这是制作着色器的代码。

首先,我从文本文件中获取着色器代码。然后我调用函数来制作着色器和着色器程序。

// Make a shader
    char* vertexShaderSourceCode = readFile("vertexShader.vsh");
    char* geometryShaderSourceCode = readFile("geometryShader.gsh");
    char* fragmentShaderSourceCode = readFile("fragmentShader.fsh");
    GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode);
    GLuint geometryShaderID = makeVertexShader(geometryShaderSourceCode);
    GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode);
    shaderProgramID = makeShaderProgram(vertShaderID, geometryShaderID, fragShaderID);

以下是制作着色器的功能。

static char* readFile(const char* filename) {
    // Open the file
    FILE* fp = fopen(filename, "r");
    // Move the file pointer to the end of the file and determing the length
    fseek(fp, 0, SEEK_END);
    long file_length = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    char* contents = new char[file_length + 1];
    // zero out memory
    for (int i = 0; i < file_length + 1; i++) {
        contents[i] = 0;
    }
    // Here's the actual read
    fread(contents, 1, file_length, fp);
    // This is how you denote the end of a string in C
    contents[file_length + 1] = '\0';
    fclose(fp);
    return contents;
}

bool compiledStatus(GLint shaderID){
    GLint compiled = 0;
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
    if (compiled) {
        return true;
    }
    else {
        GLint logLength;
        glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLength);
        char* msgBuffer = new char[logLength];
        glGetShaderInfoLog(shaderID, logLength, NULL, msgBuffer);
        printf("%s\n", msgBuffer);
        delete (msgBuffer);
        return false;
    }
}

GLuint makeVertexShader(const char* shaderSource) {
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(vertexShaderID);
    bool compiledCorrectly = compiledStatus(vertexShaderID);
    if (compiledCorrectly) {
        return vertexShaderID;
    }
    return -1;
}

GLuint makeGeometryShader(const char* shaderSource){
    GLuint geometryShaderID = glCreateShader(GL_GEOMETRY_SHADER);
    glShaderSource(geometryShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(geometryShaderID);
    bool compiledCorrectly = compiledStatus(geometryShaderID);
    if (compiledCorrectly) {
        return geometryShaderID;
    }
    return -1;
}

GLuint makeFragmentShader(const char* shaderSource) {
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(fragmentShaderID);
    bool compiledCorrectly = compiledStatus(fragmentShaderID);
    if (compiledCorrectly) {
        return fragmentShaderID;
    }
    return -1;
}

GLuint makeShaderProgram(GLuint vertexShaderID, GLuint geometryShaderID, GLuint fragmentShaderID) {
    GLuint shaderID = glCreateProgram();
    glAttachShader(shaderID, vertexShaderID);
    glAttachShader(shaderID, geometryShaderID);
    glAttachShader(shaderID, fragmentShaderID);
    glLinkProgram(shaderID);
    return shaderID;
}

1 个答案:

答案 0 :(得分:1)

GLuint geometryShaderID = makeVertexShader (geometryShaderSourceCode); ??