OpenGL - Introduction and Getting Started

By Black Dragon on Thursday, 30th November 2006. More information. Comments.

Introduction Well I’m not sure how competent the people reading this are going to be at any OpenGL programming or C++ in general, so I’ll start at the beginning. The idea of this series of articles will be so that you people can get a basic grip of how OpenGL works

Introduction

Well I’m not sure how competent the people reading this are going to be at any OpenGL programming or C++ in general, so I’ll start at the beginning.

The idea of this series of articles will be so that you people can get a basic grip of how OpenGL works and maybe a glimmer of how C++ can be used. They will start of very basic and move on at a steady pace. Not sure how many chapters there will be at this point, if a cap will happen at all, but will probably be relative to the interest put into them.

However the first few chapters can start off like this:

  1. Introduction and getting started
  2. Shapes and Colour
  3. Transformations
  4. Peripheral Controls
  5. Inputting textures

That's what I can think of at the moment anyway! Also these beginning chapters will all be in 2D, as that extra dimension can get a little confusing. Okay enough of the preliminary jabber time to get down to some code!

Getting Started

First thing to what to get personally is a blank field to start from. Normally how I end up doing it is to cannibalise an older project into a blank form, but of course if you’re a beginner that’s not particularly helpful.

You're going to need to get the OpenGL library. You can get this from the OpenGL.org Download Page. Put it somewhere where your compiler can access it easily.

So I'll now give some code and a run through of what does what and the basics you’ll need to get a lovely window to draw in. I use Microsoft Visual Studio .Net as a base for all my C++ coding, so for that IDE create a new empty project and create a new .cpp file in source files. Call it something like main and then we’re good to go!

Includes

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gl/glut.h>

Now these first few lines are just a general collection of additions I use. Standard libraries and standard input/output commands as well as extra maths commands. The last one is the real one you need, it loads the OpenGL utility file. Without that none of the OpenGL commands will be recognised.

Constants

const int width=640;
const int height=480;
const int xpos=100;
const int ypos=100;

These are the constant values used for the window size and position, which is explained lower down.

init

void init(void)
{
}

This is the initialiser method, when the program gets more complicated it is most likely that you’ll create new objects in here, as well as intialising arrays and creating global lighting. However, of course, we have nothing to initialise yet so it will remain empty. But its an important method to keep hold of.

idleFuncs

void idleFuncs(void)
{
    glutPostRedisplay();
}

Another important method to keep is an idle function, this is mainly for counting extra times when the main loop is going round the program. The sorts of things I would put in here would be, as an example from early work, a gate opening and closing with a variable called spin that would increment each time. The single line left in here at the moment though is another call to the display function to update it, normally when something ahs changed or an object moved.

display

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glutSwapBuffers();
    glFlush();
}

Here is the display function, everything you want to draw in the scene will be made in here, called in here or referred to here. At the moment the calls in there are a selection of clear calls and to flush the buffer.

Firstly the swap buffers command, this makes it possible to animate the program. As the process of sending the image through the graphical pipeline does so in pairs, so when one frame is being displayed then the second buffer is being prepared, then they are switched. If this line wasn’t included the program would seem jumpy and I’ve even seen it act quite oddly with vertical scans lines appearing.

Next, the flush command. This just means that all the data called during the display is pushed onto the pipeline to be displayed and then cleared for the next frame.

The clear colour buffer is another cleanup method, to ensure everything is good to go for the next frame.

reshape

void reshape(int w, int h)
{
    glViewport(0,0,(GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-300.0, 300.0, -200.0, 200.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

This is the method that organises the shape, projection and size of the window and what is displayed in the window. It’s the camera if you like, you can play around with the orthographic display values to make widescreen for example or to play with the ratio of the window. As it stands this is basically a square to ratio for the shape of the window I’ve made.

Tying it all together

void main(int argc, char **argv)
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(width, height);
    glutInitWindowPosition(xpos, ypos);
    
    glutCreateWindow("Graphics Programming Basics");
    
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    
    glutMainLoop();
}

The main loop. This is the starting point for the program. All the main methods are called here to be looped through as the program runs, creates the window from the constant values at the top and displays everything. The main one to take note is the display mode. This call will give a 2D field to display items on, whereas:

glutInitDisplayMode(GLUT_RGB  | GLUT_DOUBLE | GLUT_DEPTH);

will give a 3D field. You can still use the extra dimension in the 2D version put of course will only display the XY plane and any detail in the Z axis will be lost.

What you should have

All this will give you the above screen, a blank window and command prompt.

This concludes the first chapter, tune in next time for more interested results. Will be creating shapes and using the OpenGL colour command. You can download today's source code, if you need a fuller point of reference. Til next time!

Grav

Written by Black Dragon on Thursday, 30 November 2006. Tagged with opengl, c++, c/c++. Read 5372 times. If you liked it, please give it a digg.

Don't just sit there like a lemon! Reply!

Got something to say? Now's the time to share it with the author and everybody else that reads this posting! Lemons need not apply.

edtBOX - xHTML: yes - bbcode:no
Home | Advertise | About | Contact | Legal © Oli Warner 2001—2007 Proud 9rules member