Getting started with OpenGL development in linux

Started by
5 comments, last by BBeck 7 years, 7 months ago

Hi everyone,

I have few questions about getting started with OpenGL development under Linux. Please bear in mind that I have never used Linux in my life until two days ago. I have done a lot of research but I'm confused about few things.

1- What is mesa? Is it a driver or something like GLEW? if it is a driver why would you use it rather than Nvidia or AMD driver? Also how can I use OpenGL without installing libgl1-mesa-dev package? doesn't libgl come with Nvidia or AMD drivers or even with linux itself?

If I try to compile my code without installing libgl1-mesa-dev package I get an error saying "cannot find -lgl". Doesn't Linux already have the latest version of OpenGL? and if it does how can i link to it? where is it located?

2-Why would you use GLEW under linux? I thought GLEW only job to implement functions that links to the driver under windows. The reason for this is because windows only support OpenGL 1.0. So GLEW gives access to OpenGL 1.0+ functions. So Linux doesn't need that since Linux support every version of OpenGL natively. Doesn't it?

3- What does the "-l" stands for in "-lgl" or "-lX11"? does it stand for library?

What I want to do is basically create a window and an OpenGL 3.2+ context under Linux. So I assume I only need to use glx and OpenGL. I don't want to use any other dependency that I don't have to. Can I do that under Linux? I know in windows all I need is glew, wgl and windows API.

so I my final question is, How can I find glx.h and link to OpenGL under Linux without using mesa or GLEW?

Advertisement

Windows is capable of other versions of OpenGL, and GLEW is a library that helps make dealing with extensions easier (on many/all platforms). I don't have much time at the moment, but getting an OpenGL 3.2 context in Linux was similar to the process to do so in Windows. Create a context (legacy) init glew, check for the CreateContext arb, and create another context that meets the 3.2 requirements.

The -l is a setting a build option telling the linker to link with a library. So -lX11 will link the X11 library in your code when creating the executable.

I can't really answer on the drivers, but I believe I had to grab some mesa packages as well, these may actually be the drivers, but Linux is very new territory for me so I could be wrong. Sorry I don't have more time to get the names correct above, but that should help you a little. Good luck

Some links that might be useful:

https://www.opengl.org/wiki/Platform_specifics:_Linux (don't use the code at the end)

https://www.opengl.org/wiki/Programming_OpenGL_in_Linux:_GLX_and_Xlib (you can use this code to create a window). But you can also use glut (check for freeglut in your distro packages).

http://www.mesa3d.org/intro.html

On nVidia at least and as far as I know, you're not obliged to get the GL functions/extensions pointers. But it seems that every one does this since it is more portable.

Only nVidia now (and probably the latest drivers for the latest graphic cards on AMD) allow to do OpenGL without requiring Mesa. You will then need to install the official drivers, just like under Windows. Sometimes (even often) they can be provided with your distribution as different packages.

2-Why would you use GLEW under linux? I thought GLEW only job to implement functions that links to the driver under windows. The reason for this is because windows only support OpenGL 1.0. So GLEW gives access to OpenGL 1.0+ functions. So Linux doesn't need that since Linux support every version of OpenGL natively. Doesn't it?

A misunderstanding on your part.

Windows supports OpenGL versions from 1.0 to 4.5, without issues.

However, the headers and librarys supplied with the Windows SDK only support up to OpenGL 1.1, with a smattering of extensions. That's an important distinction because it's perfectly possible for other SDKs or build systems that support all current GL versions to exist; nothing about Windows prevents that.

So you use the extension loading mechanism (another important distinction; this is not the same as using extensions) to access higher functionality.

The key thing to realise is that the same constraints can exist on Linux. Depending on your build tools and/or SDK used, you may have headers and librarys for all current GL versions or you may also need to use something like GLEW.

It's not the OS, it's the tools.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I have few questions about getting started with OpenGL development under Linux. Please bear in mind that I have never used Linux in my life until two days ago. I have done a lot of research but I'm confused about few things.

1- What is mesa? Is it a driver or something like GLEW? if it is a driver why would you use it rather than Nvidia or AMD driver? Also how can I use OpenGL without installing libgl1-mesa-dev package? doesn't libgl come with Nvidia or AMD drivers or even with linux itself?

Mesa is a big amorphous thing.

