Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab: Vertex Shading Chris Wynn Ken Hurley. Objective Hands-On vertex shader programming Start with simple programs … Part 1: Textured-Lit Teapot.

Similar presentations


Presentation on theme: "Lab: Vertex Shading Chris Wynn Ken Hurley. Objective Hands-On vertex shader programming Start with simple programs … Part 1: Textured-Lit Teapot."— Presentation transcript:

1 Lab: Vertex Shading Chris Wynn Ken Hurley

2 Objective Hands-On vertex shader programming Start with simple programs … Part 1: Textured-Lit Teapot

3 What you will learn: Exposure to register/instruction set. How to do matrix transformations in a vertex program. How texture coordinates are assigned. How to do simple vertex lighting. OpenGL\src\labs\vertexlab_part1_exercise main.cpp vertex_programs.h …\Effects\TeapotExercise TeapotExercise.cpp TeapotExercise.nvv

4 The Sample Application Provides Initialization Create and set vertex shader Create vertices, normals, texture coords Create and bind texture Set-up render states and texture combining Every frame Update viewing parameters Render teapot

5 Textured-Lit Teapot Shader Inputs Constant memory contains c[0]…c[3] contains composite transform matrix. c[4]...c[7] contains the object-to-view matrix. c[8]...c[11] contains the inverse transpose of the. object-to-view matrix. c[12] contains the eye-space light position. c[13] contains the material color. c[14] contains some useful constants.

6 Textured-Lit Teapot Shader Inputs (cont.) Per-Vertex inputs Position (v0, v[OPOS]) Normal (v1, v[NRML]) Texture coordinates (v2, v[TEX0]) *For OpenGL, v[OPOS] = v[0] v[NRML] = v[2] v[TEX0] = v[8]

7 Textured-Lit Teapot Shader Program (OpenGL) // Transform position to clip space DP4 o[HPOS].x, c[0], v[OPOS] ; DP4 o[HPOS].y, c[1], v[OPOS] ; DP4 o[HPOS].z, c[2], v[OPOS] ; DP4 o[HPOS].w, c[3], v[OPOS] ; // Set the primary color MOV o[COL0], c[13];

8 Textured-Lit Teapot Shader Program (DX8) ; Transform position to clip space DP4 oPos.x, v0, c0 DP4 oPos.y, v0, c1 DP4 oPos.z, v0, c2 DP4 oPos.w, v0, c3 ; Set the primary color Mov oD0, c13

9 Extension: Texturing Add texturing Copy texture coordinates

10 Texturing Solution MOV oT0, v2 MOV o[TEX0], v[TEX0]; OR MOV o[8], v[8];

11 Extension: Diffuse Lighting Compute vertex normal dot light direction Multiply result with material color

12 Diffuse Lighting Solution // 1. Get the position of the vertex in eye-space. (OpenGL) DP4 R0.x, c[4], v[OPOS]; DP4 R0.y, c[5], v[OPOS]; DP4 R0.z, c[6], v[OPOS]; DP4 R0.w, c[7], v[OPOS]; (D3D) DP4 r0.x, c[4], v0 DP4 r0.y, c[5], v0 DP4 r0.z, c[6], v0 DP4 r0.w, c[7], v0

13 Diffuse Lighting Solution (cont.) // 2. Compute normalized L = light position - vertex position. (OpenGL) ADD R1, c[12], -R0; DP3 R1.w, R1, R1; RSQ R1.w, R1.w; MUL R1.xyz, R1, R1.w; (D3D) ADD r1, c[12], -r0 DP3 r1.w, r1, r1 RSQ r1.w, r1.w MUL r1.xyz, r1, r1.w

14 Diffuse Lighting Solution (cont.) // 3. Transform the normal to eye-space. (OpenGL) DP4 R2.x, c[8], v[NRML]; DP4 R2.y, c[9], v[NRML]; DP4 R2.z, c[10], v[NRML]; (D3D) DP4 r2.x, c[8], v1 DP4 r2.y, c[9], v1 DP4 r2.z, c[10], v1

15 Diffuse Lighting Solution (cont.) // 4. Compute N dot L and clamp the result. (OpenGL) DP3 R3, R1, R2; MAX R3, R3, c[14].w; (D3D) DP3 r3, r1, r2 MAX r3, r3, c[14].w

16 Diffuse Lighting Solution (cont.) // 5. Modulate the material color by NdotL. (OpenGL) MUL o[COL0], c[13], R3; (D3D) MUL oD0, c[13], r3

17 Part 2: Reflective Teapot What you will learn: Custom texture coordinate generation.

18 Part 2: Reflective Teapot (cont.) OpenGL\src\labs\vertexlab_part2_exercise main.cpp vertex_programs.h …\Effects\TeapotExercisePart2 TeapotExercise2.cpp TeapotExercise2.nvv

19 Reflective Teapot Shader Inputs Constant memory contains c[0]…c[3] contains composite transform matrix. c[4]...c[7] contains the object-to-view matrix. c[8]...c[11] contains the inverse transpose of the. object-to-view matrix. c[12] contains the eye-space light position. c[13] contains the material color. c[14] contains some useful constants.

20 Reflective Teapot Shader Inputs (cont.) Per-Vertex inputs Position (v0, v[OPOS]) Normal (v1, v[NRML]) *For OpenGL, v[OPOS] = v[0] v[NRML] = v[2]

