Docstoc

Graphics

Document Sample
Graphics Powered By Docstoc
					                                 1




Shaders, Viewing & Perspective

             CS 234
           Jeff Parker
                 Objectives
Shader Programs
Perspective
   What it looks like
   How we make it happen
   The limitations of the zBuffer
Debugging with –gldebug and glGetError()

Gallery – Screen Shots (at end of this document)



   2
              Shader Outline
What is a GPU?
Why perform GPU processing?
Challenges of Parallel Processing
Basics of GPU
   Vertex Shader
   Fragment Shader
Programming Paradigm
Examples



   3
            What is a GPU?
Specialized silicon for offloading graphics
  processing from CPU
While there have been GPUs going back at least to
  the Commodore Amiga, the term currently
  implies the ability to program the GPU




   4
          What Languages?
Offline Rendering
   RenderMan – the first shader language
   Houdini and Gelato – modeled on RenderMan
Real-time shaders
   ARB Shaders – low level shading
   GLSL (OpenGL Shading Language)
   Cg – NVIDIA
   DirectX HLSL (High Level Shader Language)



  5
    NVIDIA G70




6
                Parallelism
Computer Scientists have been looking for a way to
  use multiple CPUs to speed up calculations
Obstacles include
   Sequential nature of many computations
   Fighting over shared data
      a[i] = a[i-1] + i;
There have been isolated areas of success
   Numerical Simulations
   SQL

   7
                   Graphics
Shading polygoins is another place for parallelism
I can tranform v1 and v2 independently
Many scan lines with many fragments
I can scan line x + y = 2 without affecting x - y = 3
I can also scan x + y = 2, x < 50
    without affecting x + y = 2, x > 50
Both write to frame buffer and z buffer, but update
   different spots.



   8
               Two models
SIMD
  Single Instruction, Multiple Data
  One code path, multiple processors working in
    parallel
Data Driven streams
  Sea of data washes over bed of computational units
  Data module has all relevant information
  When a complete data unit meets a free
    computational unit, the data is transformed
  Flows down stream for the next operation
   9
          Graphics Languages
Unlike CPU, GPU architecture details hidden
OpenGL or DirectX provide a state machine that
  represents the rendering pipeline.
Early GPU programs used properties of the state
  machine to “program” the GPU.
Tools like Renderman provided sophisticated
  shader languages, but these were not part of the
  rendering pipeline.



   10
                   Prior Art
One “programmed” in OpenGL using state variables
 like blend functions, depth tests and stencil tests

   glClearColor(0.0, 0.0, 0.0, 0.0);
   glClearStencil(0);
   glStencilMask(1); // Can only write LSBit
   glEnable(GL_STENCIL_TEST);

   glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

   glStencilFunc(GL_ALWAYS, 1, 1);
   glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);



   11
     Programmable Shaders, v1
As rendering pipeline became more complex, new functionality
  was added to the state machine via extensions.
The introduction of vertex and fragment programs provided full
  programmability to the pipeline.
With fragment programs, one could write general programs to
  process each fragment
  MUL               tmp, fragment.texcoord[0], size.x;
  FLR               intg, tmp;
  FRC               frac, tmp;
  SUB               frac_1, frac, 1.0;


Writing (pseudo)-assembly code is clumsy and error-prone.

   12
                   GLSL
Current GPU languages, such as Cg and GLSL allow
  programmer to work in something resembling C
uniform float time; /* in milliseconds */
void main(){
   float s;
   vec4 t = gl_Vertex;
   t.y = 0.1*sin(0.001*time+ 5.0*gl_Vertex.x)
     *sin(0.001*time+5.0*gl_Vertex.z);
   gl_Position = gl_ModelViewProjectionMatrix * t;
   gl_FrontColor = gl_Color;
}


   13
                       Model
All the language models share basic properties:
1. They view the pipeline as an array of “pixel computers”,
     with the same program running at each computer
   In fact, there are two types of computers: vertex
        computers and fragment computers
2. Data is streamed to each pixel computer
3. The pixel programs have limited state.
Issues – communication between components
   Between CPU and pixel computers
   Between Vertex and Fragment Shaders
   Between different Vertex (Fragment) Shaders

   14
        Programmable Pipeline Elements

We will define vertex shader and fragment shader
  programs
There are predefined:
   variables, holding position, color, etc
   OpenGL state, such as
      matrices: Model and View transformation
Order of operations is implicit
We can define new variables and functions
   We need to declare the scope of these variables

   15
            Communication
For CPU to communicate to Shaders
Can used predefined attributes
Can define uniform variables
Can use textures (called samplers)




  16
         Pass Through Vertex Shader
void main()
{
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}


Vertex shader does not know anything about connectivity of vertices
    There is a Geometry Processor – we won't say more about this
The job of the vertex shader is to take the attributes provided by the CPU:
    the projection Matrix and the View matrix
    gl_Vertex - the position of the vertex in world space
And produce something for the Rasterizer, which feeds the fragment shaders:
    gl_Position




    17
         Pass Through Vertex Shader
void main()
{
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}



// Simpler version
void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}




    18
                 Fragment Shader
// fPassThrough.glsl
// Pass through fragment shader.

void main()
{
   gl_FragColor = gl_Color;
}

The job of fragment shader is to take attributes provided by the vertex shader
   and define the color that will be stored in the frame buffer




    19
     Wave example




20
        Non-trivial Vertex Shader
// We walk through this in the next slide
uniform float time; /* in milliseconds */

void main()
{
   float s;
   vec4 t = gl_Vertex;
   t.y = 0.1*sin(0.001*time+5.0*gl_Vertex.x) *
     sin(0.001*time+5.0*gl_Vertex.z);
   gl_Position = gl_ModelViewProjectionMatrix * t;
   gl_FrontColor = gl_Color;
}



   21
              Variable Scope

const: compile time constant
uniform: parameter is fixed over a glBegin/glEnd
  pair
attribute: per-vertex information send from CPU:
   coordinates, texture coords, normals, color, …
   Predefined and user defined
varying: interpolated data that a vertex shader
  sends to the fragment shaders



   22
                       Datatypes
Singletons -
   float, bool, int – no bitmaps
        float a,b;
        int c = 2;
        bool d = true;
Vectors
   vec{2, 3, 4} – vector of floats
        vec4   eyePosition =   gl_ModelViewMatrix * gl_Vertex;
        vec2   a = vec2(1.0,   2.0);
        vec2   b = vec2(3.0,   4.0);
        vec4   c = vec4(a,b)   // c = vec4(1.0, 2.0, 3.0, 4.0);
   bvec{2, 3, 4} – boolean vector
   ivec{2, 3, 4} – integer vector

   23
                 Vertex Shader
// The CPU modifies the value of time in the update routine
uniform float time; /* in milliseconds */

void main()
{
    float s;
    // gl_Vertex is pre-vertex attributed passed by CPU
    vec4 t = gl_Vertex;
    // t is a vector: we will modified the vertex position
    t.y = 0.1*sin(0.001*time+5.0*gl_Vertex.x) *
      sin(0.001*time+5.0*gl_Vertex.z);
    // Use new value of vertex to update gl_Position
    gl_Position = gl_ModelViewProjectionMatrix * t;
    // Pass through color
    gl_FrontColor = gl_Color;
}

   24
                        Vertex builtins
Per Vertix attributes
     in int gl_VertexID;
     in int gl_InstanceID;
     out gl_PerVertex {
            vec4 gl_Position;
            float gl_PointSize;
            float gl_ClipDistance[];
     };
Global Attributes
     in vec4 gl_Color;
     in vec4 gl_SecondaryColor;
     in vec3 gl_Normal;
     in vec4 gl_Vertex;
     in vec4 gl_MultiTexCoord0;…
     in vec4 gl_MultiTexCoord7;
     in float gl_FogCoord;

     25
        Sending time from CPU
// wave.c program running in CPU
GLint          timeParam;
GLuint         program     = 0; /* program object id */

/* GLSL initialization */
static void initShader(const GLchar* vShaderFile, const GLchar*
   fShaderFile)
{
    ...
    program = glCreateProgram();
    ...
    timeParam = glGetUniformLocation(program, "time");
}

