Friday, February 13, 2015

Matrix And Quaternion - The Coder's Nightmare

A change of pace, I will explain matrix math to coders (software developers) who struggle with it. Well, I'll try, specifically in the context of three dimensional maths.

The matrix is essentially a rigid set of instructions, a 4x4 one is used for 3d coordinates. The first thing to consider is the order of operation, it always matters when multiplying matrixes, the right hand of the formula is always computed first.

That's the simplest part of the matrix, the second simplest part is the coordinate instructions, the first three rows of the fourth column in OpenGL. This is just the coordinate of the center of the object, that last row basically says "influence anything by this much," in most cases just leave that as 1.0.

The fourth row of the other three columns has odd effects on coordinates that I will not cover here. These are best left at 0 unless you know a lot about matrix maths, eventually I may explain these but don't hold your breath.

The most important instructions are in the first three rows of the first three columns. These instructions are simple but appear very complex when you are jumping into them.

Often the 3x3 matrix is called a rotation matrix, but really it's a scale matrix. The columns each represent one of the three axis in order of x, y, and z.

The amount of influence each initial axis has over the final value of the multiplication is from each row of that column. So column 1, row 1 has determines how much of the original x value is added to the new x value; column 1, row 2 is how much of the original y value is added to the new x value; etc.

Now the part that seems even more complicated than it is. Rotating is essentially shifting the effect one axis has on another's final value, the hard part is how this is calculated, but otherwise it's just another scale value multiplied by the length of that column.

You read that right, the actual scale value is multiplied by the rotation scale values of that column, and the rotation values are always normalized. This means that the length sqrt(x² + y² + z²) is the scale value for that column.

As long as skewing or twisting is not applied as well, don't try to comprehend those until you get the basics or you'll have a bad time. The scaling values for the rotations are enough for the beginner to worry about, so here goes the most dreaded of those rotations, quaternions.

First, a quaternion is like a coordinate with an imaginary dimension added, so it's 4 dimensional. This makes them both scary and annoying for most people, I am finding them easier to work with the more I incorporate them into my code.

A quaternion is an angle, where something points to using the fourth dimension as the imaginary reference. Trying to work them out in your head will just make you wish you'd never tried so I recommend that you look at this c code instead.

void matrix_3d_setRotationFromQuaternion(GLfloat md[], int offd, float qx, float qy, float qz, float qw)
{
GLfloat lenx = matrix_3d_getScaleX(md, offd);
GLfloat leny = matrix_3d_getScaleY(md, offd);
GLfloat lenz = matrix_3d_getScaleZ(md, offd);

md[offd] = (1.0 - (2.0 * (qy * qy)) - (2.0 * (qz * qz))) * lenx;
md[offd + 4] = ((2.0 * qx * qy) - (2.0 * qz * qw)) * leny;
md[offd + 8] = ((2.0 * qx * qz) + (2.0 * qy * qw)) * lenz;

md[offd + 1] = ((2.0 * qx * qy) + (2.0 * qz * qw)) * lenx;
md[offd + 5] = (1.0 - (2.0 * (qx * qx)) - (2.0 * (qz * qz))) * leny;
md[offd + 9] = ((2.0 * qz * qy) - (2.0 * qx * qw)) * lenz;

md[offd + 2] = ((2.0 * qx * qz) - (2.0 * qy * qw)) * lenx;
md[offd + 6] = ((2.0 * qy * qz) + (2.0 * qx * qw)) * leny;
md[offd + 10] = (1.0 - (2.0 * (qx * qx)) - (2.0 * (qy * qy))) * lenz;
};

Because of the nature of the quaternion, it can represent a rotation matrix on it's own, making it very easy to use with matrixes. So now you can see why I prefer them, also they tween and stack well for skeletal animations.

Converting them from axis/angle coordinates is likely your most common method when you incorporate them into any project, because you can picture those in your head easier but then you will want them as quaternions for the actual maths. The main reason to consider using matrixes and quaternions is because they use less to accomplish more.

This Java code should explain the conversion well.

public void axisAngleToQuaternion()
{
float angle = vec[3] * 0.5f;
float sa = (float)Math.sin(angle);
normalize();
vec[0] *= sa;
vec[1] *= sa;
vec[2] *= sa;
vec[3] = (float)Math.cos(angle);
normalizeAsQuaternion();
}

As you can see, the quaternion only uses 180 degrees, or 2*pi radians, half a circle. "vec" is the float array in the Java object, the fourth index has the angle and the w coordinate is stored there when finished, normalizing is done only for the x, y, and z coordinates.

The x, y, and z coordinates indicate a point along one axis, the second angle is in the w, rotating the circle by this. There you go, that's a quaternion simplified.

