| Index || Me |

OpenGL with C

Installation

Under Debian/Ubuntu install the library with:

sudo apt-get install libglfw3-dev libglfw3-doc

After that you have to set the C-flags for the new library.

pkg-config --libs --cflags glfw3

Create a file named opengl.c and insert

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

You should be now able to compile the test with

gcc opengl.c -lglfw -lGL

It just creates a test window so you can see that everything works as it's supposed to. If you get undefined errors while compiling, try to use this makefile, which links against more libraries:

opengl: main.o glad.o
	gcc -o opengl main.o -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl -lXxf86vm

main.o: main.c
	gcc -c main.c
glad.o: glad.c
	gcc -c glad.c

GLAD

GLAD is an open source library that manages the function pointers for OpenGL. The installation method is somewhat different from regular libraries: it has a web interface http://glad.dav1d.de/ where you can get the library that you specified with your inputs.

Type

Language: C/C++
Spec.: OpenGL
Profile: Core
API - gl: Version 4.5
Option - generate a loader: Yes

Or use the version that you currently have installed - check glxinfo output!

Next copy the folders glad and KHR contained in the zip-file to your include directory (/usr/include) and glad.c to your project directory.

If you include it in your project, <glad/glad.h> must always be included before <GLFW/glfw3.h> and other OpenGL header files. This is because it needs to be initialized before you call any OpenGL function.

Also include "glad.c", which is the loader you generated over the webinterface, for everything to work on your project.