1
votes

I've just started learning c++ and I'm trying to make a program with shaders that draws a triangle with colors.

This is my vertex shader:

const GLchar* vertexSource =
"#version 150 core\n"
"in vec4 position;"
"in vec4 color;"
"out vec4 Color;"
"out vec4 gl_Position;"
"void main(){"
"   gl_Position = position;"
"   Color = color;"
"}";

and this is my fragment shader:

const GLchar* fragmentSource =
"#version 150 core\n"
"in vec4 Color;"
"out vec4 gl_FragColor"
"void main(){"
"    gl_FragColor = Color;"
"}";

I have a list of values for the co-ords and colors respectively:

float vertices[]{
    -1.0f, -1.0f, 0.0f,1.0f,   1.0f,0.0f,0.0f,1.0f,
    1.0f, -1.0f, 0.0f,1.0f,    0.0f,1.0f,0.0f,1.0f,
    0.0f,  1.0f, 0.0f,1.0f,    0.0f,0.0f,1.0f,1.0f,
};

I initialise a 'vertex buffer object' and a 'vertex array object'

GLuint vao; //Initialise vertex array object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

GLuint vbo; //Initialise vertex buffer object
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

I compile a shader program:

GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);

glUseProgram(shaderProgram);

I define some attributes for the shaders:

glBindAttribLocation(shaderProgram, 0, "position");
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);
glEnableVertexAttribArray(0);

glBindAttribLocation(shaderProgram, 1 ,"color");
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(4 * sizeof(float)));
glEnableVertexAttribArray(1);

and finally I draw the triangle in a 'do, while' loop

glDrawArrays(GL_TRIANGLES, 0, 3);

However, it doesn't print any triangles, but instead just shows a black screen.

I got most of this code from tutorials online, and I'm trying to rewrite everything out to understand how everything works.

Could anyone see where I went wrong? Any advice would be greatly appreciated :)

Edit:

I've added some code to check for errors:

if (status == GL_FALSE)
{
    GLint infoLogLength;
    glGetShaderiv(shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength);

    GLchar* strInfoLog = new GLchar[infoLogLength + 1];
    glGetProgramInfoLog(shaderProgram, infoLogLength, NULL, strInfoLog);

    std::cout << strInfoLog;
    delete[] strInfoLog;
}

...but nothing prints

1
First, /n is not the same as \n. Second, you should check for error messages when you compile the shader. Use glGetShaderInfoLog​(). - Dietrich Epp
You didn't state what you're having trouble with, compilation errors? drawing problems? - John Mark Gabriel Caguicla
it just doesnt draw out, I get a blank screen when i run the program - Adrian Ngai
Right, check for errors. Checking for errors is easy. Asking for help on the internet is a waste of everybody's time if you are not checking for errors. - Dietrich Epp
@AdrianNgai: If you need to add stuff to your question, use the "edit" button. - Nicol Bolas

1 Answers

1
votes

gl_Position is a pre defined built in variable and your GLSL compiler surely is complaining about trying to redefining it and errors out. Similarly gl_FragColor is in the reserved namespace.

Of course your code lacks queries of the shader info log, so you don't see that compiler output; your addendum just queries the program linker log.

The solution is probably as easy as simply omitting the out vec4 gl_Position and out vec4 gl_FragColor from your shader sources.