Home > OS >  Access violation occurred in glfwSwapBuffers()
Access violation occurred in glfwSwapBuffers()

Time:04-03

I'm making an OpenGL program using version 4.6, but when I wrote the texture code and ran the program, the following error occurred in glfwSwapBuffers(): Exception thrown at 0x00007FFCCD631970 (nvoglv64.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

Here's my code :

#include"shader.h"
#include"texture.h"

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

int main(){
    
    if (glfwInit() == GLFW_FALSE) {
        std::cout << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

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

    GLFWwindow* window = glfwCreateWindow(1280, 800, "What a TD", nullptr, nullptr);
    if (window == nullptr) {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    if (gladLoadGL() != 1) {
        std::cout << "Failed to load GLAD" << std::endl;
        return -1;
    }

    glViewport(0, 0, 1280, 800);

    GLfloat vertices[] = {
        -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f
    };

    GLuint indices[] = {
        0, 1, 2,
        2, 3, 0
    };

    GLuint vbo, vao, ebo;

    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
    glGenBuffers(1, &ebo);
    
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    
    texture tex0("planks.png", 0);
    
    shader Shader("default.vs", "default.fs");
    
    //main loop
    while (!glfwWindowShouldClose(window))
    {
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        Shader.use();
        tex0.active();
        glBindVertexArray(vao);
        glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(GLuint), GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window); // Access violation
        glfwPollEvents();
    }
    ...
}

vertex shader :

#version 460 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;

out vec3 Color;
out vec2 texCoord;

void main() {
    gl_Position = vec4(aPos, 1.0f);
    Color = aColor;
    texCoord = aTexCoord;
}

fragment shader :

#version 460 core

in vec3 Color;
in vec2 texCoord;

out vec4 fragColor;

uniform sampler2D tex0;

void main() {
    fragColor = texture(tex0, texCoord);
}

texture.h :

#ifndef GL_TEXTURE_H
#define GL_TEXTURE_H

#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<stb/stb_image.h> //STB_IMAGE_IMPLEMENTATION defined in other file

#include<iostream>

class texture {
private:
    GLuint ID;
    GLuint unit;
public:
    texture(const char* image, GLuint unit)  {
        stbi_set_flip_vertically_on_load(true);

        this->unit = unit;

        glGenTextures(1, &ID);
        glActiveTexture(GL_TEXTURE0   unit);
        glBindTexture(GL_TEXTURE_2D, ID);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        int width, height, channels;
        unsigned char* data = stbi_load(image, &width, &height, &channels, 0);
        if (data) {
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
        }
        else {
            std::cout << image << " : " << "Failed to load image" << std::endl;
        }
    
        glGenerateMipmap(GL_TEXTURE_2D);
    
        stbi_image_free(data);
        glBindTexture(GL_TEXTURE_2D, 0);
    }

    void active() {
        glActiveTexture(GL_TEXTURE0   unit);
        glBindTexture(GL_TEXTURE_2D, ID);
    }
};

#endif

How can I fix this problem?

CodePudding user response:

The Index Buffer (ELEMENT_ARRAY_BUFFER) binding is stored within the Vertex Array Object. When glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO) is called the element buffer object ID is stored in the currently bound Vertex Array Object.
Calling glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); when the VAO is bound breaks that binding and the index buffer is no longer bound to the VAO. Remove this line of code, you don't need it at all.

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

Unlike to the Index Buffer, the Vertex Buffer binding (ARRAY_BUFFER) is a global state.
Each attribute which is stated in the VAOs state vector may refer to a different ARRAY_BUFFER. When glVertexAttribPointer is called the buffer which is currently bound to the target ARRAY_BUFFER, is associated to the specified attribute index and the ID of the object is stored in the state vector of the currently bound VAO. Therefore there is no problem calling glBindBuffer(GL_ARRAY_BUFFER, 0);

  • Related