Another reason to use them is that there is a very simple method for multiplying then with a vertex coordinate that requires only basic math and works well in shaders, in GLSL it looks like this.

vec3 qrot(vec4 q, vec3 v)
{
    return v + 2.0*cross(q.xyz, cross(q.xyz,v) + q.w*v);
}

If you're doing 3d you should know a cross product by now. The above GLSL function will multiply a vector v by a quaternion q, and it produces no lag in shaders even for thousands of vertexes.

I came upon this while I was struggling to understand the quats, it explained them to me the best, just seeing this formula made it all connect in my head. If you don't know GLSL, I suggest you learn about swizzling to understand the above function.

Now the reasons to use these for everything, not just graphics. Matrixes can contain a large number of instructions for any operation, and multiplying them uses far fewer CPU/GPU cycles than if done procedural. I only touched on the very basics of these great mathematical concepts feared by most coders, and focused on one use.

Quaternions and matrixes can help you write applications for scientific studies, video games, even data crawling. I am working on an OpenGL game library for Android, and after that a neural network simulator with 3d interface and visualization.

Matrixes will become invaluable in the field of artificial intelligence, they are actually the computer's language. The biological brain has it's own complex and sloppy language, so we can correct that mistake by making the computer efficient and fast, we can use matrixes.

Tuesday, February 10, 2015

Living in the Future - Chaos Incarnate

So what does it mean when we say we're living in the future?

This question dawned on me when I said the phrase to someone who is less tech capable than I, he had no idea what it means. The phrase was introduced to be through a song titled "Living in the Future" by Akira the Don.

Recently he did a version 2.0 then 2.5 was the one released, those are part of the title if you search YouTube. Listening to those songs in sequence will explain it to the musical artists and enthusiasts, the rest of you will just have to keep reading this.

When I was growing up we had a lot to worry about, medicine was still primitive and technology was both unwieldy and unconnected. We took the bus to school, and lugged massive backpacks full of heavy books, and our sack lunches were in actual sacks.

By the time I graduated from high school computers became manageable, music began sounding all the same, movies lost their luster, and phones became portable … the prices became obscene.

Then the internet happened, twenty years after I was born, twenty years too late for me to truly benefit from. Now kids can miss days in school and get their homework through electronic mail, email is what we call it now.

Most kids today can get away with using a kindle or tablet PC for all their school books, weighing less than 5 lbs. for all their books. Students can safely walk to school now, there are eyes everywhere so less crime and disappearances happen.

Medicine has made us nearly immortal, and in about 26 years it may do just that. We now have access to more movies, music, games, books, art, everything, and no one can say they don't find something they like online.

We now see, hear, and read facts with the lies, online we can see what is really happening anywhere, no more censored and doctored media monopoly. Truth is more clear and everything gets tested, only empirical evidence survives this scrutiny now.

We can not only talk to anyone we want at the touch of a button, we can now see them in real time over the networks. The wool our governments and religions placed over our eyes is now cast aside and we can see everything as it is.

The future today is today, because we have no idea what's next. When I was young we predicted all of this, smart phones and world wide access to information, friends across the globe, conquering of most illnesses, and the increase in exposure giving us more safety in the physical world.

But now we have run out of predictions, we are at a point in which anything goes, tomorrow we may conquer death itself, or land a human on Mars, or find intelligent life on another world. Tomorrow we may destroy ourselves, or perfect bio technology allowing us to enhance ourselves.

We may find a manufacturing method that makes everything so inexpensive that money becomes obsolete, or we may find a new valuable resource that will drive a wedge between the rich and poor causing another revolution.

We may see peace in the Middle East, though unlikely in my lifetime it is still possible. We may see religious people become intelligent and sane, or we will see them destroy our species just to protect their delusions.

No matter what happens from here, we are living in the future, and today is the best time to be alive.

Saturday, February 7, 2015

A True Doomsday Possibility

I can be quoted numerous times saying that our species growth rate is unsustainable, this is supported by simple math so … let's do the math. Prior to modern medicine, before even primitive medicine, before safety offered by social structure, any human was lucky to live past puberty.

Because of this selective pressure we evolved a breeding cycle that rivals rabbits, and sex drives to match. There were only a few million of us during the best years at that time, luckily we were never less than a few hundred thousand or we'd not have made it at all after breeding the neanderthal into our species.

So we were short lived yet quick to reproduce and smart enough to hide from the lions instead of taming them on unarmed. It was the latter trait that gave birth to the first step toward over population.

We formed social structures, allowing us to organize tasks based on abilities, and helping to protect our young so they had a better chance of reproducing before dying, and our population began growing beyond the boundaries of land and sea. Then we invented science, by then we were past sustainable.

