Anda di halaman 1dari 13

First OpenGL program

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

GLFW and GLEW


To use these helper libraries we need to correctly
include the libraries and header paths.

Helper Libraries
See todays exercises for info on how to intialise
these libraries in order to use their functions.

The graphics pipeline

Shaders
In order to draw to the screen we need to program parts of the graphics
pipeline.
In our 2D games we only need to worry about vertex shaders and
fragment shaders.
Shaders run on the graphics card in parallel. That is, each vertex or pixel is
operated on by a shader at the same time.
These are separate files that we compile into a separate shader program.
We output data from our OpenGL program and send this to the vertex shader (which
moves our vertex positions from world space into screen space).
The vertex shader then sends data to the fragment shader (which determines the
colour value of each pixel depending on many factors).

World space, screen space? What?


Dont worry about it too
much doesnt effect
you much until you start
3D games. Will be
covered at the end of
this subject.
Basically determining
where things are in
relation to the camera.

Adding shaders to our project


Our C++ code needs to open the shader files, read in the
contents, compile the shaders and link them together
into one program before using them.
This is all covered in the exercises

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

const float vertexPositions[] =


{
0.0f, 0.03f, 0.0f, 1.0f,
-0.025f, -0.05f, 0.0f, 1.0f,
0.025f, -0.05f, 0.0f, 1.0f,
};
const float vertexColours[] =
{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};

The main game loop


By now you should have implemented the basic game loop via the first
tutorial exercise.
Before we draw anything, we need to set a few OpenGL states and setup
transfer of data to the graphics card.
glEnableVertexAttribArray(n) enables us to send data to location n in
the vertex shader
glVertexAttribPointer specify to which variable in the vertex shader we
are sending data to, the data to send, and info about the data type

glDrawArrays
Finally, we can draw our data to the screen!
We only need to specify the OpenGL primitive we
want to use, and the number of points well be using.

Er, primitive?

OpenGL Primitives
Here are the different
options we can use in
the glDrawArrays call.
Triangle Strip re-uses
the last 2 points
along with a new
point to create the
next triangle.

Next
Try to draw some different shapes
Change the colours and locations of these shapes

Anda mungkin juga menyukai