OpenGL/Hello World
In this lesson, you will display a rotating triangle on the screen.
Purpose
[edit | edit source]- Learn how to get going programming OpenGL
Requirements
[edit | edit source]- 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
[edit | edit source]Code
[edit | edit source]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 <GL/gl.h> 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
[edit | edit source]We will not explain the part marked as the SDL part.
In the OpenGL part:
#include <GL/gl.h>
Includes the OpenGL header
in initGL()
[edit | edit source] glMatrixMode(GL_PROJECTION);
Sets the matrix mode to projection mode. In projection mode, you set up the matrix. Don't render anything in projection mode!
glMatrixMode(GL_MODELVIEW);
Sets the matrix mode to modelview mode. This is where the rendering is done.
glLoadIdentity();
Loads the identity of the current matrix. Also resets any transformation of the matrix.
glOrtho(-RESOLUTION_WIDTH/2, RESOLUTION_WIDTH/2, -RESOLUTION_HEIGHT/2, RESOLUTION_HEIGHT/2, 1, -1);
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()
[edit | edit source] glClear(GL_COLOR_BUFFER_BIT);
Clears the screen of previously rendered stuff.
glRotatef(1, 0, 0, 1);
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.
glBegin(GL_TRIANGLES);
Tells the driver that you will be sending data about triangles.
glEnd();
Tells the driver that you are done sending data about geometry to render.
glColor3f(1, 0, 0);
Sets the color of the following vertices as follows: glColor3f(Red, Green, Blue). This call sets the color to red.
glVertex2f( 0, 32);
Creates a 2D vertex as follows: glVertex2f(X, Y).
Result
[edit | edit source]If all done correctly, you should see a colorful triangle rotating clockwise in the center of the screen. Hello OpenGL!