In Mesa you will find a software implementation of OpenGL and OpenGL|ES, the official Intel video drivers, the official Gallium video drivers, and an unofficial implementation of the OpenGL development libraries (it's unofficial because the Khronos group, who owns the OpenGL brand, requires a fee of US$ 10 000 per year for official recognition, and Mesa is a penniless Free software project).

Generally speaking on desktop-oriented Linux-based and BSD-based OSes, you develop against Mesa and you run against the official OpenGL or OpenGL|ES drivers, which may or may not include Mesa, Free, or proprietary binary blob drivers from chip vendors like those from AMD or nVidia. What your OS ships in its userspace is up to the distributor, but libGL.so is usually defaulted to the one supplied by Mesa and softlinked to the AMD or nVidia binary blobs during setup.

You still need an extension wrangler library like GLEW because OpenGL uses plenty of extensions.

If I try to compile my code without installing libgl1-mesa-dev package I get an error saying "cannot find -lgl". Doesn't Linux already have the latest version of OpenGL? and if it does how can i link to it? where is it located?

The classic Linux desktop OS, like all POSIX systems, has a case-sensitive filesystem. The correct linker switch to pull in libGL.so is "-lGL".

2-Why would you use GLEW under linux? I thought GLEW only job to implement functions that links to the driver under windows. The reason for this is because windows only support OpenGL 1.0. So GLEW gives access to OpenGL 1.0+ functions. So Linux doesn't need that since Linux support every version of OpenGL natively. Doesn't it?

The job of an OpenGL extension wrangler library like GLEW is to wrangle extensions for OpenGL. Not only do different version of OpenGL have different sets of optional functionality (extensions), but different vendors supply different extensions to take advantage of their specialized hardware designs. The use of GLEW to wrangle extensions is independent of the OS on which you are using OpenGL. If you have access to a shell command line, try typing 'glxinfo|less' and return to see, among other things, a list of extensions the driver you are currently using offers.

3- What does the "-l" stands for in "-lgl" or "-lX11"? does it stand for library?

Yes, the "-l" command-line switch tells the compiler driver (which in turn drives the symbolic linker) to look for a certain library in its library search path and use it to resolve any outstanding unresolved symbols. A switch of "-lGL" would tell it to look for a library named "libGL.so" which would be installed by the development package for GL. On Ubuntu, that would be the package called libgl1-mesa-dev but your OS or distribution may differ.

What I want to do is basically create a window and an OpenGL 3.2+ context under Linux. So I assume I only need to use glx and OpenGL. I don't want to use any other dependency that I don't have to. Can I do that under Linux? I know in windows all I need is glew, wgl and windows API.

Yes, you can just assume that Linux == X11 and write software like it's 1999. I'd strongly recommend that if you're going to write software for Linux, you target Linux and not X11 because the assumption that they're the same thing has not been a valid one for years. If you're just writing for yourself and don't plan on distributing your code or upgrading your system, of course you can stick with GLX. It's bundled as a part of Mesa.

I would recommend that if you want to write software for Linux and need an OpenGL context (or better yet, and OpenGL or OpenGL|ES context), you use libSDL to handle your context creation and input. The overhead is not large, the code is minimal, and your software will also work with a modern Linux OS running Mir or a Wayland display server, on desktops, laptops, tablets, phones, refrigerators, and so on. Also, it will work on Windows and Mac OS. One code base to rule them all. That's a good thing.

so I my final question is, How can I find glx.h and link to OpenGL under Linux without using mesa or GLEW?

You can't. Well, you could grab the header from khronos.org, but you're still going to need the development libraries to link to. Using Mesa is how you do it.

You will need to install the development packages required to develop against OpenGL. I really strongly recommend not using GLX directly, but use libSDL2 instead. If you install the libSDL2 development packages (on Ubuntu, that's 'sudo apt install libsdl2-dev') it should pull in all the other required development packages as dependencies -- that's how Linux distribution package managers work.

Stephen M. Webb
Professional Free Software Developer

Thank you. That makes things so much clearer. ^_^

WIndows 7 will run any version of OGL including Vulkan. I've compiled and ran OGL for OGL 4.5 and Vulkan. (The Vulkan stuff was someone else's code I compiled as I'm in the process of trying to learn it, not my own code. But the point is it ran on Windows.)

I use GLFW with OGL to communicate with Windows, not GLEW. I use GLEW to get the full power of the graphics card (wrangle extensions). So, I'm using GLEW and GLFW, but it's GLFW that communicates with Windows. (In DirectX, I use the Windows SDK to communicate with Windows in Win32, but for OGL I want it to be more portable and so I use GLFW to communicate with whatever the OS is, although I have to admit I've only done Windows with OGL. I hope to do Linux with OGL pretty soon. I've just had too much going on.)

This topic is closed to new replies.

Advertisement