Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help

GLUE

GLUE is a Graphics Library for Undergraduate Education which is designed for use by students learning C++.

When you write a graphical program, you need to cope with the user resizing or uncovering the window at any moment. In practise it means that graphical programs will be structured differently to non-graphical ones, ready to react to the user's actions. With GLUE all you need to do is to define which function to run in these circumstances - GLUE will call it for you at the right time. GLUE offers you the chance to respond to key-presses and mouse-clicks too. Here's a simple example - the code on the left and the program on the right.

// Compile using
// g++ -I/usr/local/include -I/usr/include/GL -L/usr/local/lib 
//     ... -lglue -lglui -lglut -lGLU -lGL
#include "glue.h"

void mygraphics(int w, int h) {
  color(BLUE);
  text("hello", 150,h/2);
}

int main() {
  glueWindow();  
  graphicsfunction (mygraphics);
  glueGo();
}
glue

Coordinates

A note about C++ parameter default values

In C++ you don't always need to supply all the parameters to a function. The routines below use this facility to help make your programs shorter. Any parameter mentioned below with a = sign after it can be left out as long as there are no more following parameters. The omitted parameters are given the stated default values. So glueWindow(500,500) is legal, setting the window's width and height to 500, but using the default values for the other parameters.

Routines

Example 2

This longer example uses more of the graphics routines and shows how to respond to keypresses (pressing 'q' will quit the program). The lines on the graph use the AXES coordinate system. The other objects use the default PIXEL coordinate system.
// Compile using
// g++ -I/usr/local/include -I/usr/include/GL -L/usr/local/lib 
//      ... -lglue -lglui -lglut -lGLU -lGL
#include <iostream>
#include <iostream>
#include <cmath>

using namespace std;

#include "glue.h"
 int number=61;
 float xlo=0.0, xhi = number - 1, ylo = 0.0, yhi=0.1;   

void keyboard(unsigned char key, int x, int y ) {
switch ( key ) 
  {
  case 'q':
      exit ( 0 );
      break;
  default:
      break;
  }
}

void mygraphics(int w, int h) {

 int nxticks = 7, nyticks = 6;
  color(BLACK);  
  xaxis(50,300, nxticks, 50, 5, "Outcome", 0, 60,0);

  color(GREEN);
  yaxis(50,300, nyticks, 50, -10, "Frequency", 0, 0.1,3);

  filledcircle(200,250,50);
  color(RED);

  filledrectangle(250,270,80,10);
   // Plot data as discrete distribution

   float data[61], mean=35.0, deviation=6;
   float t1, t2;
   for(int i=0; i< number; i++)
   {
     t1 = (i -  mean)/deviation;
     t2 = -0.5*t1*t1;
     data[i] = exp(t2)/(deviation*sqrt(2*M_PI));
   }
    for(int i=0; i<number; i++)
     {
       move(i,0,AXES);
       draw(i,data[i],AXES);
     }
}

int main() {
  glueWindow(500, 500);  
  graphicsfunction (mygraphics);
  keyboardfunction (keyboard);
  glueGo();
}
example2

Additions

GLUE uses GLUI and GLUT behind the scenes, which can both do a lot more than GLUE. Each uses OpenGL. GLUE is designed so that direct access to these other libraries of routines is available without you needing to change the basic structure of your code. The following code uses some openGL primitives. It resets the coordinate system so that (0,0) is in the center of the window and y goes down the window rather than up. Then it rotates the coordinate system when you press the 'r' button.
#include <iostream>
#include <iostream>
#include <cmath>

using namespace std;

#include "glue.h"
int myw, myh;
int  angle=15;

void mygraphics(int w, int h) {
myw=w;
myh=h;
  glViewport(0, 0, w, h);       /* Establish viewing area to cover entire window. */
  glMatrixMode(GL_PROJECTION);  /* Start modifying the projection matrix. */
  glLoadIdentity();             /* Reset project matrix. */
  glOrtho(0, w, 0, h, -1, 1);   /* Map abstract coords directly to window coords. */
  glScalef(1, -1, 1);           /* Invert Y axis so increasing Y goes down. */
  glTranslatef(w/2, -h/2, 0);   /* Shift origin */
  glRotated(angle, 0, 0, 1);
  angle=angle+15;
  color(BLUE);
  text("press the r key", 0,0);
  glBegin(GL_TRIANGLES);
    glColor3f(0.0, 0.0, 1.0);  /* blue */
    glVertex2i(0, h/3);
    glColor3f(0.0, 1.0, 0.0);  /* green */
    glVertex2i(h/3, h/3);
    glColor3f(1.0, 0.0, 0.0);  /* red */
    glVertex2i(0, 0);
  glEnd();
  glFlush();  /* Single buffered, so needs a flush. */
}

void keyboard(unsigned char key, int x, int y ) {
  static int angle=15;
switch ( key ) 
  {
  case 'r':
      mygraphics(myw, myh); 
      angle=angle+15;
      break;
  default:
      break;
  }
}

int main() {
  glueWindow();  
  graphicsfunction (mygraphics);
  keyboardfunction (keyboard);
  glueGo();
}

See Also

© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: February 2009