In which you generate two models using algorithms.
This lab is an exploration of 2D parametric surfaces, which are 3D shapes that can be expressed as functions of two variables. Earth is a parametric surface. Any point on its surface can be described using two variables: a latitude-longitude pair.
You will create two surfaces in this lab: a grid and a sphere. Both shapes will build on the notion of latitude and longitude. Remind yourself of the answers to these questions:
When you've successfully remembered, you're ready to start modeling.
Consider this 8x4 grid, whose latitude and and longitude indices are labeled:
Follow the steps below to generate a grid of arbitrary dimensions.
main.js
of the grid application, write a function generateGrid
that accepts two parameters: the number of lines of latitude and the number of lines of longitude. Have it implement this pseudocode:
for each longitude index
for each latitude index
assign indices to x and y
add vertex position to list of positions
construct vertex attributes
initialize
function to call this function.
When your render your scene as points, the vertices should appear in a grid formation. However, most vertices are probably offscreen. You need to scale and translate their coordinates so they fit in \(\begin{bmatrix}-1&1\end{bmatrix}\). You don't need matrices to do that.
To fill in the pixels between these intersections, you need to render triangles instead of points. Given that a single vertex is shared by as many as six triangles, as you can see in the figure above, indexed geometry is very helpful here. You need a way to figure out each vertex's index.
Given the vertex generation algorithm above, what is the index of the vertex whose ilatitude
is 0 and ilongitude
is 0? That's vertex 0. How about the one whose ilatitude
is 1 and ilongitude
is 0? That's vertex 1. How about the one whose ilatitude
is 0 and whose ilongitude
is 1? That's vertex 4.
Here's the complete list of indices for the example grid:
You need a general formula for computing the index. A vertex's ilongitude
value tells you how many lines of longitude precede it. Each of these lines has nlatitudes
vertices in it. So, you can multiply these together to get the number of indices in the rectangular block left of the vertex. Then you need to count the vertices south of the current latitude. That leads to this formula for turning a 2D latitude/longitude pair into a 1D index:
index = ilongitude * nlatitudes + ilatitude
With this formula, you are ready to generate the index list.
The grid should now render solid. However, it's hard to tell it's a grid. You could have achieved the same effect with just four vertices.
Your second challenge is to render a sphere. The sphere is actually a cousin to the grid. It is a grid that wraps around to form a ball. Follow these steps to generate your sphere:
main.js
of the sphere application, write a function named generateSphere
. Have it accept parameters for the numbers of lines of latitude, the number of lines of longitude, and the radius.
make empty seeds array
for each latitude index
compute latitude proportion
compute radians by applying proportion to radians range
convert radians and radius to Cartesian coordinates
push seed position as Vector4
Vector4
to a flat array, use flatMap
:
seeds.flatMap(seed => [seed.x, seed.y, seed.z])
for each longitude index
compute longitude proportion
compute degrees by applying longitude proportion
generate a rotation matrix around y-axis
for each latitude index
rotate seed position by matrix
push rotated position into positions list