[C++] Coloring a triangle in GLFW [SOLVED]

So, I’m following this course and I made it to 28:28, and I really thought I followed everything correctly up until this point but the triangle displayed in my application is white instead of the orange shown in the video, I tried backtracking to see what I did wrong but no luck…

This is my first time working with OpenGL so I still don’t understand how many parts of the code work, if someone could point me to the line(s) responsible for coloring the vertices I’d be thankful, I mean, I hope it’s there, I’m pretty sure I didn’t miss a line so it should only be something related to passing arguments.

Sorry for the large block of code:

#include <iostream>
#include "glad/glad.h"
#include "GLFW/glfw3.h"

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0;)\n"
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"    FragColor = vec4(0.8f, 0.3f, 0.02, 1.0;)\n"

int main() {

	//Initialize GLFW.

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Specify GLFW version (3.3).
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Specify we're using CORE profile (Modern functions).

	GLfloat vertices[] = {
		-0.5f,-0.5f * float(sqrt(3)) / 3, 0.0f,
		0.5f,-0.5f * float(sqrt(3)) / 3, 0.0f,
		0.0f, 0.5f * float(sqrt(3)) * 2 / 3, 0.0f

	GLFWwindow* window = glfwCreateWindow(800, 800, "Window Name Here!", NULL, NULL); //Create Window
	if (window == NULL) {

		std::cout << "Failed to create GLFW window" << std::endl;
		return -1;

	glfwMakeContextCurrent(window); //Introduce window to current context, allowing us to use it.

	gladLoadGL(); //Load GLAD to configure OpenGL.
	glViewport(0, 0, 800, 800); //Specify view port.

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

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

	GLuint shaderProgram = glCreateProgram();

	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);


	GLuint VAO, VBO;

	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);


	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glClearColor(0.07f, 0.13f, 0.17f, 1.0f); //Specify background color.
	glClear(GL_COLOR_BUFFER_BIT); //Clean buffer and assign new color.
	glfwSwapBuffers(window); //Swap back and front buffer.

	while (!glfwWindowShouldClose(window)) { //Main loop.

		glClearColor(0.07f, 0.13f, 0.17f, 1.0f); //Specify background color.
		glClear(GL_COLOR_BUFFER_BIT); //Clean buffer and assign new color.
		glDrawArrays(GL_TRIANGLES, 0, 3);

		//Handle GLFW events here.


	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);

	glfwDestroyWindow(window); //Delete Window
	glfwTerminate(); //Terminate GLFW and end program.


You’ve got semicolons inside the vec4 constructors, like 1.0;). Need to be after, like 1.0);

I think that’s only the issue at first glance – hard to see issues from just looking at shader code (hard to see issues when running code as well, shaders are a nightmare to debug…)

1 Like

Yup, that was it, it’s always something as trivial as a semi-colon.
Thank you!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.