How to correctly initialize GLEW when creating a library?

0

I created a library to use with OpenGL whose code is provided here.

It compiles fine and nothing seems to be wrong but when I use it with a test program then it shows an exception, Access violation at location 0x00000000 ( which is obviously due to GLEW not being initialized ).

But GLEW is already initialized in the library see the function definition for LIB_CreateWindow in window.c from the source code in the provided link above.

Also, if I try to draw something using the library with OpenGL it shows just a blank screen.

Code for the test program:- (main.c)

#define GLEW_STATIC
#include <GL/glew.h>
#include <window.h>
#include <stddef.h>
#include <lib_window.h>
#include <lib_test_draw.h>

int main()
{
    // Initialize the library...
    LIB_Initialize();

    // Get center position...
    LIB_Vector2i size; LIB_GetDisplaySize(&size);
    // Create the window...
    LIB_Window * window = LIB_CreateWindow("BLITZ Test", LIB_CreateVector2i((size.x - 1024) / 2, (size.y - 768) / 2),
        LIB_CreateVector2i(1024, 768), LIB_FALSE, LIB_FALSE, LIB_DRAWMODE_FIXED, LIB_USE_DEFAULT_CONTEXT_SETTINGS);

    // Adding these two lines again removes the exception...
    glewExperimental = GL_TRUE;
    glewInit(); // Why initialize GLEW again?

    // Use OpenGL to do cool stuff...
    LIB_TemporaryCreate();

    while (LIB_IsWindowOpened(window))
    {
        LIB_BeginRender(LIB_SetColorRGB(0, 0, 0));
        // Draw it all here...
        LIB_TemporaryRender();
        LIB_EndRender(window);
    }
    // Destroy the current window...
    LIB_DestroyWindow(window);
    // Terminate library...
    LIB_Terminate();
    return 0;
}

I created a test file in the library to test whether it can draw anything if called from the library itself but it just shows blank screen but there is no exception.

lib_test_draw.h

#ifndef LIB_TEST_DRAW
#define LIB_TEST_DRAW

#define GLEW_STATIC
#include <GL/glew.h>
#include <stddef.h>
#include <stdio.h>

const char * vertex_src =
"#version 330 core\n"
"layout (location = 0) in vec3 pos;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(pos, 0.0f);\n"
"}\0";

const char * fragment_src =
"#version 330 core\n"
"out vec4 Color;\n"
"void main()\n"
"{\n"
"Color = vec4(1.0f, 0.0f, 0.0f, 1.0f);\n"
"}\0";

GLuint shdr_prg;
GLuint VAO;
GLuint VBO;

void CreateProgram(const char * vertex_shdr_src, const char * frag_shdr_src, GLuint * prg)
{
    GLuint vertex_shdr = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shdr, 1, &vertex_shdr_src, NULL);
    glCompileShader(vertex_shdr);

    GLuint frag_shdr = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(frag_shdr, 1, &frag_shdr_src, NULL);
    glCompileShader(frag_shdr);

    // Checking if glCreateProgram was successful.
    if (glCreateProgram() == 0)
    {
        printf("Error creating program!\n");
    }

    *prg = glCreateProgram();
    glAttachShader(*prg, vertex_shdr);
    glAttachShader(*prg, frag_shdr);
    glLinkProgram(*prg);

    glDeleteShader(vertex_shdr);
    glDeleteShader(frag_shdr);
}

void LIB_TemporaryCreate()
{
    CreateProgram(vertex_src, fragment_src, &shdr_prg);
    GLfloat vertices[] =
    {
        -0.5f,  -0.5f,  0.0f,
         0.5f,  -0.5f,  0.0f,
         0.0f,   0.5f,  0.0f
    };

    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);
}

void LIB_TemporaryRender()
{
    glUseProgram(shdr_prg);
    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(0);
}

#endif /* LIB_TEST_DRAW */
c
opengl
glew
asked on Stack Overflow Mar 27, 2018 by Ruks • edited Apr 1, 2018 by Ruks

1 Answer

1

Okay,... figured out the problem,

The GLEW library needs to be initialized outside the source file(which is not accessible by the program which is using it!).

That is why you need to initialize GLEW again because once it is compiled, the program can't find(nor access) the initialization done inside the source file of the library.

You can!, however initialize it in the header file to work!

Thanks to @Spektre for helping me point out this problem.

answered on Stack Overflow Apr 7, 2018 by Ruks • edited Nov 13, 2018 by Ruks

User contributions licensed under CC BY-SA 3.0