banner



How To Start Learning Computer Graphics

Where Do I Beginning? A Very Gentle Introduction to Computer Graphics Programming

Keywords: 3D, foreshortening, stereoscopic vision, origin, coordinates, coordinate system, 3D scene, topology, model, mesh, polygon, vertices, edges, perspective projection, viewing frustum, perspective divide, similar triangles, screen space, normalize. Disclaimer: there is no guarantee that the information provided here is accurate. If you think you establish bugs or errors experience free to report them at problems@scratchapixel.com. We volition practice our all-time to look into information technology and accost the trouble in a timely manner. Nosotros do this for gratuitous, delight exist kind. Give thanks you.

Empathise How Information technology Works!

If you are here, it'south probably because you want to acquire figurer graphics. Each reader may have a different reason for being here, but we are all driven by the same want: sympathise how it works! Scratchapixel was created to respond this detail question. Here you lot will learn how it works and learn techniques used to created CGI, from the simplest and most important methods, to the more complicated and less mutual ones. Perchance you like video games, and you would similar to know how it works, how they are made. Mayhap you lot have seen a film a Pixar film and wonder what'south the magic behind information technology. Whether you are at school, academy, already working in the manufacture (or retired), information technology is never a bad time to be interested in these topics, to larn or better your cognition and we ever need a resource like Scratchapixel to find answers to these questions. That'south why we are here.

Scratchapixel is attainable to all. They are lessons for all levels. Of grade information technology requires a minimum of noesis in programming. While nosotros plan to write a quick introductory lesson on programming in the well-nigh future, Scratchapixel's mission isn't about teaching you programming. However, while yous will be learning about implementing different techniques used for producing CGI, you will also likely improve your programming skills in the process and learn a few programming tricks. Whether you consider yourself a beginner or an expert in programming, you will find here all sort of lessons adjusted to your level. Showtime elementary, with bones programs and progress from there. While nosotros are the topic of complexity, if you look at the list of lessons for each category, you will see a math label followed by a series of pluses (the sign "+"). The more pluses, the more than difficult is the lessons in terms of math (three pluses being the maximum).

A Gentle Introduction to Figurer Graphics Programming

You want to larn calculator graphics. Beginning do you know what information technology is? In the second lesson of this section, yous can discover a definition of figurer graphics, and also learn about how it generally works. Maybe you take heard almost terms such every bit modelling, geometry, animation, 3D, 2d, digital images, 3D viewport, real-time rendering, compositing merely y'all are unsure about what they mean and more importantly, how they relate to each other. The second lesson of this section will answer these questions. From there, you should know little well-nigh CG programming, but have a general understanding of CG and the different tools and processes involved in the making of CGI.

What'southward side by side? Our world is fundamentally three-dimensional. At to the lowest degree equally far every bit nosotros tin can experience it with our senses. To which some people like to add the dimension of time. Time plays an important role in CGI, but we will come back to this afterward. Objects from the real earth then are three-dimensional. That's a fact we remember nosotros can all agree on without having to get into proving information technology. However, what's interesting, is that vision, one of the senses past which this 3-dimensional world tin can be experienced, is principally a 2-dimensional process. We could peradventure say that the image created in our listen is dimensionless (we don't understand yet very well how images 'announced' in our encephalon), but when we speak of an epitome, it generally means to united states of america a flat surface, on which the dimensionality of objects has been reduced from three to two dimensions (the surface of the canvas or the surface of the screen). The only reason why this image on the canvas actually looks accurate to our brain, is considering objects gets smaller every bit they get further away from where y'all stand, an effect called foreshortening. If you are non convinced yet, recall of an epitome as nothing more than than a mirror reflection. The surface of the mirror is perfectly flat, and notwithstanding, we can't make the difference betwixt looking at the image of a scene reflected from a mirror and looking straight at the scene: you don't perceive the reflection, just the object. Information technology'southward only considering we have two optics that we can actually get a sense of seeing things in 3D, something nosotros call stereoscopic vision. Each eye looks at the same scene from a slightly different bending, and the brain tin can use these two images of the aforementioned scene to approximate the altitude and the position of objects in 3D space with respect to each other. Withal stereoscopic vision is quite express in a way as we can't measure out the distance to objects or their size very accurately (which computers tin can practise). Human vision is quite sophisticated and an impressive result of evolution, merely it's nonetheless a trick, and it can be fooled easily (many magicians tricks are based on this). To some extent, estimator graphics is a hateful by which we tin create images of bogus worlds and present them to the brain (through the hateful of vision), equally an feel of reality (something we call photograph-realism), exactly like a mirror reflection. This theme is quite common in science fiction, but technology is not far from making this actually possible.

