8/10/2019 4 - Lecture - OpenGL Intro - Practical
1/13
First OpenGL program
8/10/2019 4 - Lecture - OpenGL Intro - Practical
2/13
Introduction
Lets get started on how to make a simple coloured
triangle using OpenGL
Well talk about the different pieces of the project:
Using the libraries
Brief overview of shaders
Loading the shaders Draw code
8/10/2019 4 - Lecture - OpenGL Intro - Practical
3/13
GLFW and GLEW
To use these helper libraries we need to corrinclude the libraries and header paths.
8/10/2019 4 - Lecture - OpenGL Intro - Practical
4/13
Helper Libraries
See todays exercises for info on how to intiathese libraries in order to use their functions
8/10/2019 4 - Lecture - OpenGL Intro - Practical
5/13
The graphics pipeline
8/10/2019 4 - Lecture - OpenGL Intro - Practical
6/13
Shaders
In order to draw to the screenwe need to program parts of t
pipeline. In our 2D games we only need to worry about vertexshaders a
fragmentshaders.
Shaders run on the graphics card in parallel. That is, each verteoperated on by a shader at the same time.
These are separate files that we compile into a separate shade We output data from our OpenGL program and send this to the vertex sh
moves our vertex positions from worldspace into screenspace).
The vertex shader then sends data to the fragment shader (which determcolour value of each pixel depending on many factors).
8/10/2019 4 - Lecture - OpenGL Intro - Practical
7/13
World space, screen space? What?
Dont worry about it toomuchdoesnt effectyou much until you start3D games. Will becovered at the end of
this subject. Basically determining
where things are inrelation to the camera.
8/10/2019 4 - Lecture - OpenGL Intro - Practical
8/13
Adding shaders to our project
Our C++ code needs to open the shader files, rea
contents, compile the shaders and link them toginto one program before using them.
This is all covered in the exercises
8/10/2019 4 - Lecture - OpenGL Intro - Practical
9/13
Vertex Arrays
For now, we need to store the info
for our geometry in arrays.
Well be using one for the vertex
positions, and one for the colours
There are 4 components for
position: x, y, z and w
Also 4 components for colour: red,
green, blue and alpha
constfloatvertexPosi
{
0.0f, 0.03f, 0.0f, 1.0
-0.025f, -0.05f, 0.0
0.025f, -0.05f, 0.0f,
};
constfloatvertexColo
{
1.0f, 0.0f, 0.0f, 1.0f0.0f, 1.0f, 0.0f, 1.0f
0.0f, 0.0f, 1.0f, 1.0f
};
8/10/2019 4 - Lecture - OpenGL Intro - Practical
10/13
The main game loop
By now you should have implemented the basic game loop via
tutorial exercise.
Before we draw anything, we need to set a few OpenGL states
transfer of data to the graphics card.
glEnableVertexAttribArray(n)enables us to send data to loca
the vertex shader
glVertexAttribPointerspecify to which variable in the vertex s
are sending data to, the data to send, and info about the data t
8/10/2019 4 - Lecture - OpenGL Intro - Practical
11/13
glDrawArrays
Finally, we can draw our data to the screen!
We only need to specify the OpenGL primitiv
want to use, and the number of points well
Er, primitive?
8/10/2019 4 - Lecture - OpenGL Intro - Practical
12/13
OpenGL Primitives
Here are the different
options we can use in
the glDrawArrays call.
Triangle Strip re-uses
the last 2 pointsalong with a new
point to create the
next triangle.
8/10/2019 4 - Lecture - OpenGL Intro - Practical
13/13
Next
Try to draw some different shapes
Change the colours and locations of these shapes