Graphics
This presentation is the property of its rightful owner.
Sponsored Links
1 / 68

Graphics PowerPoint PPT Presentation


  • 99 Views
  • Uploaded on
  • Presentation posted in: General

Graphics. openGL and glut. GRAPHICS Adapter. The graphics adaptor contains 2 parts. Video Memory holds the computer’s representation of the image. can be accessed by the video control circuitry and the CPU. greater resolution requires more memory because there are more pixels.

Download Presentation

Graphics

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Graphics

Graphics

openGL and glut


Graphics adapter

GRAPHICS Adapter

  • The graphics adaptor contains 2 parts.

    • Video Memory

      • holds the computer’s representation of the image.

      • can be accessed by the video control circuitry and the CPU.

      • greater resolution requires more memory because there are more pixels.

    • Control Circuitry

      • reads the video memory and sends the required video signals to the monitor.

  • The Graphics Adaptor can be in either of two modes.

    • Graphics or Text


Graphics screen

GRAPHICS Screen

  • The screen is divided into many small dots called pixels (picture elements).

  • These are accessed using x,y coordinates and can be turned off and on individually.

  • The coordinates can be integer or floating point depending upon the graphics libraries used.

  • These are the smallest drawing elements.

  • All lines are made up of pixels which are very close together.

......................................

......................................

......................................

......................................

......................................

......................................

......................................

......................................

A pixel


Screen resolution

Screen Resolution

  • The resolution is the number of pixels used in the entire screen. This is usually quoted as the number of pixels in the x direction by number of pixels in the y direction. eg 1280 x 1024.

  • Most screen specifications are now quoted using the resolution, colour and refresh rate eg 1280 x 1024 (32 bit colour) @ 60Hz

  • When the PC was first brought out the graphics adaptors were fairly primitive. As video technology improved more powerful graphical adapters were created. These compatible modes are still retained


Graphics screen1

Graphics Screen

  • The size of the graphics screen depends upon the display mode.

  • The graphics screen has a maximum size in the x direction and a maximum size in the y direction which is determined by the display type and mode.

  • Any point on the screen is accessed like a co-ordinate system using x and y co-ordinates.

  • The coordinates can either be integer or floating point values. It depends the graphics system that you use.


Integer coordinates

X: 0 to maxX

Y: 0 to maxY

0,0 may be bottom left corner or top left corner.

Advantages

Easy to understand

Traditionally used this way

Disadvantages

Will not easily scale when resized

0,0 can be in 2 possible positions depending upon graphics library

Expects the graphics adapter to be in a set resolution.

Not generic enough for all the different computer systems.

Integer Coordinates

0,maxY

maxX,maxY

maxX,0

0,0


Floating point coordinates

X: -1 to +1

Y: -1 to +1

(0,0) at centre of screen

Advantages

Generic across systems

Easily scaled

Disadvantages

Not all graphics libraries use FP coords

Requires a portable library

Floating Point Coordinates

-1,+1

+1,+1

0,0

-1,-1

+1,-1


Windowing systems

Windowing systems

  • A window is an area on the screen in which you can draw images.

  • A window size must be specified.

  • There can be multiple windows open at the same time.

  • Only one window is active (available for mouse, keyboard) at a time. This is sometimes called focus.

  • All windows respond to actions eg drawing action, mouse action, keyboard action, close action, resize action, minimise action, maximise action etc.

  • The operating system handles the windowing functions.


Opengl portable graphics

openGL – Portable Graphics

  • GL is a portableset of functions that allow 2D graphics, 3D graphics, windowing, mouse control, and a multitude of shading/lighting effects. This is the graphics engine used in many games.

  • openGL is the open source version of GL

  • openGL is designed to be a set of portable functions that interact with the features of the windowing system that it overlies. Typically the windowing system is optimized for the 3D acceleration of the underlying graphics card. Ie the windowing system interacts with the powerful drivers.

  • In the laboratory the openGL interacts with the X11 windowing system which relies on an Xserver.


Opengl and glut

