DirectX Programming #1. Hyunna Lee Computer Graphics, 2010 Spring

Similar documents
DirectX Programming #1. Kang, Seong-tae Computer Graphics, 2007 Fall

Beginning Direct3D Game Programming: 6. First Steps to Animation. Division of Digital Contents, DongSeo University.

Providing Direct3D Features over the Desktop OpenGL

DirectX Programming #2. Hyunna Lee Computer Graphics, 2010 Spring

Kang, Seong-tae Computer Graphics, 2007 Spring. Texture mapping Texture mapping on Direct3D. CGLab

3D Programming. 3D Graphics Fundamentals Creating Your Own 3D Models with Anim8or Working with 3D Model Files...285

CIS 536/636 Introduction to Computer Graphics. Kansas State University. CIS 536/636 Introduction to Computer Graphics

D3DX Concrete Types. Chapter Overview Colors

DirectX Programming #4. Kang, Seongtae Computer Graphics, 2009 Spring

Rendering Objects. Need to transform all geometry then

Optimizing Direct3D for the GeForce 256 Douglas H. Rogers Please send me your comments/questions/suggestions

Advanced Rendering Techniques

Framebuffer Overlay Blending

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Meshes. Meshes what are they? How to use them? Progressive Meshes? Doron Nussbaum COMP Meshes 1. Doron Nussbaum COMP Meshes 2

The Application Stage. The Game Loop, Resource Management and Renderer Design

3D Graphics for Game Programming (J. Han) Chapter II Vertex Processing

Rasterization Overview

DirectX 11 First Elements

Memory is the treasury and guardian of all things. Cicero: De orature, I, c. 80 B.C.

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Supplement to Lecture 22

Shaders Part II. Traditional Rendering Pipeline

Models and Architectures

Rendering Grass with Instancing in DirectX* 10

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Lecture 2. Shaders, GLSL and GPGPU

Pipeline Operations. CS 4620 Lecture 14

CS451Real-time Rendering Pipeline

Spring 2009 Prof. Hyesoon Kim

Spring 2011 Prof. Hyesoon Kim

E.Order of Operations

Introduction to Computer Graphics with WebGL

Ciril Bohak. - INTRODUCTION TO WEBGL

The Effects Framework. October 31, Composed of the following components:

1.2.3 The Graphics Hardware Pipeline

Graphics Pipeline 2D Geometric Transformations

Chapter 1 Introduction

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

Shaders. Slide credit to Prof. Zwicker

Module 13C: Using The 3D Graphics APIs OpenGL ES

Practical Performance Analysis Koji Ashida NVIDIA Developer Technology Group

Introduction to Shaders.

The Importance of Matrices in the DirectX API. by adding support in the programming language for frequently used calculations.

3D Polygon Rendering. Many applications use rendering of 3D polygons with direct illumination

CSE 167: Introduction to Computer Graphics Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

Chapter Answers. Appendix A. Chapter 1. This appendix provides answers to all of the book s chapter review questions.

CS130 : Computer Graphics Lecture 2: Graphics Pipeline. Tamar Shinar Computer Science & Engineering UC Riverside

Real - Time Rendering. Pipeline optimization. Michal Červeňanský Juraj Starinský

Advanced 3D Game Programming with DirectX* 10.0

CSE 167: Introduction to Computer Graphics Lecture #5: Visibility, OpenGL

Technical Report. Mesh Instancing

Working with Metal Overview

Could you make the XNA functions yourself?

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

D3DX Helper Objects. Chapter Overview Matrix Stacks. For a man to help another is to be a god. Pliny the Elder, Natural History, II, 77

CHAPTER 1 Graphics Systems and Models 3

Optimisation. CS7GV3 Real-time Rendering

CS 130 Final. Fall 2015

CS4620/5620: Lecture 14 Pipeline

Lighting and Materials

Models and Architectures. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Getting Started. 1 st Week, Sun-Jeong Kim. Computer Graphics Applications

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

