Main content
Computer programming - JavaScript and the web
Drawing 3D shapes
So now that we have a representation of our cube, we need to find a way to draw it.
To draw a 3D shape on a 2D surface, we need to "project" it. Imagine shining a light behind the cube and projecting its shadow onto a screen. The simplest form of projection is an orthogonal projection, which is what you would get if the beams of light were parallel to one another.
All this talk of projections might sound complicated, but it is very easy to implement: we just ignore the z-coordinates when drawing.
Setting things up
I like to create variables at the top of my programs to control how things are displayed, so I can easily change them later. Here are some variables we will want shortly; feel free to change the values.
var backgroundColor = color(255, 255, 255);
var nodeColor = color(40, 168, 107);
var edgeColor = color(34, 68, 204);
var nodeSize = 8;
Now we add a basic draw function:
draw = function() {
background(backgroundColor);
};
We will also need to add the following to our code:
translate(200, 200);
This shifts our canvas 200 pixels right and 200 pixels down, so the pixel at position (0, 0), is now in the center of our canvas. This means that our cube will be drawn in the center of the screen. The reason for doing things this way will become clear when we start to rotate our objects.
Drawing nodes
Inside the draw function we loop through all the nodes and draw an ellipse at the (x, y) coordinate of that node:
fill(nodeColor);
noStroke();
for (var n = 0; n < nodes.length; n++) {
var node = nodes[n];
ellipse(node[0], node[1], nodeSize, nodeSize);
}
Drawing edges
Also inside the draw function we add the code for drawing edges. I would add it before the code for drawing nodes, so the nodes are drawn on top of the edges.
stroke(edgeColor);
for (var e = 0; e < edges.length; e++) {
var n0 = edges[e][0];
var n1 = edges[e][1];
var node0 = nodes[n0];
var node1 = nodes[n1];
line(node0[0], node0[1], node1[0], node1[1]);
}
This code loops through the array of edges. It gets the two numbers defined by an edge and looks up the corresponding node in the nodes array. Then it draws a line from the (x, y) coordinate of the first node to the (x, y) coordinate of the second node.
Is that it?
We set out to draw a cube, yet all we've done is draw a square and four circles:
We could have drawn that with a lot less effort. However, this really is our cube - it's just that we're viewing it end on. If we can work out how to rotate our cube so it's no longer end on to the screen, we'll see that it's not just a square and four circles.
Want to join the conversation?
- i can't really understand the
partvar node0 = nodes[n0];
var node1 = nodes[n1];
line(node0[0], node0[1], node1[0], node1[1]);
I don't know why theedges[e][1];
andedges[e][0];
numbers have to get inside thenodes[n0];
andnodes[n1];
can someone explain me it simpler? and why they have to go inside?(74 votes)- From the author:I'm going to assume you understand how arrays work. Each node is an array of 3 numbers, representing a point in 3D space. For example, a node might be
[100, 200, 0]
, which would represent a point 100 pixels along horizontally, 200 pixels down vertically and 0 pixels "into" the screen.nodes
is an array of nodes, and so an array of arrays.var node0 = nodes[n0];
gets the node at indexn0
, which might be the 4th node ([-100, 100, 100];
). When we donode0[0]
, that gets the x-value for that node, andnode0[1]
gets the y-value.edges
is also an array of arrays.edges[e]
gets the the edge at thee
th index, which might be the edge between nodes 1 and 3. That would be an array,[1, 3]
. So when we doedges[e][0]
that get the first node for that edge (which is 1), andedges[e][1]
gets the second node for that edge (which is 3). We can use these values to look up the node positions in thenodes
array.
I hope that makes a bit more sense, but maybe I've just made things more confused.(124 votes)
- When it says, edges[e][0], I assume it is going to the eth array in edges and then first value in the eth array?
I am just confused because it was not covered in the array tutorial.(15 votes)- Your assumption is correct.
You might read it as(edges[e])[0]
if you wanted.(21 votes)
- If I put any value of edges from line 16-26, will it work effectively?(8 votes)
- Do I absolutely NEED to learn trig in order to make 3D shapes? I was wondering, because I am only in 6th grade and I don't think I will be learning trig for at least another 3 years.(8 votes)
- I do not believe you need to know trig in order to make 3D shapes or program to the 3D rendering primitives.(4 votes)
- Sorry, I don't really understand this part
var edge0 = [0, 1];
var edge1 = [1, 3];
var edge2 = [3, 2];
var edge3 = [2, 0];
var edge4 = [4, 5];
var edge5 = [5, 7];
var edge6 = [7, 6];
var edge7 = [6, 4];
var edge8 = [0, 4];
var edge9 = [1, 5];
var edge10 = [2, 6];
var edge11 = [3, 7];
what are the numbers in the arrays??(3 votes)- The numbers are indexes into the
nodes
array. Soedge[7]
runs betweennode[6]
andnode[4]
. That will always be the case no matter where the cube is moved or how it is rotated.(5 votes)
- What is nodes.length equivalent to? Is it the number of nodes or something else?(3 votes)
- That's correct! In this case it's returning the length of that array.(3 votes)
- In the for loop of //drawEdges,
I don't understand this part the code..
Thanks to Ethan Luis McDonough, I understand the first part but not this part..
Can someone please explain this a little simpler?
var node0 = nodes[n0];
var node1 = nodes[n1];
line(node0[0], node0[1], node1[0], node1[1]);(3 votes)- This section is in the loop that loops through the
edges
array. Theedges
array contains the indexes of the two nodes that the edge will be drawn between. We set the variablenode0
to the index of the first point andnode1
to the index of the second point. Then, we get the x and y of each node with the indexes we just retrieved and draw a line segment between those points.
Here's the code with more detailed comments:// we loop through the edges array
for (var e = 0; e < edges.length; e++) {
// we get the indexes of both of the nodes in our segment
var n0 = edges[e][0];
var n1 = edges[e][1];
// we get each node point by using the indexes we had previously retrieved
var node0 = nodes[n0];
var node1 = nodes[n1];
// we display the line segments
line(node0[0], node0[1], node1[0], node1[1]);
}(3 votes)
- In the movies these days, does it normally take this long to create a 3d shape, or is it much simpler due to having your own software?(2 votes)
- Nguyen,
Next time you watch an animated film, pay attention to the credits and see how many animators are involved.(4 votes)
- I do not understand how z axis is accounted for here?
node0[2]
is not taken into account to draw edges or nodes.(2 votes)- From the author:Correct, the z-axis is ignored when drawing the shapes. This is called an orthogonal projection. When you convert from 3D to 2D you necessarily lose some information about the shape. If you imagine holding a 3D shape up to the screen and then having lines coming from each node perpendicular to the screen, that's what we're doing. That's why the cube looks like a square when first see it. However, the z-axis is used when we rotate the shape, so does make a difference.(4 votes)
- oh i got it. we will see in the next article(3 votes)