41
Further games and graphics concepts COSE50581 Introduction to Module and Recap Bob Hobbs Faculty of Computing, Engineering and Technology Staffordshire University

Further games and graphics concepts COSE50581 Introduction to Module and Recap Bob Hobbs Faculty of Computing, Engineering and Technology Staffordshire

Embed Size (px)

Citation preview

Further games and graphics concepts COSE50581

Introduction to Module and Recap

Bob Hobbs

Faculty of Computing, Engineering and Technology

Staffordshire University

OutlineOutline

• Module DetailsModule Details

• What is 3D programming?What is 3D programming?

• Physics & AIPhysics & AI

• Typical Processing StepsTypical Processing Steps

• Modelling and RenderingModelling and Rendering

• Windows and Direct XWindows and Direct X

• Programmable PipelineProgrammable Pipeline

• SummarySummary

Module DetailsModule Details

• Teaching TeamTeaching Team– Bob HobbsBob Hobbs [email protected]@staffs.ac.uk– Steve Foster [email protected] Foster [email protected]– Carl Mitchell [email protected] Mitchell [email protected]

• Semester 1 & 2Semester 1 & 2 30 cats30 cats• 3 Hours per week3 Hours per week

– Weeks 1 - 6 (2hrs Lecture + 1hr Tutorial)Weeks 1 - 6 (2hrs Lecture + 1hr Tutorial)– Weeks 7 – 12 (1hr Lecture + 2hrs Tutorial)Weeks 7 – 12 (1hr Lecture + 2hrs Tutorial)– TimetableTimetable

Module DetailsModule Details

Course Handbook & Lecture NotesCourse Handbook & Lecture Notes• SpecificationSpecification

• Blackboard (not yet live)Blackboard (not yet live)

Assignment DetailsAssignment Details

• 70% assignment work70% assignment work– Phased submission with reflective report Phased submission with reflective report

componentscomponents

• 30% two hour exam at end of year30% two hour exam at end of year

Program of Study – Program of Study – Semester 1Semester 1• Week 01 Week 01 Introduction to Module and DirectX11, Introduction to Module and DirectX11,

MathsMaths• Week 02 Week 02 Geometry , Transformations, Colour Geometry , Transformations, Colour

using the using the programmable pipelineprogrammable pipeline• Week 03 Week 03 Depth, Stencil Buffer, Simple LightingDepth, Stencil Buffer, Simple Lighting• Week 04Week 04 Camera, TransformationsCamera, Transformations• Week 05Week 05 Data structures for graphics enginesData structures for graphics engines• Week 06 Week 06 Advanced Lighting and ShadingAdvanced Lighting and Shading• Week 07Week 07 Textures & Texture Mapping, Render Textures & Texture Mapping, Render

StatesStates• Week 08Week 08 Cube, Normal and Bump mappingCube, Normal and Bump mapping• Week 09Week 09 Meshing and InstancingMeshing and Instancing• Week 10 Week 10 Culling, Clipping, Depth and Line Drawing Culling, Clipping, Depth and Line Drawing • Week 11Week 11 Tessellation and Terrain Tessellation and Terrain • Week 12Week 12 Review and AssessmentReview and Assessment

Program of Study – Program of Study – Semester 2Semester 2• Week 01 Week 01 Animation and TimingAnimation and Timing• Week 02 Week 02 Newtonian Physics (velocity, motion, Newtonian Physics (velocity, motion,

forces)forces)• Week 03 Week 03 Gravity and frictionGravity and friction• Week 04Week 04 Particles and BallisticsParticles and Ballistics• Week 05Week 05 Rotational PhysicsRotational Physics• Week 06 Week 06 Collision Detection and ResponseCollision Detection and Response• Week 07Week 07 Principles of AI, BehavioursPrinciples of AI, Behaviours• Week 08Week 08 Pathfinding and searching, Pathfinding and searching,

Autonomous motionAutonomous motion• Week 09Week 09 Ai for scene optimisationAi for scene optimisation• Week 10 Week 10 State managementState management• Week 11Week 11 Decisions and cognitionDecisions and cognition• Week 12Week 12 Review and AssessmentReview and Assessment

What is 3D rendering?What is 3D rendering?

Object

Viewer

Generally deals with graphical display of 3D objects as seen by viewer

Synthetic image will vary according to: viewing direction, viewer position, illumination, object

properties, ...

Projection onto 2D surface

What is 3D Computer What is 3D Computer Graphics?Graphics?• 3D graphics: generation of 3D graphics: generation of

