3

I came up with code to render a rectangle, but the shaders won't work. It still has the blank white color.

Here I will include the important code

Main:

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 (class functions)

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 Vertex Shader

#version 330 core

layout(location = 0) in vec3 position;

void main(){

    gl_Position = position;

}

GLSL Fragment Shader

#version 330 core

layout(location = 0) out vec4 color;

void main(){

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

}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Joseph
  • 127
  • 2
  • 10
  • 2
    Are you calling `glDrawArrays` somewhere? Also without seeing your shaders it's hard to know if the problem is there – gman Aug 24 '17 at 02:38
  • both of your shaders shouldn't compile. Did you actually check the output of your program? Also, your code is lacking checks for link failures. – derhass Aug 24 '17 at 17:15
  • It compiles, and it does output errors if their are any. – Joseph Aug 24 '17 at 18:04
  • There is no gl_FragColor in GLSL 300 – Sung Aug 25 '17 at 03:11
  • unless it happens in your make*shader methods, where are you checking the result of shader compilation? Checking the shader compile and link logs would have helped you find this error – jars Sep 03 '17 at 16:58
  • @Rabbid76 None of these have worked for me yet :( – Joseph Sep 04 '17 at 21:39
  • @Rabbid76 I use `glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);` and `glDrawArrays(GL_TRIANGLES, 0, 6);` – Joseph Sep 06 '17 at 21:37
  • @Rabbid76 Issue has still not been resolved. – Joseph Jan 23 '19 at 22:04

3 Answers3

2

gl_FragColor is no longer supported in modern versions of GLSL

so it will be in Vertex Shader,

layout(location = 0) in vec4 position;

void main()
{
  gl_Position = position;
}

in 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  
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Sung
  • 1,036
  • 1
  • 8
  • 22
1

The vertex and fragment shader have to look like this:

#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);
}

Explanation:

There are 2 issues in your code:

1.) While the vertex attribute position, in the Vertex Shader has the type vec3, the Built-in Variable (GLSL) gl_Position has the type vec4.
Either the type of the vertex attribute has to be changed:

layout(location = 0) in vec4 position;

or the assignment to gl_Position has to be adapted:

gl_Position = vec4( position.xyz, 1.0 );

2.) In the Fragment Shader either can be used the Built-in output Variable (GLSL) gl_FragColor:

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

or an explicit output variable has to be declared:

out vec4 color;

void main()
{
    color = [...];
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

The problem seems to lie in the vertex shader: You pass a vec3 position to a vec4 gl_Position which effectively sets the w-coordinate to 0. What you need for proper rendering is a homogeneous coordinate of 1 (otherwise the division by w is a division by 0). Try to change your code to

gl_Position = vec4(position, 1.0);
BDL
  • 21,052
  • 22
  • 49
  • 55
  • i just added that and it didn't work. Thanks for help anyway – Joseph Aug 24 '17 at 21:01
  • Just as a side-note: You are checking the compile status of your shaders, but not the link status of your program. – BDL Aug 24 '17 at 22:17