In preparation for a class in January, I’m studying Computer Graphics. I’ve spent several hours getting OpenGL working in my environment so I can try out some of the examples. This blog post will describe the steps I’ve taken (that now seem very simple!).

My use case is running Ubuntu Server 18.04.3 as a guest using VMware Fusion 11.5.1 on macOS 10.15.1 as a host.

XQuartz on macOS

Install XQuartz then, from a Terminal, configure it to enable indirect GLX:

defaults write org.macosforge.xquartz.X11 enable_iglx -bool true

Now you can log in to your Ubuntu machine by name or IP:

ssh -Y ubuntu

The remaining commands are in Ubuntu.

Update Ubuntu

sudo apt update
sudo apt upgrade -y
sudo apt dist-upgrade -y
sudo apt autoremove -y

Basic C/C++ Development

Install the basic C/C++ development tools:

sudo apt install -y build-essential

Create a test C program using your favorite Ubuntu editor:

// hello.c
#include <stdio.h>
int main()
{
   printf("Hello, World (from C)!\n");
   return 0;
}

From the shell, compile and run the program:

gcc hello.c; ./a.out

Create a test C++ program:

// hello.cpp
#include<iostream>  
using namespace std; 
int main() 
{ 
     cout<<"Hello World (from C++)!" << endl; 
    return 0; 
} 

From the shell, compile and run the program:

g++ hello.cpp; ./a.out

X11

Install X11 on your Ubuntu machine:

sudo apt install -y xorg xorg-dev

Test the X11 install by running a command in Ubuntu to open a clock on your macOS desktop:

xclock &

OpenGL

Install OpenGL on your Ubuntu machine:

sudo apt install -y mesa-common-dev mesa-utils freeglut3-dev 

Test the OpenGL install by running a command in Ubuntu to open a picture on your macOS desktop:

glxgears &

This may report an error: “Error: couldn’t find RGB GLX visual or fbconfig.” As best I can tell, OpenGL does not like the video driver provided by VMware Fusion. A workaround is to install a driver that it recognizes, and try again:

sudo apt install -y nvidia-340
glxgears &

Calling OpenGL from C/C++

Create glut-starter.c. From the shell, compile and run the program:

gcc glut-starter.c -lGL -lGLU -lglut -lm; ./a.out

Create openGL.cpp

// http://www.codebind.com/linux-tutorials/install-opengl-ubuntu-linux/
#include <GL/glut.h>
 
void displayMe(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glVertex3f(0.5, 0.0, 0.5);
        glVertex3f(0.5, 0.0, 0.0);
        glVertex3f(0.0, 0.5, 0.0);
        glVertex3f(0.0, 0.0, 0.5);
    glEnd();
    glFlush();
}
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(400, 300);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello world!");
    glutDisplayFunc(displayMe);
    glutMainLoop();
    return 0;
}

From the shell, compile and run the program:

g++ openGL.cpp -lglut -lGLU -lGL; ./a.out

Conclusion

I’ll be giving this as instructions to some students in January. If you find issues or have questions, let me know in the comments.