简体   繁体   English

在 CobwebDiagram.exe 中的 0x00000000 处引发异常:0xC0000005:访问冲突执行位置 0x00000000

[英]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 .所以当我使用 function glGenBuffer时抛出异常。 can anyone help me to fix it?谁能帮我修复它?

PS I am using glad instead of glew. PS我使用高兴而不是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;
}

gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) needs to be called before the very first OpenGL instruction.需要在第一个 OpenGL 指令之前调用gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) Call it right after glfwMakeContextCurrent(window) :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) . 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);
      }
)";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Project0_opengl.exe 中的 0x00000000 处抛出异常:0xC0000005:访问冲突执行位置 0x00000000 - Exception thrown at 0x00000000 in Project0_opengl.exe: 0xC0000005: Access violation executing location 0x00000000 为什么我在 ConsoleApplication1.exe 中的 0x00000000 处出现异常异常:0xC0000005:访问冲突执行位置 0x00000000 - Why do I get an Exception Exception thrown at 0x00000000 in ConsoleApplication1.exe: 0xC0000005: Access violation executing location 0x00000000 0xC0000005:访问冲突执行位置0x00000000 - 0xC0000005: Access violation executing location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 在0xC0000005中的0x6ececafa出现未处理的异常:访问冲突写入位置0x00000000 - Unhandled Exception as at 0x6ececafa in 0xC0000005 : Access violation writing location 0x00000000 NVIDIA未处理的异常,位于0x002a2da2中 <work.exe> 0xC0000005:访问冲突读取位置0x00000000 - NVIDIA Unhandled exception at 0x002a2da2 in <work.exe>0xC0000005: Access violation reading location 0x00000000 Project4.exe中0x00998876处的首次机会异常:0xC0000005:访问冲突写入位置0x00000000 - First-chance exception at 0x00998876 in Project4.exe: 0xC0000005: Access violation writing location 0x00000000 Engine.exe中0x00000000的未处理异常:0xC0000005:访问冲突 - Unhandled exception at 0x00000000 in Engine.exe: 0xC0000005: Access violation tester.exe中0x00b21840处未处理的异常:0xC0000005:访问冲突读取位置0x00000000 - Unhandled exception at 0x00b21840 in tester.exe: 0xC0000005: Access violation reading location 0x00000000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM