Content Introduction Opengl Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Skip to Navigation

Search

An Introduction to OpenGL Programming


Nov 12, 2014 By Mihalis Tsoukalos
in HOW-TOs OpenGL Programming

Like 42 people like this. Sign Up to see w hat


your friends like.

207

OpenGL is a well-known standard for generating 3-D as well as 2-D graphics that is
extremely powerful and has many capabilities. OpenGL is defined and released by
the OpenGL Architecture Review Board (ARB).
This article is a gentle introduction to OpenGL that will help you understand drawing
using OpenGL.
The latest version of OpenGL at the time of this writing is 4.4, which uses a different
technique for drawing from the one presented here. Nevertheless, the purpose of this
article is to help you understand OpenGL philosophy, not teach you how to code using
the latest OpenGL version. Therefore, the presented source code can be run on Linux
machines that have an older OpenGL version installed.
Installing OpenGL

TRENDING TOPICS
SysAdmin

DevOps

Security

Cloud

If you run the following command on a Debian 7 system to find all packages that
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

If you run the following command on a Debian 7 system to find all packages that
contain the word "opengl", you'll get plenty of output (Figure 1):

HPC

Mobile

Virtualization

Web Development

$ apt-cache search opengl


The Latest

Popular

Recent Comments

New Products
Android Candy: Google Keep
Handling the workloads of the Future
Raspi-Sump
diff -u: What's New in Kernel
Development
Non-Linux FOSS: Don't Type All
Those Words!

Dec
Dec
Dec
Dec

22,
19,
18,
16,

2014
2014
2014
2014

Dec 12, 2014


Dec 10, 2014
more

RELATED JOBS
Linux Unix Systems Architect
Request Technology-Craig Jo...
Northbrook, IL
Senior Linux and Unix Platform Architect
Request Technology-Anthony ...
Northbrook, IL
IT Manager Linux/Windows Server Lifec...
Request Technology-Robyn Ho...
Hudson, OH

Figure 1. Running apt-cache search opengl


There are many free OpenGL implementations for Linux, but you need only one. I
installed FreeGLUT, because it is the most up to date. FreeGLUT is an open-source
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

Technisch Applicatiebeheer Linux


Arrows Group - NL
Hilversum, Noord-Holland, N...
Linux Project Engineer
Arrows Group - NL
Son en Breugel, Noord-Braba...

pdfcrowd.com

alternative to the OpenGL Utility Toolkit (GLUT) library:


root@mail:~# apt-get install freeglut3 freeglut3-dev libglew-dev
Reading package lists... Done
Building dependency tree

More Jobs >


Post a Job >
Subscribe to Jobs:
Pow ered by

Reading state information... Done


The following package was automatically installed
and is no longer required:
fonts-liberation
Use 'apt-get autoremove' to remove it.
The following extra packages will be installed:
libgl1-mesa-dev libglu1-mesa-dev libice-dev libpthread-stubs0

EDITORIAL ADVISORY PANEL


Thank you to our 2014 Editorial Advisors!

libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc


libxau-dev libxcb1-dev libxdmcp-dev libxext-dev libxt-dev
mesa-common-dev x11proto-core-dev x11proto-input-dev
x11proto-kb-dev x11proto-xext-dev xorg-sgml-doctools xtrans-dev
Suggested packages:
libice-doc libsm-doc libxcb-doc libxext-doc libxt-doc
The following NEW packages will be installed:
freeglut3 freeglut3-dev libgl1-mesa-dev libglu1-mesa-dev
libice-dev libpthread-stubs0 libpthread-stubs0-dev libsm-dev
libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev
libxext-dev libxt-dev mesa-common-dev x11proto-core-dev
x11proto-input-dev x11proto-kb-dev x11proto-xext-dev
xorg-sgml-doctools xtrans-dev

Jeff Parent
Brad Baillio
Nick Baronian
Steve Case
Chadalavada Kalyana
Caleb Cullen
Keir Davis
Michael Eager
Nick Faltys
Dennis Frey
Philip Jacob
Jay Kruizenga
Steve Marquez
Dave McAllister
Craig Oda

