void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) { matrix_4x4 mp; m4x4_zeros(&mp); int mode = ctr_state.matrix_current; int depth = ctr_state.matrix_depth[mode]; matrix_4x4 *mat = &ctr_state.matrix[mode][depth]; if (!mat) { return; } // Build standard orthogonal projection matrix mp.r[0].x = 2.0f / (right - left); mp.r[0].w = (left + right) / (left - right); mp.r[1].y = 2.0f / (top - bottom); mp.r[1].w = (bottom + top) / (bottom - top); mp.r[2].z = 2.0f / (zNear - zFar); mp.r[2].w = (zFar + zNear) / (zFar - zNear); mp.r[3].w = 1.0f; // Fix depth range to [-1, 0] matrix_4x4 mp2, mp3; m4x4_identity(&mp2); mp2.r[2].z = 0.5; mp2.r[2].w = -0.5; m4x4_multiply(&mp3, &mp2, &mp); // Fix the 3DS screens' orientation by swapping the X and Y axis m4x4_identity(&mp2); mp2.r[0].x = 0.0; mp2.r[0].y = 1.0; mp2.r[1].x = -1.0; // flipped mp2.r[1].y = 0.0; m4x4_multiply(mat, &mp2, &mp3); }
void m4x4_ortho_tilt(matrix_4x4* mtx, float left, float right, float bottom, float top, float near, float far) { matrix_4x4 mp; m4x4_zeros(&mp); // Build standard orthogonal projection matrix mp.r[0].x = 2.0f / (right - left); mp.r[0].w = (left + right) / (left - right); mp.r[1].y = 2.0f / (top - bottom); mp.r[1].w = (bottom + top) / (bottom - top); mp.r[2].z = 2.0f / (near - far); mp.r[2].w = (far + near) / (far - near); mp.r[3].w = 1.0f; // Fix depth range to [-1, 0] matrix_4x4 mp2, mp3; m4x4_identity(&mp2); mp2.r[2].z = 0.5; mp2.r[2].w = -0.5; m4x4_multiply(&mp3, &mp2, &mp); // Fix the 3DS screens' orientation by swapping the X and Y axis m4x4_identity(&mp2); mp2.r[0].x = 0.0; mp2.r[0].y = 1.0; mp2.r[1].x = -1.0; // flipped mp2.r[1].y = 0.0; m4x4_multiply(mtx, &mp2, &mp3); }
void m4x4_rotate(matrix_4x4* mtx, float angle, float x, float y, float z, bool bRightSide) { float axis[3]; float sine = sinf(angle); float cosine = cosf(angle); float one_minus_cosine = 1.0f - cosine; matrix_4x4 rm, om; vector_4f vec = { { 1.0f, z, y, x } }; v4f_norm4(&vec); axis[0] = vec.x; axis[1] = vec.y; axis[2] = vec.z; m4x4_zeros(&rm); rm.r[0].x = cosine + (one_minus_cosine * axis[0] * axis[0]); rm.r[0].y = (one_minus_cosine * axis[0] * axis[1]) - (axis[2] * sine); rm.r[0].z = (one_minus_cosine * axis[0] * axis[2]) + (axis[1] * sine); rm.r[1].x = (one_minus_cosine * axis[0] * axis[1]) + (axis[2] * sine); rm.r[1].y = cosine + (one_minus_cosine * axis[1] * axis[1]); rm.r[1].z = (one_minus_cosine * axis[1] * axis[2]) - (axis[0] * sine); rm.r[2].x = (one_minus_cosine * axis[0] * axis[2]) - (axis[1] * sine); rm.r[2].y = (one_minus_cosine * axis[1] * axis[2]) + (axis[0] * sine); rm.r[2].z = cosine + (one_minus_cosine * axis[2] * axis[2]); rm.r[3].w = 1.0f; if (bRightSide) m4x4_multiply(&om, mtx, &rm); else m4x4_multiply(&om, &rm, mtx); m4x4_copy(mtx, &om); }
void m4x4_rotate_z(matrix_4x4* mtx, float angle, bool bRightSide) { matrix_4x4 rm, om; float cosAngle = cosf(angle); float sinAngle = sinf(angle); m4x4_zeros(&rm); rm.m[0] = cosAngle; rm.m[1] = sinAngle; rm.m[4] = -sinAngle; rm.m[5] = cosAngle; rm.m[10] = 1.0f; rm.m[15] = 1.0f; if (bRightSide) m4x4_multiply(&om, mtx, &rm); else m4x4_multiply(&om, &rm, mtx); m4x4_copy(mtx, &om); }
void m4x4_rotate_z(matrix_4x4* mtx, float angle, bool bRightSide) { matrix_4x4 rm, om; float cosAngle = cosf(angle); float sinAngle = sinf(angle); m4x4_zeros(&rm); rm.r[0].x = cosAngle; rm.r[0].y = sinAngle; rm.r[1].x = -sinAngle; rm.r[1].y = cosAngle; rm.r[2].z = 1.0f; rm.r[3].w = 1.0f; if (bRightSide) m4x4_multiply(&om, mtx, &rm); else m4x4_multiply(&om, &rm, mtx); m4x4_copy(mtx, &om); }
void m4x4_translate(matrix_4x4* mtx, float x, float y, float z) { matrix_4x4 tm, om; m4x4_identity(&tm); tm.m[3] = x; tm.m[7] = y; tm.m[11] = z; m4x4_multiply(&om, mtx, &tm); m4x4_copy(mtx, &om); }
void m4x4_translate(matrix_4x4* mtx, float x, float y, float z) { matrix_4x4 tm, om; m4x4_identity(&tm); tm.r[0].w = x; tm.r[1].w = y; tm.r[2].w = z; m4x4_multiply(&om, mtx, &tm); m4x4_copy(mtx, &om); }
/** * Rotates a 4x4 matrix. * * @param[in,out] m the matrix to rotate * @param angle the angle to rotate in degrees * @param x the x component of the direction to rotate to * @param y the y component of the direction to rotate to * @param z the z component of the direction to rotate to */ static void m4x4_rotate(GLfloat *m, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { float s, c; angle = 2.0f * M_PI * angle / 360.0f; s = sinf(angle); c = cosf(angle); GLfloat r[16] = { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0, x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0, x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0, 0, 0, 0, 1 }; m4x4_multiply(m, r); }
/** * Inverts a 4x4 matrix. * * This function can currently handle only pure translation-rotation matrices. * Read http://www.gamedev.net/community/forums/topic.asp?topic_id=425118 * for an explanation. */ static void m4x4_invert(GLfloat *m) { GLfloat t[16]; m4x4_identity(t); // Extract and invert the translation part 't'. The inverse of a // translation matrix can be calculated by negating the translation // coordinates. t[12] = -m[12]; t[13] = -m[13]; t[14] = -m[14]; // Invert the rotation part 'r'. The inverse of a rotation matrix is // equal to its transpose. m[12] = m[13] = m[14] = 0; m4x4_transpose(m); // inv(m) = inv(r) * inv(t) m4x4_multiply(m, t); }
void m4x4_persp_tilt(matrix_4x4* mtx, float fovx, float invaspect, float near, float far) { // Notes: // We are passed "fovy" and the "aspect ratio". However, the 3DS screens are sideways, // and so are these parameters -- in fact, they are actually the fovx and the inverse // of the aspect ratio. Therefore the formula for the perspective projection matrix // had to be modified to be expressed in these terms instead. // Notes: // fovx = 2 atan(tan(fovy/2)*w/h) // fovy = 2 atan(tan(fovx/2)*h/w) // invaspect = h/w // a0,0 = h / (w*tan(fovy/2)) = // = h / (w*tan(2 atan(tan(fovx/2)*h/w) / 2)) = // = h / (w*tan( atan(tan(fovx/2)*h/w) )) = // = h / (w * tan(fovx/2)*h/w) = // = 1 / tan(fovx/2) // a1,1 = 1 / tan(fovy/2) = (...) = w / (h*tan(fovx/2)) float fovx_tan = tanf(fovx / 2); matrix_4x4 mp; m4x4_zeros(&mp); // Build standard perspective projection matrix mp.r[0].x = 1.0f / fovx_tan; mp.r[1].y = 1.0f / (fovx_tan*invaspect); mp.r[2].z = (near + far) / (near - far); mp.r[2].w = (2 * near * far) / (near - far); mp.r[3].z = -1.0f; // Fix depth range to [-1, 0] matrix_4x4 mp2; m4x4_identity(&mp2); mp2.r[2].z = 0.5; mp2.r[2].w = -0.5; m4x4_multiply(mtx, &mp2, &mp); // Rotate the matrix one quarter of a turn CCW in order to fix the 3DS screens' orientation m4x4_rotate_z(mtx, M_PI / 2, true); }
/** * Draws a gear in GLES 2 mode. * * @param gear the gear to draw * @param transform the current transformation matrix * @param x the x position to draw the gear at * @param y the y position to draw the gear at * @param angle the rotation angle of the gear * @param color the color of the gear */ static void draw_gearGLES2(gear_t *gear, GLfloat *transform, GLfloat x, GLfloat y, GLfloat angle) { // The direction of the directional light for the scene */ static const GLfloat LightSourcePosition[4] = { 5.0, 5.0, 10.0, 1.0}; GLfloat model_view[16]; GLfloat normal_matrix[16]; GLfloat model_view_projection[16]; /* Translate and rotate the gear */ m4x4_copy(model_view, transform); m4x4_translate(model_view, x, y, 0); m4x4_rotate(model_view, angle, 0, 0, 1); /* Create and set the ModelViewProjectionMatrix */ m4x4_copy(model_view_projection, state->ProjectionMatrix); m4x4_multiply(model_view_projection, model_view); glUniformMatrix4fv(state->ModelViewProjectionMatrix_location, 1, GL_FALSE, model_view_projection); glUniformMatrix4fv(state->ModelViewMatrix_location, 1, GL_FALSE, model_view); /* Set the LightSourcePosition uniform in relation to the object */ glUniform4fv(state->LightSourcePosition_location, 1, LightSourcePosition); glUniform1i(state->DiffuseMap_location, 0); /* * Create and set the NormalMatrix. It's the inverse transpose of the * ModelView matrix. */ m4x4_copy(normal_matrix, model_view); m4x4_invert(normal_matrix); m4x4_transpose(normal_matrix); glUniformMatrix4fv(state->NormalMatrix_location, 1, GL_FALSE, normal_matrix); /* Set the gear color */ glUniform4fv(state->MaterialColor_location, 1, gear->color); if (state->useVBO) { glBindBuffer(GL_ARRAY_BUFFER, gear->vboId); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gear->iboId); } /* Set up the position of the attributes in the vertex buffer object */ // setup where vertex data is glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_t), gear->vertex_p); // setup where normal data is glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_t), gear->normal_p); // setup where uv data is glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), gear->texCoords_p); /* Enable the attributes */ glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); // Bind texture surface to current vertices glBindTexture(GL_TEXTURE_2D, state->texId); glDrawElements(state->drawMode, gear->tricount, GL_UNSIGNED_SHORT, gear->index_p); /* Disable the attributes */ glDisableVertexAttribArray(2); glDisableVertexAttribArray(1); glDisableVertexAttribArray(0); }
/** * Translates a 4x4 matrix. * * @param[in,out] m the matrix to translate * @param x the x component of the direction to translate to * @param y the y component of the direction to translate to * @param z the z component of the direction to translate to */ static void m4x4_translate(GLfloat *m, GLfloat x, GLfloat y, GLfloat z) { GLfloat t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }; m4x4_multiply(m, t); }