Our population has begun to grow exponentially due to modern medicine, a result of science, increasing our lifespans to nearly five times what it was at our beginnings. This same science has also explained why we must stop breeding, hypocritically many people deny that part while enjoying the medicine the same science developed.

The aspect of our modern species that is denied most is our tenacity to deny our own mistakes, even when our very existence is threatened by said mistakes. We invent reasons why it's either not a mistake, or some magical savior who will come and correct it for us like this Jesus myth.

Many of us have taken to living in our own fantasy worlds just because the fantasies that are causing harm to us and the planet are holding onto the minds of our society like a cancer. We become self absorbed in video games and novels to escape the horror caused by the mass delusion of religion.

Very few of us have the stomach, and the patience, to attack these delusions head on, but we are losing the battle against the cancers of religion, war, and ignorance. Now we are at the point of no return, we must slow our breeding even if it means forced sterilization and neutering, or those born of these acts will have no world to live on.

Isms Are Not That Complicated

The "ism" is the most commonly misused, misrepresented, and misunderstood concept than any other part of language. The three most misused ones by theists are theism, materialism, and commercialism.

While other such adjectives are abused nearly as often, I will focus on these because even a few atheists and scientists abuse these three. An "ism" has nothing inherently to do with belief or worship, nor does it inherently have anything to do with obsession.

An ism is really just an adjective made out of a verb or noun, and that's it. Adding the suffix ism to something makes it "about this," in other words, a single descriptive trait. Thus, materialism is NOT "worshipping of material objects," it means "pertaining to the material world" or the physical world as opposed to the philosophical or imaginary world.

When a person is materialistic it means they are concerned with the physical world, such as food, shelter, safety, … on and on. It does not mean they worship luxury objects, theists often misconstrue this to distract people from the fact that they are greedy and often addicted to luxury objects more than anyone else.

Which brings us to the related, though also misused, adjective of commercialism. Commercialism applies to any system in whicb an arbitrary value is placed on something, even barter and trade is commercialism.

Money is just a marker of value, a scale used to measure the comparative values of everything, even your very life. You earn value by working for it, then you spend that value on things you need and want.

Corporatism is rarely abused as a word, but often people will mistakenly use commercialism when they intend to say corporatism. Corporatism is the incorporation of values, allowing a few elite to decide the worth or value of everything instead of society.

Enter capitalism, another commonly misused word. Capitalism is where the values are determined by society by popularity, what people are able and willing to give for something is it's value and nothing else impacts it.

Back to the original three before I stray too far. Everyone who wants to survive is materialistic, because surviving in the physical world requires that one be concerned with the physical world.

Everyone who adheres to a value system of any sort is a commercalist, if you place a value on your work you are a commercialist. If you believe a god exists then you are a theist, if you don't believe a god exists then you are technically an atheist.

Some isms do include belief, but belief is not inherent. So pull your heads out of your asses and learn to communicate, instead of making everything up like you do in church.

Wednesday, February 4, 2015

The Real Illuminati

The computer world is not only real, it is controlled by us geeky atheists. The geeks have inherited the Earth, and this is a good thing.

First, often people will call the physical world the "real" world and refer to the computer world as just "the internet." The computer world is real though, and the physical world today is dependent on the computer world's state, in such profound ways that no human society can exist without it now.

This is evidenced in regions where they attempt to censor or ban access to the computer world, those regions are often blights on our species, chaos and disease run the societies there. Modern society, the very health we all enjoy, money, business, everything depends on the computer world and has depended on it since the 1950s, in spite of some nostalgic lies told by deluded old fools in bars.

Our masks in the physical world are gone online, the honest personality, the real us, is exposed when we are typing in text. Often people will use slurs like "keyboard commando" when someone else shows their true colors, and this is true but it's the reason you can actually see who they are online, but not in the physical world.

In the physical world we wear masks, we are vulnerable and our fear forces most of us to behave how society demands even if contrary to our own personality, even if the behavior is bad. In the computer world we are not forced to behave based on social tenets and thus, we become ourselves.

The second part is why atheists and geeks control the computer world, most geeks and atheists are persecuted or even physically harmed by societies. In the computer world that we created we are safe to share facts, information, and blasphemy, in the safety of the world we created where we are in control of everything.

The drawback is that to avoid being hypocrites we must allow all people the same privilege, at least some think this is a drawback. It is actually a very great thing, for those who are causing the physical world societies to be dangerous to us are showing their true colors online, and it's easy to get them to say the stupid things online that they fear saying in the physical world.

The funny part is that they actually think they are anonymous, but only Anonymous is truly anonymous, and they remain such as a way of keeping control. Anonymous is our police, our military, and our judges for the computer world, so they are welcomed by geeks and atheists, feared by the religious, and hated by dictators, because Anonymous is us, all of us.