Mike Roberts
Chris Stark
Patrick Swartz
David Lynch
Alicia Gibb
Thomas Quinlan
Carson McDonald
Kristen Shoemaker
Charnell Luchich
James Walker
Victor Gregorio
Hari Boukis
Brian Conner
David Lane

0 upgraded, 22 newly installed, 0 to remove and 0 not upgraded.


Need to get 7,651 kB of archives.
After this operation, 24.9 MB of additional disk space
will be used.
Do you want to continue [Y/n]?

You also will need a C++ compiler to compile the code.


Finally, you may need to install the mesa-utils package in order to be able to use the
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

glxinfo command:
# apt-get install mesa-utils

The glxinfo command displays useful information about your OpenGL installation, as
you can see in the following output:
Already a subscriber? Click here for subscriber services.
...
GLX version: 1.4
GLX extensions:
GLX_ARB_get_proc_address, GLX_ARB_multisample,
GLX_EXT_import_context, GLX_EXT_texture_from_pixmap,
GLX_EXT_visual_info, GLX_EXT_visual_rating,
GLX_MESA_copy_sub_buffer, GLX_MESA_multithread_makecurrent,
GLX_OML_swap_method, GLX_SGIS_multisample, GLX_SGIX_fbconfig,
GLX_SGIX_pbuffer, GLX_SGI_make_current_read
OpenGL vendor string: VMware, Inc.
OpenGL renderer string: Gallium 0.4 on llvmpipe
(LLVM 3.4, 128 bits)
OpenGL version string: 2.1 Mesa 10.1.3
OpenGL shading language version string: 1.30
OpenGL extensions:

December 2014 Video Preview

...

Mesa is a 3-D graphics library with an API that is so very similar to OpenGL's, it is
pretty much indistinguishable.
OpenGL Pipeline
Figure 2taken from the OpenGL Shading Language book (aka "The Orange Book")
shows the programmable OpenGL pipeline with the vertex and fragment
processors. As you can imagine, the OpenGL pipeline is complex, but you do not have
to understand it fully in order to be able to use OpenGL. The Pipeline shows how
OpenGL operates in the background. Newer versions of the OpenGL Pipeline are
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

November 2014 Video


Preview

October 2014 Video


Preview

Click here for more videos

pdfcrowd.com

even more complex!

Figure 2. OpenGL Architecture


OpenGL is a big state machine. Most calls to OpenGL functions modify a global state
that you cannot access directly.
The OpenGL Shading Language code that is intended for execution on one of the
OpenGL programmable processors is called a Shader. The OpenGL Shading
Language has its roots in C (presenting the OpenGL Shading Language is beyond the
scope of this article).
OpenGL does not define a windowing layer, because it tries to be platform-neutral
and leaves this functionality to the operating system. The OS must provide a
rendering context that accepts commands as well as a framebuffer that keeps the
results of the drawing commands.
Matrix Algebra is extensively used in 3-D graphics, so it is good for you to know how
to add, multiply, subtract and divide matrices, although you will not need to code such
operations yourself. It also is useful to become familiar with 3-D coordinates and be
able to sketch on paper the 3-D scene you are trying to draw.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Drawing a Triangle
Now it's time for some real OpenGL code. The code in Listing 1, when executed,
draws a triangle on-screen using OpenGL.
Listing 1. triangle.cc
// Programmer: Mihalis Tsoukalos
// Date: Wednesday 04 June 2014
//
// A simple OpenGL program that draws a triangle.
#include "GL/freeglut.h"
#include "GL/gl.h"
void drawTriangle()
{
glClearColor(0.4, 0.4, 0.4, 0.4);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.7, 0.7, 0);
glVertex3f(0.7, 0.7, 0);
glVertex3f(0, -1, 0);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL - Creating a triangle");
glutDisplayFunc(drawTriangle);
glutMainLoop();
return 0;
}

The code in Listing 1 for setting up OpenGL is large, but you will have to learn it only
once.
On a Debian 7 system, the following command compiled the triangle.cc OpenGL
program without any error messages:
$ g++ triangle.cc -lglut -o triangle

On an Ubuntu Linux system, the same command produced the following error
messages:
/usr/bin/ld: /tmp/ccnBP5li.o: undefined reference to symbol
↪'glOrtho'
//usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding
//symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