Order Matters in Resource Creation

Graphics Pipeline & APIs

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

CSE 167: Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

Graphics Performance Optimisation. John Spitzer Director of European Developer Technology

!" "!#$!"%!&% ' ( )) * ) )+( ( ))+ ))(, ) ( - +,./ )) ) ) $( -( 1), , ) ( -)) ') 5 ' +) 67 4 ) ' 8, ( ) ( 9 :! ; $) ) ; 5<4,4$6 ( =) >

Graphics Pipeline & APIs

Evolution of GPUs Chris Seitz

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Graphics Processing Unit Architecture (GPU Arch)

CMSC427 Transformations II: Viewing. Credit: some slides from Dr. Zwicker

OpenGL and Direct3D are on a par and rather similar. Please notice the following differences:

Programming Graphics Hardware

CS 591B Lecture 9: The OpenGL Rendering Pipeline

RASTERISED RENDERING

CIS 581 Interactive Computer Graphics (slides based on Dr. Han-Wei Shen s slides) Requirements. Reference Books. Textbook

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak

Open Source Tools for Game Development. Ryan C. Gordon, icculus.org

Graphics Hardware, Graphics APIs, and Computation on GPUs. Mark Segal

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Computer graphics 2: Graduate seminar in computational aesthetics

Advanced Lighting Techniques Due: Monday November 2 at 10pm

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

CSE 167: Lecture #4: Vertex Transformation. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Implementing Fog in Direct3D Douglas Rogers NVIDIA Corportaion

CIS 581 Interactive Computer Graphics

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Optimizing DirectX Graphics. Richard Huddy European Developer Relations Manager

Transcription:

DirectX Programming #1 Hyunna Lee Computer Graphics, 2010 Spring

Contents } Installation and Settings } Introduction to Direct3D 9 Graphics } Initializing Direct3D } Rendering Vertices } The D3D coordinate system } Transformation and matrix manipulation

Installation } DirectX SDK } You can download from Microsoft s homepage for free } Latest version: August 2009 } http://www.microsoft.com/downloads/details.aspx?familyid=b66e14b 8-8505-4b17-bf80-edb2df5abad4&displaylang=en } You need not to get the latest version of SDK } April 2007 or later are OK

Visual Studio Settings } Add DirectX include and library directories } Tools > Options > Projects and Solutions > VC++ Directories } Move the DirectX directories to the top } Visual Studio contains some old-version of DirectX headers and libraries, which may cause confliction with newer version

Sample and tutorial codes } Sample codes } [DX directory]/samples/c++/direct3d } Tutorial Codes } [DX directory]/samples/c++/direct3d/tutorials

Sample and tutorial codes } Using Sample Browser

Direct3D 9 Architecture Vertex data Primitive data Vertex processing Geometry processing Pixel processing Texture sampler Pixel rendering Texture data

Direct3D 9 Pipeline Components } Vertex Data } Untransformed model vertices are stored in vertex buffer } Primitive Data } Geometric primitives, including points, lines, triangles, and polygons, are referenced in the vertex data with index buffer } When index buffer is not defined, the pipeline build primitives using vertices in vertex buffer sequentially Vertex data Primitive data Vertex processing Geometry processing Texture data Pixel processing Texture sampler Pixel rendering

Direct3D 9 Pipeline Components } Vertex processing } Vertex transformation : world, view, projection transformation } Geometry processing } Clipping, back face culling, attribute evaluation } Rasterization } Texture Sampler } Level-of-detail filtering for texture sampling with input texture values from D3D texture surfaces Vertex data Primitive data Vertex processing Geometry processing Texture data Pixel processing Texture sampler Pixel rendering

Direct3D 9 Pipeline Components } Pixel processing } Modify input vertex and texture data using geometry data } Output pixel color values } Pixel Rendering } Modify pixel color values finally } Alpha blending } Depth, stencil, alpha test } Fog blending Vertex data Primitive data Vertex processing Geometry processing Texture data Pixel processing Texture sampler Pixel rendering

