cs101c gpu programming
Download
Skip this Video
Download Presentation
CS101c GPU Programming

Loading in 2 Seconds...

play fullscreen
1 / 33

CS101c GPU Programming - PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on

CS101c GPU Programming. Rendering, OpenGL, and Lighting. Today. Review Rendering and OpenGL Go from vertices/triangles to colors on screen. Spaces. Object space Convenient place to model the object. Spaces. World space Common coordinates for the scene. Spaces. Camera space

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CS101c GPU Programming' - fynn


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs101c gpu programming

CS101cGPU Programming

Rendering, OpenGL, and Lighting

today
Today
  • Review Rendering and OpenGL
  • Go from vertices/triangles to colors on screen

CS101 GPU Programming

spaces
Spaces
  • Object space
    • Convenient place to model the object

CS101 GPU Programming

spaces1
Spaces
  • World space
    • Common coordinates for the scene

CS101 GPU Programming

spaces2
Spaces
  • Camera space
    • Camera placed at origin
    • Generally looking down –Z, with Y up, X right

C

CS101 GPU Programming

spaces ctd
Spaces ctd
  • (Clipped) Normalize Device Coordinates
    • NDC
    • Camera still down –Z, Y up, X right
    • Space scaled to x, y, z in [-1, 1]
    • ‘3D effect’

CS101 GPU Programming

transformations between spaces
Transformations Between Spaces
  • Matrix multiplication for Object, World, Camera
  • Take V = (x, y, z, 1)T
    • Simply set up a stack of matrix transformations Oi
    • Multiply together in reverse order to be applied
      • Vfinal = O1.O2.O3…On.V
    • Note: TRS = TSR, TRS != RTS != RST,
      • For T translation, R rotation, S scaling

CS101 GPU Programming

transformations
Transformations
  • All transformations as 4x4 matrices
  • Translation
    • Translate x, y, z :T=
  • Scaling
    • Scale x,y,z by a, b, c: S =

CS101 GPU Programming

transformations1
Transformations
  • Rotation
  • Let be the axis of rotation, and a be the angle of rotation, then we define R with:
  • S = , and
  • Then R =
  • Note that R-1 = RT

CS101 GPU Programming

transformations2
Transformations
  • Projection transformation (Camera -> NDC)

CS101 GPU Programming

transformations3
Transformations
  • Projection transformation (Camera -> NDC)
  • n – near, f – far, l – left, r – right,t – top, b -bottom

CS101 GPU Programming

transformations between spaces1
Transformations Between Spaces
  • Projection
    • Just add P to start of stack:
      • Vfinal = P .O1.O2…On.V
    • Vfinal = (x, y, z, w)
    • VfinalNDC = (x/w, y/w, z/w, 1), clipped to [-1,1]

CS101 GPU Programming

rasterization
Rasterization
  • Once vertices in NDC, draw triangles
  • Place pixels everywhere inside triangle boundary, interpolating data at boundaries
    • Barycentric coordinates
  • Initial pixel color interpolated from vertex data
    • Modify with GPU shader code

CS101 GPU Programming

buffers
Buffers
  • Z-Buffer
    • Store the (linearly interpolated) depth values of each pixel
    • When drawing new pixel, if there’s already a closer pixel, don’t draw the new pixel.
    • Correctly renders objects in front of each other
  • Double Buffering
    • Render to one buffer while drawing other buffer onscreen, swap buffers to redraw.

CS101 GPU Programming

opengl
OpenGL
  • How does OpenGL fit into all of this?
  • Implements basic pipeline
  • Works as a massive state machine to modify rendering properties
    • Set a property, won’t change until gets reset by code
    • E.g. color, transparency mode, lighting mode

CS101 GPU Programming

opengl state machine
OpenGL State Machine

CS101 GPU Programming

opengl transformation pipeline
OpenGL Transformation Pipeline
  • Modelview Matrix combines Object-World-Camera

CS101 GPU Programming

opengl transformation stacks
OpenGL Transformation Stacks
  • Stacks of matrices; multiplied together at render time
  • Two stacks to control – Modelview, Projection

Modelview

Matrix Stack:

(32 4x4 matrices)

Projection

Matrix Stack:

(2 4x4 matrices)

CS101 GPU Programming

