GLSL着色器不起作用。没有编译错误,并使用了glUseProgram

时间:2017-08-24 00:31:19

标签: c++ opengl glsl

我想出了渲染矩形的代码,但着色器不起作用。它仍然是空白的白色。

这里我将包含重要的代码

主:

float verts[] = {
    -.5f, -.5f, .0f,
    -.5f,  .5f, .0f,
     .5f,  .5f, .0f,
     .5f,  .5f, .0f,
     .5f, -.5f, .0f,
    -.5f, -.5f, .0f
};

Shader shader("basicVert.glsl", "basicFrag.glsl");

GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

shader.enable();

Shader.cpp(类函数)

Shader::Shader(const string vpath, const string fpath) {
    Shader();
    current_vpath = vpath;
    current_fpath = fpath;
    shaderID = init();
}

Shader::Shader(const char *vpath, const char *fpath) {
    Shader(string(vpath), string(fpath));
}

Shader::~Shader() {
    shaderID = NULL;
    glDeleteProgram(shaderID);
}

void Shader::enable() {
    glUseProgram(shaderID);
}

GLuint Shader::makeVertextShader(const char* source) {
    GLuint vertShaderID = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertShaderID, 1, &source, NULL);
    glCompileShader(vertShaderID);
    GLint r;
    glGetShaderiv(vertShaderID, GL_COMPILE_STATUS, &r);
    if (r == GL_FALSE) {
        GLint l;
        glGetShaderiv(vertShaderID, GL_INFO_LOG_LENGTH, &l);
        cout << l << endl;
        char *bfer = new char[l];
        glGetShaderInfoLog(vertShaderID, l, &l, bfer);
        cerr << "Failed to compile VERTEXT SHADER! FILE NAME: " << 
        current_vpath << endl;
        cerr << bfer << endl;
        glDeleteShader(vertShaderID);
        delete[] bfer;
        return NULL;
    }
    return vertShaderID;
}

GLuint Shader::makeFragmentShader(const char* source) {
  GLuint fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragShaderID, 1, &source, NULL);
  glCompileShader(fragShaderID);
  GLint r;
  glGetShaderiv(fragShaderID, GL_COMPILE_STATUS, &r);
  if (r == GL_FALSE) {
      GLint l;
      glGetShaderiv(fragShaderID, GL_INFO_LOG_LENGTH, &l);
      char *bfer = new char[l];
      glGetShaderInfoLog(fragShaderID, l, &l, bfer);
      cerr << "Failed to compile FRAGMENT SHADER! FILE NAME: " << 
              current_fpath << endl;
      cerr << bfer << endl;
      glDeleteShader(fragShaderID);
      delete[] bfer;
      return NULL;
  }
  return fragShaderID;
}

GLuint Shader::init() {
    GLuint program = glCreateProgram();
    const string vs = readFile(current_vpath);
    const string vf = readFile(current_fpath);

    const char *vertexsrc = vs.c_str();
    const char *fragmentsrc = vf.c_str();

    GLuint vertShaderID = this->makeVertextShader(vertexsrc);
    GLuint fragShaderID = this->makeFragmentShader(fragmentsrc);

    glAttachShader(program, vertShaderID);
    glAttachShader(program, fragShaderID);
    glLinkProgram(program);
    glValidateProgram(program);
    glDeleteShader(vertShaderID);
    glDeleteShader(fragShaderID);
    return program;
}

GLSL顶点着色器

#version 330 core

layout(location = 0) in vec3 position;

void main(){

    gl_Position = position;

}

GLSL片段着色器

#version 330 core

layout(location = 0) out vec4 color;

void main(){

    color = vec4(1.0, 0.0, 1.0, 1.0);
    gl_FragColor = color;

}

3 个答案:

答案 0 :(得分:2)

现代版本的GLSL

不再支持

gl_FragColor

所以它将在Vertex Shader中,

layout(location = 0) in vec4 position;

void main()
{
  gl_Position = position;
}
在FS中

layout(location = 0) out vec4 color;

void main()
{
color = vec4(1.0, 0.0, 1.0, 1.0);
//gl_FragColor is no longer supported in modern versions of GLSL  
}

答案 1 :(得分:1)

顶点和片段着色器必须如下所示:

#version 330 core

layout(location = 0) in vec3 position;

void main()
{
    gl_Position = vec4( position.xyz, 1.0 );
}
#version 330 core

layout(location = 0) out vec4 color;

void main()
{
    color = vec4(1.0, 0.0, 1.0, 1.0);
}

说明:

您的代码中有2个问题:

1。)虽然Vertex Shader中的顶点属性position的类型为vec3,但Built-in Variable (GLSL) gl_Position的类型为vec4 }。
要么必须更改顶点属性的类型:

layout(location = 0) in vec4 position;

或者gl_Position的作业必须进行调整:

gl_Position = vec4( position.xyz, 1.0 );

2.。在Fragment Shader中,可以使用Built-in output Variable (GLSL) gl_FragColor

void main()
{
    gl_FragColor = [...];
}

或必须声明显式输出变量:

out vec4 color;

void main()
{
    color = [...];
}

答案 2 :(得分:0)

问题似乎在于顶点着色器:您将vec3 position传递给vec4 gl_Position,它有效地将w坐标设置为0.正确渲染所需的是齐次坐标为1(否则w除以0除以)。尝试将代码更改为

gl_Position = vec4(position, 1.0);