The indigoparadox Web Zone

OpenGL 1.0: Part 1

Back to tutorials

Web Zone Navigation

Related Web Zones

Other Web Zones

This page was generated from a source code file. Click here to download the file this page was generated from.

The inside of a colorful cube rendered in a window.

1. Introduction

This is an "anti-tutorial" for working with OpenGL 1.0. A working example is laid out below, along with the rationale for why certain things are implemented the way they are.

Please note that this may not be a good source for learning how to write *modern* OpenGL code. This is specifically a guide for writing code targeting older OpenGL implementations such as those found in early versions of Windows NT or older video game consoles.

2. Compiling

This source file also depends on the files below:

These files can be compiled together by adding them to your Borland or Visual C IDE. You may also compile them by installing the MingW compiler on your Linux distro (e.g. sudo apt install mingw-w64-i686-dev on recent Ubuntu) and using the command i686-w64-mingw32-gcc -o opengl01.exe opengl01.c oglwin.c oglpoly.c -lopengl32 -lgdi32 to produce opengl01.exe.

3. The Code: Preamble

Include The system OpenGL header for constants and functions used below. Also include ogldefs.h (linked above), a header with some program-specific constants we'll also be using.

4. The Code: Setup Function

opengl01.c
1#include <GL/gl.h>
2#include "ogldefs.h"

A setup function to be called once from our wrapper program during initialization. This is C89, so all of the variables are declared up front!

opengl01.c
3int ogl_opengl_setup() {
4 int retval = 0;
5 float aspect_ratio = 0;
6 float rzoom = 0;

Set the viewport to the size of the window. This is not strictly necessary in some implementations. However, some other implementations will behave weirdly if it's not done, so it's best to just always do it.

Reminder that the OGL_SCREEN_W and OGL_SCREEN_H constants are defined in ogldefs.h!

opengl01.c
7 glViewport( 0, 0, OGL_SCREEN_W, OGL_SCREEN_H );

Calculate the aspect ratio of the screen as a float, which we will use to determine the properties of our frustum.

opengl01.c
8 aspect_ratio = OGL_SCREEN_W / OGL_SCREEN_H;

Change the state of the OpenGL machine so that further commands will affect the projection matrix. We are doing this now so that we can setup our projection!

opengl01.c
9 glMatrixMode( GL_PROJECTION );

Reset the matrix properties to defaults... We haven't done anything yet, so this will probably not do anything, but it's good hygiene.

opengl01.c
10 glLoadIdentity();

Setup the frustum... This distorts the lines drawn by the rasterizer, so that objects appear to have depth.

There are more complicated explanations regarding the math elsewhere, but the bottom line is that the smaller the rzoom variable is, the closer everything appears to the "camera," or point-of-view.

opengl01.c
11 rzoom = 1.0f;
12 glFrustum(
13 -1.0f * rzoom * aspect_ratio,
14 rzoom * aspect_ratio,
15 -1.0f * rzoom,
16 rzoom,
17 0.5f, 20.0f );

Change the state of the OpenGL machine back so that further commands will affect the model matrix, now that we've setup our projection above.

opengl01.c
18 glMatrixMode( GL_MODELVIEW );

Set the clear color. Here we've chosen a sensible default of black.

opengl01.c
19 glClearColor( 0, 0, 0, 0 );

Enable face culling.

opengl01.c
20 glEnable( GL_CULL_FACE );

Renormalize normals after irregular transformations. We're not scaling yet, and the normals specified in