graphical display (graphical display (renderingrendering) ) of 3D object(s) from of 3D object(s) from specification (specification (model(s)model(s)))

Specification Modelling

Graphical display

Rendering

such as

Wireframe polygonal model Solid object

Vertices

Viewpoint

Typical Processing StepsTypical Processing Steps

Facets

Transformation

Hidden surface removal

Light source

Shading

Object model(s)

Illumination model

Viewing and projection

specification

Graphics engine

Graphical display

Rendering: operations that produce view of scene projected onto view surface

Modelling: numerical description of scene objects, illumination, and viewer

Typical Processing StepsTypical Processing Steps

20 Apr 202320 Apr 2023 1111

2D pictureLight source

Surface material

Typical Processing StepsTypical Processing Steps

xm

zm

ym

xm

zm

ym

3D world coordinates

3D modelling coordinates

2D device coordinates

3D viewing coordinates& Culling

xw

zw

ywxv zv

yv

Graphics Graphics pipelinepipeline:: generation of picture involves generation of picture involves sequence of sequence of coordinatecoordinate transformationstransformations, , hidden hidden surface removalsurface removal, , shading/texturingshading/texturing and and etc etc ......

20 Apr 202320 Apr 2023 1212Typical Typical ggraphics raphics ppipelineipeline

Typical Processing StepsTypical Processing Steps

• Pos., orient. of display area

Geometry models

• Shapes• World pos., orient., sizes

Viewpoint

• Pos., orient.• Field of view• Projection type

Viewport

Object & back-face culling

View mapping onto display

device

Generation / configuration

of world

Mesh operationsGeometry processing: shape, world

geometry, viewpoint

2D p

ictu

re

Lights & materials

Shading, texturing, ..

.

• Light pos., orient. • Optical char.

Pixel operationsMaterial processing: colour,

texture, transparency/occlusion, …

Hidden surface

removal

Viewpoint

ModellingModelling

Human Head Model

1438 facets

ModellingModelling

Human Head Model

7258 facets

ModellingModelling

Teacher and Board Model

2074 facets

RenderingRendering

Shaded Human Head

1438 facets

RenderingRendering

Shaded Teacher and Board

20 Apr 202320 Apr 2023 2222

x

yRasterisation of filled triangleRasterisation of filled triangle

Point grid

(raster)Scan line

Point / Pixel

External point

RenderingRendering

Common approach: polygon interior filled Common approach: polygon interior filled line by lineline by line, , one point at a time one point at a time (rasterisation) (rasterisation)

• linearlinear interpolationinterpolation (i.e. line equation) often used for colour (i.e. line equation) often used for colour shadeshade, , depthdepth and other calculations and other calculations at each pixelat each pixel

Windows ProgrammingWindows Programming• Rendering takes place in a windowRendering takes place in a window

• Application must generate window in Application must generate window in which to render graphicswhich to render graphics

• IP3DA used C and GLUT to handle IP3DA used C and GLUT to handle windows. OpenGL can run in GLUT or other windows. OpenGL can run in GLUT or other windowing framework including MS windowing framework including MS windowswindows

• DirectX is integrated into windows DirectX is integrated into windows programming APIprogramming API

What is a “window?”What is a “window?”

• The most obvious windows are the The most obvious windows are the application windows on your desktop.application windows on your desktop.

• The term “window” also refers to any The term “window” also refers to any child window or control (such as a child window or control (such as a button) within a window.button) within a window.

• Windows are organized in a Windows are organized in a hierarchy.hierarchy.

Example - CalculatorExample - Calculator

Event Driven ProgrammingEvent Driven Programming

• Windows programming is Windows programming is event drivenevent driven..

• An event driven program sits and waits An event driven program sits and waits for an event to process.for an event to process.

• Events include moving or sizing a Events include moving or sizing a window, clicking the mouse button over a window, clicking the mouse button over a window, or typing keys on the keyboard.window, or typing keys on the keyboard.

• Windows programs are notified of events Windows programs are notified of events by messages sent from the OS.by messages sent from the OS.

Win32 APIWin32 API

• We will use the Win32 Platform API to We will use the Win32 Platform API to write a windows program.write a windows program.– Typically more tedious, but we only Typically more tedious, but we only

need to create one window.need to create one window.– Faster and Smaller executable.Faster and Smaller executable.– Written in C++Written in C++

• Other options include:Other options include:– MFC, Qt, wxWindows, SDL, .NET FormsMFC, Qt, wxWindows, SDL, .NET Forms