openGL and glut

  • The basic windowing functioning can be quite complex to initiate in the OpenGL environment.

  • A GLUtility Toolbox was created (known as GLUT).

  • GLUT is a set of functions that overly the GL and have simplified calls to create window.

  • Glut and openGL are different libraries and must be linked in accordingly.

  • The X windowing libraries must be linked in as well.

    • (These libraries start with a capital X)


Writing an opengl glut program

Writing an openGL & glut program

  • The basic concept is that every window created will generate a series of events. Note that in order to use the openGL drawing functions then a window must exist for it to draw in. [Aside: it is possible to put the system into fullscreen mode]

  • The standard window operations such as minimize , maximize , destroy , close etc are handled by the windowing system. Note that we can override their actions if we wish.

  • When an event occurs, the window system will call your functions to handle that event. These are known as callback functions.


Writing an opengl glut program cont

Writing an openGL & glut program (cont)

  • The callback functions are functions you write to handle the events generated. Some typical events are:

    - a key was pressed. (Key event)

    - a window is created. (Draw event)

    - a single or double mouse click. (Mouse event)

  • The window needs to know which function to call when an specific event occurs. Therefore the callback function needs to be registered with the event. ie when an event occurs then the handler will call the function you specify.

  • If no callback functions are registered to an event then no action is performed on that event occurring.


Writing an opengl glut program cont1

Writing an openGL & glut program (cont)

  • The typical events generated by glut are for

    • drawing the window

    • refreshing the window

    • key press handling

    • mouse handling

    • There are many more

  • In the graphics template and example programs given later, only the draw and key press events are handled with all other events ignored.


Writing an opengl glut program cont2

Writing an openGL & glut program (cont)

  • A typical sequence for programming openGL using glut is given below:

    • Initiate the glut system

    • Define the window size

    • Define the coordinate system used if required

    • Create the window with the title specified.

    • Register the callback functions for all the events.

    • Call the glut main loop which loops indefinitely waiting for an event to occur.


Drawing in opengl

Drawing in openGL

  • The window must be cleared initially using :

    glClear(GL_COLOR_BUFFER_BIT)

  • The colors to be drawn must be defined prior to drawing using :

    glColor3f(Red, Green, Blue)

  • The colors are made by varying the intensities of the red, green, blue. (known as rgb values)

  • The colours are floating point values varying between

    • 0 (off)

    • 1 (fully on)


Drawing objects in opengl

GL bases its drawing on vertices (corners) which are (x,y) coordinate pairs

Points1 vertex

Lines2 vertices

PolyN vertices

All vertices must be stored as a list.

Drawing will not start until openGL is told to start drawing.

The typical sequence for drawing in openGL is:

- define the colour to use to draw

- state how to connect vertices

- list the vertices to draw between

- issue a command to start drawing now

Drawing objects in openGL


Listing the vertices in opengl

Listing the vertices in openGL

  • The vertices are defined between glBegin() and glEnd() functions. The glBegin() defines the type of item to draw and therefore the number of vertices to use.

    GL_POINTS(1 vertex)

    GL_LINES (2 vertices)

  • The vertices are listed with a glVertex2f() function for each (x,y) coordinate pair.

    Eg

    glBegin(GL_POINTS) ;

    glVertx2f(0.5,0.5) ;// list of vertices to draw as point

    glVertex2f(-0.5,-0.5) ;

    glEnd()


Issuing a draw command in opengl

Issuing a draw command in openGL

  • All the graphics actions are stacked into a buffer to be performed in the order they are placed into the buffer.

  • To immediately force a graphics action then you must flush the buffer.

  • The function to do this is glFlush().


Format of opengl functions

Format of openGL Functions

  • All the openGL functions are of the following format:

    glfunction#i(integer arguments)

    glfunction#f(floating point arguments)

    where

    #meansthe number of arguments used to represent the action eg 2 or 3 for 2D or 3D coordinate systems etc

    i means that the arguments used are integers.

    f means that the arguments used are floating point values.


Format of glut functions

Format of glut Functions

  • All the glut functions are of the following format:

    glutFunction(arguments)

  • The function starts with an uppercase letter


