Can You Use OpenGL With Pure C?


When you search for tutorials and documentation about OpenGL in the web you will mostly find examples that use C++. But is it also possible to use pure C for programming OpenGL programs?

It is possible to use OpenGL purely with the C programming language. OpenGL is an API which is defined as a set of language independent functions. These functions are called through a binding and implemented either in hardware or software.

In this article you will find out why this works and what an example of this will look like.

How Can You Use OpenGL with Pure C?

OpenGL is an API which is defined as a set of functions. The implementation of this functions can be purely in software, purely in hardware or a mixture of both. Generally it is better to implement it in the hardware (graphics card drivers) because this is way faster. The software implementation is regarded more as a fallback level.

OpenGL Triangles
A Windows Application showing Triangles which were generated with OpenGL

The function declarations in the API look like normal C functions, but they are indeed language independent. They are also cross-platform which makes OpenGL programs very portable.

Although many examples and tutorials use C++, it is possible to use only C. Keep in mind that there may be libraries that you use in conjunction with OpenGL that are written in C++ and can therefore be used in C++ programs only. One example is the GLM Opens in a new tab.library.

OpenGL is a pure graphics API. There is no window management, no input handling and no sound. For all these things you have to either use existing libraries or write your own implementation with the help of your system API (like for example the Windows API).

OpenGL Loading Libraries and C

When you write a program and you want to use OpenGL functionality, you will have to access the OpenGL API functions respectively their implementations. Therefore you have to use one of the OpenGL Loading Libraries. These libraries load pointers to the functions at runtime.

OpenGL Extension Wrangler (GLEW)

The most famous loading library is GLEWOpens in a new tab.. It is written in C and suitable for C/C++ projects. You only have to include the header file and call the initialization function. You can use core and extension OGL functions with this library.

#include <GL/glew.h>
...

int main() 
{
    ...
    GLenum err = glewInit();
    if(GLEW_OK != err) {
        /* Error Handling: Glew did not initialize */
    }
    ..
}

Glad

Glad Opens in a new tab.is an interesting loader. It is the one I use currently for my projects. When you visit the glad homepage, you can chose the language that you want to program in and then it generates a loader for you in this language. This one is based on the official specifications from the Khronos SVN which means that the loader is up to date.

After Generating the loader you can download the .c and .h file and include them in your project. Then you can use them like GLEW and most of the other loaders. You have to initialize the loader as well.

#include <glad/glad.h>
...

int main() 
{
    ...
    /* glfw is used in this example */
    if(!gladLoadGLLoader(GLADloadproc) glfwGetProcAddress)) {
        /* Error Handling: Glad did not initialize */
    }
    ..
}

GL3W

The GL3W loader has another angle on OpenGL. It focuses on the core profile of the OpenGL 3 and 4 Versions. This means that older functionality is not supported, so this loader may only be used for modern OpenGL projects.

The special thing here is that it relies on a python script for its code generation. It parses from the OpenGL Registry website and therefore is also up to date. The code is generated on your machine and it must be initialized.

#include <GL/gl3w.h>
...
int main()
{
    if (gl3wInit()) {
        /* Error Handling: GL3W did not initialize */
    }
}

Other OpenGL Loader Libraries

There are way more OpenGL loader libraries. The differ in the ways that the code is generated, how to include them and how to initialize them. Some only support core functions, others also the extensions. For a good overview visit the wiki Opens in a new tab.at the official Khronos homepage.

OpenGL Rendering Pipeline And Shaders

If you want to create a program with modern OpenGL you have to know some things about shaders. You also have to choose libraries for at least window management.

The OpenGL Rendering Pipeline describes the stages which each vertex passes before it is rendered to the screen. For a detailed explanation about the pipeline and shaders I will provide other articles.

Modern OpenGL Graphics Pipeline
Modern OpenGL: Graphics Pipeline

Most of the time you will only need vertex and fragment shaders. For short, these shaders can modulate the appearance of vertices on the screen. They determine, among other things, the size, position and colour of each vertex.

Shaders in OpenGL have their own language, called GLSL (OpenGL Shader Language). A simple vertex shader in GLSL looks like this:

#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 colorIn;

out vec3 colorOut;

void main()
{
   gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
   colorOut = colorIn;
}

If you want to load this shader from a file into your program, you can find out how to do it in my article about Loading Shaders from File in C.

A xyz-position is the input of this shader and it is converted to a xyzw position and passed down the pipeline. Also, a colour is put in and passed straight to the fragment shader. If this makes no sense to you at the moment, don’t worry. We will cover this in other articles on this page.

What (other) languages can you use OpenGL with?

OpenGL has many so called language bindings which means that many programming languages are supported. Besides C and C++ you can also use OpenGL in Python, C#, Java, Pascal, Visual Basic, Lua, Ruby and many other languages. For a detailed overview and the corresponding loading libraries, take a look at the official Khronos PageOpens in a new tab..

But not the languages alone, OpenGL is also supported by the major operating and windowing systems. This makes it a true cross-platform graphics programming tool.


We did not cover Window Management Libraries in this article simply because it would go way beyond scope. I personally use glfw Opens in a new tab.because it is modern, covers window and context management, input handling, surfaces, events and more. And it is written in pure C.

Marco Lieblang

Professional Programmer since 2003, passionate Programmer since the mid 90's. Developing in many languages from C/C++ to Java, C#, Python and some more. And I also may know a bit about Assembly Languages and Retro Systems.

Recent Posts