opengl transformation stacks1
OpenGL Transformation Stacks
  • Stacks are part of state associated with vertices
  • Push and pop transformations
    • I.e., render an arm; push
    • Translate to hand; draw palm; push
      • Translate to and draw finger 1; pop
      • Translate to and draw finger 2; pop
    • Pop; Translate to other hand…

CS101 GPU Programming

opengl transformations
OpenGL Transformations
  • Function calls to build 4x4 matrices, and place them on the transformation stack
  • glTranslate3f(x, y, z);
  • glRotate3f(a, x, y, z);
    • a in degrees; x, y, z don’t have to be normalized
  • glScale3f(x, y, z);
  • glFrustum(l, r, b, t, n, f);
    • Sets up projection transformation

CS101 GPU Programming

opengl transformations1
OpenGL Transformations
  • An easier way to set up projection using GLU-GL Utility Library
  • gluPerspective(fovy, aspect, near, far);
    • Aspect – i.e., gl window w/h ratio

Aspect = w/h

CS101 GPU Programming

opengl geometry rendering
OpenGL Geometry/Rendering
  • Now we have these transformations, we can use them to render triangles.
  • Render triangle with:
    • glBegin(GL_TRIANGLES);
      • glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3);
      • // more sets of 3 vertices for more triangles.
    • glEnd();
    • Add glNormal3f(xi, yi, zi); before each vertex to attach a normal.

CS101 GPU Programming

opengl geometry rendering1
OpenGL Geometry/Rendering
  • glShade(GL_SMOOTH/GL_FLAT);
    • Tells OpenGL whether to use per vertex lighting or per face lighting (flat vs. gaussian)
    • For fixed pipeline, this won’t matter when we do lighting in GPU shaders
  • Lighting functions to set up light state
    • Same for material properties
    • Generally done at initialization
    • Light/Material state can be read in GPU code, so this step is still useful.

CS101 GPU Programming

opengl things to do to render
OpenGL Things to do to render
  • Initialize
    • Set up lights, materials, buffering, window size, camera, projection, etc.
  • Redraw
    • Clear current buffer (from double buffer setup)
    • Setup transformations, Begin/End vertices, normals
    • Swap buffers

CS101 GPU Programming

opengl utility toolkit glut
OpenGL Utility Toolkit (GLUT)
  • Very simple windowing system nicely interfaced with OpenGL
  • Sets up ‘Callback’ functions that get called when OpenGL needs to update state (redraw, user interface, etc).
  • We’ll use this for assignments…
    • … But we’ll give you most of the code

CS101 GPU Programming

simple lighting models
Simple Lighting Models
  • Flat, Gaussian, Phong
    • Per face, per vertex, and per pixel versions of the same lighting equation

CS101 GPU Programming

simple lighting models1
Simple Lighting Models
  • Sum of several components

CS101 GPU Programming

simple lighting models2
Simple Lighting Models
  • Diffuse component equation
  • Id = Cd * cos( a )
    • Cd the diffuse color, L light vector, N normal vector

N

L

a

CS101 GPU Programming

simple lighting models3
Simple Lighting Models
  • Specular component equation
  • Is = Cs * (R . Eye)S
    • Cs the specular color, S the ‘shininess’
    • R = 2N (L . N) – L
  • Note: All vectors must

be normalized!

R

N

L

a

a

Eye

b

CS101 GPU Programming

simple lighting models4
Simple Lighting Models
  • Specular component graph

Specular Power, S

R dot Eye

CS101 GPU Programming

simple lighting models5
Simple Lighting Models
  • Lighting equation
  • Color = Ia + Id + Is
    • Ia ambient intensity
    • Id diffuse intensity
    • Is specular intensity

R

N

L

a

a

Eye

b

CS101 GPU Programming

homework 1
Homework 1
  • Implement per pixel lighting using GLSL
  • Should be very short; mainly want to make sure GLSL is up and running for everyone
  • Due by class next Wednesday
  • Submit code for this assignment by either emailing a zip to Tamas, or emailing him the location of the code (CS cluster, UGCS, …)
    • Include a README explaining issues, and how to run the code!

CS101 GPU Programming

upcoming
Upcoming…
  • Friday
    • Recitation for HW1
    • Finally start GLSL/GPU programming basics
  • Monday
    • More GLSL
    • Start talking about texturing
    • Introduce Bump mapping and Normal mapping

CS101 GPU Programming

ad