It's actually important to say hither that while nosotros may seem more focused on the process of generating these images, a process we phone call rendering, computer graphics is non but about making images, but also about developing techniques to simulate things such as the motion of fluids, the motion of soft and rigid bodies, finding ways of animating objects and avatars such that their motility and every effects resulting from that movement is accurately imitation (for instance when you walk the shape of your muscles changes and the overall outside shape of your torso is a result of these muscles deformation ? to create a realistic avatar you need to discover ways of simulating these furnishings. We will likewise learn virtually these techniques on Scratchapixel.

What have we learned so far? That the globe is three-dimensional, that the way we look at information technology is two-dimensional, and that if you can replicate the shape and the advent of objects, the brain tin can non make the difference between looking at this objects directly, and looking at an epitome of these objects. Computer graphics is not express to creating photoreal images but while it's easier to create not photograph-realistic images than creating perfectly photo realistic ones, the goal of estimator graphics is clearly realism (as much in the way things move than they announced).

All nosotros need to practise now, is learn what the rules for making such a photo-real prototype are, and that's what yous volition also learn here on Scratchapixel.

Describing Objects Making Upwardly the Virtual World

The difference between the painter who is actually painting a existent scene (unless the subject of the painting comes from his/her imagination), and united states of america, trying to create an epitome with a calculator, is that we actually have to start somehow describe the shape (and the appearance) of objects making upwards the scene nosotros want to render an epitome of to the computer.

Effigy i: a second Cartesian coordinative systems defined by its two axis (x and y) and the origin. This coordinate system tin be used equally a reference to define the position or coordinates of points inside the plane.

Figure 2: a box can exist described past specifying the coordinates of its eight corners in a Cartesian coordinate organization.

One of the simplest and almost important concept nosotros learn at school is the thought of space in which points tin exist divers. The position of a point is generally divers with relation to an origin. On a ruler, this is generally the tick marked with the number zero. If we apply two rulers, i perpendicular to the other, nosotros can define the position of points in two dimensions. Add a third ruler, perpendicular to the first two, and you can define the position of points in three dimensions. The actual numbers representing the position of the point with respect to one of the tree rulers are called the points coordinates. Nosotros are all familiar with the concept of coordinates to marking were we are with respect to some reference betoken or line (for example the Greenwich meridian). We tin can now ascertain points in three dimensions. Let'southward imagine that you lot just bought a computer. This computer probably came in a box, and this box has eight corners (pitiful for stating the obvious). One way of describing this box, is to measure the distance of these eight corners with respect to 1 of the corners. This corner acts as the origin of our coordinate system and obviously the distance of this reference corner with respect to itself will be 0 in all dimensions. Still the distance from the reference corner to the other seven corners, volition be different than 0. Allow's image that our box has the following dimensions:

corner ane: ( 0, 0, 0) corner two: (12, 0, 0) corner iii: (12, 8, 0) corner iv: ( 0, eight, 0) corner v: ( 0, 0, 10) corner 6: (12, 0, ten) corner seven: (12, eight, ten) corner eight: ( 0, viii, ten)

Figure three: a box can exist described past specifying the coordinates of its viii corners in a Cartesian coordinate organization.

The first number correspond the width, the second number the height, and the third number the corner'southward depth. Corner ane as you can meet, is the origin from which all the over corners have been measured. All y'all need to practice from here, is somehow write a program in which y'all will define the concept of a 3-dimensional point, and use it to store the coordinates of the 8 points yous simply measured. In C/C++, such a plan could expect similar this:

typedef bladder Bespeak[3]; int main() { Point corners[8] = { { 0, 0, 0}, {12, 0, 0}, {12, 8, 0}, { 0, 8, 0}, { 0, 0, ten}, {12, 0, ten}, {12, 8, ten}, { 0, 8, 10}, }; return 0; }

