I can't believe I just did this, but I deleted my code! I still have the transformation code:
CODE
#include <iostream>
#include <cmath>
#include <cstring>
#include <GLES2/gl2.h>
#include "matrix.hpp"
void CreateIdentityMatrix(Matrix* target)
{
memset( target, 0x0, sizeof(Matrix) );
for(int i = 0; i < 4; i++)
{
target->m = 1.0f;
}
}
void MultiplyMatrices(Matrix* target, Matrix* Input1, Matrix* Input2)
{
Matrix tmp;
for(int i = 0; i < MATRIX_SIZE; i++)
{
tmp.m[0] = ( (Input1->m[0] * Input2->m[0][0]) +
(Input1->m[1] * Input2->m[1][0]) +
(Input1->m[2] * Input2->m[2][0]) +
(Input1->m[3] * Input2->m[3][0]) );
tmp.m[1] = ( (Input1->m[0] * Input2->m[0][1]) +
(Input1->m[1] * Input2->m[1][1]) +
(Input1->m[2] * Input2->m[2][1]) +
(Input1->m[3] * Input2->m[3][1]) );
tmp.m[2] = ( (Input1->m[0] * Input2->m[0][2]) +
(Input1->m[1] * Input2->m[1][2]) +
(Input1->m[2] * Input2->m[2][2]) +
(Input1->m[3] * Input2->m[3][2]) );
tmp.m[3] = ( (Input1->m[0] * Input2->m[0][3]) +
(Input1->m[1] * Input2->m[1][3]) +
(Input1->m[2] * Input2->m[2][3]) +
(Input1->m[3] * Input2->m[3][3]) );
}
memcpy( target, &tmp, sizeof(Matrix) );
}
bool CreateOrthographicMatrix(Matrix* target, float left, float right, float bottom, float top, float nearZ, float farZ)
{
float deltaX = right - left;
float deltaY = top - bottom;
float deltaZ = farZ - nearZ;
Matrix ortho;
CreateIdentityMatrix(target);
if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) )
{
return false;
}
CreateIdentityMatrix(&ortho);
ortho.m[0][0] = 2.0f / deltaX;
ortho.m[3][0] = -(right + left) / deltaX;
ortho.m[1][1] = 2.0f / deltaY;
ortho.m[3][1] = -(top + bottom) / deltaY;
ortho.m[2][2] = -2.0f / deltaZ;
ortho.m[3][2] = -(nearZ + farZ) / deltaZ;
MultiplyMatrices(target, &ortho, target);
return true;
}
void ScaleMatrix(Matrix* target, GLfloat scaleX, GLfloat scaleY, GLfloat scaleZ)
{
target->m[0][0] *= scaleX;
target->m[0][1] *= scaleX;
target->m[0][2] *= scaleX;
target->m[0][3] *= scaleX;
target->m[1][0] *= scaleY;
target->m[1][1] *= scaleY;
target->m[1][2] *= scaleY;
target->m[1][3] *= scaleY;
target->m[2][0] *= scaleZ;
target->m[2][1] *= scaleZ;
target->m[2][2] *= scaleZ;
target->m[2][3] *= scaleZ;
}
void TranslateMatrixRelative(Matrix* target, GLfloat translateX, GLfloat translateY, GLfloat translateZ)
{
target->m[3][0] += (target->m[0][0] * translateX + target->m[1][0] * translateY + target->m[2][0] * translateZ);
target->m[3][1] += (target->m[0][1] * translateX + target->m[1][1] * translateY + target->m[2][1] * translateZ);
target->m[3][2] += (target->m[0][2] * translateX + target->m[1][2] * translateY + target->m[2][2] * translateZ);
target->m[3][3] += (target->m[0][3] * translateX + target->m[1][3] * translateY + target->m[2][3] * translateZ);
}
void TranslateMatrixAbsolute(Matrix* target, GLfloat translateX, GLfloat translateY, GLfloat translateZ)
{
target->m[3][0] = (target->m[0][0] * translateX + target->m[1][0] * translateY + target->m[2][0] * translateZ);
target->m[3][1] = (target->m[0][1] * translateX + target->m[1][1] * translateY + target->m[2][1] * translateZ);
target->m[3][2] = (target->m[0][2] * translateX + target->m[1][2] * translateY + target->m[2][2] * translateZ);
target->m[3][3] = (target->m[0][3] * translateX + target->m[1][3] * translateY + target->m[2][3] * translateZ);
}
bool RotateMatrix(Matrix* target, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
{
GLfloat magnitude = sqrtf(x * x + y * y + z * z);
if (magnitude <= 0.0f)
{
return false;
}
GLfloat sinAngle = sinf(angle * PI / 180.0f);
GLfloat cosAngle = cosf(angle * PI / 180.0f);
Matrix rotationmatrix;
x /= magnitude;
y /= magnitude;
z /= magnitude;
GLfloat xx = x * x;
GLfloat yy = y * y;
GLfloat zz = z * z;
GLfloat xy = x * y;
GLfloat yz = y * z;
GLfloat zx = z * x;
GLfloat xs = x * sinAngle;
GLfloat ys = y * sinAngle;
GLfloat zs = z * sinAngle;
GLfloat oneMinusCos = 1.0f - cosAngle;
rotationmatrix.m[0][0] = (oneMinusCos * xx) + cosAngle;
rotationmatrix.m[0][1] = (oneMinusCos * xy) - zs;
rotationmatrix.m[0][2] = (oneMinusCos * zx) + ys;
rotationmatrix.m[0][3] = 0.0F;
rotationmatrix.m[1][0] = (oneMinusCos * xy) + zs;
rotationmatrix.m[1][1] = (oneMinusCos * yy) + cosAngle;
rotationmatrix.m[1][2] = (oneMinusCos * yz) - xs;
rotationmatrix.m[1][3] = 0.0F;
rotationmatrix.m[2][0] = (oneMinusCos * zx) - ys;
rotationmatrix.m[2][1] = (oneMinusCos * yz) + xs;
rotationmatrix.m[2][2] = (oneMinusCos * zz) + cosAngle;
rotationmatrix.m[2][3] = 0.0F;
rotationmatrix.m[3][0] = 0.0F;
rotationmatrix.m[3][1] = 0.0F;
rotationmatrix.m[3][2] = 0.0F;
rotationmatrix.m[3][3] = 1.0F;
MultiplyMatrices(target, &rotationmatrix, target);
}
That was mostly copied from the OpenGL ES 2.0 Programming Guide.
The relevant header parts:
CODE
#define MATRIX_SIZE 4
#define PI 3.1415926535897932384626433832795f
typedef struct
{
GLfloat m[MATRIX_SIZE][MATRIX_SIZE];
} Matrix;
The vertex shader was something like this:
CODE
attribute highp vec4 myVertex;
uniform mediump mat4 myPMVMatrix;
uniform mediump mat4 modelMatrix;
void main(void)
{
gl_Position = myPMVMatrix * (myVertex * modelMatrix);
}
The fragment shader was simple:
CODE
void main (void)
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
The code that was being used for rotations (approx):
CODE
Matrix ModelMatrix;
Matrix ProjectionMatrix;
CreateIdentityMatrix(&ModelMatrix);
CreateOrthographicMatrix(&ProjectionMatrix, -320.0f, 320.0f, -240.0f, 240.0f, -240.0f, 240.0f);
(compile shaders, set what is passed to them, etc.)
(vertices for 2 different triangles, one with 0-2, the other with 3-5)
for(int i = 0; i < 200; i++)
{
RotateMatrix(&ModelMatrix, 45.0f, 0.0, 0.0, 1.0);
glDrawArrays(GL_TRIANGLES, 0, 3);
CreateIdentityMatrix(&ModelMatrix);
glDrawArrays(GL_TRIANGLES, 3, 3);
(swap buffers)
}
What I would expect from this is one triangle rotated 45 degrees (so it appears "thinner") and one not so, but neither of them is rotated.