Jump to content
  • Advertisement
test opty

OpenGL My first triangle

This topic is 393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!