OpenGL/Hello World

In this lesson, you will display a rotating triangle on the screen.

Purpose

 * Learn how to get going programming OpenGL

Requirements

 * C knowledge
 * The ability to create a window with an OpenGL rendering context (one method is provided in this lesson using SDL)
 * Installed dependencies

Note: In this tutorial, we are using SDL to set up the window in a cross-platform manner. These things are irrelevant to OpenGL, and are only to present it with a rendering context. If you know what you are doing, you can use a different method for initializing a window and providing OpenGL with a rendering context.

Dependencies

 * Recent graphics drivers
 * OpenGL
 * Window creation library (The one we use in this lesson is SDL)

Code
Window initialization in SDL: /* main.cpp */ /* NOTE: CONTAINS NO OpenGL CODE. */   /*       IF YOU KNOW WHAT YOU ARE DOING, YOU CAN REPLACE IT WITH DIFFERENT WINDOW INITIALIZATION CODE. */   #include "SDL.h"    #define RESOLUTION_WIDTH  640 #define RESOLUTION_HEIGHT 480 void initGL(void); void render(void); int main(int argc, char* argv[]) {       int running; if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {           return 1; }       SDL_WM_SetCaption("OpenGL Rendering", NULL); SDL_SetVideoMode(RESOLUTION_WIDTH, RESOLUTION_HEIGHT, 0, SDL_OPENGL); initGL; running = 1; while(running) {           SDL_Event sdl_event; while(SDL_PollEvent(&sdl_event) > 0) {               if(sdl_event.type == SDL_QUIT) {                   running = 0; }           }            render; SDL_GL_SwapBuffers; }       SDL_Quit; return 0; }

The OpenGL code: #include    void initGL(void) {       glMatrixMode(GL_PROJECTION); glLoadIdentity; glOrtho(-RESOLUTION_WIDTH/2, RESOLUTION_WIDTH/2, -RESOLUTION_HEIGHT/2, RESOLUTION_HEIGHT/2, 1, -1); glMatrixMode(GL_MODELVIEW); glLoadIdentity; }   void render(void) {       glClear(GL_COLOR_BUFFER_BIT); glRotatef(1, 0, 0, 1); glBegin(GL_TRIANGLES); glColor3f(1, 0, 0); glVertex2f( 0, 32); /* Bottom      */ glColor3f(0, 1, 0); glVertex2f(-32, 0 ); /* Upper Left */ glColor3f(0, 0, 1); glVertex2f( 32, 0 ); /* Upper Right */ glEnd; }

Code explanation
We will not explain the part marked as the SDL part. In the OpenGL part: Includes the OpenGL header

in initGL
Sets the matrix mode to projection mode. In projection mode, you set up the matrix. Don't render anything in projection mode! Sets the matrix mode to modelview mode. This is where the rendering is done. Loads the identity of the current matrix. Also resets any transformation of the matrix. Sets the matrix type to orthogonal (2D). The format is as follows: glOrtho(Left, Right, Top, Bottom, Nearest, Furthest) with: Left being the left edge of the screen, Right being the right edge of the screen, Top being the top edge of the screen, Bottom being the bottom edge of the screen, Nearest being the nearest depth to render, and Furthest being the furthest depth to render.

in render
Clears the screen of previously rendered stuff. Rotates the matrix. Basically whatever is rendered after such call will be rotated. The format is as follows: glRotatef(Degrees, X, Y, Z) with: Degrees being how much to rotate (in degrees), X being rotation on the X plane, Y being rotation on the Y plane, and Z being rotation on the Z plane. This call rotates the matrix on the Z plane by 1 degree. Tells the driver that you will be sending data about triangles. Tells the driver that you are done sending data about geometry to render. Sets the color of the following vertices as follows: glColor3f(Red, Green, Blue). This call sets the color to red. Creates a 2D vertex as follows: glVertex2f(X, Y).

Result
If all done correctly, you should see a colorful triangle rotating clockwise in the center of the screen. Hello OpenGL!