Direct3D System Integration Win32 Application Win32 Application Direct3D API GDI Hal Device Device Driver Interface (DDI) Graphics Hardware

Tutorial 1 : Creating Devices } Brief Structure of Win32 D3D Program } Create a window } Create Direct3D bound to the window } Create a device bound to Direct3D } Define fixed data and parameters } Vertices and indices, textures, primitive types } Setting per-render parameters } Viewing parameters, lights, } Begin rendering } Draw primitives } End rendering } Present Rendering loop

Creating Devices } Initializing Direct3D } After the window is created } Creating Direct3D } Creating device } Setting presentation parameters LPDIRECT3D9 g_pd3d = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3ddevice = NULL; // Our rendering device if( NULL == ( g_pd3d = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof( d3dpp ) ); d3dpp.windowed = TRUE; d3dpp.swapeffect = D3DSWAPEFFECT_DISCARD; d3dpp.backbufferformat = D3DFMT_UNKNOWN; if( FAILED( g_pd3d->createdevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3ddevice ) ) ) return E_FAIL;

Locating the Rendering Routine } WM_PAINT handler } When the rendered scene is static } Minimum load for rendering } Refresh by WM_PAINT message LRESULT WINAPI MsgProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) { switch( msg ) { } case WM_PAINT: Render(); ValidateRect( hwnd, NULL ); return 0; } return DefWindowProc( hwnd, msg, wparam, lparam );

Basic Rendering Routine } Clear } Clear back buffer, depth buffer, and stencil buffer } BeginScene / EndScene pair } Compose a rendering block } Present } Presents the back buffer // Clear the backbuffer to a blue color g_pd3ddevice->clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 ); if( SUCCEEDED( g_pd3ddevice->beginscene() ) ) { // Rendering of scene objects can happen here g_pd3ddevice->endscene(); } // Present the backbuffer contents to the display g_pd3ddevice->present( NULL, NULL, NULL, NULL );

Finalization } When the D3D application ends } Usually located in WM_DESTROY message handler } Clear objects } Release all created objects if( g_pd3ddevice!= NULL ) g_pd3ddevice->release(); if( g_pd3d!= NULL ) g_pd3d->release();

Result : Tutorial 1

Tutorial 2 : Rendering Vertices } Definition of the vertex type } Vertex declaration object } Flexible declaration } Complicated code } FVF (Flexible Vertex Format) } Combination of pre-defined type templates } Fixed order } Simple

Vertex Type Definition : FVF // Our custom FVF, which describes our custom vertex structure #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW D3DFVF_DIFFUSE) } FVF (Flexible Vertex Format) } Description of structure for a vertex } OR combination of predefined FVF templates } Element order } position normal diffuse, specular texture coordinates } e.g. } D3DFVF_XYZ D3DFVF_NORMAL D3DFVF_DIFFUSE 3D position, normal and diffuse } D3DFVF_XYZ D3DFVF_TEX2 3D position and two 2D texture coordinates } D3DFVF_XYZ D3DFVF_TEX1 D3DFVF_TEXCOORDSIZE3(0) 3D position with one 3D texture coordinates