Like in any language, there is always unlike ways of doing the same thing. This programme shows one possible way in C/C++ for defining the concept of point (line 1) and storing the box corners in memory (in this case every bit an array of viii points)

You have somehow created your first 3D plan. Information technology doesn't produce an image yet, but you can already store the clarification of a 3D object in memory. In CG, the collection of these objects is called a scene (a scene also includes the concept of camera and lights but we volition talk most this another time). As suggested before, we are lacking two very important things to make the process actually complete and interesting. First to actually represent the box in the memory of the computer, ideally, we also need a system that defines how these viii points are connected to each other to make up the faces of the box. In CG, this is called the topology of the object (an object is also called a model). We will talk well-nigh this in the Geometry section and the 3D Basic Render section (in the lesson on rendering triangles and polygonal meshes). Topology refers to how points which nosotros generally telephone call vertices are connected to each other to class faces (or flat surfaces). These faces are as well called polygons. The box would be made of 6 faces or vi polygons and the prepare of polygons forms what we phone call a polygonal mesh or simply a mesh. The 2d affair we are missing, is a system to create an image of that box. This requires to actually projection the corners of the box onto an imaginary canvas, a process we call perspective projection.

Creating an Image of this Virtual World

Figure 4: if you connect the corners of the canvas to the eye which by default is aligned with our Cartesian coordinate system, and extend the lines further away into the scene, yous get some sort of pyramid which we telephone call a viewing frustum. Any object contained inside the frustum (or overlapping it) is visible and will testify up on the image.

The process of projecting 3D point of the surface of the canvas, really involves a special matrix chosen the perspective matrix (don't worry if you don't know what a matrix is). Using this matrix to project point is not absolutely necessary just makes things much easier. However, y'all don't really need mathematics and matrices to figure out how it works. Yous tin can see an image, or a canvas as some sort of flat surface placed some distance away from the eye. Trace four lines all starting from the eye to each one of the 4 corners of the canvas and extend these lines further abroad into the world (as far as you can encounter). You get a pyramid which we call a viewing frustum (and not frustrum). The viewing frustum defines some sort of book in 3D infinite and the canvass itself it merely a plane cutting of this volume perpendicular to the eye line of sight. Place your box in front of the canvass. Next, trace a line from each corner of the box to the eye and mark a dot where the line intersects the sail. Find out on the canvas, the dots corresponding to each of the twelve edges of the box, and trace a line betwixt these dots. What do you see? An image of the box.

Effigy 5: the box is move in forepart of our camera setup. The coordinates of the box corners are expressed with respect to this Cartesian coordinate arrangement.

Effigy half dozen: connecting the box corners to the center.

Effigy seven: the intersection points between these lines and the canvas are the project of the box corners onto the sheet. Past connecting these points to each other, an wireframe image of the box is created.

The 3 rulers used to mensurate the coordinates of the box corner class what nosotros phone call a coordinate system. Information technology's a arrangement in which points can exist measured to. All points' coordinates chronicle to this coordinate system. Note that a coordinate tin either exist positive or negative (or nil) depending on whether it's located on the right or the left of the ruler'due south origin (the value 0). In CG, this coordinate system is ofttimes called the world coordinate system, and the point (0,0,0), the origin.

Let's move the apex of the viewing frustum at the origin and orient the line of sight (the view management) along the negative z-centrality (figure three). Many graphics applications use this configuration every bit their default "viewing organisation". Keep in heed that the top of the pyramid is really the betoken from which nosotros will exist looking at the scene. Allow's also movement the canvas one unit abroad from the origin. Finally, let's move the box some distance away from the origin, so that it is fully contained within the volume of the frustum. Because the box is in a new position (we moved it), the coordinates of its eight corners changed and we need to mensurate them once again. Note that because the box is on left side of the ruler'southward origin from which nosotros measure out the object's depth, all depth coordinates which likewise called z-coordinates will exist negative. Four of the corners are also beneath the reference point used to measure the object's height, and will have a negative height or y-coordinate. Finally, four of the corners will exist to the left of the ruler's origin measuring the object's width: their width, or x-coordinates will likewise be negative. The new coordinates of the box'southward corners are:

corner i: ( 1, -1, -5) corner 2: ( i, -1, -3) corner 3: ( 1, 1, -5) corner 4: ( ane, 1, -three) corner five: (-1, -i, -v) corner half dozen: (-one, -1, -three) corner 7: (-1, ane, -five) corner 8: (-1, 1, -iii)

Figure viii: the coordinates of the bespeak P', the project of P on the canvass can be computed using uncomplicated geometry. The rectangle ABC and AB'C' are said to be similar.

Let'due south expect at our setup from the side and trace a line from one of the corners to the origin (the viewpoint). We tin define two triangles: ABC and AB'C'. Equally yous can come across these ii triangles have the same origin (A). They are too somehow copies of each other, in the sense that the bending divers by the edges AB and AC is the same as the bending defined past the edge AB', Air-conditioning'. Such triangles are said to be similar. Similar triangles accept an interesting property: the ratio between their adjacent and opposite sides is the same. In other words:

$${ BC \over AB } = { B'C' \over AB' }.$$

Because the canvas is one unit abroad from the origin, we know that AB' equals i. We also know the position of B and C which are the z (depth) and y coordinates (summit) respectively of the corner. If we substitute these numbers in the higher up equation, we get:

$${ P.y \over P.z } = { P'.y \over 1}.$$

Where y' is really the y coordinate of the point where the line going from the corner to the viewpoint intersects the canvas, which is, was nosotros said earlier, the dot from which nosotros can draw an image of the box on the canvas. Thus:

$$P'.y = { P.y \over P.z }.$$

As you can see, the projection of the corner's y-coordinate on the canvas, is nothing more than the corner'south y-coordinate divided past its depth (the z-coordinate). This is probably one the simplest and nearly fundamental relation in reckoner graphics, known as the z or perspective divide. The exact aforementioned principle applies to the ten coordinate. The projected point 10 coordinate (ten') is the corner'due south x coordinate divided past its z coordinate.

Note though that because the z-coordinate of P is negative in our case (we will explain why this is always the case in the lesson from the Foundations of 3D Rendering section dedicated to the perspective projection matrix), when the x-coordinate is positive, the projected bespeak'southward x-coordinate volition go negative (similarly, if P.x is negative, P'.x will become positive. The same problem happens with the y-coordinate). As a result the image of the 3D object is mirrored both vertically and horizontally, which is not an issue we desire. Thus, to avert this problem, we will divide the P.x and P.y coordinates with -P.z instead; this volition preserve the sign of the 10 and y coordinates. Nosotros finally get:

$$ \begin{assortment}{l} P'.ten = { P.x \over -P.z }\\ P'.y = { P.y \over -P.z }. \terminate{array} $$

We now have a method to compute the actual positions of the corners as they appear on the surface of the sail. These are the ii-dimensional coordinates of the points projected on the canvass. Let's update our bones program to compute these coordinates:

typedef float Bespeak[three]; int main() { Point corners[8] = { { one, -i, -5}, { i, -1, -3}, { i, 1, -5}, { 1, 1, -3}, {-ane, -one, -five}, {-1, -1, -3}, {-1, i, -5}, {-1, 1, -3} }; for (int i = 0; i < 8; ++i) { // divide the 10 and y coordinates by the z coordinate to // project the point on the sheet float x_proj = corners[i][0] / -corners[i][two]; float y_proj = corners[i][i] / -corners[i][2]; printf("projected corner: %d x:%f y:%f\n", i, x_proj, y_proj); } return 0; }

Figure 9: in this example, the canvass is 2 units forth the x-axis and 2 units forth the y-axis. You can modify the dimension of the sheet if yous wish. Past making information technology bigger or smaller you will run into more or less of the scene.

The size of the canvas itself is also arbitrary. It can as well be a foursquare or a rectangle. In our example, we made it two units wide in both dimensions, which means that the x and y coordinates of whatever points lying on the sail are contained in the range -1 to ane (figure 9).

Question: what happens if any of the projected point coordinates is not in this range, if for case x' equals -1.ane? The point is merely not visible, it lies outside the boundary of the sheet.

At this bespeak we say that the projected point coordinates are in screen infinite (the space of the screen, where screen and canvas in this context our synonymous). Simply they are not piece of cake to manipulate, because they tin can either exist negative or positive, and we don't really know what they refer to with respect to for example the dimension of you figurer screen (if nosotros desire to display these dots on the screen). For this reason, we will showtime normalize them, which simply ways that we catechumen them from whatever range they are initially in, to the range [0,1]. In our example, because nosotros need to map the coordinates from -ane,1 to 0,ane we can just write:

float x_proj_remap = (1 + x_proj) / 2; float y_proj_remap = (1 + y_proj) / 2;

Coordinates of the projected points are not in the range 0,1. Such coordinates are said to be defined in NDC space, which stands for Normalized Device Coordinates. This is convenient considering regardless of the original size of the sheet (or screen), which tin be unlike depending on the settings you used, we at present have all points coordinates defined in a mutual infinite. The term normalize is very common. It means that you somehow remap values from whatsoever range they were originally in, to the range [0,1]. Finally, we by and large prefer to define point coordinates with regards to the dimensions of the final image, which equally you may know or not, is divers in terms of pixels. A digital prototype is nothing else that a two-dimensional array of pixels (as is you lot figurer screen).

Practice yous know what is the resolution or dimension of your screen in pixels?

A 512x512 image is a digital image having 512 rows of 512 pixels, of y'all prefer to run across it the other way around, 512 columns of 512 vertically aligned pixels. Since our coordinates are already normalised, all nosotros demand to practise to limited them in terms of pixels, is to multiply these NDC coordinates by the image dimension (512). Hither, our canvas beingness square, we volition also use a foursquare prototype:

#include <cstdlib> #include <cstdio> typedef float Bespeak[3]; int main() { Point corners[8] = { { 1, -1, -five}, { i, -1, -3}, { 1, ane, -5}, { 1, 1, -3}, {-i, -1, -v}, {-1, -1, -3}, {-1, 1, -5}, {-1, one, -three} }; const unsigned int image_width = 512, image_height = 512; for (int i = 0; i < 8; ++i) { // divide the x and y coordinates past the z coordinate to // project the indicate on the canvas float x_proj = corners[i][0] / -corners[i][2]; bladder y_proj = corners[i][1] / -corners[i][2]; bladder x_proj_remap = (1 + x_proj) / 2; float y_proj_remap = (1 + y_proj) / 2; bladder x_proj_pix = x_proj_remap * image_width; float y_proj_pix = y_proj_remap * image_height; printf("corner: %d x:%f y:%f\n", i, x_proj_pix, y_proj_pix); } return 0; }

The resulting coordinates are said to exist in raster space (20 what does raster mean, please explain). Our program is yet limited considering information technology doesn't really create an image of the box, but if you compile information technology and run information technology with the following commands (re-create/paste the code in a file and save it equally box.cpp):

c++ box.cpp ./a.out corner: 0 x:307.200012 y:204.800003 corner: i x:341.333344 y:170.666656 corner: 2 10:307.200012 y:307.200012 corner: iii 10:341.333344 y:341.333344 corner: four x:204.800003 y:204.800003 corner: 5 x:170.666656 y:170.666656 corner: 6 ten:204.800003 y:307.200012 corner: 7 ten:170.666656 y:341.333344

Y'all can use a paint program to create an epitome (set its size to 512x512), and add together dots at the pixel coordinates that your computed with the program. Then connect the dots to grade the edges of the box, and you will go an bodily prototype of the box (as showed in the video below). Pixel coordinates are integers, so you will need to round off the numbers given by the plan.

What take we learned?

  • That we starting time need to describe three-dimensional objects using things such as vertices and topology (information about how these vertices are connected to each other to form polygons or faces) before nosotros can produce an image of the 3D scene (a scene is a collection of objects).
  • That rendering is the process by which an image of a 3D scene is created. No matter which technique you use to create 3D models (there are quite a few), rendering is a necessary step to 'meet' any 3D virtual earth.
  • From this uncomplicated practice it should be quite apparent that mathematics (more than programming) are essential in the procedure of making an epitome with a figurer. Really the computer is merely a tool used to speed up the computation, just the rules used to create this paradigm are pure mathematics. Geometry plays a peculiarly important role in this process, peculiarly to handle objects transformations (scale, rotation, translation) only also provide solutions to bug such as calculating angles between lines, or finding out the intersection betwixt a line and other elementary shapes (a plane, a sphere, etc.).
  • In conlusion, computer graphics is by and large mathematics practical to a figurer program which purpose is to generate an image (photo-existent or non) at the quickest possible speed (and the accuracy that computers are capable of).
  • Modeling includes all techniques used to create 3D models. Modeling techniques volition be discussed in the Geometry/Modeling section.
  • While static models are fine, it is also possible to animate them over fourth dimension. Which means that an image of the model at each time step needs to be rendered (you can for instance interpret, rotate or scale the box a little between each consecutive prototype whether by animating the corners' coordinates or applying a transformation matrix to the model). More than avant-garde animation techniques can exist used to simulate the deformation of skin by basic and muscles. Only all these techniques have in common that geometry (the faces making up the models) are deformed over time. Hence fourth dimension, as suggested in the introduction is important in CGI as well. Check the Animation section to learn nigh this topic.
  • One particular field overlaps both animation and modeling. It includes all techniques used to simulate the motility of objects in a realistic manner. A very big field of computer graphics is devoted to simulating the motion of fluids (h2o, fire, smoke), cloth, hair, etc. The laws of physics are applied to 3D models to make them move, deform or break similar they would in the real world. Physics simulation are generally very computationally expensive, but they can likewise run in real time (it all depends on the complexity of the scene y'all simulate).
  • Rendering is also computationally expensive task. How expensive depends of how much geometry your scene is made up and how photo-real you lot desire the concluding paradigm to exist. In rendering, we differentiate two modes, an off-line and a realtime rendering mode. Real-time is used (it's actually a requirement) for video games, in which the content of the 3D scenes needs to exist rendered at least at 30 frames per 2d (by and large 60 frames a second is considered a standard). Most realtime rendering are performed on the GPU, a processor particularly designed to render 3D scenes at the quickest possible speed. Realtime rendering techniques will be discussed in the Realtime section. Offline rendering is unremarkably used in the production of CGI for films where realtime is not a requirement (images are precomputed and stored before being displayed at 24 or 30 or lx fps). It may take from a few seconds to many hours before one unmarried prototype is complete, merely information technology generally handles far more geometry and produce higher quality images than realtime rendering. However, real-time or offline rendering do tend to overlap more and more these days, with video games pushing the amount of geometry they can handle also every bit quality, and offline rendering engines trying to take reward of the latest advancements in the field of CPU applied science to greatly improve their performances. Off-line rendering is the master topic of a few sections on Scratchapixel: Foundations of 3D Rendering, Techniques Specific to Ray Tracing, Light Ship Algorithms, Shading and Procedural Texturing. We communication you to read lessons of the get-go department in chronological order.

Where Should I start?

We promise the simple box example got you hooked but the main goal of this introduction is to underline the role that geometry plays in computer graphics. Of course it'south not only almost geometry, simply a lot of the problems can be solved with geometry. Nigh computer graphics books offset with a chapter on geometry, which is always a flake discouraging because it seems like you demand to report a lot before you lot can really get to making fun stuff. Nonetheless, we really recommend yous to read the lesson on Geometry beginning before annihilation else. Nosotros will talk and larn about points just also about the concept of vector and normal. We volition learn about coordinate systems and more importantly virtually the concept of matrix. Matrices are used extensively to handle transformations such as rotation, scaling or translation. These concepts are used everywhere throughout all computer graphics literature which is why you need to study them first.

Many CG books do non provide a very practiced introduction to geometry maybe because the authors assume that readers already know about it or that it's better to read books devoted to this particular topic. Our lesson on geometry is very different. It's very thorough and explain everything in very unproblematic words (including things that only people who worked in production volition tell you nigh). Do start your studies with reading this lesson first.

What Should I Read Adjacent?

It'southward generally easier and more fun to start learning computer graphics programming with rendering. One possible way for you to become through the content of this website, is to commencement reading the lessons from the Foundation of 3D Rendering section in chronological social club. To understand the content of a lesson yous may need prior knowledge about another techniques. At the beginning of each lesson you will discover a list of other lessons that contains everything you demand to know in order to understand the content of the lesson yous are near to start (we phone call them prerequisites). Use this list to guide you lot through the content of the website and more chiefly get the foundations you demand in gild to progress in your studies.

Source: https://www.scratchapixel.com/lessons/3d-basic-rendering/get-started

Posted by: mcmullenalliat.blogspot.com

0 Response to "How To Start Learning Computer Graphics"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel