1 / 68

Graphics

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.

joelle
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphics openGL and glut

  2. 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

  3. 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

  4. 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

  5. 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.

  6. 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

  7. 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

  8. 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.

  9. 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.

  10. 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)

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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)

  16. GL bases its drawing on vertices (corners) which are (x,y) coordinate pairs Points 1 vertex Lines 2 vertices Poly N 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

  17. 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()

  18. 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().

  19. 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.

  20. Format of glut Functions • All the glut functions are of the following format: glutFunction(arguments) • The function starts with an uppercase letter

  21. 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 */

  22. 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

  23. 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().

  24. 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 <= 1 0 <= y <= 480

  25. 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.

  26. 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.

  27. 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.

  28. 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_WIDTH 1024 /* WINDOW Width in pixels */ #define WIN_HEIGHT 768 /* WINDOW Height in pixels */ /* Define the key to exit the system */ #define ESC 27 /* function prototypes */ void myDraw(void) ; void myKey(unsigned char key,int x, int y)

  29. 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 */ }

  30. 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 */ }

  31. 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 */ } }

  32. 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_WIDTH 1024 /* WINDOW Width in pixels */ #define WIN_HEIGHT 768 /* WINDOW Height in pixels */ /* Define the key to exit the system */ #define ESC 27 /* function prototypes */ void myDraw(void) ; void myKey(unsigned char key,int x, int y)

  33. 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 */ }

  34. 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 */ }

  35. 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 */ } }

  36. 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):

  37. Drawing – Basic Colours

  38. 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.

  39. Verticies and Drawing (Detailed) http://fly.srk.fer.hr/~unreal/theredbook/chapter02.html

  40. 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.

  41. 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.

  42. 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.

  43. 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.

  44. 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() ;

  45. 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)

  46. 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.

  47. 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,y are the centre coordinates R is the radius ω is the angle to sweep ( 0 to 2π ).

  48. 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.

  49. 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)

  50. 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.

More Related