21 Extension: Reflection vector Start with basic program and compute reflection vector. R = 2 ( N E ) N - E Hints in the code…

22 Reflection Vector Solution // A1. Get the position of the vertex in eye-space. (OpenGL) DP4 R0.x, c[4], v[OPOS]; DP4 R0.y, c[5], v[OPOS]; DP4 R0.z, c[6], v[OPOS]; DP4 R0.w, c[7], v[OPOS]; (D3D) DP4 r0.x, c[4], v0 DP4 r0.y, c[5], v0 DP4 r0.z, c[6], v0 DP4 r0.w, c[7], v0

23 Reflection Vector Solution (cont.) // A2. Compute the vector from the eye-space vertex to the camera origin (0,0,0) // A3. Normalize (OpenGL) DP3 R0.w, R0, R0; RSQ R0.w, R0.w; MUL R1.xyz, -R0, R0.w; (D3D) DP3 r0.w, r0, r0 RSQ r0.w, r0.w MUL r1.xyz, -r0, r0.w

24 Reflection Vector Solution (cont.) // B. Determine the normal vector N (in eye-space). (OpenGL) DP4 R2.x, c[8], v[NRML]; DP4 R2.y, c[9], v[NRML]; DP4 R2.z, c[10], v[NRML]; (D3D) DP4 r2.x, c[8], v1 DP4 r2.y, c[9], v1 DP4 r2.z, c[10], v1

25 Reflection Vector Solution (cont.) // C. Calculate the reflected vector R = 2*(N dot E)*N - E // R = 2 * ( N dot E )N - E // R1 = E, R2 = N // R4 = 2 * (R2 dot R1)R2 - R1 (OpenGL)(D3D) DP3 R3, R2, R1;DP3 r3, r2, r1 MUL R3, R3.x, R2;MUL r3.xyz, r3.x, r2 MUL R3, c[14].z, R3;MUL r3.xyz, c[14].z, r3.xyz ADD R4, R3, -R1;ADD r4.xyz, r3, -r1 MOV o[TEX0].xyz, R4;MOV oT0.xyz, r4.xyz

26 Part 3: Diffuse Bump-mapping setup What you will learn: Basics of per-pixel bump-mapping. How to implement “tangent space” computations in a vertex shader program.

27 Part 3: Diffuse Bump-mapping setup

28 Basics of per-pixel bump-mapping Basic idea: Provide a normal per-pixel. Provide other lighting parameters on a per-pixel basis (light vector, halfangle-vector, etc.) Compute a lighting equation using operations performed on a per-pixel basis Typically compute Phong diffuse and specular lighting: intensity = (NL) + (NH) m

29 Basics of per-pixel bump-mapping How this is done in real-time: Encode normals into an RGB texture. Map the “normal map” texture onto a model using standard 2D texture mapping. Additionally, compute L and/or H vectors on a per- vertex basis and interpolate these across a triangle. Compute the necessary dot-products using texture combining hardware (ex. Texture combiners) Phong diffuse and specular lighting: intensity = (NL) + (NH) m

30 Basics of per-pixel bump-mapping Computing Dot Products in a Common Space: When computing dot products, L, H, and N must be in the same coordinate space World, Eye, other. One convenient space for per-pixel lighting operations is called Texture Space Z axis of Texture Space is roughly parallel to the surface normal at a vertex X and Y axes are perpendicular to the Z axis and can be arbitrarily oriented around the Z axis

31 Basics of per-pixel bump-mapping Texture Space gives us a way to redefine the lighting coordinate system on a per-vertex basis SxT S T T S S T T

32 Basics of per-pixel bump-mapping It also allows us to define our normal maps in texture space. If we didn’t use texture space as for computing the per-pixel dot products, we’d either: 1. Have to define the normal map in some other space (and recompute the normal map each frame) 2. Define the normal map in texture space, but perform a per-pixel transformation to take the normal into the correct space.

33 Basics of per-pixel bump-mapping Texture space is convenient (so we use it). To compute N L or N H on a per-pixel basis, L and H must be provided in texture space for each pixel This means we must rotate the L and H vectors into texture space at each vertex (and use an appropriate form of linear interpolation to generate texture L and H vectors per-pixel)

34 Part 3: Diffuse Bump-mapping setup In this lab you will do the per-vertex math necessary for performing simple N L per-pixel lighting. Specifically you will: Pass through (s,t) texture coordinates for the normal map. Compute a light position in world space. Derive a world-space L vector. Rotate the L vector from world-space to texture space and provide this as texture coords for a normalization cube-map

35 Part 3: Diffuse Bump-mapping setup The per-vertex basis is given as T, B, N. The matrix for rotating a vector into this space is: Given an L vector in world-space, can transform to local-space using 3 dot products.

36 Part 3: Bump-mapping setup OpenGL\src\labs\vertexlab_part3_exercise main.cpp vertex_programs.h …\Effects\TeapotExercisePart3 TeapotExercise3.cpp TeapotExercise3.nvv

37 Questions, comments, feedback Chris Wynn (cwynn@nvidia.com)cwynn@nvidia.com Matthias Wloka (mwloka@nvidia.com)mwloka@nvidia.com www.nvidia.com/developer


Download ppt "Lab: Vertex Shading Chris Wynn Ken Hurley. Objective Hands-On vertex shader programming Start with simple programs … Part 1: Textured-Lit Teapot."

Similar presentations


Ads by Google