Header files required

Header files required

#include <GL/gl.h>/* Header File For The openGL32 Library */

#include <GL/glu.h>/* Header File For The glu32 Library */

#include <GL/glut.h>/* Header File For The glut Library */


Specifics to drawing in opengl

Specifics to drawing in openGL

  • To compile a graphics program.

    • To compile the graphics you need to add the graphics libraries. The libraries to add are given below.

    • These need to be added to end of the gcc command.

      -L/usr/X11R6/lib-lglut-lGL–lGLU –lXext –lX11

      Eg

      gcc -L/usr/X11R6/lib -lglut -lGL –lGLU –lXext –lX11 –lm glprog.c –o glprog


Initialising the graphics system

Initialising the graphics system

glutInit(int *argc, char **argv);

  • This function initialises the graphics and negotiates a session with the window manager

    glutInitWindowSize(int width, int height);

  • This function sets the size in pixels for the window but does not create a window.

    glutCreateWindow (char titlestring[ ]);

  • This function creates a top level window and gives the title bar the name specified by titlestring. The window is created to size specified by glutInitWindowSize().


Initialising the graphics system1

Initialising the graphics system

glOrtho(GLdouble left,GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)

  • This functions maps the screen size in pixels to the native coordinate system used in gl.

  • The native coordinate system for openGL is:

    -1 <= x <= 1

    -1 <= y <= 1

  • Therefore to access the graphics window as pixels the window extents in pixels must be mapped to the native coordinate system. After this function all operations on the graphics window will be in pixels.

    eg

    glOrtho(0, MAXX, 0, MAXY, -1, 1) to map GL coords to VGA coords

    -1 <= x <= 1 0 <= x <= 640

    -1 <= y <= 10 <= y <= 480


Registering callback functions

Registering callback functions

glutDisplayFunc(function_name)

  • This function registers the callback function function_name with the current graphics window. This means that when the window is displayed openGL will call function_name. Aside: This is really using a function pointer.

    glutKeyboardFunc(function_name)

  • This function registers the callback function function_name with the current graphics window. This means that when a key is pressed, openGL will call function_name. Aside: This is really using a function pointer.


Waiting for events to happen

Waiting for events to happen

glutMainLoop()

  • This is the main event handler in the system. It waits for an event and then calls the function associated with that event.

  • When a keyboard event is generated through a keypress then it calls the function registered using glutKeyboardFunc() .

  • In the graphics template given later the function called is myKey(). The arguments passed to the myKey(unsigned char key, int mouse_x, int mouse_y) function are the key that was pressed and the mouse’s x,y coordinates when that key was pressed.


Closing the graphics system

Closing the graphics system

  • The graphics system is closed either through the user pressing the destroy/close button on the window title bar or the program calling the glutDestroyWindow() function. Note that the glutGetWindow() function must be called to identify the window to destroy.

    glutDestroyWindow(int win_identifier)

    • This function tells the window system to close the window. Note that you must call glutGetWindow() to get the window identifier before calling this function.

      intglutGetWindow()

    • Return the identifier for the current window. The current window is the window that is in focus.


Graphics template floating point

Graphics Template (floating point)

#include <GL/gl.h>/* Header File For OpenGL32 Library */

#include <GL/glu.h>/* Header File For The GLu32 Library */

#include <GL/glut.h>/* Header File For The GLut Library */

#include <stdlib.h>

#include <stdio.h>

/* Define the Window dimensions */

#define WIN_WIDTH1024/* WINDOW Width in pixels */

#define WIN_HEIGHT768/* WINDOW Height in pixels */

/* Define the key to exit the system */

#define ESC27

/* function prototypes */

void myDraw(void) ;

void myKey(unsigned char key,int x, int y)


Graphics template fp continued

Graphics Template FP (continued)

/* main program – execution begins here */

int main(void)

{

glutInit(&argc, argv); // initialise gl utility system

glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT); // define window size

glutCreateWindow ("FP GRAPHICS WINDOW"); // create window with title

/* Register the callback functions to the current window */

