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); }
/** * Calculate a perspective projection transformation. * * @param m the matrix to save the transformation in * @param fovy the field of view in the y direction * @param aspect the view aspect ratio * @param zNear the near clipping plane * @param zFar the far clipping plane */ void m4x4_perspective(GLfloat *m, GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) { GLfloat tmp[16]; m4x4_identity(tmp); float sine, cosine, cotangent, deltaZ; GLfloat radians = fovy / 2.0 * M_PI / 180.0; deltaZ = zFar - zNear; sine = sinf(radians); cosine = cosf(radians); if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) return; cotangent = cosine / sine; tmp[0] = cotangent / aspect; tmp[5] = cotangent; tmp[10] = -(zFar + zNear) / deltaZ; tmp[11] = -1; tmp[14] = -2 * zNear * zFar / deltaZ; tmp[15] = 0; m4x4_copy(m, tmp); }
void glPushMatrix() { int mode = ctr_state.matrix_current; int depth = ctr_state.matrix_depth[mode]; matrix_4x4 *mat = &ctr_state.matrix[mode][depth]; if (mat == 0 || depth < 0 || depth >= 30) { return; } m4x4_copy(mat + 1, mat); ctr_state.matrix_depth[mode]++; }
/** * Transposes a 4x4 matrix. * * @param m the matrix to transpose */ static void m4x4_transpose(GLfloat *m) { const GLfloat t[16] = { m[0], m[4], m[8], m[12], m[1], m[5], m[9], m[13], m[2], m[6], m[10], m[14], m[3], m[7], m[11], m[15]}; m4x4_copy(m, t); }
/** * Creates an identity 4x4 matrix. * * @param m the matrix make an identity matrix */ static void m4x4_identity(GLfloat *m) { static const GLfloat t[16] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, }; m4x4_copy(m, t); }
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); }
/** * Multiplies two 4x4 matrices. * * The result is stored in matrix m. * * @param m the first matrix to multiply * @param n the second matrix to multiply */ static void m4x4_multiply(GLfloat *m, const GLfloat *n) { GLfloat tmp[16]; const GLfloat *row, *column; div_t d; int i, j; for (i = 0; i < 16; i++) { tmp[i] = 0; d = div(i, 4); row = n + d.quot * 4; column = m + d.rem; for (j = 0; j < 4; j++) tmp[i] += row[j] * column[j * 4]; } m4x4_copy(m, tmp); }
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); }
/** * 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); }