Since the first computer systems were installed, no one alive today has lived before that, geeks have had control of everything. From government defenses to records of everything you do and are, every keystroke is filed and recorded, every phone call is tracked and the number stored, every purchase (even with cash) is also on file somewhere.

The point is, the conspiracy nuts are all wrong, there is an Illuminati like organization, we are just not very organized.

Tuesday, February 3, 2015

The War of Religion

If this offends you, then you are proving that your religion is oppressive and vile.

The life of a gnat is short, it has evolved to fit a very specific niche and it does this well. The gnat knows only one thing, find food, and thus the gnat knows nothing more of the world around it. Note: religion here refers only to the theistic religions and their gods.

Does this change anything about how humans live? Not at all, as fascinating as the gnat it, it's limited view of reality does not alter how reality works in any way.

Often people will defend religion with "everyone's entitled to their own beliefs." This is just a lie to protect the snake oil, reality does not change just because you believe you can fly without wings.

What's worse is that denying reality allows one to ignore the horror done by others who deny reality, even some atheists have demonstrated this huge problem. Many are against showing the horrific pictures of those slain in the name of a god, instead they focus only on the victim's good pictures.

This is a defensive action, protecting religion from scrutiny, allowing people to deny reality instead of embracing and improving it. We need these horrible images shown, to spread the truth and fuel the outrage against all religion, but more importantly to show their kids how horrible their parents behave because of these religions.

Indoctrination is difficult to break in older people who are set in their ways, neurology explains why this is. So the best strategy to end this insanity is to show their children the reality before they have the wool pulled over it, and instead of fear display anger and mock the fucking religions.

We must also remind ourselves why we fight against the indoctrination, the delusion, and why any delusion is bad when it's protected. We all need to look at these images and put ourselves in the shoes of the victims.

To burn to death us the most painful way to die, imagine the pain through your whole life suffered tenfold in one moment knowing that this is the last thing you will ever feel. Then cry for the victim and raise your fists toward the religious people in outrage, demand justice, not fear, not excuses, not cowardice.

Demand they are all held accountable for this, moderates of any religion are nothing more than subversive liars, those who deny this fact are enablers and complicate. "Moderates" defend and support the doctrine used by those who commit these atrocities, they are just as responsible.

We must end this insanity, or we will all face the same fate at the hands of those who do not like us.

Disproving Gods

Religion offers nothing positive to the species, and theism is a truly horrific thing. But that aside, let's disprove all gods and be done with it once and for all.

This is often thought of as impossible, but not if we use the modern definition of a god, the ones used by Christians and Muslims. This particular definition makes said god impossible to exist because of one simple fact, it demands infinite regression.

Religion as a whole is nothing more than fanciful stories, rituals, and essentially clubs. Nothing bad is inherent in these, until dogma is added, theism requires dogma.

Deism is often mistaken to theism, but it is not. Deism is theism without dogma, a very agnostic view in all reality, and technically deists are atheists.

The reason is that theism forces the deities to be human and necessary, this makes their gods impossible. For if everything requires a creator, then so does the creator require a creator.

This rather lame argument used by theists was started by the very primitive brain of pre-Stone Age humans. They were unable to comprehend the chemical reactions that occur all the time, much less the atomic reactions and forces involved.

When I was growing up I had studied Chaos Theory, at the time that's what it was called. After some discoveries Chaos Theory vanished and was replaced by a series of laws, mathematical formulas used to predict natural behaviors of reality.

I still call it Chaos Theory in casual discussion, and almost no layperson actually understands it. To my surprise it's a fundamental part of quantum mechanics now.

This simple yet profound change in knowledge illustrates how religion was born. I could start a cult by manipulating the people who know nothing of chaos as easily as one who manipulated those who did not understand the forces of nature.

The difference between me and a preacher is that I have morals and empathy. To manipulate the gullible fools in such a way would only be self destructive, for unless those fools become intelligent, we will die and the more fools there are the more horrible our deaths will be.

Preachers need to consider this point thoroughly, for it is with the tools created by our intelligence that we can have any possibility of avoiding this horrific fate. You need to become more self serving to avoid being self destructive.

The point often forgotten by religious people is that without a god we must protect the planet and our own species in order to survive, with a god you have no reason to worry about being good to anything. With a life after death you have no reason to worry about the life before it, without a life after death you must work to delay the inevitable annihilation.


This requires we stop being so selfish as to believe we are the "chosen" or some special species outside the laws of the wild and nature. We must accept our humble place as animals, dependent on this world, on this universe that so wants to destroy us, for our very existence.

We must be honest enough to admit that there is nothing out there that will save us from ourselves, we must do it all, as we always have, without help from some superior being. We must embrace our own existence to continue existing.