void CCMatrixFrustum(CCMatrix &result, float left, float right, float bottom, float top, float nearZ, float farZ) { float deltaX = right - left; float deltaY = top - bottom; float deltaZ = farZ - nearZ; CCMatrix frust; if( (nearZ <= 0.0f) || (farZ <= 0.0f) || (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) ) return; frust.m[0][0] = 2.0f * nearZ / deltaX; frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f; frust.m[1][1] = 2.0f * nearZ / deltaY; frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f; frust.m[2][0] = (right + left) / deltaX; frust.m[2][1] = (top + bottom) / deltaY; frust.m[2][2] = -(nearZ + farZ) / deltaZ; frust.m[2][3] = -1.0f; frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ; frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f; CCMatrixMultiply(result, frust, result); }
void CCSetViewMatrix() { CCMatrix &modelViewMatrix = CCCameraBase::currentCamera->pushedMatrix[CCCameraBase::currentCamera->currentPush]; static CCMatrix modelViewProjectionMatrix; modelViewProjectionMatrix = modelViewMatrix; if( gUseProjectionMatrix ) { CCMatrixMultiply( modelViewProjectionMatrix, modelViewMatrix, CCCameraBase::currentCamera->getProjectionMatrix() ); } const GLint *uniforms = gEngine->renderer->getShader()->uniforms; GLUniformMatrix4fv( uniforms[UNIFORM_MODELVIEWPROJECTIONMATRIX], 1, GL_FALSE, modelViewProjectionMatrix.m ); if( uniforms[UNIFORM_MODELVIEWMATRIX] != -1 ) { GLUniformMatrix4fv( uniforms[UNIFORM_MODELVIEWMATRIX], 1, GL_FALSE, modelViewMatrix.m ); } if( uniforms[UNIFORM_MODELNORMALMATRIX] != -1 ) { static CCMatrix inverseModelViewMatrix; static CCMatrix modelNormalMatrix; CCMatrixInverse( inverseModelViewMatrix, modelViewMatrix ); CCMatrixTranspose( modelNormalMatrix, inverseModelViewMatrix ); GLUniformMatrix4fv( uniforms[UNIFORM_MODELNORMALMATRIX], 1, GL_FALSE, modelNormalMatrix.m ); } }
void CCMatrixRotate(CCMatrix &result, float angle, float x, float y, float z) { angle = -angle; float sinAngle, cosAngle; float mag = sqrtf(x * x + y * y + z * z); sinAngle = sinf( angle * CC_PI / 180.0f ); cosAngle = cosf( angle * CC_PI / 180.0f ); if ( mag > 0.0f ) { float xx, yy, zz, xy, yz, zx, xs, ys, zs; float oneMinusCos; CCMatrix rotMat; x /= mag; y /= mag; z /= mag; xx = x * x; yy = y * y; zz = z * z; xy = x * y; yz = y * z; zx = z * x; xs = x * sinAngle; ys = y * sinAngle; zs = z * sinAngle; oneMinusCos = 1.0f - cosAngle; rotMat.m[0][0] = (oneMinusCos * xx) + cosAngle; rotMat.m[0][1] = (oneMinusCos * xy) - zs; rotMat.m[0][2] = (oneMinusCos * zx) + ys; rotMat.m[0][3] = 0.0f; rotMat.m[1][0] = (oneMinusCos * xy) + zs; rotMat.m[1][1] = (oneMinusCos * yy) + cosAngle; rotMat.m[1][2] = (oneMinusCos * yz) - xs; rotMat.m[1][3] = 0.0f; rotMat.m[2][0] = (oneMinusCos * zx) - ys; rotMat.m[2][1] = (oneMinusCos * yz) + xs; rotMat.m[2][2] = (oneMinusCos * zz) + cosAngle; rotMat.m[2][3] = 0.0f; rotMat.m[3][0] = 0.0f; rotMat.m[3][1] = 0.0f; rotMat.m[3][2] = 0.0f; rotMat.m[3][3] = 1.0f; CCMatrixMultiply( result, rotMat, result ); } }
void CCSetModelViewProjectionMatrix() { const CCMatrix &viewMatrix = CCCameraBase::CurrentCamera->getViewMatrix(); const CCMatrix &modelMatrix = CCCameraBase::CurrentCamera->pushedMatrix[CCCameraBase::CurrentCamera->currentPush]; if( gRenderer->openGL2() ) { const CCMatrix &projectionMatrix = CCCameraBase::CurrentCamera->getProjectionMatrix(); const GLint *uniforms = gRenderer->getShader()->uniforms; gRenderer->GLUniformMatrix4fv( uniforms[UNIFORM_PROJECTIONMATRIX], 1, GL_FALSE, projectionMatrix.m ); gRenderer->GLUniformMatrix4fv( uniforms[UNIFORM_VIEWMATRIX], 1, GL_FALSE, viewMatrix.m ); gRenderer->GLUniformMatrix4fv( uniforms[UNIFORM_MODELMATRIX], 1, GL_FALSE, modelMatrix.m ); if( uniforms[UNIFORM_MODELNORMALMATRIX] != -1 ) { static CCMatrix modelViewMatrix; modelViewMatrix = viewMatrix; CCMatrixMultiply( modelViewMatrix, modelMatrix, modelViewMatrix ); static CCMatrix inverseModelViewMatrix; static CCMatrix modelNormalMatrix; CCMatrixInverse( inverseModelViewMatrix, modelViewMatrix ); CCMatrixTranspose( modelNormalMatrix, inverseModelViewMatrix ); gRenderer->GLUniformMatrix4fv( uniforms[UNIFORM_MODELNORMALMATRIX], 1, GL_FALSE, modelNormalMatrix.m ); } } else { #ifdef IOS static CCMatrix modelViewMatrix; modelViewMatrix = viewMatrix; CCMatrixMultiply( modelViewMatrix, modelMatrix, modelViewMatrix ); glLoadMatrixf( modelViewMatrix.data() ); #endif } }
void CCMatrixOrtho(CCMatrix &result, float left, float right, float bottom, float top, float nearZ, float farZ) { float deltaX = right - left; float deltaY = top - bottom; float deltaZ = farZ - nearZ; CCMatrix ortho; if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) ) return; CCMatrixLoadIdentity( 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; CCMatrixMultiply( result, ortho, result ); }
void GLMultMatrixf(CCMatrix &matrix) { CCMatrixMultiply( CCCameraBase::currentCamera->pushedMatrix[CCCameraBase::currentCamera->currentPush], matrix, CCCameraBase::currentCamera->pushedMatrix[CCCameraBase::currentCamera->currentPush] ); }
void CCCameraBase::GluLookAt(CCMatrix &modelViewMatrix, float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz) { float x[3], y[3], z[3]; float mag; /* Make rotation matrix */ /* Z vector */ z[0] = eyex - centerx; z[1] = eyey - centery; z[2] = eyez - centerz; mag = sqrtf( z[0] * z[0] + z[1] * z[1] + z[2] * z[2] ); if( mag ) { /* mpichler, 19950515 */ z[0] /= mag; z[1] /= mag; z[2] /= mag; } /* Y vector */ y[0] = upx; y[1] = upy; y[2] = upz; /* X vector = Y cross Z */ x[0] = y[1] * z[2] - y[2] * z[1]; x[1] = -y[0] * z[2] + y[2] * z[0]; x[2] = y[0] * z[1] - y[1] * z[0]; /* Recompute Y = Z cross X */ y[0] = z[1] * x[2] - z[2] * x[1]; y[1] = -z[0] * x[2] + z[2] * x[0]; y[2] = z[0] * x[1] - z[1] * x[0]; /* mpichler, 19950515 */ /* cross product gives area of parallelogram, which is < 1.0 for * non-perpendicular unit-length vectors; so normalize x, y here */ mag = sqrtf( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] ); if( mag ) { x[0] /= mag; x[1] /= mag; x[2] /= mag; } mag = sqrtf( y[0] * y[0] + y[1] * y[1] + y[2] * y[2] ); if( mag ) { y[0] /= mag; y[1] /= mag; y[2] /= mag; } CCMatrix m; // TODO: Remove define and feed directly into matrix.m #define M(row, col) m.data()[col*4+row] M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0f; M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0f; M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0f; M(3,0) = 0.0f; M(3,1) = 0.0f; M(3,2) = 0.0f; M(3,3) = 1.0f; #undef M CCMatrixMultiply( modelViewMatrix, m, modelViewMatrix ); /* Translate Eye to Origin */ CCMatrixTranslate( modelViewMatrix, -eyex, -eyey, -eyez ); }
void GLMultMatrixf(CCMatrix &matrix) { CCMatrixMultiply( pushedMatrix[currentPush], matrix, pushedMatrix[currentPush] ); }