Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exception thrown at 0x00000000 in CobwebDiagram.exe: 0xC0000005: Access violation executing location 0x00000000

so I got an exception thrown when I used the function glGenBuffer. can anyone help me to fix it?

P.S. I am using glad instead of glew.

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

#include <iostream>

using namespace std;

unsigned int createShader(unsigned int shadertype, const char* shaderSource) {
  unsigned int shader;
  shader = glCreateShader(shadertype);
  glShaderSource(shader, 1, &shaderSource, NULL);
  glCompileShader(shader);

  int status;
  char info[512];
  glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  if (!status) {
    glGetShaderInfoLog(shader, 512, NULL, info);
    cout << "FAILED TO COMPILE!\n" << info << '\n';
    glDeleteShader(shader);
  }

  return shader;
}

unsigned int createShaderProgram(const char* VshaderSrc, const char* FshaderSrc) {
  unsigned int vertexShader = createShader(GL_VERTEX_SHADER, FshaderSrc);
  unsigned int fragmentShader = createShader(GL_FRAGMENT_SHADER, FshaderSrc);

  unsigned int shaderProgram;
  shaderProgram = glCreateProgram();

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

  int status;
  char info[512];
  glGetProgramiv(shaderProgram, GL_LINK_STATUS, &status);
  if (!status) {
    glGetProgramInfoLog(shaderProgram, 512, NULL, info);
    cout << "FAILED TO LINK PROGRAM\n" << info << '\n';
  }

  return shaderProgram;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
  glViewport(0, 0, width, height);
}

void closeWindow(GLFWwindow* window) {
  if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
    glfwSetWindowShouldClose(window, true);
  }
}

void cobwebDiagmramForLogistics(float x0, float miu, float* ptr) {
  *(ptr) = x0;
  *(ptr + 1) = 0.0f;
  *(ptr + 2) = 0.0f;

  *(ptr + 3) = x0;
  *(ptr + 4) = miu * x0 * (1.0f - x0);
  *(ptr + 5) = 0.0f;

  float x = x0;

  for (int i = 1; i < 100; i++) {
    float horPlot = miu * x * (1.0f - x);

    *(ptr + i * 6) = horPlot;
    *(ptr + i * 6 + 1) = horPlot;
    *(ptr + i * 6 + 2) = 0.0f;

    float verPlot = miu * horPlot * (1.0f - horPlot);

    *(ptr + i * 6 + 3) = horPlot;
    *(ptr + i * 6 + 4) = verPlot;
    *(ptr + i * 6 + 5) = 0.0f;

    x = verPlot;
  }
}

int main() {
  float CobwebPlot[606];
  cobwebDiagmramForLogistics(0.2f, 3.6f, &CobwebPlot[0]);
  unsigned int positions[1212];
  for (int i = 0; i < 606; i++) {
    positions[i] = i;
    positions[i + 1] = i + 1;
  }

  glfwInit();
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  GLFWwindow* window = glfwCreateWindow(800, 600, "Cobweb_Diagram", NULL, NULL);

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

  glfwMakeContextCurrent(window);

  unsigned int cobwebBuffer;
  unsigned int bufferArray;
  unsigned int elementArray;
  glGenVertexArrays(1, &bufferArray);
  glGenBuffers(1, &cobwebBuffer);
  glGenBuffers(1, &elementArray);

  glBindBuffer(GL_ARRAY_BUFFER, cobwebBuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(CobwebPlot), CobwebPlot, GL_STATIC_DRAW);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArray);
  glBufferData(GL_ELEMENT_ARRAY_BARRIER_BIT, sizeof(positions), positions,
               GL_STATIC_DRAW);

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

  const char* vertexShaderSource =
      "#version 330 core\n"
      "layout (location = 0) in vec3 aPos;\n"
      "void main()\n"
      "{\n"
      "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
      "}\0";

  const char* fragmentShaderSource =
      "#version 330 core\n"
      "out vec4 lineColor;\n"
      "void main()\n"
      "{\n"
      "    lineColor = vec4(1.0f, 0.1f, 0.2f, 1.0f)"
      "}\n";

  unsigned int shaderProgram =
      createShaderProgram(vertexShaderSource, fragmentShaderSource);
  glUseProgram(shaderProgram);

  if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
    cout << "Failed to initialize GLAD" << endl;
    return -1;
  }

  glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

  while (!glfwWindowShouldClose(window)) {
    closeWindow(window);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(bufferArray);
    glDrawElements(GL_LINE_STRIP, 1212, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  glfwTerminate();
  return 0;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) needs to be called before the very first OpenGL instruction. Call it right after glfwMakeContextCurrent(window):

glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
    cout << "Failed to initialize GLAD" << endl;
    return -1;
}

There is also a problem in your shader code. A semicolon is missing at the end of lineColor = vec4(1.0f, 0.1f, 0.2f, 1.0f). I suggest using raw string literals:

const char* fragmentShaderSource = R"(
      #version 330 core
      out vec4 lineColor;
      void main()
      {
          lineColor = vec4(1.0f, 0.1f, 0.2f, 1.0f);
      }
)";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading