Page 179 - Computer Graphics Handout
P. 179

where

          are the spacing between the samples in the x- and z-directions, respectively. If f is known analytically, then we can sample it to
          obtain a set of discrete data with which to work.
          Probably the simplest way to display the data is to draw a line strip for each value of x and another for each value of z, thus generating
          N + M line strips. Suppose
          that the height data are in an array data. We can form a single array with the data converted to vertices arranged by rows with the
          code
          float data[N][M];
          point4 vertices[2*N*M]
          int k =0;
          for(int I = 0; i<N; i++) for(int j=0; j<M; j++)
          {
          vertices[k] = vec4(I, data[i][j], j, 1.0);
          k++;
          }
          We can form an array for the vertices by column by switching roles of I and j as follows:
          point4 vertices[N*M];
          int k =0;
          for(int I = 0; i<M; i++) for(int j=0; j<N; j++)
          {
          vertices[k] = point4(j, data[j][i],I,1.0);
          k++;
          }
          We usually will want to scale the data to be over a convenient range, such as (0, 1), and scale the x and z values to make them easier
          to display as part of the model-view matrix or, equivalently, by adjusting the size of the view volume.
          We can display these vertices by sending both arrays to the vertex shader. So in the initialization we set up the vertex buffer object
          with the correct size but without sending any data:
          Gluint buffer;
          glBindVertexArray(buffer);
          loc = glGetAttribLocation(program, “vPosition”);
          glEnableVertexAttribArray(loc);
          glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, 0,
          BUFFER_OFFSET(0));
          glGenBuffers(1, &buffer);
          glBindBuffer(GL_ARRAY_BUFFER, buffer);
          glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), NULL,
          GL_DYNAMIC_DRAW)
          In the display callback, we load the two vertex arrays successively and display them:
          /* form array of vertices by row here */
          glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
          GL_DYNAMIC_DRAW);
          glDrawArrays(GL_LINE_STRIP, 0, N*M);
          /* form array of vertices by column here */
          glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
          GL_DYNAMIC_DRAW);
          glDrawArrays(GL_LINE_STRIP, 0, N*M);
          You should now be able to complete a program to display the data. Figure 4.44 shows a rectangular mesh from height data for a
          part of Honolulu, Hawaii. These data are available on the Web site for the book. There are a few problems with this simple










                                                             179
   174   175   176   177   178   179   180   181   182   183   184