From patchwork Thu Jul 21 12:36:30 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandros Frantzis X-Patchwork-Id: 2955 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 769A623F4D for ; Thu, 21 Jul 2011 12:42:47 +0000 (UTC) Received: from mail-qy0-f173.google.com (mail-qy0-f173.google.com [209.85.216.173]) by fiordland.canonical.com (Postfix) with ESMTP id 463ABA18287 for ; Thu, 21 Jul 2011 12:42:47 +0000 (UTC) Received: by mail-qy0-f173.google.com with SMTP id 10so3962301qyk.11 for ; Thu, 21 Jul 2011 05:42:47 -0700 (PDT) Received: by 10.229.68.200 with SMTP id w8mr190830qci.114.1311252167045; Thu, 21 Jul 2011 05:42:47 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.229.217.78 with SMTP id hl14cs139512qcb; Thu, 21 Jul 2011 05:42:46 -0700 (PDT) Received: by 10.227.37.97 with SMTP id w33mr156347wbd.106.1311251794519; Thu, 21 Jul 2011 05:36:34 -0700 (PDT) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id o17si2401003wbh.14.2011.07.21.05.36.34; Thu, 21 Jul 2011 05:36:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) client-ip=91.189.90.139; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) smtp.mail=bounces@canonical.com Received: from loganberry.canonical.com ([91.189.90.37]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1QjsUS-0000DA-FN for ; Thu, 21 Jul 2011 12:36:32 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id 5278E2E8953 for ; Thu, 21 Jul 2011 12:36:30 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: glmark2 X-Launchpad-Branch: ~glmark2-dev/glmark2/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 7 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~glmark2-dev/glmark2/trunk] Rev 7: Add matrix class. Message-Id: <20110721123630.17019.58223.launchpad@loganberry.canonical.com> Date: Thu, 21 Jul 2011 12:36:30 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13475"; Instance="initZopeless config overlay" X-Launchpad-Hash: 90e4ffa78e5ab7dad638b5f90cd3b716df363cb5 ------------------------------------------------------------ revno: 7 committer: Alexandros Frantzis timestamp: Thu 2010-07-08 11:07:56 +0300 message: Add matrix class. added: matrix.cpp matrix.h --- lp:glmark2 https://code.launchpad.net/~glmark2-dev/glmark2/trunk You are subscribed to branch lp:glmark2. To unsubscribe from this branch go to https://code.launchpad.net/~glmark2-dev/glmark2/trunk/+edit-subscription === added file 'matrix.cpp' --- matrix.cpp 1970-01-01 00:00:00 +0000 +++ matrix.cpp 2010-07-08 08:07:56 +0000 @@ -0,0 +1,150 @@ +#include "matrix.h" + +/** + * Multiply arrays representing 4x4 matrices in column-major order. + * + * The result is stored in the first matrix. + * + * @param m the first matrix + * @param n the second matrix + */ +static void 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]; + } + memcpy(m, &tmp, sizeof tmp); +} + +/** + * Multiply this matrix with another. + * + * @param pM the matrix to multiply with. + * + * @return reference to this matrix (multiplied) + */ +Matrix4f &Matrix4f::operator*=(const Matrix4f &pM) +{ + multiply(m, pM.m); + + return *this; +} + +/** + * Rotates a matrix. + * + * @param angle the angle to rotate + * @param x the x component of the rotation axis + * @param y the y component of the rotation axis + * @param z the z component of the rotation axis + * + * @return reference to the matrix + */ +Matrix4f &Matrix4f::rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) +{ + double s, c; + + sincos(angle, &s, &c); + + 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 + }; + + multiply(m, r); + + return *this; +} + +/** + * Translates a matrix. + * + * @param x the x component of the translation + * @param y the y component of the translation + * @param z the z component of the translation + * + * @return reference to the matrix + */ +Matrix4f &Matrix4f::translate(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 }; + + multiply(m, t); + + return *this; +} + +/** + * Transposes a matrix. + * + * @return reference to the matrix + */ +Matrix4f &Matrix4f::transpose() +{ + 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]}; + + memcpy(m, t, sizeof(m)); + + return *this; +} + +/** + * Creates an empty matrix. + * + * All matrix components are 0.0 expect the lower right + * which is 1.0. + */ +Matrix4f::Matrix4f() +{ + memset(m, 0, sizeof(m)); + m[15] = 1.0; +} + +/** + * Creates a matrix with specified values in the diagonal. + * + * The lower right value is initialized to 1.0. + * + * @param x the x component of the diagonal + * @param y the y component of the diagonal + * @param z the z component of the diagonal + */ +Matrix4f::Matrix4f(GLfloat x, GLfloat y, GLfloat z) +{ + memset(m, 0, sizeof(m)); + m[0] = x; + m[5] = y; + m[10] = z; + m[15] = 1.0; +} + +/** + * Displays a matrix. + * + * @param str string to display before matrix + */ +void Matrix4f::display(const char *str) +{ + return; + int r; + if (str != NULL) + printf("%s\n", str); + for(r = 0; r < 4; r++) + printf("%f %f %f %f\n", m[r], m[r + 4], m[r + 8], m[r + 12]); +} === added file 'matrix.h' --- matrix.h 1970-01-01 00:00:00 +0000 +++ matrix.h 2010-07-08 08:07:56 +0000 @@ -0,0 +1,27 @@ +#ifndef _MATRIX_H +#define _MATRIX_H + +#include "oglsdl.h" + +#include +#include + +class Matrix4f +{ + +public: + GLfloat m[16]; + + Matrix4f(); + Matrix4f(GLfloat x, GLfloat y, GLfloat z); + + Matrix4f &translate(GLfloat x, GLfloat y, GLfloat z); + Matrix4f &rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z); + Matrix4f &transpose(); + + Matrix4f &operator*=(const Matrix4f &pM); + + void display(const char *str); +}; + +#endif