Vertex Type Definition : C++ Struct } Vertex structure } Just for comfortable coding } FVF components have fixed order and size } Size of elements } Position and texture coordinate : (# of dim.) x FLOAT } Diffuse/specular color : DWORD (D3DCOLOR_ARGB) } Element ordering } Defining order of elements in the structure must be matched to FVF element ordering #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW D3DFVF_DIFFUSE) struct CUSTOMVERTEX { FLOAT x, y, z, rhw; DWORD color; }; // The transformed position for the vertex // The vertex color

Creating the Vertex Buffer } Vertex Buffer } Holds vertices to render } Reside in the video memory } Creating a vertex buffer HRESULT CreateVertexBuffer( UINT Length, DWORD Usage, DWORD FVF, D3DPOOL Pool, IDirect3DVertexBuffer9** ppvertexbuffer, HANDLE* psharedhandle ); Length : Size of the vertex buffer, in bytes Usage : Usage of the resource, usually 0 for vertex buffer FVF : FVF to use for this vertex buffer, combination of D3DFVF Pool : Member of the D3DPOOL enumerated type, usually D3DPOOL_DEFAULT or D3DPOOL_MANAGED for vertex buffer ppvertexbuffer : Pointer of the vertex buffer object psharehandle : Not used, set this parameter to NULL LPDIRECT3DVERTEXBUFFER9 g_pvb = NULL; // Buffer to hold Vertices if( FAILED( g_pd3ddevice->createvertexbuffer( 3 * sizeof( CUSTOMVERTEX ), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pvb, NULL ) ) ) return E_FAIL;

Creating the Vertex Buffer } Locking and unlocking } Lock } Lock the buffer and obtains a pointer to the memory } CPU can access the locked resource buffer } GPU memory CPU memory (download) } Unlock } CPU memory GPU memory (upload) HRESULT Lock( UINT OffsetToLock, UINT SizeToLock, VOID ** ppbdata, DWORD Flags ); OffsetToLock : Offset into the index data to lock, in bytes. Lock the entire buffer by specifying 0 SizeToLock : Size of the index data to lock, in bytes. Lock the entire buffer by specifying 0 ppbdata : VOID* pointer to a memory buffer Flags : Combination of zero or more locking flags, see D3DLOCK VOID* pvertices; if( FAILED( g_pvb->lock( 0, sizeof( Vertices ), ( void** )&pvertices, 0 ) ) ) return E_FAIL; memcpy( pvertices, Vertices, sizeof( Vertices ) ); g_pvb->unlock();

Binding the Vertex Buffer } Vertex buffers need to be attached to the pipeline HRESULT SetStreamSource( UINT StreamNumber, IDirect3DVertexBuffer9 * pstreamdata, UINT OffsetInBytes, UINT Stride ); StreamNumber : Specifies the data stream pstreamdata : Pointer to a vertex buffer OffsetInBytes : Offset from the beginning of the stream to the beginning of the vertex data Stride : Stride, i.e. size of the component, in bytes g_pd3ddevice->setstreamsource( 0, g_pvb, 0, sizeof( CUSTOMVERTEX ) ); g_pd3ddevice->setfvf( D3DFVF_CUSTOMVERTEX );

Drawing Primitives } Primitive types determine how the pipeline interpret incoming vertex stream to build primitives HRESULT DrawPrimitive( D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount ); PrimitiveType : Member of the D3DPRIMITIVETYPE enumerated type StartVertex : Index of the first vertex to load PrimitiveCount : Number of primitives to render g_pd3ddevice->drawprimitive( D3DPT_TRIANGLELIST, 0, 1 );

Result : Tutorial 2

The D3D Coordinate System } Left-handed } Right-handed coordinate is also available } Take care of backface-culling order

Vector Representation } D3DXVECTORn class } Float elements } x, y for D3DXVECTOR2 } x, y, z for D3DXVECTOR3 } Basic operators } Scalar multiplication/division } Vector addition/subtraction and equality comparison

Matrix Representation } D3DXMATRIX Structure } 4x4 homogeneous matrix } Row-major order } Column-major order matrices must be transposed } Can be accessed as float[16] array typedef struct D3DXMATRIX { FLOAT _11, FLOAT _12, FLOAT _13, FLOAT _14, FLOAT _21, FLOAT _22, FLOAT _23, FLOAT _24, FLOAT _31, FLOAT _32, FLOAT _33, FLOAT _34, FLOAT _41, FLOAT _42, FLOAT _43, FLOAT _44; } D3DXMATRIX;