WinMainWinMain

• Instead of Instead of mainmain, windows programs use , windows programs use WinMainWinMain

• WinMainWinMain has different parameters: has different parameters:– HINSTANCE hInstanceHINSTANCE hInstance – a handle to the program – a handle to the program– HINSTANCE hPrevInstanceHINSTANCE hPrevInstance – no longer used. – no longer used.– LPSTR lpCmdLineLPSTR lpCmdLine – – unparsedunparsed command line. command line. Doesn’tDoesn’t include executable’s filename. include executable’s filename.

– int nCmdShowint nCmdShow – Specifies how the window should be – Specifies how the window should be initially shown (ie. Minimized, Maximized, Normal)initially shown (ie. Minimized, Maximized, Normal)

• WinMainWinMain must have a must have a WINAPIWINAPI modifier. modifier.– int WINAPI WinMain (…)int WINAPI WinMain (…)

• WinMainWinMain still returns an integer like main. still returns an integer like main.

HeadersHeaders

• To use the Win32 API we need to To use the Win32 API we need to #include windows.h#include windows.h

• If we #define If we #define WIN32_LEAN_AND_MEANWIN32_LEAN_AND_MEAN before #including windows.h, the before #including windows.h, the compiler will skip compiling the more compiler will skip compiling the more rarely used windows code.rarely used windows code.

Windows Program OutlineWindows Program Outline

• Create Window ClassCreate Window Class

• Create WindowCreate Window

• Show the WindowShow the Window

• Enter Message LoopEnter Message Loop

Window ClassesWindow Classes

• A A window classwindow class acts as a template for window acts as a template for window creation.creation.

• Window classes are Window classes are notnot classes in the C++ classes in the C++ sense.sense.

• We can set such properties as:We can set such properties as:– Name of the window class – Name of the window class – importantimportant for identifying it for identifying it

later!later!– Window style – How it looks and its default behaviors.Window style – How it looks and its default behaviors.– Window procedure – a pointer to function that handles Window procedure – a pointer to function that handles

messages for a window.messages for a window.– Default cursor, icon, and menu.Default cursor, icon, and menu.

Creating a Window ClassCreating a Window Class

• Fill out a Fill out a WNDCLASSWNDCLASS or or WNDCLASSEXWNDCLASSEX structure.structure.

• Use the structure to register the Use the structure to register the class with a call to class with a call to RegisterClassRegisterClass or or RegisterClassExRegisterClassEx, respectively., respectively.

Create the WindowCreate the Window

• Create a Window with a call to Create a Window with a call to CreateWindowCreateWindow or or CreateWindowExCreateWindowEx..

• Some of the parameters you will need to Some of the parameters you will need to enter include:enter include:– The name of a window class. The name of a window class. – The name of the window.The name of the window.– A handle to a parent window. (A handle to a parent window. (NULLNULL if no parent) if no parent)– Initial size and position of the window.Initial size and position of the window.

• CreateWindowCreateWindow and and CreateWindowExCreateWindowEx will will return a return a window handlewindow handle ( (HWNDHWND) if successful ) if successful or or NULLNULL if failed. if failed.

Showing the WindowShowing the Window

• When we create a window it starts off hidden.When we create a window it starts off hidden.

• To show the window you must call To show the window you must call ShowWindow (hWnd, nCmdShow)ShowWindow (hWnd, nCmdShow)– hWndhWnd – returned from – returned from CreateWindowCreateWindow//ExEx..– nCmdShownCmdShow – parameter from – parameter from WinMainWinMain..– This will send a message to the window telling it This will send a message to the window telling it

to show.to show.

• Call Call UpdateWindow (hWnd)UpdateWindow (hWnd) to force the to force the window to process the show message window to process the show message immediately. immediately.

Message LoopsMessage Loops• A A message loopmessage loop is needed to forward is needed to forward

messages to their windows.messages to their windows.

• There are different ways to make a message There are different ways to make a message loop but here is the most common one:loop but here is the most common one:

MSG msg = {0};

// returns FALSE when message is WM_QUITwhile (GetMessage (&msg, NULL, 0, 0)){ TranslateMessage (&msg); // translate keyboard messages. DispatchMessage (&msg); // send message to its window.}

Message Loop (Cont’d)Message Loop (Cont’d)• The loop uses:The loop uses:

– A A MSGMSG structure – Holds message information. structure – Holds message information.– GetMessageGetMessage – Retrieves the next message. Waits – Retrieves the next message. Waits

