AI for Games

Document Sample
AI for Games Powered By Docstoc
					CSC345: Advanced Graphics &
   Virtual Environments
            Lecture 1: Introduction to OpenGL (1)
                         Patrick Olivier
                                 p.l.olivier@ncl.ac.uk
                         2nd floor in the Devonshire Building


30/1/2006      Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   1
Course structure
      Introduction to OpenGL
      Visual appearance (fogging & transparency)
      Discrete Techniques (aliasing & textures)
      Advanced lighting & shading (shadows & reflections)
      Curves & curved surfaces
      Solid object modelling
      Visualisation
      Procedural modelling
      Acceleration algorithms
      Intersection and collision detection
      Non-photorealistic rendering
      Virtual environments
31/1/2006     Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   2
Required reading (1)

                                                        Edward Angel:
                                                        “Interactive Computer
                                                        Graphics: A Top-Down
                                                        Approach Using OpenGL”
                                                        3rd Edition
                                                        Addison Wesley, 2002
                                                        Required for theory

31/1/2006   Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   3
Required reading (2)
      Put image here
                                                     Edward Angel:
                                                     “OpenGL: A Primer”
                                                     2nd Edition
                                                     Addison Wesley, 2004
                                                     Compulsory for practicals




31/1/2006   Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   4
Extra reading…
      Put image here
                                                     Tomas Akeine-Möller &
                                                     Eric Haines:
                                                     “Real-time Rendering”
                                                     2nd Edition
                                                     AK Peters, 2002
                                                     Advanced topics & for
                                                     serious graphics types

31/1/2006   Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   5
Extra reading…
      Put image here
                                                     “OpenGL Programming
                                                     Guide: The Official Guide
                                                     to Learning OpenGL”
                                                     4th Edition
                                                     Addison Wesley, 2004
                                                     Old version online: see
                                                     module webpages


31/1/2006   Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   6
Practical (programming)
schedule…
1.      Two-dimensional programming & C (OpenGL primer: ch. 2)
2.      Two-dimensional programming & C (OpenGL primer: ch. 2)
           Exercise 1 (worth 3% is due: 13th February)
3.      Interaction & animation (OpenGL primer: ch. 3)
4.      Basic three-dimensional programming (OpenGL primer: ch. 4)
5.      Transformations (OpenGL primer: ch. 5)
           Exercise 2 (worth 3% is due: 6th March)
6.      Lights & materials (OpenGL primer: ch. 6)
7.      Images (OpenGL primer: ch. 7)
8.      Texture mapping (OpenGL primer: ch. 8)
           Exercise 3 (worth 4% is due: 27th March)
9.      Curves & surfaces (OpenGL primer: ch. 9)
           Exercise 4 (worth 15% is due: 12th May)
           Exam is worth 80% date to be confirmed


31/1/2006        Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   7
OpenGL library
      OpenGL core library
           OpenGL32 on Windows
           GL on most unix/linux systems (libGL.a)
      OpenGL Utility Library (GLU)
           Provides functionality in OpenGL core but avoids
            having to rewrite code
      Links with window system
           GLX for X window systems
           WGL for Windows
           AGL for Macintosh

31/1/2006       Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   8
GLUT library
      OpenGL Utility Toolkit (GLUT)
      Provides functionality common to all window
       systems
           open a window
           get input from mouse and keyboard
           menus
           event-driven
      Code is portable but GLUT lacks functionality of
       a good toolkit for a specific platform
           e.g. no slide bars

31/1/2006       Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   9
OpenGL architecture
              Immediate Mode
                                                                                  geometry pipeline
                                             Per Vertex
              Polynomial                    Operations &
               Evaluator                      Primitive
                                             Assembly



            Display                                                                   Per Fragment    Frame
CPU           List
                                                     Rasterization
                                                                                       Operations     Buffer



                                                    Texture
                                                    Memor
                        Pixel                          y
                      Operations
31/1/2006      Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)                       10
OpenGL functions
      Primitives
           Points
           Line Segments
           Polygons
      Attributes
      Transformations
           Viewing
           Modeling
      Control (GLUT)
      Input (GLUT)
      Query

31/1/2006       Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   11
OpenGL state
      OpenGL is a state machine
      OpenGL functions are of two types
           Primitive generating
               Can cause output if primitive is visible
               How vertices are processed and appearance of
                primitive are controlled by the state
           State changing
               Transformation functions
               Attribute functions


31/1/2006       Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   12
OpenGL function format
      OpenGL is not object oriented so that there are multiple
       functions for a given logical function
           glVertex3f
           glVertex2i
           glVertex3dv
      Underlying storage mode is the same
      Function format:   glVertex3f(x,y,z)
      belongs to GL library                 function name dimensions x,y,z are floats

                                              glVertex3fv(p)
                                            p is a pointer to an array