glutDisplayFunc(myDraw); // register function for drawing event

glutKeyboardFunc(myKey);// register function for keyboard event

/* Draw window and loop forever. Any keypress will call mykey() */

glutMainLoop();

/* program will never get here */

}


Graphics template fp continued1

Graphics Template FP (continued)

void myDraw(void)

{

glClear(GL_COLOR_BUFFER_BIT) ; /* clear the screen */

/* vertices list and drawing function go here */

/* vertex list uses glVertex2f() function */

glFlush() ;/* force the graphics system to act */

}


Graphics template fp continued2

Graphics Template FP (continued)

void myKey(unsigned char key,int x, int y)

{

int win ;

/* key press actions go here */

/* check if keypressed was the ESC key then close window and leave program */

if (key == ESC)

{

win=glutGetWindow() ; /* get identifier for current window */

glutDestroyWindow(win) ; /* close the current window */

exit(-1) ; /* leave the program */

}

}


Graphics template integer

Graphics Template (Integer)

#include <GL/gl.h>/* Header File For OpenGL32 Library */

#include <GL/glu.h>/* Header File For The GLu32 Library */

#include <GL/glut.h>/* Header File For The GLut Library */

#include <stdlib.h>

#include <stdio.h>

/* Define the Window dimensions */

#define WIN_WIDTH1024/* WINDOW Width in pixels */

#define WIN_HEIGHT768/* WINDOW Height in pixels */

/* Define the key to exit the system */

#define ESC27

/* function prototypes */

void myDraw(void) ;

void myKey(unsigned char key,int x, int y)


Graphics template int continued

Graphics Template INT (continued)

/* main program – execution begins here */

int main(void)

{

glutInit(&argc, argv); // initialise gl utility system

glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT); // define window size

glOrtho(0,WIN_WIDTH,0,WIN_HEIGHT,-1.0,1.0); // map to pixel coord system

glutCreateWindow ("INT GRAPHICS WINDOW"); // create window with title

/* Register the callback functions to the current window */

glutDisplayFunc(myDraw); // register function for drawing event

glutKeyboardFunc(myKey);// register function for keyboard event

/* Draw window and loop forever. Any keypress will call mykey() */

glutMainLoop();

/* program will never get here */

}


Graphics template int continued1

Graphics Template INT (continued)

void myDraw(void)

{

glClear(GL_COLOR_BUFFER_BIT) ; /* clear the screen */

/* vertices list and drawing function go here */

/* vertex list uses glVertex2i() function

glFlush() ;/* force the graphics system to act */

}


Graphics template int continued2

Graphics Template INT (continued)

void myKey(unsigned char key,int x, int y)

{

int win ;

/* key press actions go here */

/* check if keypressed was the ESC key then close window and leave program */

if (key == ESC)

{

win=glutGetWindow() ; /* get identifier for current window */

glutDestroyWindow(win) ; /* close the current window */

exit(-1) ; /* leave the program */

}

}


Drawing colours

Drawing - Colours

  • Colours (RGB)

    glColor3f(float red, float green, float blue)

    • Sets the current drawing color using red, green, blue floating point values as arguments.

    • 1.0 is full intensity

    • 0.0 is zero intensity

    • Any value between 0 and 1 is that fraction of full intensity eg 0.5 is half intensity.

    • A typical 16 (EGA) colour set is given below. Note that the standard names have been used so that LIGHT actually refers to darker eg LIGHTBLUE is dark blue (lower intensity than blue):


Drawing basic colours

Drawing – Basic Colours


Verticies and drawing detailed

Verticies and Drawing (Detailed)

  • In openGL the drawing is grouped into blocks.

  • Each block identifies the type of drawing to be performed eg drawing points, lines, polygons etc.

  • All drawing is based upon vertices which are a set of single (x,y) coordinates. The vertices must be contained within the drawing block.

  • If the drawing mode is POINTS then it only requires one (x,y) coordinate set to identify the position to draw on the screen.

  • If the drawing mode is LINES then it requires two (x,y) coordinate sets to identify the position to draw on the screen. ie (x1,y1), (x2,y2)

  • There can be any number of blocks used in the program. The blocks are identified between the glBegin() and glEnd() functions. The glBegin(int mode) requires the drawing mode to be passed as the argument. It is permissible to have many blocks with the same drawing mode.


