Question

I have seen similar questions to mine else where but none have answered or fixed my problem.

I have GLEW initiated properly, right after the Context creation but before I call glfwMakeContextCurrent()

After I have initiated I try to use glGenBuffers() but it doesn't work. Throws an error.

My OpenGL version is 3.2 so from what I have read I can use this functionality in my program. Please to let me know otherwise though if I can't. I will try to figure out a different way to do this.

I am using Windows 7 and VS2012, and it seems that everything is linked properly. Hope I provided all the info I needed.

#include <stdlib.h>

#include <stdio.h>

#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include <thread>

int main(){

//Initialize GLFW
if(!glfwInit()){
    printf("GLFW was not initialized successfully!\n");
    std::this_thread::sleep_for(std::chrono::seconds(5));
    exit(EXIT_FAILURE);
}
else{
    printf("GLFW was initialized successfully!\n");
}

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

GLFWwindow* window = glfwCreateWindow(800, 600, "PONG!", nullptr, nullptr); // Windowed

glewExperimental = GL_TRUE;
if(!glewInit()){
    printf("GLEW was not initialized successfully!\n");
    std::this_thread::sleep_for(std::chrono::seconds(5));
    exit(EXIT_FAILURE);
}
else{
    printf("GLEW was initialized successfully!\n");
}

glfwMakeContextCurrent(window);

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);

printf("%u\n", vertexbuffer);

fprintf(
    stdout,
    "INFO: The OpenGL version is: %s\n",
    glGetString(GL_VERSION)
);

fprintf(
    stdout,
    "INFO: The GLEW version is: %s\n",
    glewGetString(GLEW_VERSION)
);

while(!glfwWindowShouldClose(window)){
    glfwSwapBuffers(window);
    glfwPollEvents();

    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
        glfwDestroyWindow(window);
        window = glfwCreateWindow(800, 600, "PONG!", nullptr, nullptr);
    }
    else if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS){
        glfwDestroyWindow(window);
        window = glfwCreateWindow(800, 600, "PONG!", glfwGetPrimaryMonitor(), nullptr);
    }
}
}
Was it helpful?

Solution

I have GLEW initiated properly, right after the Context creation but before I call glfwMakeContextCurrent()

Welp, there's your problem right there:

Successful creation does not change which context is current. Before you can use the newly created context, you need to make it current using glfwMakeContextCurrent.

Call glewInit() after glfwMakeContextCurrent().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top