2009 graphics : project 1 based on dx9 basics

Post on 16-Jan-2016

29 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

note. 2009 Graphics : project 1 based on DX9 basics. Documented by Dongjoon Kim SNU CS Ph.D Course Student Contact : dojo@cglab.snu.ac.kr. REQUIREMENTS. Requirements for Project1 based on DX9. 1. 3D Space Transformation (Closed in Projection Space) 2. Camera Control in WS - PowerPoint PPT Presentation

TRANSCRIPT

2009 GRAPHICS : PROJECT 1 BASED ON DX9 BASICS

. Documented by Dongjoon Kim SNU CS Ph.D Course Student

Contact : dojo@cglab.snu.ac.kr

NOTE

REQUIREMENTS

REQU

IREMEN

TSRequirements for Project1 based on DX9

1. 3D Space Transformation (Closed in Projection Space)

2. Camera Control in WS

3. Shader model 3.0 (HLSL)

4. Render To Texture and Its Texture Mapping ( Read-back Techniques )

5. Phong-based Illumination

3D SPACE TRANSFORMATION

3D SPACE TRAN

SFORM

ATION

Space and Transformation Matrix

1. Transformation in Projection Space(3D) can be defined by 4x4 matrix 2. DX API defines row major matrix and vector.

- mat1~3 = mat1*mat2*mat3

Rigid Space Linear Space

Affine Space

Projective Space

Translation RotationShear

Scale

Perspective TR

x1 y1 z1 p1x2 y2 z2 p2x3 y3 z3 p3t1 t2 t3 1

Linear Transform

Translation

Projection

3D SPACE TRAN

SFORM

ATION

Transformation : 3D Space Definition for the Scene

Object SpaceObject Space

World SpaceWorld Space

Viewing SpaceViewing Space

Projection SpaceProjection Space

Window SpaceWindow Space

3D SPACE TRAN

SFORM

ATION

Transformation : 3D Space Definition for the Scene

Object SpaceObject Space

World SpaceWorld Space

Viewing SpaceViewing Space

Projection SpaceProjection Space

Window SpaceWindow Space

• Object Designer's Space

• Coordinate system and unit are defined by the Designer

x

y

z

3D SPACE TRAN

SFORM

ATION

Transformation : 3D Space Definition for the Scene

Object SpaceObject Space

World SpaceWorld Space

Viewing SpaceViewing Space

Projection SpaceProjection Space

Window SpaceWindow Space

• Scene Stage

• Objects and Camera(Eye) and Light resources

3D SPACE TRAN

SFORM

ATION

Transformation : 3D Space Definition for the Scene ( D3DX )

Object SpaceObject Space

World SpaceWorld Space

Viewing SpaceViewing Space

Projection SpaceProjection Space

Window SpaceWindow Space

World Space

Viewing Space

• Camera(Eye)-Origin Coordinate System

• Unit is same as WS's one

• Use 'D3DXMatrixLookAtLH(…)’

3D SPACE TRAN

SFORM

ATION

Transformation : 3D Space Definition for the Scene ( D3DX )

Object SpaceObject Space

World SpaceWorld Space

Viewing SpaceViewing Space

Projection SpaceProjection Space

Window SpaceWindow Space

• Normalized Viewing Space

• Clipping and Depth Test

• Use 'D3DXMatrixPerspectiveLH(…)' or Orthogonal

Viewing Space

view frustum

(-1, -1, 0)

Normalized view frustum

(1, 1, 1)

Projection Space

z

3D SPACE TRAN

SFORM

ATION

Transformation : 3D Space Definition for the Scene ( D3DX )

Object SpaceObject Space

World SpaceWorld Space

Viewing SpaceViewing Space

Projection SpaceProjection Space

Window SpaceWindow Space

(-1, -1, 0)

(1, 1, 1)

z

Projection Space

Normalized view frustum

typedef struct D3DVIEWPORT9 { DWORD X, Y; DWORD Width, Height; float MinZ, MaxZ; } D3DVIEWPORT9, *LPD3DVIEWPORT9;Use IDirect3DDevice9::SetViewport or Default ViewPort

View Port

x (pix)

y (pix)

z (0.f~1.f)

XY

Width

Height

MinZ

MaxZx (pix)

y (pix) Width

Height

X

Y

Window Space

RENDER TO TEXTURE AND ITS TEXTURE MAPPING

REND

ER TO TEXTU

RE AND

ITS TEXTURE M

APPING

Render To Texture

1st Make 2D Texture as Render Target - IDirect3DDevice9::CreateTexture- Usage : D3DUSAGE_RENDERTARGET - Ref : Find the Index with ‘D3DUSAGE’ in DX SDK Document

2nd Bind 2D Texture resource to Surface (Display Memory – Back buffer)- IDirect3DTexture9::GetSurfaceLevel

3rd Define Rectangle Vertice (POSITION and TEXTURE)- IDirect3DDevice9::CreateVertexBuffer

-----------------------------------------------------------------------------------------------------4th Set Render Target with the Surface

- IDirect3DDevice9::SetRenderTarget5th Set Proxy Vertex Buffer Resources

- IDirect3DDevice9::SetStreamSource6th Setting another resources and states7th Draw

REND

ER TO TEXTU

RE AND

ITS TEXTURE M

APPING

Texture Mapping by Using Shade model 3.0