The solution is to compile the triangle.cc program by linking the executable to an


additional library (-lGL):
mtsouk@mtsouk-VirtualBox:/media/sf_OpenGL.LJ/code$ g++
↪triangle.cc -lglut -lGL -o triangle

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The libGL.so library accepts OpenGL commands and makes sure that they are put on
the screen in some way. If your graphics card does not have 3-D acceleration, libGL
contains a software renderer that gives a 2-D image as output to the X Window
System. This is the case with Mesa. libGL also can pass the OpenGL information to
the X Window System, if the GLX extension is present. Then, the X Window System
can do software rendering with the help of Mesa or use hardware acceleration.
The output from the executable will produce the triangle shown in Figure 3. The
correct compilation of triangle.cc is proof that your Linux system can be used for
developing OpenGL applications.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Figure 3. Drawing a Triangle Using OpenGL


1

next

last

______________________
Login to save this as favorite

4 Comments

Linux Journal

Sort by Best

Join the discussion

Juan de Seven Wexford

open in browser PRO version

a month ago

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

What's with the gratuitous shot of Mac OS terminal? we in the Linux community work hard building and maintaining
our own OS, and unlike Apple we have basically no advertising budget, so maybe highlighting Linux would serve the
community more than free nudges and winks to Apple.
8

Reply Share

Shahbaz Youssefi

a month ago

To anyone reading this article!


This is the code style of OpenGL 1. It is virtually deprecated and no one codes like this anymore. While it's nice to
have an idea how OpenGL works, don't waste your time much learning ancient stuff like this.
You can learn on modern computer graphics (with OpenGL of course) for free here: http://www.arcsynthesis.org/gl...
6
Sam

Reply Share

a month ago

I get this on your last example:


g++ rotateCube.cc -lglut -lGL -o rotateCube
/tmp/ccwd9YdJ.o: In function `handleResize(int, int)':
rotateCube.cc:(.text+0xa65): undefined reference to `gluPerspective'
collect2: error: ld returned 1 exit status
Is there another library required?

Reply Share

Julin > Sam

a month ago

I've found that you have to add the -lGLU flag to compile this in Ubuntu 13.04. Hope that helps.

Subscribe

Reply Share

Add Disqus to your site

open in browser PRO version

Privacy

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Webinar

More Resources

One Click, Universal Protection:


Implementing Centralized Security
Policies on Linux Systems
As Linux continues to play an ever increasing
role in corporate data centers and institutions,
ensuring the integrity and protection of these
systems must be a priority. With 60% of the
world's websites and an increasing share of
organization's mission-critical workloads
running on Linux, failing to stop malware and
other advanced threats on Linux can increasingly
impact an organization's reputation and bottom
line.
Learn More
Sponsored by Bit9

Webinar

More Resources

Linux Backup and Recovery Webinar


Most companies incorporate backup procedures
for critical data, which can be restored quickly if a
loss occurs. However, fewer companies are
prepared for catastrophic system failures, in
which they lose all data, the entire operating
system, applications, settings, patches and
more, reducing their system(s) to bare metal.
After all, before data can be restored to a system,
there must be a system to restore it to.
In this one hour webinar, learn how to enhance
your existing backup strategies for better disaster
recovery preparedness using Storix System
Backup Administrator (SBAdmin), a highly flexible
bare-metal recovery solution for UNIX and Linux
systems.
Learn More
Sponsored by Storix

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

First Name

Last Name

Address 1

Address 2

City

State

Zip

Email

Digital Edition

Magazine Formats

The Store

About Us

The Site

Subscribe/Renew
Pay My Bill
Customer Service
Digital Downloads
Gift Subscriptions

PC/Mac
Android
iPhone/iPad
EPUB
Kindle

Stickers
T-shirts
Posters
Back Issues
Archive CD

Advertise
Author Info
Write a Letter
FAQ
Masthead

Copyright
RSS Feeds
Privacy Policy
Events
Contact Us

Canadian Residents | Foreign Residents | Gift Subsc riptions |


Customer Service | Privac y Polic y

Copyright 1994 - 2014 Linux Journal. All rights reserved.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

You might also like