Search code examples
c++openglglutglfw

glCreateShader(GL_VERTEX_SHADER); access violation executing location


I am trying to draw some text and while trying to create the shader program for the text I get an access violation executing location error from GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

start of main Function where problem is:

int main(int argc, char **argv) {
    //GLTtext* text = gltCreateText();
    //gltSetText(text, "ElectroCraft");

    //GLFWwindow* window;
    int width = 860, height = 490;
    if (!glfwInit()) {
        printf("failed to init glfw");
        return -1;
    }
    glutInit(&argc, argv);
    window = glfwCreateWindow(width, height, "ElectroCraft", NULL, NULL);
    glfwMakeContextCurrent(window);
    if (!window) {
        printf("failed to start window");
        glfwTerminate();
        return -1;
    }
    /*if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }*/
    //build and compile shader program
    //-----------------------------------
    //vertex shader
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    //check for shader compile errors
    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    //fragment shader
    int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    //check for shader compile errors
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // link shaders
    int shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    //check for liunking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
...

all Includes as this may be the problem as well:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/freeglut.h>
#include <map>
#include <GL/GL.h>
#include <stdio.h>
#include <string>
#include <glm\glm.hpp>
#include <iostream>
#include <ft2build.h>
//#include <glad.h>
#include FT_FREETYPE_H
using namespace std;

I could be getting this problem as i might not of provided the correct arguments or something else.


Solution

  • You have include glew but you hadn't initialized it.

    Before accessing any GL functions, insert this code:

    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
       /* Problem: glewInit failed, something is seriously wrong. */
       std::cerr << "Error: " << glewGetErrorString(err) << std::endl;
       ...
    }
    std::cerr << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
    

    Reference: GLEW Basics