Hello all,

I'm very new on OpenGL and at this beginning I've found it very complex. I would think C++ is the most complex language but it's better.

Anyway, the code below is for rendering my first triangle. Please take a look:

Code :
#include <glad/glad.h> 
#include <GLFW/glfw3.h>
#include <C:\Users\Abbasi\Desktop\std_lib_facilities_4.h>
using namespace std;
 
 
//*********************************
 
int main()
{
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 
	GLFWwindow* window = glfwCreateWindow(800, 600, "The First Triangle", NULL, NULL);
 
	if (window == NULL)
	{
		cout << "Failed to create GLFW window" << endl;
		glfwTerminate();
		return -1;
	}
 
	glfwMakeContextCurrent(window);
 
 
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) 
	{
		cout << "Failed to initialize GLAD" << endl;
		return -1;
	}
 
	glViewport(0, 0, 700, 500);
 
	float vertices[] = {
	   -0.5f, -0.5f, 0.5f,
		0.5f, -0.5f, 0.5f,
		0.0f,  0.5f, 0.0f
	};
 
  unsigned int VBO;  // Creating a vertex buffer object
  glGenBuffers(1, &VBO);
  glBindBuffer(GL_ARRAY_BUFFER, VBO);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 
    // Creating the Vertex Shader
  const char* vertexShaderSource = "#version 330 core\nlayout (location = 0)"
		"in vec3 aPos;\n\nvoid main()\n{\ngl_Position ="
		"vec4(aPos.x, aPos.y, aPos.z, 1.0);\n}\n\0";
 
  unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
  glCompileShader(vertexShader);
 
   //check the vertex shader compilation error(s)
  int success;
  char infoLog[512];
  glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  if (!success)
  {
	  glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
	  cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << endl;
  }
 
   // Creating the Fragment Shader
  const char* fragmentShaderSource = "#version 330 core\n"
	  "out vec4 FragColor;\n\nvoid main()\n{\n"
	  "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n}\n\0";
 
  unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
  glCompileShader(fragmentShader);
 
  //check the fragment shader compilation error(s)
  glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  if (!success)
  {
	  glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
	  cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << endl;
  }
 
    // Linking both shaders into a shader program for rendering
  unsigned int shaderProgram = glCreateProgram();
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glLinkProgram(shaderProgram);
 
  //check the shader program linking error(s)
  glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  if (!success)
  {
	  glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
	  cout << "ERROR::PROGRAM::SHADER::LINKING_FAILED\n" << infoLog << endl;
  }
 
  glUseProgram(shaderProgram);
 
    // We no longer need the prior shaders after the linking
  glDeleteShader(vertexShader);
  glDeleteShader(fragmentShader);
 
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  glEnableVertexAttribArray(0);
 
  unsigned int VAO;
  glGenVertexArrays(1, &VAO);
  glBindVertexArray(VAO);
  glDrawArrays(GL_TRIANGLES, 0, 3);
 
  system("pause");
	return 0;
}

The output is:

Click image for larger version. 

Name:	1.jpg 
Views:	18 
Size:	11.9 KB 
ID:	2543

What is the problem of this code that doesn't show the triangle, please?