What is Computer Graphics?
By: Nipun David
Tags: General, Computer Graphics, 3d, Virtual Reality, Augmented Reality

CG In Action

I believe it is the time to see some of the stuff we do in CG before this post becomes boring.

Let’s take-up an exercise of modeling and rendering a cube on a screen. When I say modeling it means storing the definition of the cube(representation — length, breadth and height) and rendering means drawing that 3D cube on a 2D screen which we can see.

GOAL — Generate a realistic drawing of a cube

But before we can actually begin there are some questions we must address about modeling — How do we describe the Cube?, rendering — How we visualize a 3D object in 2D?

Part I: Modeling — How do we describe the Cube

The problem to model a cube is how do we save a 3D object on a file disk. So for that, we make few assumptions to begin with:-

Cube’s centre is at the origin in 3D-space (0,0,0)

Cube’s length, breadth and height is 2 units

We are looking at the cube such that our eyes are aligned to the x-y-z-axis

But these assumptions are not enough for complex objects like a face or a car or anything else so we capture the coordinates vertices of the cube

Coordinates of the cube vertices

But this is still not enough as, if we draw these coordinates on a 2D canvas we won’t be able to perceive the shape of the cube, so we also capture the edges of the cube

Edges of the cube

We have a digital description of the cube — vertices and edges. We can now encode this data as binary to save it memory and hence the problem of modeling is solved

Part II: Rendering — How we visualize a 3D object in 2D

Rendering is tricky, you will see why in a moment but for starters how do we represent a 3D object on a 2D-Canvas, we have two options:-

Opt 1 — Throw away one space/axis i.e. z-axis but we will see only a square in that case, and we certainly don’t need that — Rejected Opt 2 — Map 3D-vertices to 2D points in the 2D plane, connect 2D points with the straight lines…

Okay but how??

So my dear readers here come projections and matrix…and from now things will start getting little freaky, but we will not go all the way in. We will see just a glimpse of it and gradually will turn up the heat

Perspective Projection — Have you ever wondered why objects get smaller as we move further away from them. We all at some point in our life have stumbled upon this question. This is one of the key concepts in CG, and we will try to understand this now.

In the image below you can see the concept on which the camera works, well at least the old cameras where you have to keep a film which you have to get processed later to get the photograph…

This is how the camera captures images

Here you can see we have a tree (A 3D Object), and on the left side of the image, we have our simple camera. Lights enter in the camera from the pinhole and a 2d image is created on the projection screen. The longer you keep the pinhole open more exposure you will get (all that camera stuff).

So now let's turn our simple camera into not so simple and overlay some geometry on it.

I have intentionally kept only one ray starting from the point — p on the tree and ending on point — q in our camera on the projection screen.

Here we are going to assume a few things so that we can converge to an equation which we will use to create our rendering algorithm

Assumptions:-

There is a Z-axis passing from the centre of the cube through the pinhole. Y-axis is vertically perpendicular to the Z-axis, positive in up-direction. The X-axis is perpendicular to both the Z & Y-axis — not visible in the diagram for the sake of simplicity Assuming that Y-axis is vertical and X-axis is horizontal. The size of our cube camera is 1 unit The distance of the point p on 3D-object(tree) along Z-axis is z — unit Also, point p is y — unit above Z-axis along with Y-axis Lightray starts from point p(x,y,z) on the 3D object and meets on point q on the projection plane — (u,v,1)

Note: That the u-unit and x-unit is the distance on the horizontal plane that is perpendicular to both Y and Z-axis, in the above image Y and Z-axis are clearly visible and imagine X-axis on a horizontal plane perpendicular to Y and Z-axis

We can see two similar triangles on this image.

Considering the similar triangle property : Ratio of edge length is same

From property above we can say that v/1 = y/z → v = y/z

v = y/z — vertical coordinate

therefore we can say that v is the slope and it is equal to y/z

In a similar way, we can get the horizontal coordinate

i.e. u/1 = x/z → u = x/z

u = x/z — horizontal coordinate

Therefore we can infer from the above two equations that if z becomes bigger which means if we move away from the 3D object, the value for u and v will become smaller and the image size will shrink on the projection plane.

Now replace the pinhole camera with a human eye, the pinhole becomes pupil in the eye. The projection plane is the retina that sends the image to the brain via the optic nerve. Now you know why objects looks smaller as we move further away from it.

From the above two equations, we can create our algorithm for the rendering of the cube (remember our problem …How to draw 3D on 2D?)

Assume the camera is at c = (2,3,5)

Convert the (x,y,z) points on 3d object to (u,v) for all 12 edges as below

Subtract the camera c from the vertex (x,y,z) — as we have assumed the camera is on (2,3,5) — to normalize the position of the cube w.r.t camera

Divide (x,y) by z to get the (u,v)

Draw line between (u1,v1) and (u2,v2)

BAAM!!! 😎

phew!!!

And here we go, we have successfully found out the coordinates of the 3d representation on a 2d screen without losing the shape of the cube.

In the above steps, we saw how we covert digital information into purely visual information with an algorithm that we derived with the help of high-school maths.