31/1/2006       Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)    13
OpenGL #defines
      Most constants are defined in the include
       files gl.h, glu.h and glut.h
           Note #include <GL/glut.h> should
            automatically include the others
           Examples:
               glBegin(GL_POLYGON)
               glClear(GL_COLOR_BUFFER_BIT)
      include files also define OpenGL data
       types: GLfloat, GLdouble,….

31/1/2006       Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   14
A simple program: simple.c
#include <GL/glut.h>
void mydisplay(){
          glClear(GL_COLOR_BUFFER_BIT);
          glBegin(GL_POLYGON);
                  glVertex2f(-0.5, -0.5);
                  glVertex2f(-0.5, 0.5);
                  glVertex2f(0.5, 0.5);
                  glVertex2f(0.5, -0.5);
          glEnd();
          glFlush();
}
int main(int argc, char** argv){
          glutCreateWindow("simple");
          glutDisplayFunc(mydisplay);
          glutMainLoop();
}
31/1/2006     Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   15
Event loop & defaults
      program defines display callback function
       named mydisplay
           every glut program must have a display callback
           display callback is executed whenever OpenGL
            decides the display must be refreshed, (e.g the
            window is opened)
           main function end by entering an event loop
      simple.c is too simple
      makes heavy use of state variable defaults
           viewing / colours / window parameters
      next version makes defaults more explicit
31/1/2006       Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   16
Compilation
      Unix/linux
           include files usually in …/include/GL
           compile with –lglut –lglu –lgl loader flags
           may have to add –L flag for X libraries
           make file provided on module webpage
      Windows (Visual Studio)
           get glut.h, glut32.lib and glut32.dll from web
           create a console application
           add opengl32.lib, glut32.lib, glut32.lib to project
            settings (under link tab)

31/1/2006       Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   17
Program structure
      most OpenGL programs have same structure:
           main()
               defines the callback functions
               opens one or more windows with the required properties
               enters event loop (last executable statement)
           init()
               sets the state variables (viewing / attributes)
           callbacks…
               display function
               input and window functions
      rewrite simple.c that sets colour, view & window…

31/1/2006        Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   18
main.c
#include <GL/glut.h>

int main(int argc, char** argv)
{
   glutInit(&argc,argv);
   glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
   glutInitWindowSize(500,500); …define window size
   glutInitWindowPosition(0,0); …define window position
   glutCreateWindow("simple");
   glutDisplayFunc(mydisplay);
   init();          …set OpenGL state
   glutMainLoop(); …enter event loop
}
31/1/2006   Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   19
GLUT functions
      glutInit allows application to get command line
       arguments and initializes system
      gluInitDisplayMode requests properties for the window
       (the rendering context)
           RGB color
           single buffering
           properties logically ORed together
      glutWindowSize in pixels
      glutWindowPosition from top-left corner of display
      glutCreateWindow create window with title “simple”
      glutDisplayFunc display callback
      glutMainLoop enter infinite event loop



31/1/2006        Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   20
init.c
   void init()
   {
      glClearColor (0.0, 0.0, 0.0, 1.0);
      /* first 3 values = black, final = opaque */
      glColor3f(1.0, 1.0, 1.0);
      /* set current draw colour to white */
      glMatrixMode (GL_PROJECTION);
      glLoadIdentity ();
      glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
      /* bounds of the view volume */
   }

31/1/2006   Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   21
Coordinate systems & cameras
      units of glVertex in object coordinates
      viewing specifications also in object coordinates
      OpenGL converts to camera (eye) coordinates and
       later to screen coordinates
      OpenGL puts camera at
       the origin pointing in -z
       direction direction
      The default viewing
       volume is a box centered
       at the origin of length 2

31/1/2006     Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   22
Transformations and viewing
      In OpenGL, projection is carried out by a projection
       matrix (transformation)
      There is only one set of transformation functions so
       we must set the matrix mode first
       glMatrixMode (GL_PROJECTION)
      Transformation functions are incremental so:
           we start with an identity matrix
           alter it with projection matrix that gives the view volume
       glLoadIdentity();
       glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);


31/1/2006        Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   23
2-D and 3-D viewing
      The near & far distances are measured from
       the camera, in:
       glOrtho(left,right,bottom,top,near,far)
      Two-dimensional vertex commands place all
       vertices in the plane z=0
      If the application is in two dimensions, we can
       use the function:
       gluOrtho2D(left,right,bottom,top)
      In two dimensions, the view or clipping volume
       becomes a clipping window

31/1/2006    Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   24
mydisplay.c
void mydisplay()
{
       glClear(GL_COLOR_BUFFER_BIT);
       glBegin(GL_POLYGON);
          glVertex2f(-0.5, -0.5);
          glVertex2f(-0.5, 0.5);
          glVertex2f(0.5, 0.5);
          glVertex2f(0.5, -0.5);
       glEnd();
       glFlush();
}
31/1/2006     Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)   25

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:3/25/2012
language:
pages:25
yaohongm yaohongm http://
About