Page 185 - Computer Graphics Handout
P. 185

glGenVertexArrays(1, &abuffer);
          glBindVertexArray(abuffer);
          int loc = glGetAttribLocation(program, “vPosition”);
          glEnableVertexAttribArray(loc);
          glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, 0,
          BUFFER_OFFSET(0));
          color_loc = glGetUniformLocation(program, “fcolor”);
          glGenBuffers(1, &buffer);
          glBindBuffer(GL_ARRAY_BUFFER, buffer);
          glBufferData(GL_ARRAY_BUFFER, sizeof(square), square,
          GL_STATIC_DRAW);
          If the data do not change, we can also set the projection matrix and model-view matrix as part of initialization and send them to the
          vertex shader:
          model_view = LookAt(eye, at, up);
          projection = Ortho(left, right, bottom, top, near, far);
          glUniformMatrix4fv(matrix_loc, 1, GL_TRUE, model_view);
          glUniformMatrix4fv(projection_loc, 1, GL_TRUE, projection);
          The core of the display callback is
          void display()
          {
          mat4 mm;
          // clear the window
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
          // render red square
          glUniform4fv(color_loc, 1, red);
          glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
          // matrix to compute vertices of shadow polygon
          mm = model_view*Translate(light[0], light[1],
          light[2])*m*Translate(-light[0], -light[1], -light[2]);
          glUniformMatrix4fv(matrix_loc, 1, GL_TRUE, mm);
          // render shadow polygon
          glUniform4fv(color_loc, 1, black);
          glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
          glutSwapBuffers();
          }

          Note that although we are performing a projection with respect to the light source, the matrix that we use is the model-view
          matrix.We render the same polygon twice: the first time as usual and the second time with an altered model-view matrix that
          transforms the vertices. The same viewing conditions are applied to both the polygon and its shadow polygon. The results of
          computing shadows for the colored cube are shown in Color Plate 29. For a simple environment, such as an airplane flying over flat
          terrain casting a single shadow, this technique works well. It is also easy to convert from point sources to distant (parallel) light
          sources (see Exercise 4.17). However, when objects can cast shadows on other objects, this method becomes impractical. In Chapter
          11, we address more general, but slower, rendering methods that will create shadows automatically as part of the rendering process.


          4.10 Windowing and Viewport Transformations



          Windowing and viewport transformations are essential steps in the computer graphics viewing process. They define how objects
          described in world coordinates are mapped onto a two-dimensional display device. These transformations ensure that the visible
          portion of a scene is correctly positioned and scaled within the display area.






                                                             185
   180   181   182   183   184   185   186   187   188   189   190