OpenGL introduction

Graphic Processing Units (GPUs) were originally designed to render graphics very quickly. They have evolved into parallel processing systems with resources similar to a normal computer. They have their own memories, processors, and programming languages. Since the GPU is like a computer and it is inside of our regular computer, we refer to it as a 'device' within a 'host'. We will use OpenGL to communicate with the GPU device. We need to tell it what the input data is, how to process the data using a program, and where to put the output.

OpenGL is a graphics API that provides a standard interface for rendering graphics. It also serves to abstract the difference in hardware implementations, allowing a single interface to address many types of graphics hardware. There are other graphics APIs: DirectX, PHIGS, Metal. We are using OpenGL as the standard is not vendor specific and is supported on many different OS and hardware platforms.

We first need to allocate memory on the GPU, tell the GPU how to layout our data in the memory, then upload the data and tell the GPU to use it. We will also send programs to the GPU to manipulate the data. OpenGL has commands for all of these operations.

Primitives

The basic graphics primitive we will use is the vertex. We often think of vertices as having a position in space, but in OpenGL no vertex attributes are fixed. Vertex attributes can be anything: position, color, happiness.

The easiest attribute to begin with is position. We will start with 2D positions in the range [-1.0, 1.0]. These are specified as x, y pairs. We will also use another attribute: color as an RGB triplet. Our RGB values will range [0.0, 1.0].

OpenGL has several primitive types: points, lines, and triangles. All are based on vertices and require the attribute of position to be visible.

OpenGL has several primitive shapes.
GL_POINTSPoints use a single vertex.
GL_LINESForms lines between each pair of vertices. In lines, all attributes are interpolated between the given vertices.
GL_LINE_STRIPLike GL_LINES, but additional vertices form a continuing line chain.
GL_LINE_LOOPLike GL_LINE_STRIP, but a loops back to the first vertex at the end of the chain.
GL_TRIANGLESForms a triangle from each set of 3 vertices.
GL_TRIANGLE_STRIPLike GL_TRIANGLE, but each additional vertex forms a new triangle with the previous 2 vertices.
TRIANGLE_FANLike GL_TRIANGLE, but each additional vertex forms a new triangle with the first vertex and the most recent vertex.

Useful OpenGL functions

glVertexAttribPointer(slot, size, type, normalize, stride, offset);
slot: the shader attach point.
size: how many values are needed for the attribute.
type: the type of the data contained in the buffer.
normalize: normalize vectors- set to GL_FALSE usually.
stride: offset between entries- usually 0.
offset: offset from start.

glDrawArrays(primitive mode, first, count);
primitive mode: the GL type to use (GL_LINES, etc).
first: the starting element.
count: how many elements to draw.