Jump to content
  • Advertisement
test opty

OpenGL My first triangle

Recommended Posts

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:


#include <glad/glad.h> 
#include <GLFW/glfw3.h>
#include <C:\Users\Abbasi\Desktop\std_lib_facilities_4.h>
using namespace std;


int main()

	GLFWwindow* window = glfwCreateWindow(800, 600, "The First Triangle", NULL, NULL);

	if (window == NULL)
		cout << "Failed to create GLFW window" << endl;
		return -1;


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

   //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);

  //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);

  //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;


    // We no longer need the prior shaders after the linking

  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  unsigned int VAO;
  glGenVertexArrays(1, &VAO);
  glDrawArrays(GL_TRIANGLES, 0, 3);

	return 0;

the output is the following image. My questions are:

1- why doesn't the code render the triangle which is meant in the code please?

2- Apart from that part, is the code standard? That is is the code the one a teacher would write for a student to be well written and good code?



Share this post

Link to post
Share on other sites

You need to call glfwSwapBuffers(window) at the end of your function to present the rendered result to the screen.

Edited by Styves

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!