1st Use D3DXCreateEffectFromFile(…)2nd Bind texture resource to Shader Effect

- ID3DXBaseEffect::SetTexture3rd Flush effect state changes before ‘Draw’

- ID3DXEffect::CommitChanges----------------------------------------------------------------------------------------------------4th Declare

- ‘texture g_tex2DTest;’ in fx code5th Sampler State Setting

- sampler2D TestSampler2D = sampler_state{Texture = (g_tex2DTest);…

}6th tex2D( TestSampler2D, float2(x, y) );

- x, y must be normalized : 0~1- Return type is float4 vector

SCENE STAGE SETTING & PROJECTION

SCENE STAG

E SETTING

& PRO

JECTION

Scene Stage Setting #1

x

y

z

1 : (1, 1, 1) 6 : (-1, -1, -1)

Object Space

3

2

1

Center : (2, 2, 1)

Center : (-4, 2, -2)

Center : (1, -3, -2)

z

x

y

World Space

z

x

Parameter Setting for- D3DXMatrixLookAtLH- D3DXMatrixOrthogonalLH

Phong Illumination!

0

2

1

3

4 5

6 7

Object1Red Color

Object2Green Color

Object3Blue Color

Rotation Axis : (1, -1, -1)

SCENE STAG

E SETTING

& PRO

JECTION

Scene Stage Setting #2

z

x

y

World Space

Parameter Setting for- D3DXMatrixLookAtLH- D3DXMatrixPerspectiveLH

(1000, 500, 0)

(0, -500, -500) (1000, -500, 0)

(0, 500, -500)

Rotation Axis : X-axis

SCENE STAG

E SETTING

& PRO

JECTION

Projections

z

x

x (pix)

y (pix)

Width

Height

z

x

y

World Space for Scene 2

(1000, 500, 0)

(0, -500, -500)

World Space for Scene 1

Render To Textureby orthogonal projection

Texture Mapping

x (pix)

y (pix)

Width

HeightRender To Screen (Back-buf)Perspective projection

(1000, -500, 0)

(0, 500, -500)

PROJECT SAMPLE CODE

PROJECT SAM

PLE COD

ECRenderManager Class

1. CRenderManager Class in Project1Renderer.cpp/h

• Scene1,2’s initial Camera State

• Two Vertex Declarations

POSITION&NORMAL for Scene1

POSITION&TEXTURE for Scene2

• Two Vertex Buffers

Cube vertex buffer with index buffer for Scene1

Rect. Vertex buffer for Scene2

• You may change the Camera state

CameraControlScene1(), CameraControlScene2()

• Don’t have to do ‘Present(…)’

CRenderManager Class

• RenderScene1()

Use m_matWS2OS_3 array

Setting the Shading Space to the Object Space

Cheaper way

May be used the World Space as the Shading Space

Orthogonal Projection

Render To Texture

• RenderScene2()

Texture Mapping

Perspective Projection

Render To Back-buffer (Window Screen)

PROJECT SAM

PLE COD

ESample Shader Code

2. Project1DX9.fx

• You may change the sampler states and data structure

• Use the Shader model 3.0

• Phong illumination model is available only in the shader code

• You may assume that the light resource model is simplified.

may use parallel-light ray

may use the View direction as the parallel-light ray direction

In this case, specular can be represented as power of diffuse term

PROJECT SAM

PLE COD

EPseudo code for CRenderManager Class

1. Initialization

• Declare Rendertarget Texture

• Create Shader Effect

• Initial Matrix Setting : World Space Object Space

• Vertex / Index Bufffers : Given buffers

PROJECT SAM

PLE COD

EPseudo code for CRenderManager Class

1. RenderScene1

// Backup Old RenderTarget Surface with Scene1’s camera states// Make View and Projection matrix// Set Device Resources such as buffers and declarations// Set Texture as RenderTarget// Effect Technique and Pass Begin

for i : 0 to 2{

// Object[i] Parameters to the Shading Space and Set the parameters// Set the matrix : Object to Display(Screen) Space// CommitChanges// Draw Run the Shader code

} // Effect Technique and Pass End// Set the Old Render Target

// Backup Old RenderTarget Surface with Scene1’s camera states// Make View and Projection matrix// Set Device Resources such as buffers and declarations// Set Texture as RenderTarget// Effect Technique and Pass Begin

for i : 0 to 2{

// Object[i] Parameters to the Shading Space and Set the parameters// Set the matrix : Object to Display(Screen) Space// CommitChanges// Draw Run the Shader code

} // Effect Technique and Pass End// Set the Old Render Target

PROJECT SAM

PLE COD

EPseudo code for CRenderManager Class

1. RenderScene2

// Make View and Projection matrix with Scene2’s camera states// Set Device Resources such as buffers and declarations and Textures// Effect Technique and Pass Begin

// CommitChanges// Draw Run the Shader code // Effect Technique and Pass End// Set the Old Render Target

// Make View and Projection matrix with Scene2’s camera states// Set Device Resources such as buffers and declarations and Textures// Effect Technique and Pass Begin

// CommitChanges// Draw Run the Shader code // Effect Technique and Pass End// Set the Old Render Target

PROJECT SUBMISSION

E-MAIL : korfriend@gmail.comDue-Day : 2009/10/6, 23:59:59 (My Gmail Time --;)Delay Penalty : -20% per day

Don’t have to submit a document about this HW.Submit your source files.

top related