for a message if the message queue is empty.for a message if the message queue is empty.– TranslateMessageTranslateMessage – Translate some keyboard – Translate some keyboard

messages.messages.– DispatchMessageDispatchMessage – Dispatches the message to – Dispatches the message to

the appropriate windows procedure.the appropriate windows procedure.

Windows ProcedureWindows Procedure• Every window has an associate Every window has an associate

procedure for handling messages.procedure for handling messages.• Windows procedures have the Windows procedures have the

following format:following format:

– LRESULT CALLBACK ProcName (HWND LRESULT CALLBACK ProcName (HWND hwnd, UINT msg, WPARAM wp, LPARAM hwnd, UINT msg, WPARAM wp, LPARAM lp);lp);

Window Procedure ExampleWindow Procedure Example

• Here is an example procedure:Here is an example procedure:LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){

switch (msg){case WM_DESTROY:

PostQuitMessage (0);return 0;

}

return DefWindowProc (hwnd, msg, wp, lp);}

Window Procedure Window Procedure ParametersParameters• The procedure has 4 parameters:The procedure has 4 parameters:

– HWND hwndHWND hwnd – handle to the window receiving – handle to the window receiving the message. This is useful when multiple the message. This is useful when multiple windows share the same procedure. You can windows share the same procedure. You can think of it sort of like a think of it sort of like a thisthis pointer. pointer.

– UINT msgUINT msg – The message. – The message.– WPARAM wp WPARAM wp andand LPARAM lp LPARAM lp – These are – These are

parameters to the message and may parameters to the message and may represent different things.represent different things.

Window Procedure ReturnWindow Procedure Return

• When the procedure completely When the procedure completely handles a message, it should return handles a message, it should return 00..

• The procedure should let the OS The procedure should let the OS handle all other messages by calling handle all other messages by calling DefWindowProcDefWindowProc and then return its and then return its result.result.

WM_DESTROY and WM_DESTROY and WM_QUITWM_QUIT

• Initially we only want to handle one Initially we only want to handle one message: message: WM_DESTROYWM_DESTROY..– This message is called when a window is This message is called when a window is

closed.closed.– Note that just because the window is Note that just because the window is

destroyed, the program will still run.destroyed, the program will still run.– To end the program we send To end the program we send WM_QUITWM_QUIT by by

calling calling PostQuitMessagePostQuitMessage..

WM_*WM_*• Other messages you may want to handle Other messages you may want to handle

include:include:– WM_CREATEWM_CREATE– WM_SIZEWM_SIZE– WM_LBUTTONDOWNWM_LBUTTONDOWN– WM_LBUTTONUPWM_LBUTTONUP– WM_MOUSEMOVEWM_MOUSEMOVE– WM_KEYDOWNWM_KEYDOWN– WM_KEYUPWM_KEYUP– WM_PAINTWM_PAINT

• Check MSDN literature for information Check MSDN literature for information about these messages: about these messages: http://msdn.microsoft.com/http://msdn.microsoft.com/

Direct XDirect X• The window has to obtain a handle to a The window has to obtain a handle to a

Direct 3D and swap chain to render Direct 3D and swap chain to render graphics objects in the windows object graphics objects in the windows object created.created.

• In DirectX 11 this is based on the In DirectX 11 this is based on the Programmable Pipeline.Programmable Pipeline.

20 Apr 202320 Apr 2023 4646

Suggested ReadingSuggested Reading

• http://msdn.microsoft.com/en-us/library/ee423531.aspx http://msdn.microsoft.com/en-us/library/ee423531.aspx

(Microsoft DirectX (Microsoft DirectX Graphics Documentation) Graphics Documentation)

• http://developer.nvidia.com/page/cg_main.html (Cg http://developer.nvidia.com/page/cg_main.html (Cg

homepage) homepage)

20 Apr 202320 Apr 2023 4747

• Programmable hardware pipeline on GPUProgrammable hardware pipeline on GPU– concurrent processing on single chipconcurrent processing on single chip

• Shaders offer code development flexibility and Shaders offer code development flexibility and processing speedprocessing speed

• Shader: program segment inserted into Shader: program segment inserted into graphics pipeline and executed on GPUgraphics pipeline and executed on GPU– replaces section of fixed-function pipeline replaces section of fixed-function pipeline

• vertex shader, pixel shader, geometry shadervertex shader, pixel shader, geometry shader

• Variety of shader languagesVariety of shader languages

SummarySummary