0% found this document useful (0 votes)
41 views10 pages

Understanding OpenGL Basics and Core Profile

Uploaded by

texig40736
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views10 pages

Understanding OpenGL Basics and Core Profile

Uploaded by

texig40736
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OpenGL

Before starting our journey we should first define what OpenGL actually is. OpenGL is mainly considered an AP
(an Application Programming Interface) that provides us with a large set of functions that we can use to
Introduction
manipulate graphics and images. However, OpenGL by itself is not an API, but merely a specification,
Getting started developed and maintained by the Khronos Group.

OpenGL The OpenGL specification specifies exactly what the


Creating a window result/output of each function should be and how it should
perform. It is then up to the developers implementing this
Hello Window specification to come up with a solution of how this function
should operate. Since the OpenGL specification does not give us
Hello Triangle
implementation details, the actual developed versions of
Shaders OpenGL are allowed to have different implementations, as long
as their results comply with the specification (and are thus the
Textures
same to the user).
Transformations
The people developing the actual OpenGL libraries are usually the graphics card manufacturers. Each graphics
Coordinate Systems card that you buy supports specific versions of OpenGL which are the versions of OpenGL developed specificall
for that card (series). When using an Apple system the OpenGL library is maintained by Apple themselves and
Camera
under Linux there exists a combination of graphic suppliers' versions and hobbyists' adaptations of these
Review libraries. This also means that whenever OpenGL is showing weird behavior that it shouldn't, this is most likely
the fault of the graphics cards manufacturers (or whoever developed/maintained the library).
Lighting
Model Loading
Since most implementations are built by graphics card manufacturers, whenever there is a bug in
Advanced OpenGL the implementation this is usually solved by updating your video card drivers; those drivers include
Advanced Lighting the newest versions of OpenGL that your card supports. This is one of the reasons why it's always
advised to occasionally update your graphic drivers.
PBR
In Practice
Khronos publicly hosts all specification documents for all the OpenGL versions. The interested reader can find
Guest Articles the OpenGL specification of version 3.3 (which is what we'll be using) here which is a good read if you want to
delve into the details of OpenGL (note how they mostly just describe results and not implementations). The
Code repository specifications also provide a great reference for finding the exact workings of its functions.
Translations
Core-profile vs Immediate mode
Privacy
In the old days, using OpenGL meant developing in immediate mode (often referred to as the fixed function
About pipeline) which was an easy-to-use method for drawing graphics. Most of the functionality of OpenGL was
hidden inside the library and developers did not have much control over how OpenGL does its calculations.
Developers eventually got hungry for more flexibility and over time the specifications became more flexible as
a result; developers gained more control over their graphics. The immediate mode is really easy to use and
understand, but it is also extremely inefficient. For that reason the specification started to deprecate
immediate mode functionality from version 3.2 onwards and started motivating developers to develop in
OpenGL's core-profile mode, which is a division of OpenGL's specification that removed all old deprecated
functionality.

When using OpenGL's core-profile, OpenGL forces us to use modern practices. Whenever we try to use one of
OpenGL's deprecated functions, OpenGL raises an error and stops drawing. The advantage of learning the
modern approach is that it is very flexible and efficient. However, it's also more difficult to learn. The
immediate mode abstracted quite a lot from the actual operations OpenGL performed and while it was easy to
learn, it was hard to grasp how OpenGL actually operates. The modern approach requires the developer to
truly understand OpenGL and graphics programming and while it is a bit difficult, it allows for much more
flexibility, more efficiency and most importantly: a much better understanding of graphics programming.

This is also the reason why this book is geared at core-profile OpenGL version 3.3. Although it is more difficult,
it is greatly worth the effort.

As of today, higher versions of OpenGL are available to choose from (at the time of writing 4.6) at which you
may ask: why do I want to learn OpenGL 3.3 when OpenGL 4.6 is out? The answer to that question is relatively
simple. All future versions of OpenGL starting from 3.3 add extra useful features to OpenGL without changing
OpenGL's core mechanics; the newer versions just introduce slightly more efficient or more useful ways to
accomplish the same tasks. The result is that all concepts and techniques remain the same over the modern
OpenGL versions so it is perfectly valid to learn OpenGL 3.3. Whenever you're ready and/or more experienced
you can easily use specific functionality from more recent OpenGL versions.

When using functionality from the most recent version of OpenGL, only the most modern graphics
cards will be able to run your application. This is often why most developers generally target lower
versions of OpenGL and optionally enable higher version functionality.

In some chapters you'll find more modern features which are noted down as such.

Extensions

A great feature of OpenGL is its support of extensions. Whenever a graphics company comes up with a new
technique or a new large optimization for rendering this is often found in an extension implemented in the
drivers. If the hardware an application runs on supports such an extension the developer can use the
functionality provided by the extension for more advanced or efficient graphics. This way, a graphics developer
can still use these new rendering techniques without having to wait for OpenGL to include the functionality in
its future versions, simply by checking if the extension is supported by the graphics card. Often, when an
extension is popular or very useful it eventually becomes part of future OpenGL versions.

The developer has to query whether any of these extensions are available before using them (or use an
OpenGL extension library). This allows the developer to do things better or more efficient, based on whether an
extension is available:

if(GL_ARB_extension_name)
{
// Do cool new and modern stuff supported by hardware
}
else
{
// Extension not supported: do it the old way
}

With OpenGL version 3.3 we rarely need an extension for most techniques, but wherever it is necessary proper
instructions are provided.

State machine

OpenGL is by itself a large state machine: a collection of variables that define how OpenGL should currently
operate. The state of OpenGL is commonly referred to as the OpenGL context. When using OpenGL, we often
change its state by setting some options, manipulating some buffers and then render using the current
context.

Whenever we tell OpenGL that we now want to draw lines instead of triangles for example, we change the
state of OpenGL by changing some context variable that sets how OpenGL should draw. As soon as we change
the context by telling OpenGL it should draw lines, the next drawing commands will now draw lines instead of
triangles.

When working in OpenGL we will come across several state-changing functions that change the context and
several state-using functions that perform some operations based on the current state of OpenGL. As long as
you keep in mind that OpenGL is basically one large state machine, most of its functionality will make more
sense.

Objects

The OpenGL libraries are written in C and allows for many derivations in other languages, but in its core it
remains a C-library. Since many of C's language-constructs do not translate that well to other higher-level
languages, OpenGL was developed with several abstractions in mind. One of those abstractions are objects in
OpenGL.

An object in OpenGL is a collection of options that represents a subset of OpenGL's state. For example, we
could have an object that represents the settings of the drawing window; we could then set its size, how many
colors it supports and so on. One could visualize an object as a C-like struct:

struct object_name {
float option1;
int option2;
char[] name;
};

Whenever we want to use objects it generally looks something like this (with OpenGL's context visualized as a
large struct):

// The State of OpenGL


struct OpenGL_Context {
...
object_name* object_Window_Target;
...
};

// create object
unsigned int objectId = 0;
glGenObject(1, &objectId);
// bind/assign object to context
glBindObject(GL_WINDOW_TARGET, objectId);
// set options of object currently bound to GL_WINDOW_TARGET
glSetObjectOption(GL_WINDOW_TARGET, GL_OPTION_WINDOW_WIDTH, 800);
glSetObjectOption(GL_WINDOW_TARGET, GL_OPTION_WINDOW_HEIGHT, 600);
// set context target back to default
glBindObject(GL_WINDOW_TARGET, 0);
This little piece of code is a workflow you'll frequently see when working with OpenGL. We first create an object
and store a reference to it as an id (the real object's data is stored behind the scenes). Then we bind the object
(using its id) to the target location of the context (the location of the example window object target is defined
as GL_WINDOW_TARGET). Next we set the window options and finally we un-bind the object by setting the
current object id of the window target to 0. The options we set are stored in the object referenced by
objectId and restored as soon as we bind the object back to GL_WINDOW_TARGET.

The code samples provided so far are only approximations of how OpenGL operates; throughout
the book you will come across enough actual examples.

The great thing about using these objects is that we can define more than one object in our application, set
their options and whenever we start an operation that uses OpenGL's state, we bind the object with our
preferred settings. There are objects for example that act as container objects for 3D model data (a house or a
character) and whenever we want to draw one of them, we bind the object containing the model data that we
want to draw (we first created and set options for these objects). Having several objects allows us to specify
many models and whenever we want to draw a specific model, we simply bind the corresponding object before
drawing without setting all their options again.

Let's get started

You now learned a bit about OpenGL as a specification and a library, how OpenGL approximately operates
under the hood and a few custom tricks that OpenGL uses. Don't worry if you didn't get all of it; throughout the
book we'll walk through each step and you'll see enough examples to really get a grasp of OpenGL.

Additional resources

[Link]: official website of OpenGL.


OpenGL registry: hosts the OpenGL specifications and extensions for all OpenGL versions.
222 Comments 
1 Login

Join the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

 54 Share Best Newest Oldest

Xiangkui Guo − ⚑
10 years ago

The website perfect for opengl ,thank you,JoeyDeVries

94 0 Reply ⥅

Code Learner − ⚑
11 years ago

The best place to learn OpenGL so far, and believe me I've been checking a lot of sources.

44 0 Reply ⥅

This comment was deleted. −


ismet > Guest
− ⚑
6 years ago

hey @mobstersquirrel how are you doing? Still coding?


Show us some projects :D
Have a nice day!

4 0 Reply ⥅

Delta__Machine > ismet


− ⚑
6 years ago edited

hey @ismett08 how are you doing? Still coding?


Show us some projects :D
Have a nice day!

2 1 Reply ⥅

lol > Delta__Machine − ⚑


6 years ago

hey @Delta__Machine how are you doing? Still coding?


Show us some projects :D
Have a nice day!

2 1 Reply ⥅

Gee Transit > lol


− ⚑
6 years ago

hey @lol how are you doing? Still coding?


Show us some projects :D
Have a nice day!

2 0 Reply ⥅

nicolasvycas > Gee Transit


− ⚑
6 years ago

Hey @Gee Transit how are you doing? Still coding?


Show us some projects :D
Have a nice day!

2 0 Reply ⥅

Emil Schilberg > nicolasvycas


− ⚑
5 years ago

Hey @nicolasvycas how are you doing? Still coding?


Show us some projects :D
Have a nice day!

1 0 Reply ⥅
yEra > Emil Schilberg
− ⚑
5 years ago edited

Hey @Emil Schilberg how are you doing? Still coding?


Show us some projects :D
Have a nice day!

1 0 Reply ⥅

Dhakshith > yEra − ⚑


5 years ago

Hey @yEra how are you doing? Still coding?


Show us some projects :D
Have a nice day!

1 0 Reply ⥅

Sein Lee > Dhakshith


− ⚑
5 years ago

Hey @Dhakshith how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Bob > Sein Lee − ⚑


5 years ago

Hey @이세인 how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

daddy > Bob − ⚑


5 years ago

Hey @Bob how are you doing?

Still coding?Show us some projects :D

Have a nice day!

0 0 Reply ⥅

hahkjsd > daddy − ⚑


5 years ago

Hey @daddy how are you doing?

Still coding?Show us some projects :D

Have a nice day!

0 0 Reply ⥅

qweo > hahkjsd − ⚑


5 years ago

Hey @hahkjsd how are you doing?

Still coding?Show us some projects :D

Have a nice day!

0 0 Reply ⥅

iawindowss > qweo − ⚑


5 years ago

Hey @qweo how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Snowdingerr > Gee Transit − ⚑


3 years ago

hey @Gee Transit how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅
Mta > Snowdingerr − ⚑
a year ago

hey @Snowdingerr how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

minh > Mta − ⚑


a year ago

hey Mta how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Abdullah Saafan > ismet


− ⚑
5 years ago

Hey @qweo how are you doing?

Still coding?Show us some projects :D

Have a nice day!

0 0 Reply ⥅

Roqaia > ismet − ⚑


6 years ago

hey @ismet how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

ismet > Roqaia − ⚑


6 years ago

oh. I knew this day would come but this was far to early. Sadly, I didn't
pass the introduction part very much. I am handing over my dreams and
hopes to you @Roqaia . Please create a game engine

4 0 Reply ⥅

joe pergo > Roqaia − ⚑


a year ago

hey @Roqaia how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Casper > Roqaia − ⚑


6 years ago

hey @Roqaia how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Doctype_ > Casper − ⚑


6 years ago

Hey @Casper how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

TehBrian > Doctype_ − ⚑


6 years ago

Hey @Doctype_ how are you doing? Still coding?


Show us some projects :D
Have a nice day!

1 0 Reply ⥅

cunt247 > TehBrian − ⚑


6 years ago

Hey @Tehbrian how are you doing? Still coding?


Show us some projects :D
Have a nice day!

2 0 Reply ⥅

Mayank Shigaonker > cunt247


− ⚑
6 years ago

Hey @cunt247 how are you doing? Still coding?


Show us some projects :D
Have a nice day!

1 0 Reply ⥅

Nope > Mayank Shigaonker − ⚑


6 years ago

Hey @Mayank Shigaonker how are you doing? Still coding?


Show us some projects :D
Have a nice day!

1 0 Reply ⥅

NiceProgramer > Nope − ⚑


5 years ago

Hey @nope how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Wiertek > NiceProgramer − ⚑


5 years ago

Hey @NiceProgramer how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

BluntPika > Wiertek − ⚑


5 years ago edited

Hey @wiertek how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

gpot > BluntPika − ⚑


5 years ago

Hey @BluntPika how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

ppsucc123 > gpot − ⚑


5 years ago

Hey @gpot how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

simgamedev > ppsucc123 − ⚑


5 years ago

Hey @ppsucc123 how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

sani > simgamedev − ⚑


5 years ago

Hey @simgamedev how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Cogni > sani


− ⚑
Cogni > sani − ⚑
5 years ago

Hey @sani how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Mageze > Cogni − ⚑


5 years ago

Hey @Cogni how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

Denver Cox > Mageze − ⚑


5 years ago

Hey @Mageze how are you doing? Still coding?


Show us some projects :D
Have a nice day

0 0 Reply ⥅

whereismena > Denver Cox − ⚑


5 years ago

Hey @Denver Cox how are you doing? Still coding?


Show us some projects :D
Have a nice day

0 0 Reply ⥅

Its me > whereismena − ⚑


4 years ago

Hey @whereismena how are you doing? Still coding?


Show us some projects :D
Have a nice day

0 0 Reply ⥅

Nerd > Its me − ⚑


4 years ago

Hey @Its me how are you doing? Still coding?


Show us some projects :D
Have a nice day

0 0 Reply ⥅

Egg42 > Nerd


− ⚑
4 years ago

Hey @Nerd how are you doing? Still coding?


Show us some projects :D
Have a nice day

0 0 Reply ⥅

Hugo > Egg42 − ⚑


4 years ago

Hey @Egg42 how are you doing? Still coding?


Show us some projects :D
Have a nice day

0 0 Reply ⥅

Makauil > Hugo − ⚑


4 years ago

Hey @Hugo how are you doing? Still coding?


Show us some projects :D
Have a nice day

0 0 Reply ⥅

xxeliezer > Makauil − ⚑


4 years ago

hey how are you doing? Still coding?


Show us some projects :D
Have a nice day!
0 0 Reply ⥅

SSBVillage > xxeliezer


− ⚑
4 years ago

hey @xxeliezer how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

uMetalooper > SSBVillage − ⚑


4 years ago

hey @SSBVillage how are you doing? Still coding?


Show us some projects :D
Have a nice day!

0 0 Reply ⥅

pigroy > uMetalooper − ⚑


4 years ago

hey @uMetalooper how are you doing? still coding?


show us some projects :D
have a nice day!

0 0 Reply ⥅

Load more comments

Subscribe Privacy Do Not Sell My Data

You might also like