Basic Matrix Operations } Identity matrix D3DXMATRIX * D3DXMatrixIdentity( D3DXMATRIX * pout ); } Transpose D3DXMATRIX * D3DXMatrixTranspose( D3DXMATRIX * pout, CONST D3DXMATRIX * pm ); } Inverse D3DXMATRIX * D3DXMatrixInverse( D3DXMATRIX * pout, FLOAT * pdeterminant, CONST D3DXMATRIX * pm ); } Multiplication D3DXMATRIX * D3DXMatrixMultiply( D3DXMATRIX * pout, CONST D3DXMATRIX * pm1, CONST D3DXMATRIX * pm2 );

Matrix Multiplication Order } Row-major matrix } Row vector } Post-multiplication

Setting Transform Matrices } World transformation } Model space World space g_pd3ddevice->settransform( D3DTS_WORLD, &matworld ); } Viewing transformation } World space Viewing space g_pd3ddevice->settransform( D3DTS_VIEW, &matview ); } Projection transformation } Viewing space Projection space g_pd3ddevice->settransform( D3DTS_PROJECTION, &matproj );

World Transformation } Scaling, rotation and translation } Many predefined functions are defined in DX library } D3DXMatrixRotationAxis } D3DXMatrixRotationQuaternion } D3DXMatrixRotationX } D3DXMatrixScaling } D3DXMatrixTranslation } Composite predefined transformations using multiplication D3DXMATRIX matworld; D3DXMatrixRotationX ( &matrotx, RotateX ); D3DXMatrixRotationY ( &matroty, RotateY ); D3DXMatrixRotationZ ( &matrotz, RotateZ ); D3DXMatrixMultiply ( &matworld, &matrotz, &matroty); D3DXMatrixMultiply ( &matworld, &matworld, &matrotx); g_pd3ddevice->settransform( D3DTS_WORLD, &matworld );

Viewing Transformation } Camera transformation } Transformation specifier } Eye point } Look-at position } Up vector D3DXMATRIXA16 matview; D3DXVECTOR3 veyept( 0.0f, 3.0f,-5.0f ); D3DXVECTOR3 vlookatpt( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 vupvec( 0.0f, 1.0f, 0.0f ); D3DXMatrixLookAtLH( &matview, &veyept, &vlookatpt, &vupvec ); g_pd3ddevice->settransform( D3DTS_VIEW, &matview );

Projection Transformation } 3D viewing space 2D viewport space } Perspective projection } D3DXMatrixPerspectiveLH } D3DXMatrixPerspectiveFovLH } D3DXMatrixPerspectiveOffCenterLH } Orthogonal projection } D3DXMatrixOrthoLH } D3DXMatrixOrthoOffCenterLH

Projection Transformation } Projection space } X, Y : [-1, 1], viewport } Z : [0,1], depth

World Transformation : Tutorial 3 D3DXMATRIXA16 matworld; UINT itime = timegettime() % 1000; FLOAT fangle = itime * ( 2.0f * D3DX_PI ) / 1000.0f; D3DXMatrixRotationY( &matworld, fangle ); g_pd3ddevice->settransform( D3DTS_WORLD, &matworld );

Viewing Transformation : Tutorial 3 D3DXMATRIXA16 matview; D3DXVECTOR3 veyept( 0.0f, 3.0f,-5.0f ); D3DXVECTOR3 vlookatpt( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 vupvec( 0.0f, 1.0f, 0.0f ); D3DXMatrixLookAtLH( &matview, &veyept, &vlookatpt, &vupvec ); g_pd3ddevice->settransform( D3DTS_VIEW, &matview );

Projection Transformation : Tutorial 3 D3DXMATRIXA16 matproj; D3DXMatrixPerspectiveFovLH( &matproj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f ); g_pd3ddevice->settransform( D3DTS_PROJECTION, &matproj );

Result : Tutorial 3