Verticies and drawing detailed1

Verticies and Drawing (Detailed)

http://fly.srk.fer.hr/~unreal/theredbook/chapter02.html


Verticies and drawing detailed2

Verticies and Drawing (Detailed)

glBegin(mode)

  • This function identifies the start of a drawing block and requires the drawing mode to be passed as the argument. The valid modes are:

    • GL_POINTS

      • Treats each vertex as a single point. Vertex n defines point n.N points are drawn.

    • GL_LINES

      • Treats each pair of vertices as an independent line segment. Vertices 2n−1 and 2n define line n. N/2 lines are drawn.

    • GL_LINE_STRIP

      • Draws a connected group of line segments from the first vertex to the last. Vertices n and n+1 define line n. N−1 lines are drawn.


Verticies and drawing detailed3

Verticies and Drawing (Detailed)

  • GL_LINE_LOOP

    • Draws a connected group of line segments from the first vertex to the last, then back to the first. Vertices n and n+1 define line n. The last line, however, is defined by vertices N and 1N lines are drawn.

  • GL_TRIANGLES

    • Treats each triplet of vertices as an independent triangle. Vertices 3n−2,3n−1, and 3n define triangle n. N/3 triangles are drawn.

  • GL_TRIANGLE_STRIP

    • Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. For odd n, vertices n, n+1, and n+2 define triangle n. For even n, vertices n+1, n, and n+2 define triangle n. N−2 triangles are drawn.


  • Verticies and drawing detailed4

    Verticies and Drawing (Detailed)

    • GL_TRIANGLE_FAN

      • Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. Vertices 1, n+1, and n+2 define triangle n. N−2 triangles are drawn.

  • GL_QUADS

    • Treats each group of four vertices as an independent quadrilateral. Vertices 4n−3, 4n−2, 4n−1, and 4n define quadrilateral n. N/4 quadrilaterals are drawn.

  • GL_QUAD_STRIP

    • Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair. Vertices 2n−1, 2n, 2n+2, and 2n+1 define quadrilateral n. N/2−1 quadrilaterals are drawn. Note that the order in which vertices are used to construct a quadrilateral from strip data is different from that used with independent data.


  • Verticies and drawing detailed5

    Verticies and Drawing (Detailed)

    • GL_POLYGON

      • Draws a single, convex polygon. Vertices 1 through N define this polygon.

        glEnd()

  • This function identifies the end of a drawing block.


  • Defining vertices detailed

    Defining Vertices (Detailed)

    • Vertices are specified using the following commands. The 2D vertex is specified through its x and y coordinates

      2D

      glVertex2i(int x, int y) integer coordinates

      glVertex2f(float x, float y) floating point coordinates

      Note: a 3D coordinate system requires 3 arguments eg for a 3D integer coordinate system use glVertex3i(int x, int y, int z)

      Example to draw 2 separate points

      glBegin(GL_POINTS) ;

      glVertex2i(300,300) ;

      glVertex2i(310,310) ;

      glEnd() ;


    Shapes

    Shapes

    • Shapes

      • Rectangle

        • A rectangle can be drawn using verticies between glBegin() and glEnd() functions OR can be drawn using the glRectf() or glRecti() function.

          glRectf(left, top, right, bottom)

        • A rectangle is defined by specifying 2 corners

    (Top,Left)

    (Bottom,Right)


    Shapes1

    Shapes

    • Rectangle (continued)

      • By specifying the top-left corner and the bottom-right corner the other two corners can be deduced.

      • The four corners of a rectangle are (left, top), (left, bottom), (right, top), (right, bottom). These points contain the left, top, right, and bottom co-ordinates. Note that all these co-ordinates are contained in the 2 corners (left, top), (right,bottom), therefore the other two corners can be deduced.


    Shapes2

    R

    (x,y)

    Shapes

    • Circle

      • There is no circle function but it can be drawn using points. To do this we note a circle is defined by the centre point (x,y) and the radius.

    If we sweep the point at R from the centre through 360o then we draw a circle. Note that the C language uses radians as its default trigonometric unit.

    A circle can be drawn by looping ω through 0 to 2π and plotting at the coordinate pair given below:

    ( x+Rcos(ω) , y+Rsin(ω) )

    where

    x,yare the centre coordinates

    Ris the radius

    ωis the angle to sweep ( 0 to 2π ).


    Using text with graphics

    Using Text with Graphics

    • Sometimes you may want to use text on the graphics screen. For example, labelling axes of graphs, labelling parts of a diagram etc.

    • The graphics printing functions allow a character to be drawn at any position on the screen.

    • The graphics functions also allow changing the font style, size, and direction. The font is the style of the letters. For example Greek letters would require a different font to ordinary letters.


    Using text with graphics1

    Using Text with Graphics

    • The position to draw the character on the screen can be specified using glRasterPos2f() or glRasterPos2i() .

      • This point represents the bottom left corner of the character.

        glRasterPos2f(float x, float y)

        glRasterPos2i(int x, int y)

    • The character can be drawn using glutBitmapCharacter() which expects the font type and the character. Note that the character position can be set by glRasterPos2f() otherwise it extends from the position given allowing another character to be drawn in the next character position.

    Hello World

    (x,y)


    Using text with graphics2

    Using Text with Graphics

    glutBitmapCharacter(font, character)

    • Draws the character in the specified font on the graphics screen. Note that the character position can be set by glRasterPos2f() otherwise it extends from the position given allowing another character to be drawn in the next character position.

    • The fonts follow the X11 standard fonts

    • It is possible to use other X11

    • The 7 fonts on the next page come from the GLUT system. These are 2D bitmap fonts.


    Using text with graphics fonts

    Using Text with Graphics - Fonts

    GLUT_BITMAP_8_BY_13

    A fixed width font with every character fitting in an 8 by 13 pixel rectangle.

    GLUT_BITMAP_9_BY_15

    A fixed width font with every character fitting in an 9 by 15 pixel rectangle.

    GLUT_BITMAP_TIMES_ROMAN_10

    A 10-point proportional spaced Times Roman font.

    GLUT_BITMAP_TIMES_ROMAN_24

    A 24-point proportional spaced Times Roman font.

    GLUT_BITMAP_HELVETICA_10

    A 10-point proportional spaced Helvetica font.

    GLUT_BITMAP_HELVETICA_12

    A 12-point proportional spaced Helvetica font.

    GLUT_BITMAP_HELVETICA_10

    A 18-point proportional spaced Helvetica font.


    Example for writing text to the screen

    Example for writing text to the screen

    • This code fragment writes an integer to the screen at position (200,200) in 10pt Times-Roman font.

    • sprintf() writes the integer into the string. sprintf() is similar to fprintf() except that the string name is used instead of the file pointer.

    • sprintf() requires #include <string.h>


    Example for writing text to the screen1

    Example for writing text to the screen

    int main(void)

    {

    char text[100] ;/* holds the text to be displayed */

    int i ;/* loop counter */

    int num=42 ;/* the meaning of life (HHGTTG – Douglas Adams */

    /* create the text to be displayed */

    sprintf(text,"The Integer value is %i",num) ;

    /* set the coordinate to start writing */

    glRasterPos2i(-0.2,0.2) ;

    /* write the string one character at a time */

    for (i=0; string[i] != '\0'; i++)

    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, string[i]) ;

    }


    Example graphics program fp coords

    Example Graphics Program – FP coords

    #include <GL/gl.h> /* Header File For The OpenGL32 Library */

    #include <GL/glu.h> /* Header File For The GLu32 Library */

    #include <GL/glut.h> /* Header File For The GLut Library */

    #include <stdlib.h>

    #include <stdio.h>

    /* Define the Window dimensions */

    #define WIN_WIDTH1024 /* WINDOW Width in pixels */

    #define WIN_HEIGHT768 /* WINDOW Height in pixels */

    /* Define the key to exit the system */

    #define ESC27

    /* function prototypes */

    void myDraw(void) ;

    void myKey(unsigned char key,int x, int y) ;


    Example graphics program int main void

    Example Graphics Programint main(void)

    /* main program – execution begins here */

    int main(int argc, char** argv)

    {

    glutInit(&argc, argv); /* initialise gl utility system */

    glutInitWindowSize (WIN_WIDTH, WIN_HEIGHT); /* define window size */

    glutCreateWindow ("GRAPHICS WINDOW"); /* create window with title */

    glutDisplayFunc(myDraw); /* register myDraw() to current window */

    glutKeyboardFunc(myKey); /* register myKey() to current window */

    /* Draw window and loop forever. Any keypress will call mykey() */

    glutMainLoop();

    /* Program should never reach here*/

    }


    Example graphics program void mydraw void

    Example Graphics Programvoid myDraw(void)

    /*

    myDraw()

    The drawing function that is called after the window is created.

    This function has to be registered in the glutDisplayFunc()

    This function draws :

    2 red pixels

    1 blue line

    1 rectangle in green

    1 string in pink

    */


    Example graphics program void mydraw void continued

    Example Graphics Programvoid myDraw(void) (continued)

    void myDraw(void)

    {

    int i ;

    char string[ ] ="Hello world" ;

    /* clear the screen */

    glClear(GL_COLOR_BUFFER_BIT) ;

    /* draw individual points in red */

    glColor3f(1,0,0) ;/* Set the drawing color to Red */

    glBegin(GL_POINTS) ;

    glVertex2f(-0.41,-0.22) ;

    glVertex2f(-0.39,-0.19) ;

    glEnd() ;


    Example graphics program void mydraw void continued1

    Example Graphics Programvoid myDraw(void) (continued)

    /*

    Draw lines therefore require 2 points

    Lines are drawn in blue

    */

    glColor3f(0,1,0) ;/* Set the drawing color to Green */

    glBegin(GL_LINES) ;

    glVertex2f(-0.22,0.04) ;

    glVertex2f(0.02,0.30) ;

    glEnd() ;

    /* Draw a rectangle in Blue (left,top, right, bottom) */

    glColor3f(0,0,1) ;/* Set the drawing color to green */

    glRectf(-0.98,-0.97,-0.61,-0.48) ;/ * bottom left quadrant of window */


    Example graphics program void mydraw void continued2

    Example Graphics Programvoid myDraw(void) (continued)

    /* write the string at position in pink in TIMES ROMAN 10 point font*/

    glColor3f(1,0,1) ; /* Set the drawing colour to pink */

    glRasterPos2f(-0.61,0.56) ; /* Set the coordinates to write */

    /* write the string a character at a time

    for (i=0; string[i] != '\0'; i++)

    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, string[i]) ;

    /*

    flush the graphics buffer to screen

    ie force the graphics to appear in the window

    */

    glFlush() ;

    }


    Example graphics program void mykey unsigned char key int x int y

    Example Graphics Programvoid myKey(unsigned char key,int x, int y)

    /*

    myKey()

    The keyboard function that is called after the window is created and a key is pressed.

    This function has to be registered in the glutKeyboardFunc()

    */

    void myKey(unsigned char key,int x, int y)

    {

    int win ;

    /* check if keypressed was the ESC key */

    if (key == ESC)

    {

    win=glutGetWindow() ; /* get identifier for current window */

    glutDestroyWindow(win) ;/* close the current window */

    exit(-1) ; /* leave the program */

    }

    }


    Example graphics program int coords

    Example Graphics Program – int coords

    #include <GL/gl.h> /* Header File For The OpenGL32 Library */

    #include <GL/glu.h> /* Header File For The GLu32 Library */

    #include <GL/glut.h> /* Header File For The GLut Library */

    #include <stdlib.h>

    #include <stdio.h>

    /* Define the Window dimensions */

    #define WIN_WIDTH1024 /* WINDOW Width in pixels */

    #define WIN_HEIGHT768 /* WINDOW Height in pixels */

    /* Define the key to exit the system */

    #define ESC27

    /* function prototypes */

    void myDraw(void) ;

    void myKey(unsigned char key,int x, int y) ;


    Example graphics program int main void1

    Example Graphics Programint main(void)

    /* main program – execution begins here */

    int main(int argc, char** argv)

    {

    glutInit(&argc, argv); /* initialise gl utility system */

    glutInitWindowSize (WIN_WIDTH, WIN_HEIGHT); /* define window size */

    glOrtho(0,WIN_WIDTH,0,WIN_HEIGHT,-1.0,1.0) ; /* map to pixel values */

    glutCreateWindow ("GRAPHICS WINDOW"); /* create window with title */

    glutDisplayFunc(myDraw); /* register myDraw() to current window */

    glutKeyboardFunc(myKey); /* register myKey() to current window */

    /* Draw window and loop forever. Any keypress will call mykey() */

    glutMainLoop();

    /* Program should never reach here*/

    }


    Example graphics program void mydraw void1

    Example Graphics Programvoid myDraw(void)

    /*

    myDraw()

    The drawing function that is called after the window is created.

    This function has to be registered in the glutDisplayFunc()

    This function draws :

    2 red pixels

    1 blue line

    1 rectangle in green

    1 string in pink

    */


    Example graphics program void mydraw void continued3

    Example Graphics Programvoid myDraw(void) (continued)

    void myDraw(void)

    {

    int i ;

    char string[ ] ="Hello world" ;

    /* clear the screen */

    glClear(GL_COLOR_BUFFER_BIT) ;

    /* draw individual points in red */

    glColor3f(1,0,0) ;/* Set the drawing color to Red */

    glBegin(GL_POINTS) ;

    glVertex2i(300,300) ;

    glVertex2i(310,310) ;

    glEnd() ;


    Example graphics program void mydraw void continued4

    Example Graphics Programvoid myDraw(void) (continued)

    /*

    Draw lines therefore require 2 points

    Lines are drawn in blue

    */

    glColor3f(0,1,0) ;/* Set the drawing color to Blue */

    glBegin(GL_LINES) ;

    glVertex2i(400,400) ;

    glVertex2i(500,500) ;

    glEnd() ;

    /* Draw a rectangle in Green (left,top, right, bottom) */

    glColor3f(0,0,1) ;/* Set the drawing color to green */

    glRecti(10,10,200,200) ;/* bottom left quadrant of window */


    Example graphics program void mydraw void continued5

    Example Graphics Programvoid myDraw(void) (continued)

    /* write the string at position in pink in TIMES ROMAN 10 point font*/

    glColor3f(1,0,1) ; /* Set the drawing colour to pink */

    glRasterPos2i(200,600) ; /* Set the coordinates to write */

    /* write the string a character at a time

    for (i=0; string[i] != '\0'; i++)

    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, string[i]) ;

    /*

    flush the graphics buffer to screen

    ie force the graphics to appear in the window

    */

    glFlush() ;

    }


    Example graphics program void mykey unsigned char key int x int y1

    Example Graphics Programvoid myKey(unsigned char key,int x, int y)

    /*

    myKey()

    The keyboard function that is called after the window is created and a key is pressed.

    This function has to be registered in the glutKeyboardFunc()

    */

    void myKey(unsigned char key,int x, int y)

    {

    int win ;

    /* check if keypressed was the ESC key */

    if (key == ESC)

    {

    win=glutGetWindow() ; /* get identifier for current window */

    glutDestroyWindow(win) ;/* close the current window */

    exit(-1) ; /* leave the program */

    }

    }


    Further opengl notes

    Further openGL Notes

    • More functions can be found in the appendices on the website.

    • This is only a small subset of the openGL graphics system. OpenGL is a powerful graphics language containing many 3D function to render bitmaps, 3D shape rotation, lighting etc. It is the engine of many games and many graphics adapters are optimized to perform the openGL commands in hardware.

    • There are many good tutorials on the web.


  • Login