0

I had started studying Opengl from tutorials and for that I used GLFW and GLAD. I created this code to draw a simple triangle:

#include <Glad/glad.h>
#include <Glfw/glfw3.h>

#include <iostream>

void error_callback(int error, const char* description) {
  std::cout << description << std::endl;
  std::cout << error << std::endl;
}

int main() {
  GLFWwindow* window;

  if (!glfwInit()) {
    std::cout << "Glfw init failed!" << std::endl;
    return -1;
  }
  glfwSetErrorCallback(error_callback);

  window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);

  if (!window) {
    glfwTerminate();
    std::cout << "Failed to load glfw window!" << std::endl;
    return -1;
  }
  glfwMakeContextCurrent(window);

  if( !gladLoadGLLoader((GLADloadproc) glfwGetProcAddress) ) {
    std::cout << "Failed to load Opengl Context!" << std::endl;
    return -1;
  }

  float positions[6] = {
    -0.5f, -0.5f,
     0.0f,  0.5f,
     0.5f, -0.5f
  };

  unsigned int buffer;
  glGenBuffers(1, &buffer);
  std::cout << "Buffer generated successfully" << std::endl;

  glBindBuffer(GL_ARRAY_BUFFER, buffer);
  glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

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

  while (!glfwWindowShouldClose(window)) {

    glClear(GL_COLOR_BUFFER_BIT);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glfwSwapBuffers(window);

    glfwPollEvents();
  }

  glfwTerminate();
  return 0;
}

And the executable is produced by this makefile:

VER = -std=c++17
DEPDIR = -IInclude
LIBDIR = -LLibraries
LIBS = -lglfw3 -lopengl32 -lglu32 -lgdi32 -luser32 -lkernel32

main: main.cpp
    g++ $(VER) -Wall main.cpp glad.c -o main $(DEPDIR) $(LIBDIR) $(LIBS)

clean:
    rm *.o main

The code is compiled without any error, but when I run the program it crashes.

I also noticed that the console does not print the message that comes after the glGenBuffers () function.

I researched issues similar to this one, but none of them solved the problem.

The version of Opengl is 1.4

0 Answers0