Saturday, April 30, 2011

Creating a more object-oriented OpenGL Window with Freeglut in C++

As per my previous post where I described how to build an OpenGL window, it was basically a c-to-c++ code transition with explainations of what different methods did.

In this post, I will be posting my code where the window preferences and display information is inside a class and only the main() method is not part of the object.

There are two parts to the following code: the cpp file and the header file. You need them in two separate files and in the same directory for things to compile properly without modifications.

Please feel free to rate and critique the code I post. Also, the method names and filenames are part of a project I'm working on, so feel free to change them. They make sense to me.

Also, I am licensing the following code under the GPL 2. Feel free to distribute and modify the source as you like.


#include <stdlib.h>

#include <stdio.h>

#include <GL/glew.h>

#include <GL/freeglut.h>

#include <GL/gl.h>

#include "rwindow.h"



str_window_title = "Hello World";

usi_hwindow = 250;

usi_vwindow = 250;

usi_windowx = 100;

usi_windowy = 100;


RWindow::RWindow(string newtitle)


str_window_title = newtitle;

usi_hwindow = 250;

usi_vwindow = 250;

usi_windowx = 100;

usi_windowy = 100;


void RWindow::SetWindowTitle(string newtitle)


str_window_title = newtitle;


string RWindow::GetWindowTitle() const


return str_window_title;


void RWindow::SetWindowHorizontal(unsigned short int newsize)


usi_hwindow = newsize;


unsigned short int RWindow::GetWindowHorizontal() const


return usi_hwindow;


void RWindow::SetWindowVertical(unsigned short int newsize)


usi_vwindow = newsize;


unsigned short int RWindow::GetWindowVertical() const


return usi_vwindow;


unsigned short int RWindow::GetWindowX() const


return usi_windowx;


void RWindow::SetWindowX(unsigned short int newsize)


usi_windowx = newsize;


unsigned short int RWindow::GetWindowY() const


return usi_windowy;


void RWindow::SetWindowY(unsigned short int newsize)


usi_windowy = newsize;




// remove new variables if any

// remove self

delete this; // Make sure pointer is null


void RWindow::display_contents()


/* clear all pixels */


/* draw white polygon (rectangle) with corners at

* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)


glColor3f (1.0, 1.0, 1.0);


glVertex3f (0.25, 0.25, 0.0);

glVertex3f (0.75, 0.25, 0.0);

glVertex3f (0.75, 0.75, 0.0);

glVertex3f (0.45, 0.45, 0.0);


/* don't wait!

* start processing buffered OpenGL routines


glFlush ();


void RWindow::init ()


/* select clearing (background) color */

glClearColor (0.0, 0.0, 0.0, 0.0);

/* initialize viewing values */



glOrtho(0.0, 1.0, 0.0, 1.0, 1.0, -1.0);



* Declare initial window size, position, and display mode

* (single buffer and RGBA). Open window with "hello"

* in its title bar. Call initialization routines.

* Register callback function to display graphics.

* Enter main loop and process events.


int main(int argc, char** argv)


// Code to initialize window. Normally this would be in another class

RWindow *testWindow = new RWindow();

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);

glutInitWindowSize (testWindow->GetWindowHorizontal(), testWindow->GetWindowVertical());

glutInitWindowPosition (testWindow->GetWindowX(), testWindow->GetWindowY());

// .c_str() converts string to constant char*, which this function needs

glutCreateWindow (testWindow->GetWindowTitle().c_str());

testWindow->init ();



delete testWindow;

return 0; /* ISO C requires main to return int. */



#ifndef R_WINDOW

#define R_WINDOW

#include <string>

using namespace std;

class RWindow





virtual ~RWindow();

void SetWindowTitle(string);

string GetWindowTitle() const;

void SetWindowHorizontal(unsigned short int);

unsigned short int GetWindowHorizontal() const;

void SetWindowVertical(unsigned short int);

unsigned short int GetWindowVertical() const;

void SetWindowX(unsigned short int);

unsigned short int GetWindowX() const;

void SetWindowY(unsigned short int);

unsigned short int GetWindowY() const;

static void init();

static void display_contents();


string str_window_title;

unsigned short int usi_hwindow;

unsigned short int usi_vwindow;

unsigned short int usi_windowx;

unsigned short int usi_windowy;



In order to compile type this:

g++ -Os rwindow.cpp -lGL -lglut -lX11 -lXext -o ~/example-window

Things you can expand on: adding shape-building methods and including them in display_contents(), random polygon generation, etc. etc.

More to come.

Powered by Blogilo

No comments: