C Game Dev 3d Vector Color

Posted on by
C Game Dev 3d Vector Color 4,7/5 8625 reviews
  1. Cnc Military
  2. C Game Dev 3d Vector Coloring
  3. C Game Dev 3d Vector Color Background
  4. C Game Dev 3d Vector Colors

Apr 09, 2017 C Game Tutorial - Simple C Space Shooter Game - for Beginner to learn basics of game programming. Without using graphics/graphics.h and pointers with audio effects with source code Download. $ begingroup $ It's not just games but consider comic books, for example: raster graphics are generally used more there with Photoshop than Illustrator. It's hard to draw subtle gradations of color, blend pixels, soften edges, etc. In a very precise way using vector graphics. To work with vectors, we have to firstly #include, and then we can use the vector name; syntax to declare a vector (as usual, any data-type can be used - int, string, your custom classes or structs, whatever!). We can optionally pass one or two parameters on creating a vector, the number of elements (to start out with), and the value. A 3D vector is something like. You're talking about a 3D array (simulated using a vector of vectors of vectors). A vector is a 1D array, no matter how large a number of dimensions it has. Please don't do this. Use boost::multiarray.

Note for math weenies: This is not meant to be a full tutorial on matrices, and is also not intended to cover the math behind them in the usual manner. It is my personal way of thinking about rotation matrices in 3D graphics, and it will hopefully provide at least some people with a new (and possibly more intuitive) way of visualizing matrices that they may not have considered before.
For serious 3D graphics, you will need to use matrix math. The problem is that at first glance it seems bloody complicated. The truth is there are simpler ways to think about matrix math than as abstract rectangular arrays of numbers. My personal favorite way of thinking about and visualizing 3D rotation matrices is this:
[bquote][color=#a52a2a]Simple 3-by-3 Rotation Matrices can be thought of simply as three 3D vectors. And those three vectors are usually unit-length (which means having a length of 1.0, where sqrt(x*x + y*y + z*z) = 1.0) and point along the X, Y, and Z axes, respectively, of the coordinate space the matrix represents.[/color][/bquote]
Matrices can be thought of as representing the transformation (or change) in orientation and position required to get from one Coordinate Space, or Frame of Reference, to another one. Imagine two people, one standing up, and one lying down (or try it yourself). To the person who is standing, up is up, down is down, forward is forward, etc. But to the person lying down, forward is really up, and backward is really down, and down is forward, and up is backward. Earth's gravity dictates that our normal frame of reference has 'down' pointing towards the center of the earth, but even then, everything has its own local frame of reference, where 'up' and 'down' are in relation to the person or thing and not in relation to the earth. All a matrix contains is a vector pointing in the direction that the local reference frame considers 'up' when seen from the global, Identity reference frame (you have to have some fixed frame that you measure everything else in relation to), plus another vector pointing 'right', and another vector pointing either 'forward' or 'backward', depending on convention.
The Identity Matrix, which produces no rotation at all, simply has an X Axis Vector of (1, 0, 0), a Y Axis Vector of (0, 1, 0), and a Z Axis Vector of (0, 0, 1). Notice how each Axis Vector extends only along the same axis (X, Y, or Z) of the Identity coordinate system. In Matrix notation, this is usually represented (in column-major order) as:
[color=#000080][font=courier new,courier,monospace]

[bquote][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [1, 0, 0][nbsp][nbsp]<- X Components of Axis Vectors
identity = [0, 1, 0][nbsp][nbsp]<- Y Components of Axis Vectors
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [0, 0, 1][nbsp][nbsp]<- Z Components of Axis Vectors
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]^[nbsp][nbsp]^[nbsp][nbsp]^- Z Axis Vector
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [nbsp][nbsp] ---- Y Axis Vector
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] ------- X Axis Vector
The cells are ordered (in RAM) like this:
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [0, 3, 6]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [1, 4, 7]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [2, 5, 8][/bquote]

[/font][/color]
This notation (which is OpenGL's notation style) fits perfectly into C's two-dimensional arrays, where the first index into the array is the Axis Vector you want (0 for X, 1 for Y, and 2 for Z), and the second index is the component (X, Y, or Z) of that Axis Vector. (It's also backwards from C's normal syntax of [row][column], where in this case you specify [column][row] in the matrix.) For example, the number in 'matrix[2][1]' is the Y component of the Z Axis Vector for the matrix. A very useful product of that arrangement in RAM is that if you take the address of the X component (second index is 0) of say, the Y Axis Vector (e.g. '&matrix[1][0]'), the result is a pointer to an ordinary 3D vector, which can be passed into vector math functions that expect a pointer to a 3-value vector. This leads into a different way of thinking about the actual calculation of rotations:
[bquote][color=#a52a2a]Rotating a 3D point through the use of a 3-by-3 rotation matrix can be thought of (and practiced) simply as the scaling and addition of vectors. You don't even need a 'matrix' to be able to do it.[/color][/bquote]
What you do is this: Take the X Axis Vector of the Rotation Matrix, and scale it (multiply all three of its components by the same scaling factor) by the X component of the 3D point to be rotated, scale the Y Axis Vector by the Y component of the point, and scale the Z Axis Vector by the Z component of the point. Finally, take those three scaled vectors and add them together (add all of the X components to produce the new X, add all of the Ys to produce the new Y, etc.), and the result is your original 3D point, but rotated into the coordinate space defined by the Axis Vectors in your Matrix. The end result is the same as doing a 'normal' point times matrix operation, but with an entirely different way of thinking about it.
See Visualizing Vector Addition for a visual representation of vector addition. Vector Scaling is simply the act of changing the length of the vectors without changing their direction.
To help visualize a rotation matrix, hold out your right hand in a 'hand gun' position with thumb up and index finger out, then extend your middle finger so it points out of your palm. Now rotate your whole hand so that your index finger points at you, and you have a standard right-handed Identity Matrix, with your index finger the Z Axis Vector, your thumb the Y Axis Vector, and your middle finger the X Axis Vector. As you rotate your hand, those vectors will rotate in 3D space, and any points multiplied by that matrix will follow. To see for yourself how points are rotated by the matrix using the above scaling and adding method, first start with a point lying along one of the axes, say the point (5, 0, 0) along the X axis. Now the Y and Z components are zero, so the Y and Z Axis Vectors of the Matrix will be scaled to zero, and all you have to think about is the X Axis Vector, which is simply multiplied by 5 (made 5 times as long). So as the X Axis Vector of the Matrix rotates, so does the point which lies on it.
In practice, the scaling and adding of vectors can be simplified to the following math, applicable for use in an actual program. Notice that the X coordinate of the point is only ever multiplied with a component of the X Axis Vector. This is just a simplification and condensation of the scaling and adding of vectors that is still going on under the covers.
NewX = X * Matrix[0][0] + Y * Matrix[1][0] + Z * Matrix[2][0];
NewY = X * Matrix[0][1] + Y * Matrix[1][1] + Z * Matrix[2][1];
NewZ = X * Matrix[0][2] + Y * Matrix[1][2] + Z * Matrix[2][2];

The previous section only covered rotational, or 'Linear' transformations. Usually you also want to have a matrix be able to translate, or shift, a point through space as well as rotate it, and that sort of operation is called an 'Affine' transformation. You do that with either a 3x4 or a 4x4 matrix, but I'll deal with 3x4 matrices to keep things simpler. 4x4 matrices enter into the realm of Homogeneous Coordinates, and Perspective transforms, which are not things you usually have to deal with yourself. A 3x4 matrix (3 tall, 4 wide) basically just adds another Vector to the matrix, but instead of being an Axis Vector, it is a Translation Vector. The representation is usually as follows:

Cnc Military

[color=#000080][font=courier new,courier,monospace]

[bquote][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [1, 0, 0, 0][nbsp][nbsp]<- X Components of Vectors
identity = [0, 1, 0, 0][nbsp][nbsp]<- Y Components of Vectors
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [0, 0, 1, 0][nbsp][nbsp]<- Z Components of Vectors
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]^[nbsp][nbsp]^[nbsp][nbsp]^[nbsp][nbsp]^- Translation Vector
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [nbsp][nbsp] [nbsp][nbsp] ---- Z Axis Vector
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [nbsp][nbsp] ------- Y Axis Vector
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] ---------- X Axis Vector
The cells are ordered (in RAM) like this:
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [0, 3, 6, 9 ]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [1, 4, 7, 10]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [2, 5, 8, 11][/bquote]

[/font][/color]
The Translation Vector is merely the amount to shift the point in X, Y, and Z, as seen from the Identity reference frame. It would work the same as if it was outside of the matrix, and you simply added it to the point after the matrix transform. If the matrix was representing a jet plane's orientation in the world, the Translation Vector would merely be the coordinates of the center of the jet plane in the world. When describing matrix multiplication as the scaling and adding of vectors before, the X, Y, and Z components of the point to be rotated scaled the X, Y, or Z Axis Vectors respectively, but what scales the Translation Vector? If you're working with matrices that are larger in one or both dimensions than your vectors, you usually fill in the extra vector components with 1s. So a 3-component X, Y, Z vector gets an extra imaginary Translation Component added to the end which is always a 1, and that Translation Component scales the Translation Vector of the matrix. Scaling by 1 is easy, since it means no change at all. Here's the code to transform a point through a 3x4 matrix:
NewX = X * Matrix[0][0] + Y * Matrix[1][0] + Z * Matrix[2][0] + Matrix[3][0];
NewY = X * Matrix[0][1] + Y * Matrix[1][1] + Z * Matrix[2][1] + Matrix[3][1];
NewZ = X * Matrix[0][2] + Y * Matrix[1][2] + Z * Matrix[2][2] + Matrix[3][2];

Multiplying forwards through a matrix is great, but what if you want to multiply 'backwards', to take a point that has been transformed through a matrix, and bring it back into the Identity reference frame where it started from? In the general case, this requires calculating the Inverse of the matrix, which is a lot of work for general matrices. However in the case of standard rotation matrices (said to represent an Orthonormal Basis) where the three Axis Vectors are at perfect right angles to each other, the Transpose of the matrix happens to also be the Inverse, and the Transpose is created merely by flipping the matrix about its primary diagonal (which runs from upper left to lower right). Here is a visual representation:
[color=#000080]

C Game Dev 3d Vector Coloring

[font=courier new,courier,monospace][bquote][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [0,[nbsp][nbsp]0, -1][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]T[nbsp][nbsp] [ 0, 1,[nbsp][nbsp]0]
matrix = [1,[nbsp][nbsp]0,[nbsp][nbsp]0][nbsp][nbsp]matrix[nbsp][nbsp]= [ 0, 0, -1]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [0, -1,[nbsp][nbsp]0][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][-1, 0,[nbsp][nbsp]0]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]^[nbsp][nbsp] ^[nbsp][nbsp] ^[nbsp][nbsp]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [nbsp][nbsp] [nbsp][nbsp] -- Z Axis Vector
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [nbsp][nbsp] ------ Y Axis Vector
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] ---------- X Axis Vector
Effective cell order change (in RAM):
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [0, 3, 6][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][0, 1, 2]
[nbsp][nbsp][nbsp][nbsp]normal [1, 4, 7][nbsp][nbsp]transpose [3, 4, 5]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] [2, 5, 8][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][6, 7, 8][/bquote][/font]

[/color]

You can either make a transposed version of your matrix and then multiply by that, or you can do the math to directly multiply through the transpose of the matrix without actually changing it. If you go for the latter, it just so happens that instead of scaling and adding, you do Dot Products. The Dot Product of the vectors (X, Y, Z) and (A, B, C) is [color=#a52a2a]X*A + Y*B + Z*C[/color]. The following code to multiply a point through the Transpose of a matrix simply does 3 Dot Products, Point Dot X Axis Vector, Point Dot Y Axis Vector, and Point Dot Z Axis Vector.
NewX = X * Matrix[0][0] + Y * Matrix[0][1] + Z * Matrix[0][2];
NewY = X * Matrix[1][0] + Y * Matrix[1][1] + Z * Matrix[1][2];
NewZ = X * Matrix[2][0] + Y * Matrix[2][1] + Z * Matrix[2][2];

Going in reverse works similarly for 3x4 matrices with Translation, except that you have to subtract the Translation Vector from the point before rotating it, to properly reverse the order of operations, since when going forwards, the Translation Vector was added to the point after rotation.
TX = X - Matrix[3][0];
TY = Y - Matrix[3][1];
TZ = Z - Matrix[3][2];
NewX = TX * Matrix[0][0] + TY * Matrix[0][1] + TZ * Matrix[0][2];
NewY = TX * Matrix[1][0] + TY * Matrix[1][1] + TZ * Matrix[1][2];
NewZ = TX * Matrix[2][0] + TY * Matrix[2][1] + TZ * Matrix[2][2];

Now things start to get really interesting. Multiplying matrices together, or Concatenating them, allows you to combine the actions of multiple separate matrices into a single matrix, such that when points are multiplied through it, will produce the same result as if you multiplied the points through each of the original matrices in turn. Imagine all the computation that can save you, when you have a lot of points to transform!
I like to say that you multiply one matrix 'through' another matrix, since that's what you're really doing mathematically. The process is astoundingly simple, and all you basically do is take the three Axis Vectors of the first matrix (for 3x3 matrices), and do a normal Vector Times Matrix operation on each of them with the second matrix to produce the three Axis Vectors in the result matrix. The same is true for 3x4 matrices being multiplied through each other, in which case you multiply each of the X, Y, and Z Axis Vectors and the Translation Vector of the first matrix through the second matrix as normal, and you get your resultant 3x4 matrix out the other side. Keep in mind that you can also reverse multiply one matrix through another, by using the Transpose of the second matrix (or the appropriate code to do a transposed multiply directly). For a 3x3 matrix, the code for the normal forward case might look like this:
for(i = 0; i < 3; i++){ //Cycle through each vector of first matrix.
NewMatrix[0] = MatrixA[0] * MatrixB[0][0] +
MatrixA[1] * MatrixB[1][0] + MatrixA[2] * MatrixB[2][0];
NewMatrix[1] = MatrixA[0] * MatrixB[0][1] +
MatrixA[1] * MatrixB[1][1] + MatrixA[2] * MatrixB[2][1];
NewMatrix[2] = MatrixA[0] * MatrixB[0][2] +
MatrixA[1] * MatrixB[1][2] + MatrixA[2] * MatrixB[2][2];
}

If you have two matrices, A and B, where A pitches forwards a bit, and B rotates to the right a bit, and you multiply A through B to produce matrix C, then multiplying a point through matrix C will produce the same result as if the point was first multiplied through matrix A, and the resulting point was multiplied through matrix B. Note: This is the way I prefer to think of my matrix operations right now, but it is actually the reverse of the normal way. When working with matrix operations in APIs such as OpenGL, matrix concatenations actually work in reverse order. In the above example of concatenating A and B into C, multiplying a point by C would actually be the same as first multiplying the point by B, and then multiplying the result by A. You can think of it as each successively concatenated matrix acting in respect to the local coordinate reference frame built up by the previously concatenated matrices, whereas with my method each successive matrix acts in the identity reference frame, which keeps the same order as if you took the time to rotate the point by each original matrix in turn.
This is by no means a complete treatment of matrix math, but hopefully it will help you to understand the basics better. Once you grasp this much, go dig up a few good books on matrices and learn some of their REAL power, while remembering that at their core, the simpler matrices don't have to be thought of as matrices at all.
Copyright 1998-1999 by Seumas McNally.
No reproduction may be made without the author's written consent.
Courtesy Of Longbow Digital Artists

We've talked a fair bit about arrays during this C++ course, but there are some limitations to using the default array functionality. The most obvious limitation that you may have already ran into when creating applications which make use of arrays, is that arrays (at least without some workarounds) have a fixed size. This becomes problematic as some situations require arrays which change size, or at the very least require some sort of storage form or 'container' in which the length is not initially known. For these situations we can use things called vectors.

A vector is a container in the C++ Standard Library (a bunch of stuff which sort of comes 'bundled' with C++) which is essentially just an array that can grow and shrink in size. These things have been highly optimized and tried and tested for several years, and as such are generally considered a standard when creating C++ applications. It's worth remembering that all the containers which we're going to cover in the next few tutorials use the std:: namespace.

To sum up the main advantages and disadvantages of vectors in a sentence: They're array-based, dynamic, and have some very good error-checking built in, however can be slower than arrays. Generally speaking, you should use arrays where possible (when you don't need dynamic sizing or want to optimize completely for speed), however if you need a variable sized container (and want to write minimal code to make it possible), vectors are a great solution. As vectors aren't as primitive as arrays and are part of the standard library, they also have a bunch of nice member functions which make some tasks a lot easier -- we'll talk about these in a minute.

To work with vectors, we have to firstly #include <vector>, and then we can use the vector name; syntax to declare a vector (as usual, any. It's always good practice to know how to break things and what to expect when things do go wrong, and as such you should always be testing all of the rules and boundaries that I present to you.

Most of the further vector functionality simply comes with knowing the other member functions which vector objects have. What is little snitch fr. With a vector declared, elements can easily be added and removed to/from the end of the container by using the push_back and pop_back member functions respectively, take for example the following:

C Game Dev 3d Vector Color Background

The last element in the vector can be handily grabbed using the .back member function, and the first element with the .front member function, vectors can be resized using the resize member function, the size of a vector can be retrieved using the size member function.. the list goes on. If you want to know more of the member functions that can be used on vectors, I suggest the relevant cplusplus.com reference page.

Elements can even be inserted into the vector at any point in the vector by using the insert member function with two parameters. Note, however, that the insert member function is pretty damn inefficient memory-wise, and if you're going to be regularly inserting data into the vector, you may want to look for another container. Vectors are good at defining and accessing elements through the whole container, but they're especially good at working with the elements at the end of the vector (pushing, popping, and so forth). This is why a good knowledge of the different containers is useful, as some containers are more fit for certain jobs than others (and hence, in future tutorials we will be covering some different containers).

If you want a bit of an insight into how vectors work (since they use arrays in the back-end), you can see that they actually just change array size at certain thresholds. The current size of the actual array behind the scenes (not the size of the vector) can be seen by using the capacity member function - and as such you can see the points at which the vector switches to a bigger array by using a simple loop:

The above loop shows the array size (vector 'capacity') expanding as the array is filled. This should explain the basic system behind vectors, and should hopefully make comparison to other containers, which we'll learn about in the future, much easier. /massive-vst-free-download-crack-windows.html. Iterating through arrays using 'for' loops like this (as we have previously - using an iterator variable), however, isn't generally the best method of iteration. When we #include <vector>, we also get these great little things called vector iterators bundled along.

Iterators are essentially slightly less intelligent pointers, and the idea is that you can set the iterator to point to the first element of your vector (or other component) and then use pointer arithmetic to iterate through the vector, having the iterator point to the current object at each iteration. Vector iterators are defined similarly to their normal vector counterparts, however with ::iterator stuck on the end -- for example:

From here functionality is much as previously described, with the .begin() and .end() vector methods returning references to the first and last elements respectively:

C Game Dev 3d Vector Colors

Using these standard iterators has a whole bunch of advantages (convenience, safety, and so on), but one of the big immediately notable advantages in the case of vectors is that it allows you to use some of the vector member functions that require an iterator parameter. .erase is a useful example of this which allows for the removal of an element in the vector via an iterator parameter of the element you want to remove.