570 likes | 596 Views
Discover what is needed, what tools are available, and how to design and implement a game engine in an agile way.
E N D
Game Engine Development • Yingcai Xiao
Game Engine Development What do we need? What tools do we have? How can we design and implement? We will answer those questions in an agile way.
Video Game: Interactive animation Display Device Driver (GDI) Input Device Driver Game (Software)
Video Game: Interactive animation Similar to all other programs: data, algorithms, input, output. Data: Game Objects Algorithms: Animation Input: Interactive Events Output: Display
Game Objects (GO) GO = Geometry + Attribute
Game Objects (GO) Geometry: primitives: points, lines, triangles, polygons, spheres tetrahedrons, hexahedrons, … meshes: grids: elevation, uniform, rectlinear, structured, tin: triangular integrated network
Game Objects (GO) Geometry: meshes: indexed Free/Smooth curves and surfaces: B-Splines
Game Objects (GO) Geometry: Stroked: stroked primitives stroked free curves and surfaces composited: avatars, prefabs, packages, …
Game Objects (GO) Conversions to indexed meshes:
Game Objects (GO) Convert a plane to an indexed mesh: y = 0; 0<=x<=10 0<=z<=10
Graphics Engine / Display Engine / Shading Engine
OpenGL: https://www.opengl.org • Open Graphics Library • SGI • Open Source • De facto Industry Standard
OpenGL: https://www.opengl.org • With shaders to support GPU. • Without shaders for easy learning. • We are going to start without shaders.
OpenGL: https://www.opengl.org • Tutorial with shaders: • http://www.opengl-tutorial.org • Tutorial without shaders: http://www.cs.uccs.edu/~ssemwal/indexGLTutorial.html • We are going to have examples for both but start with http://www.cs.uccs.edu/~ssemwal/indexGLTutorial.html without shaders.
Computer Graphics Textbooks Edward Angel, University of New Mexico Dave Shreiner, ARM, Inc Interactive Computer Graphics: A Top-Down Approach Using OpenGL, 5E. (no shaders) Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL, 6/E Interactive Computer Graphics: A Top-Down Approach with WebGL, 7/E
Rotating Color CubeAn OpenGL Example for Interactive Animationwithout Using a Game Enginehttps://youtu.be/cwbL47ovzFYhttps://www.youtube.com/watch?v=L5jloVbgAdk
Interactive Animation Programming with OpenGL • Input: interaction, user control: EDP (event driving programming). • Output: Graphics: OpenGL API. • Data Structures: graphics objects representations. • Algorithms: animations in event handlers. • Non OOP: Using C, a subset of C++, for speed.
An OpenGL Example for Interactive Animation • Users: interactive 3D animation • Programmers: event-driven 3D animation of geometric transformations. • C part of C++ • OpenGL for display • Glut (GL Utility Toolkit) for interaction • https://www.opengl.org/resources/libraries/glut/
The “Color Cube” example • The “main” function: • initializes windows, display settings, and event handlers. • Starts the event loop. • Post the first Redisplay event.
The “Color Cube” example • // Initialization • While (1) { // event loop • event = getEvent(); // from the queue • switch (event) { • case Redisplay: display (); break; • case Keyboard: keyboard(); break; • default: idle(); • } }
The “Color Cube” example • The “display” function: • draws the game objects • OpenGL is a state machine: set the active parameters before specifying vertices. • Per vertex setting: color, normal, texture, … • New vertices will use the current settings until they are changed.
The “Color Cube” example • The “keyboard” function: • The event handler for keyboard events. • Change display parameters to control the animation. E.g. rotation angles, parametric variable t, simulation variables such as speed. • Don’t directly call the “display” function. • Post the Redisplay event after everything is set. glutPostRedisplay();
The “Color Cube” example • The “idle” function: • The event handler for the “idle” event. • Invoked when no more event in the event queue. • Change display parameters to control the background animation. E.g. rotation angles, parametric variable t, simulation variables such as speed. • Don’t directly call the “display” function. • Post the Redisplay event after everything is set. glutPostRedisplay();
The “Color Cube” example // Data structures and data, no objects enum AXIS { xAxis = 0, yAxis = 1, zAxis = 2}; double rotate_y=2; double rotate_x=2; double rotate_z=2; int axis = xAxis;
The “Color Cube” example void display(){ // Clear screen and Z-buffer glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // Set rotation matrix to identity, no rotation // Set rotation (amount of rotation from initial position, vector of rotation) glRotatef( rotate_x, 1.0, 0.0, 0.0 ); // (amount of rotation, x axis) glRotatef( rotate_y, 0.0, 1.0, 0.0 ); // (amount of rotation, y axis) glRotatef( rotate_z, 0.0, 0.0, 1.0 ); // (amount of rotation, z axis)
The “Color Cube” example // display() function continues // Create the color cube (Game Object) as six colored surfaces // FRONT glBegin(GL_POLYGON); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.5, -0.5, -0.5 ); // P1 is red glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 0.5, 0.5, -0.5 ); // P2 is green glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -0.5, 0.5, -0.5 ); // P3 is blue glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( -0.5, -0.5, -0.5 ); // P4 is purple glEnd();
The “Color Cube” example // display() function continues // Create the color cube (Game Object) as six colored surfaces // White side - BACK glBegin(GL_POLYGON); glColor3f( 1.0, 1.0, 1.0 ); glVertex3f( 0.5, -0.5, 0.5 ); glVertex3f( 0.5, 0.5, 0.5 ); glVertex3f( -0.5, 0.5, 0.5 ); glVertex3f( -0.5, -0.5, 0.5 ); glEnd();
The “Color Cube” example // display() function continues // Create the color cube (Game Object) as six colored surfaces // Purple side - RIGHT glBegin(GL_POLYGON); glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( 0.5, -0.5, -0.5 ); glVertex3f( 0.5, 0.5, -0.5 ); glVertex3f( 0.5, 0.5, 0.5 ); glVertex3f( 0.5, -0.5, 0.5 ); glEnd();
The “Color Cube” example // display() function continues // Create the color cube (Game Object) as six colored surfaces // Green side - LEFT glBegin(GL_POLYGON); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( -0.5, -0.5, 0.5 ); glVertex3f( -0.5, 0.5, 0.5 ); glVertex3f( -0.5, 0.5, -0.5 ); glVertex3f( -0.5, -0.5, -0.5 ); glEnd();
The “Color Cube” example // display() function continues // Create the color cube (Game Object) as six colored surfaces // Blue side - TOP glBegin(GL_POLYGON); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( 0.5, 0.5, 0.5 ); glVertex3f( 0.5, 0.5, -0.5 ); glVertex3f( -0.5, 0.5, -0.5 ); glVertex3f( -0.5, 0.5, 0.5 ); glEnd();
The “Color Cube” example // display() function continues // Create the color cube (Game Object) as six colored surfaces // Red side - BOTTOM glBegin(GL_POLYGON); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.5, -0.5, -0.5 ); glVertex3f( 0.5, -0.5, 0.5 ); glVertex3f( -0.5, -0.5, 0.5 ); glVertex3f( -0.5, -0.5, -0.5 ); glEnd();
The “Color Cube” example // display() function continues // Finished creating the color cube // Draw it by flush to the (back) display buffer glFlush(); // Make the back display buffer visible (swap it to the front). Double buffering. glutSwapBuffers(); } // end of display function.
The “Color Cube” example // Keyboard event handler changes display parameter and then invoke display() void keyboard( int key, int x, int y ) { if (key == 'x') axis = xAxis; else if (key == 'y') axis = yAxis; else if (key == 'z') axis = zAxis; glutPostRedisplay(); // create an event to trigger the system to call display() }
The “Color Cube” example // “idle” event handler, changes display parameter and then invoke display() void idle() { if (axis == xAxis) rotate_x += 0.1; else if (axis == yAxis) rotate_y += 0.1; else if (axis == zAxis) rotate_z +=0.1; glutPostRedisplay(); // create an event to trigger the system to call display() }
The “Color Cube” example // The main function for initialization and setup int main(int argc, char* argv[]){ // initialize display window glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow(“Rotating Cube"); glEnable(GL_DEPTH_TEST);
The “Color Cube” example // The main function for initialization and setup // Register event handlers glutDisplayFunc(display); glutSpecialFunc(keyboard); glutIdleFunc(idle); // start the event loop glutMainLoop(); } // end of main
Rotating Cube with Texture Mapping https://www.khronos.org/webgl/wiki/Tutorial
Rotating Cube with Texture Mapping WebGL based. Coding Shaders. Can run on GPUs.
The “UA” example • Matrix Stack • Matrix Mode • Display Aspect Ratio • Lighting • Material Properties of GOs.
OpenGL Matrix Stack • Matrices for geometric transformations are stacked. • Stack: first come last serve. The last matrix will be applied first. • The following code segment rotate before translation • glTranslated(-2,-1.0,-6); • glRotatef(rotation.x, 1, 0, 0); • Use glPushMatrix to save the current stack. • Use glPopMatrix to throw away newly added matrices and recover to the last pushed stack.
OpenGL Matrix Mode • Two types of geometric transformation matrices: Projection and ModelView • Two types of projection matrices: perspective and parallel. • Perspective projection: foreshortening effect for realistic landscape display. • Parallel projection: no foreshortening effect for accurate measurements in, e.g., CAD. • ModelView mode take care all non projection matrices (translation, scaling, rotation, view orientation, …)
P1 P2 P’1 z P’2 Default: eye is at a negative z location and looks towards the positive z direction. Perspective Projection: Foreshortening: The size of the projected object becomes smaller when the object moves away from the eye.
Perspective Projection void glFrustum( • GLdouble left, • GLdouble right, • GLdouble bottom, • GLdouble top, • GLdouble nearVal, • GLdouble farVal • ); • http://tildegarro.info
Parallel (Orthoganal) Projection void glOrtho( • GLdouble left, • GLdouble right, • GLdouble bottom, • GLdouble top, • GLdouble nearVal, • GLdouble farVal • ); • http://www.cs.sun.ac.za/~lvzijl
gluLookAt: eye location and direction void gluLookAt( GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ); Edward Angel
Display Aspect Ratio • To keep round objects round. • ar = display-width / display-height; • glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); • glOrtho(-ar, ar, -1.0, 1.0, 2.0, 100.0);
Shading • Rendering of shades on 3D objects. • Lighting. • Material Properties of GOs.