static void draw(void)
{
    /* send elapsed time to shaders */
    glUniform1f(timeParam, glutGet(GLUT_ELAPSED_TIME));
    ...
   26
            Read Shader Source
// These routines can be used to read any pair of shaders
static char* readShaderSource(const char* shaderFile)
{
    FILE* fp = fopen(shaderFile, "rb");

    char* buf;
    long size;

    if (fp==NULL)
        return NULL;
    fseek(fp, 0L, SEEK_END);
    size = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
    buf = (char*) malloc((size+1) * sizeof(char));
    fread(buf, 1, size, fp);
    buf[size] = '\0';
    fclose(fp);
    return buf;
}
    27
                           initShader
static void initShader(const GLchar* vShaderFile, const GLchar* fShaderFile)
{
    GLint status;
    GLchar *vSource, *fSource;
    GLuint vShader, fShader;


    /* read shader files */


    vSource = readShaderSource(vShaderFile);
    if (vSource==NULL)
    {
         printf( "Failed to read vertex shader\n");
         exit(EXIT_FAILURE);
    }


    fSource = readShaderSource(fShaderFile);
    if (fSource==NULL)
    {
         printf("Failed to read fragment shader");
         exit(EXIT_FAILURE);
    }
    28
                     initShader (cont)
/* create program and shader objects */


vShader = glCreateShader(GL_VERTEX_SHADER);
fShader = glCreateShader(GL_FRAGMENT_SHADER);
program = glCreateProgram();


/* attach shaders to the program object */


glAttachShader(program, vShader);
glAttachShader(program, fShader);


/* read shaders */


glShaderSource(vShader, 1, (const GLchar**) &vSource, NULL);
glShaderSource(fShader, 1, (const GLchar**) &fSource, NULL);




    29
                    initShader (cont)
glCompileShader(vShader);


/* error check */


glGetShaderiv(vShader, GL_COMPILE_STATUS, &status);



                    void glGetShaderiv( GLuint      shader,
                                          GLenum        pname,
                                          GLint *       params);



                    glGetShaderiv — Returns a parameter from a shader object




    30
                      initShader (cont)
glCompileShader(vShader);


/* error check */


glGetShaderiv(vShader, GL_COMPILE_STATUS, &status);
if (status==GL_FALSE)
{
    printf("Failed to compile the vertex shader.\n");
    glGetShaderiv(vShader, GL_INFO_LOG_LENGTH, &elength);
    ebuffer = malloc(elength*sizeof(char));
    glGetShaderInfoLog(vShader, elength, NULL, ebuffer);
    printf("%s\n", ebuffer);
    exit(EXIT_FAILURE);
}


/* compile fragment   shader shader */
glCompileShader(fShader);


/* error check */


glGetShaderiv(fShader, GL_COMPILE_STATUS, &status);
    31
if (status==GL_FALSE) // Blah, Blah, Blah...
                   initShader (cont)
    /* link   and error check */


    glLinkProgram(program);
    glGetProgramiv(program, GL_LINK_STATUS, &status);
    if (status==GL_FALSE)
    {
         printf("Failed to link program object.\n");
         glGetProgramiv(program, GL_INFO_LOG_LENGTH, &elength);
         ebuffer = malloc(elength*sizeof(char));
         glGetProgramInfoLog(program, elength, &elength, ebuffer);
         printf("%s\n", ebuffer);
         exit(EXIT_FAILURE);
    }


    /* use program object */


    glUseProgram(program);


    /* set up uniform parameter */


    timeParam = glGetUniformLocation(program, "time");
}   32
                mesh – build mesh
void mesh()
{
    int i,j;
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(2.0, 2.0, 2.0, 0.5, 0.0, 0.5, 0.0, 1.0, 0.0);
    for(i=0; i<N; i++) for(j=0; j<N;j++)
    {
         glColor3f(1.0, 1.0, 1.0);
         glBegin(GL_POLYGON);
               glVertex3f((float)i/N, data[i][j], (float)j/N);
               glVertex3f((float)i/N, data[i][j], (float)(j+1)/N);
               glVertex3f((float)(i+1)/N, data[i][j], (float)(j+1)/N);
               glVertex3f((float)(i+1)/N, data[i][j], (float)(j)/N);
         glEnd();
         glColor3f(0.0, 0.0, 0.0);
         glBegin(GL_LINE_LOOP);
               glVertex3f((float)i/N, data[i][j], (float)j/N);
               glVertex3f((float)i/N, data[i][j], (float)(j+1)/N);
               glVertex3f((float)(i+1)/N, data[i][j], (float)(j+1)/N);
               glVertex3f((float)(i+1)/N, data[i][j], (float)(j)/N);
    33   glEnd();
    }
                 mesh – build mesh
void mesh() {
    int i,j;
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(2.0, 2.0, 2.0, 0.5, 0.0, 0.5, 0.0, 1.0, 0.0);
    for(i=0; i<N; i++)
         for(j=0; j<N;j++)
         {
                glColor3f(1.0, 1.0, 1.0);
                glBegin(GL_POLYGON);
                    glVertex3f((float)i/N, data[i][j], (float)j/N);
                    glVertex3f((float)i/N, data[i][j], (float)(j+1)/N);
                    glVertex3f((float)(i+1)/N, data[i][j], (float)(j+1)/N);
                    glVertex3f((float)(i+1)/N, data[i][j], (float)(j)/N);
                glEnd();
                glColor3f(0.0, 0.0, 0.0);
                glBegin(GL_LINE_LOOP);
                    glVertex3f((float)i/N, data[i][j], (float)j/N);
                    glVertex3f((float)i/N, data[i][j], (float)(j+1)/N);
                    glVertex3f((float)(i+1)/N, data[i][j], (float)(j+1)/N);
                    glVertex3f((float)(i+1)/N, data[i][j], (float)(j)/N);
    34          glEnd();
    }
                       Main Program
int main(int argc, char** argv) {
    int i,j;


    /* flat mesh */
    for(i=0;i<N;i++)
         for(j=0;j<N;j++)
                data[i][j]=0.0;


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(512, 512);
    glutCreateWindow("Simple GLSL example");
    glutDisplayFunc(draw);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);


    init();
    initShader("vmesh.glsl", "fPassthrough.glsl");


    glutMainLoop();


    return 0;
}   35
                     Recap
Program loads vertex and fragment shaders
Shaders take standard attributes and any program
  specific additions, such as time in this example
and compute standard results, and additional variables
We have only seen use of standard attributes to
  communicate between vertex and fragment shader.
   Per-vertex attributes for velocity in the Angel's
     Particle example
   Varying data in toon example to pass information
     from vertex shader to fragment shader

   36
              Teapot Colors
Example with pass-through vertex shader, with
  work done in fragment shader




   37
          Trivial Vertex Shader
void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_FrontColor = gl_Color;
}




   38
                 Fragment Shader
uniform float time;

void main()
{
    float d = length(gl_FragCoord.xy);
    gl_FragColor.r = 0.5*(1.0+sin(0.001*time))*gl_FragCoord.x/d;
    gl_FragColor.g = 0.5*(1.0+cos(0.001*time))*gl_FragCoord.y/d;
    gl_FragColor.b = gl_FragCoord.z;
    gl_FragColor.a = 1.0;
}


Fragment shader is in charge of color – easy thing for it to change
The rest of the program is as before: the main program opens and reads
   the shaders, and passes in the current time.



    39
                 Fragment Shader
uniform float time;
void main()
{
    float d = length(gl_FragCoord.xy);
    gl_FragColor.r = 0.5*(1.0+sin(0.001*time))*gl_FragCoord.x/d;
    gl_FragColor.g = 0.5*(1.0+cos(0.001*time))*gl_FragCoord.y/d;
    gl_FragColor.b = gl_FragCoord.z;
    gl_FragColor.a = 1.0;
}
(From GLSL Spec) The built-in special variables that are accessible
   from a fragment shader are intrinsically declared as follows:
    in vec4 gl_FragCoord;
    in bool gl_FrontFacing;
    in float gl_ClipDistance[];
    out vec4 gl_FragColor; // deprecated
    out vec4 gl_FragData[gl_MaxDrawBuffers]; // deprecated
    out float gl_FragDepth;
    40
                 Fragment Shader
uniform float time;
out vec4 gl_FragColor;
void main()
{
    float d = length(gl_FragCoord.xy);
    gl_FragColor.r = 0.5*(1.0+sin(0.001*time))*gl_FragCoord.x/d;
    gl_FragColor.g = 0.5*(1.0+cos(0.001*time))*gl_FragCoord.y/d;
    gl_FragColor.b = gl_FragCoord.z;
    gl_FragColor.a = 1.0;
}
Fragment shaders output values to the OpenGL pipeline using the built-
   in variables gl_FragColor, gl_FragData, and gl_FragDepth, unless
   the discard statement is executed.
Both gl_FragColor and gl_FragData are deprecated; the preferred usage
   is to explicitly declare these outputs in the fragment shader using the
   out storage qualifier.

    41
                 Example
So far, we have limited ourselves to standard
  graphics
Can we think outside the Frustum?
Angel gives one example:
Problem is normalizing vectors
Have (x, y, z), and need a normalized version
He proposes uses a texture map to speed up the
  computation (!?!$!!)



   42
                   Normalize
Store a 3D Texture Norm
For each value (x, y, z), store 1/sqrt(x2 + y2 + z2)
Take (x, y, z) * T(x, y, z)
Issues –
   We need to precompute in the CPU
   The solution is an estimate, due to aliasing




   43
           Example: Voronoi
You need to mail a letter – where is the closest Post
  Office?
Given a set of points S in the plane (3 space) The
  Voronoi diagram splits the plane (3 space) into
  sets of points closest to a member of S.




   44
     Voronoi




45
              Example: Voronoi
How can we compute Voronoi Diagrams quickly?
They are an important datastructure – much studied
http://www.cs.cornell.edu/info/people/chew/oldDelaunay.html
http://www.pi6.fernuni-hagen.de/GeomLab/VoroGlide/index.html.en
http://www.diku.dk/hjemmesider/studerende/duff/Fortune/


Can we use parallel computation to help?




    46
           Example: Voronoi
Can we use parallel computation to help?
  Hint: Use an additional dimension




   47
          Voronoi Computation
Each point in S is given a unique color
In order to compute the lower envelope, we need to
   determine, at each pixel, the fragment with the smallest
   depth value.
This can be done with a simple depth test.
    Allow a fragment to pass only if it is smaller than the
      current depth buffer value, and update the buffer
      accordingly.
The fragment that survives has the correct color.




   48
              Variable Scope

const: compile time constant
uniform: parameter is fixed over a glBegin/glEnd
  pair
attribute: per-vertex information send from CPU:
   coordinates, texture coords, normals, color, …
   Predefined and user defined
varying: interpolated data that a vertex shader
  sends to the fragment shaders



   49
                 toon vertex shader
// simple toon vertex shader
// www.lighthouse3d.com

varying vec3 normal, lightDir;

void main()
{
        lightDir = normalize(vec3(gl_LightSource[0].position));
        normal = normalize(gl_NormalMatrix * gl_Normal);

        gl_Position = ftransform(); // Deprecated... - jdp
}




       50
              toon fragment shader
varying vec3 normal, lightDir;
void main()
{
        ...
        if (intensity > 0.98)
                color = vec4(0.8,0.8,0.8,1.0);
        else if (intensity > 0.5)
                color = vec4(0.4,0.4,0.8,1.0);
        else if (intensity > 0.25)
                color = vec4(0.2,0.2,0.4,1.0);
        else
                color = vec4(0.1,0.1,0.1,1.0);

        gl_FragColor = color;
}



       51
Architectural Perspectives
                         Projections
How many angles on the corner are the same?
    none: trimetric
    two: dimetric
    three: isometric
Isometric is particularly easy to fake: see next slide
                  Orthogonal Isometric Projection
    What is going on?
v




      Sim City, Electronic Arts
       Perspective and Geometry
Where is the eye for each of these elevations?
Let's review the geometry of the cube
Where is the eye for one point, two point, and three point perspective?
                        Projections
Assume the cube is the set of points such that
|x| <= ½
|y| <= ½
|z| <= ½

Where do we put the eye to see only one face?
Where do we put the eye to see two faces?
Where do we put the eye for isometric view?
How can we rotate that axis to the z-axis?
        Perspective Projection
Orthogonal vs Perspective Projection




   57
        Perspective Projection
Discovered in Renaissance




  58               Albrecht Durer
     Perspective Projection




59
         Perspective Projection




 60
15th century illustration from William of Tyre's Histoire d'Outremer.
61
     Giotto, Exorcism of the demons at Arezzo
      Perspective Projection




62   Pietro Perugino, Christ Handing the Keys to St. Peter
                                                Note that the
                                                vanishing
                                                point is off
                                                the canvas




     Carpaccio, The Disputation of St Stephen
63
     Perspective Projection




       Paolo Uccello, Battle of San Romano
64
Perspective Projection




   Street painting by Kurt Wenner
           Defining Perspective
It is often simplest to define Model View transformations in
    terms of translations, rotations, scaling, etc.
We can also define Projection View this way: move the camera
    back, rotate to pan over a scene
However, it is most natural to use some special calls
Two parts: position camera, and define perspective

glLookAt(eyex, eyey, eyez, atx, aty, atz,
  upx, upy, upz)

glOrtho(left,right,bottom,top,near,far)
glFrustum(left,right,bottom,top,near,far)
gluPerpective(fovy, aspect, near, far)
       Perspective Projection
glOrtho(left,right,bottom,top,near,far)
glFrustum(left,right,bottom,top,near,far)
gluPerpective(fovy, aspect, near, far)
                    Perspective
Angel presents three programs that cover the same territory
He modifies the Color Cube program as follows
   Cube is fixed, but eye and frustum can change
   He uses three different ways to specify the viewing frustum
   LookAt() and Ortho()
   LookAt() and Frustum()
   LookAt() and Perspective()
                                                     69




                     Variations

// Version 1
mat4 p = Ortho(left, right, bottom, top, zNear, zFar);
glUniformMatrix4fv( projection, 1, GL_TRUE, p );


// Version 2
mat4 p = Frustum(left, right, bottom, top, zNear, zFar);


// Version 3
mat4 p = Perspective( fovy, aspect, zNear, zFar );
        Current Transformation Matrix


The following are combined to define a 4x4 matrix called the Current
   Transformation Matrix (CTM)
    glMatrixMode(GL_MODELVIEW);
    glMatrixMode(GL_PROJECTION);
We can manipulate them independently, but all vertices go through both
               Transformations
OpenGL keeps track of these matrices as part of the state
   Model-View (GL_MODELVIEW)
   Projection (GL_PROJECTION)
   Texture (GL_TEXTURE) (ignore for now)
   Color(GL_COLOR) (ignore for now)
Single set of functions for manipulation
Select which to manipulated by
   glMatrixMode(GL_MODELVIEW);
   glMatrixMode(GL_PROJECTION);

                                C
                  p                       p’=Cp
    vertices                CTM                   vertices
          Handling Raw Matrix
Can load and multiply by matrices defined in the application
   program
    glLoadMatrixf(m)
    glMultMatrixf(m)
The matrix m is a one dimensional array of 16 elements
   which are the components of the desired 4 x 4 matrix
   stored by columns
In glMultMatrixf, m multiplies the existing matrix on
   the right
We can save the current state with push, restore it with pop
Can also query the current matrix
    double m[16];
    glGetFloatv(GL_MODELVIEW, m);
         Orthographic Projection
Let's define these projections by hand
We will look at simple examples before looking at the most
   general example
The simplest is an orthographic: (x, y, z, 1)  (x, y, 0, 1)
Singular – sends non-zero items, such as (0, 0, 1, 0) to zero
    Singular matrices have a determinate of 0


                 
                  1   0   0      
                               0 x x
                               
                                   
                  0
                    1   0    0 y y
                                  
                 
                  0   0   0      
                               0 z   
                                         0
                               
                                   
                  0
                    0   0              1
                               1 1  
                                 
                                                              74




               Pinhole Camera




Use similar trianges to find projection of point at (x,y,z)

             zp = d     xp= -x/z/d      yp= -y/z/d

 These are equations of simple perspective
            Review perspective
Look at xz and yz planes




                     x         y
               xp      yp       zd
                    z/d      z /d
               Perspective Divide
                                            x         y
                                      xp      yp       zd
                                           z/d      z /d
How do we express that with a matrix?
Remember that (tx, ty, tz, t) = (z, y, z, 1)

                            x 
           1   0     0 0   x      x       z / d 
                          
                                
           0
             1     0 0 y  y 
                                             y
                                            
          
           0   0            
                     1 0 z   z  z / d 
                          
                                d 
           0
             0                  z
                    1/ d 0 1  / d 
                                           1 
                                             
                     In practice
Rather than derive a projection matrix for each type of
  projection, convert all projections to orthogonal projections
  with default view volume
Allows us to use standard transformations in the pipeline and
  makes for efficient clipping
Delay projection to preserve z-depth for z-Buffer computation




   77
        Orthographic Projection
Convert clipping box to standard cube
Two steps –
    Move center to origin
       T(-(left + right)/2, -(bottom+top)/2, -(near+far)/2
    Scale sides
       S(2/(left-right), 2/(top-bottom), 2(near-far)
P = ST




   78
        Orthographic Projection
T(-(left + right)/2, -(bottom+top)/2, -(near+far)/2
S(2/(left-right), 2/(top-bottom), 2(near-far)
P = ST
              2                                       
                                                                        (left right) 
        left  right          0            0         0 1       0 0
                                                                              2        
                                                      
                                                         
                              2                        0             (bottom top) 
                0                            0         0         1 0                   
                        top  bottom                  
                                                                              2        
                                           2          
                                                                        (near  far) 
              0               0                      0 0       0 1
                                         near  far                           2        
                                                       0
                                                         
                                                                 0 0          1        
                                                                                         
              0               0            0         1
                   2                                       left right 
             left  right         0            0             
                                                             left  right 
                                                                        
                                  2                       top  bottom
                     0                           0        
        P                  top  bottom                  top  bottom
                                               2           near  far 
                   0              0                      
                                            near     far    near  far 
   79                                                                   
                   0              0            0                1       
            Perspective Projection
Orthographic vs Perspective

              2                                     left  right 
        left  right        0             0        
                                                      left  right 
                                                                 
                             2                     top  bottom
                0                           0      
                       top  bottom                top  bottom
                                          2         near  far 
              0             0                     
                                        near  far    near  far 
                                                                 
              0             0             0              1       
         2  near                      left right                  
        left  right        0                              0       
                                         left  right
                                                                    
                         2  near     top  bottom                  
                0                                            0
                       top  bottom   top  bottom                  
                                      near  far      2  far near 
              0              0                      
                                        near  far       near  far 
                                                                    
              0              0              1               0       
   80
            Perspective Projection
Effect on (left, bottom, near)

     2  near                      left right                  
    left  right        0                              0       
                                     left  right                        
                                                                 left
                       2  near     top  bottom                         
          0                                            0       bottom
                                                                   
                   top  bottom   top  bottom                  
                                                                    near 
                                  near  far      2  far near 
            0              0                                            
                                  near  far       near  far    1 
                                                                   
                                                                
          0              0              1               0       
       near(left  right) 
       left  right               
                           near  1 
      near(bottom top)             1
                                near  
      top  bottom                
                                      
       near(near  far)  near  1 
          near  far       near  1 
                          
             near         
   81
                                 z-Buffer
General effect on z

           2  near                      left  right                 
          left  right        0                              0       
                                           left  right                   
                                                                       x
                             2  near     top  bottom                    
                0                                            0        y
                           top  bottom   top  bottom                    
                                                                      
                                                                         z 
                                        near  far      2  far near  
                  0              0                                      
                                        near  far       near  far  1
                                                                      
                0              0              1               0       
                       ...                         ...              
                                                                    
                       ...                         ...              
           z(near  far)  2  far near  z(near  far)  2  far near
                                                                    
                  near  far                 z(near  far)          
           
                        z               
                                                     1                
                                                                          



   82
                                                      83
                     Z-Buffer

   Range for (Zmin, Zmax) = (0.1, 10)


                   Range for (Zmin, Zmax) = (1, 10)




Uniform distances in z do not give us uniform distances in z'
Pick as large a value for near as you can
                     OpenGL Errors
Finding and fixed problems with OpenGL calls
The manual tells you what to expect
glClear(GLbitfield mask)
     The glClear function clears buffers to preset values.
     Parameters mask: Bitwise OR operators of masks that indicate the buffers to be
        cleared. The four masks are as follows.
GL_COLOR_BUFFER_BIT The buffers currently enabled for color writing.
GL_DEPTH_BUFFER_BIT The depth buffer.
GL_ACCUM_BUFFER_BIT The accumulation buffer.
GL_STENCIL_BUFFER_BIT The stencil buffer.
Return Value Returns the following error codes and their conditions.
GL_INVALID_VALUE Any bit other than the four defined bits was set in mask.
GL_INVALID_OPERATION glClear was called between a call to glBegin and
   the corresponding call to glEnd.

My standard advice is to check the return code for every function call.
     84
                             gldebug
When you run your program, can pass in command line parameters such as -
   gldebug
You program must be able to ignore them
    main(int argc, char *argv[]){
         int i;
         glutInit(&argc, argv);

-gldebug 
After processing callbacks and/or events, check if there are any OpenGL
    errors by calling glGetError. If an error is reported, print out a warning by
    looking up the error code with gluErrorString.

% ./my_cube_view -gldebug
2009-10-01 10:46:24.067 cube_view[75593:10b] GLUT Warning: GL error: invalid
    operation


    85
                        glGetError()
void glGetError(void);
The glGetError function returns the value of the error flag. Each detectable
   error is assigned a numeric code and symbolic name. When an error
   occurs, the error flag is set to the appropriate error code value.
    No other errors are recorded until glGetError is called, the error code is
       returned, and the flag is reset to GL_NO_ERROR.
    If a call to glGetError returns GL_NO_ERROR, there has been no
       detectable error since the last call to glGetError, or since OpenGL
       was initialized.
To allow for distributed implementations, there may be several error flags.
   If any single error flag has recorded an error, the value of that flag is
   returned and that flag is reset to GL_NO_ERROR when glGetError is
   called. If more than one flag has recorded an error, glGetError returns
   and clears an arbitrary error flag value.
    If all error flags are to be reset, you should always call glGetError in a
       loop until it returns GL_NO_ERROR.
     86
                       glGetError()
We know there is a problem, but we don't know where it is.
We could add a check to every call, or we could sprinkle calls between
   blocks of calls that check for an error.
For example, let's sprinkle our code with the following
    if (glGetError() != GL_NO_ERROR)
        printf("GL Error: (%s)\n",
           gluErrorString(glGetError()));
When we run the program, we see the following
    % ./cube_view
    GL Error: no error
    GL Error: no error
    GL Error: no error
    GL Error: no error
    GL Error: no error
    GL Error: no error
     87
                       glGetError()
The first call to glGetError returns the error, and clears it
    if (glGetError() != GL_NO_ERROR)
          printf("GL Error: (%s)\n",
            gluErrorString(glGetError()));
What we should say is
    GLenum error;
    if ((error = glGetError()) != GL_NO_ERROR)
          printf("GL Error: %s\n",
            gluErrorString(error));

With this code added, I see the following
    % ./cube_view
    GL Error: invalid operation
    GL Error: invalid operation
    GL Error: invalid operation…
    88
         Define function checkErr
void checkError(char *str) {
   GLenum error;
   if ((error = glGetError()) != GL_NO_ERROR)
       printf("GL Error: %s (%s)\n",
         gluErrorString(error), str);
}
void polygon(int a, int b, int c , int d {
   checkError("Poly 1");
   glBegin(GL_POLYGON);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glColor3fv(colors[a]);
   glVertex3fv(vertices[a]);          % ./cube_view
                                      GL Error: invalid operation (Poly 2)
   ...                                GL Error: invalid operation (Poly 2)
   glEnd();                           GL Error: invalid operation (Poly 2)
                                      GL Error: invalid operation (Poly 2)
   checkError("Poly 2");              GL Error: invalid operation (Poly 2)
}   89                                GL Error: invalid operation (Poly 2)
                      checkError()
void checkError(char *str) {
   GLenum error;
   if ((error = glGetError()) != GL_NO_ERROR)
       printf("GL Error: %s (%s)\n",
         gluErrorString(error), str);
}
void polygon(int a, int b, int c , int d {
   checkError("Poly 1");
   glBegin(GL_POLYGON);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glColor3fv(colors[a]);
   glVertex3fv(vertices[a]);          % ./cube_view
                                      GL Error: invalid operation (Poly 2)
   ...                                GL Error: invalid operation (Poly 2)
   glEnd();                           GL Error: invalid operation (Poly 2)
                                      GL Error: invalid operation (Poly 2)
   checkError("Poly 2");              GL Error: invalid operation (Poly 2)
}   90                                GL Error: invalid operation (Poly 2)
                            Homework

Pen and Paper – Given line segment, determine if it intersects a line or
   another line segment.

Project – Create a 3D world, and allow the user to rotate his point of view




    91
                 Summary

We can offload a great deal of processing to GPU
We cannot depend upon global variables to pass
  information around
However, we can augment the standard attributes
Viewing allows us to change our point of view
Perspective helps make things look more realistic




  92
                        Resources
The OpenGL® Shading Language – The Orange Book
Sample programs from Angel –
    Book examples on my examples page
    Examples from his Primer can be found off his webpage
LightHouse tutorials
    www.lighthouse3d.com/opengl/glsl/
LightHouse Source
    www.lighthouse3d.com/opengl/glsl/examples/
OpenGL(R) Shading Language, Randi Rost, John M Kessenich
The Cg Tutorial: The Definitive Guide to Programmable Real-Time
   Graphics Randima Fernando, Mark Kilgard
SIGGRAPH GP GPU course
    http://gpgpu.org/s2004
Journal of Graphics, GPU, and Game tools - http://jgt.akpeters.com/

   93
     Gallery




94
     Gallery




95
     Gallery




96
     Gallery




97
     Gallery




98
     Gallery




99
      Gallery




100
      Gallery




101
      Gallery




102
      Gallery




103

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:11/25/2012
language:Unknown
pages:103