113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda/*
213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * Copy_right 2013 The Android Open Source Project
313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda *
413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * Licensed under the Apache License, Version 2.0 (the "License");
513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * y_ou may_ not use this file ex_cept in compliance with the License.
613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * You may_ obtain a copy_ of the License at
713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda *
813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda *      http://www.apache.org/licenses/LICENSE-2.0
913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda *
1013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * Unless required by_ applicable law or agreed to in writing, software
1113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * distributed under the License is distributed on an "AS IS" BASIS,
1213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either ex_press or implied.
1313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * See the License for the specific language governing permissions and
1413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda * limitations under the License.
1513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda */
1613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
1713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
1813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda// vecmath.cpp
1913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
2013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda#include "vecmath.h"
2113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
2213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsudanamespace ndk_helper
2313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
2413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
2513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
2613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda// vec3
2713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
2813754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaVec3::Vec3( const Vec4& vec )
2913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
3013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    x_ = vec.x_;
3113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    y_ = vec.y_;
3213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    z_ = vec.z_;
3313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
3413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
3513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
3613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda// vec4
3713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
3813754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaVec4 Vec4::operator*( const Mat4& rhs ) const
3913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
4013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Vec4 out;
4113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    out.x_ = x_ * rhs.f_[0] + y_ * rhs.f_[1] + z_ * rhs.f_[2] + w_ * rhs.f_[3];
4213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    out.y_ = x_ * rhs.f_[4] + y_ * rhs.f_[5] + z_ * rhs.f_[6] + w_ * rhs.f_[7];
4313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    out.z_ = x_ * rhs.f_[8] + y_ * rhs.f_[9] + z_ * rhs.f_[10] + w_ * rhs.f_[11];
4413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    out.w_ = x_ * rhs.f_[12] + y_ * rhs.f_[13] + z_ * rhs.f_[14] + w_ * rhs.f_[15];
4513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return out;
4613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
4713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
4813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
4913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda// mat4
5013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
5113754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4::Mat4()
5213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
5313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    for( int32_t i = 0; i < 16; ++i )
5413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        f_[i] = 0.f;
5513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
5613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
5713754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4::Mat4( const float* mIn )
5813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
5913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    for( int32_t i = 0; i < 16; ++i )
6013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        f_[i] = mIn[i];
6113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
6213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
6313754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::operator*( const Mat4& rhs ) const
6413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
6513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 ret;
6613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[0] = f_[0] * rhs.f_[0] + f_[4] * rhs.f_[1] + f_[8] * rhs.f_[2]
6713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[12] * rhs.f_[3];
6813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[1] = f_[1] * rhs.f_[0] + f_[5] * rhs.f_[1] + f_[9] * rhs.f_[2]
6913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[13] * rhs.f_[3];
7013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[2] = f_[2] * rhs.f_[0] + f_[6] * rhs.f_[1] + f_[10] * rhs.f_[2]
7113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[14] * rhs.f_[3];
7213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[3] = f_[3] * rhs.f_[0] + f_[7] * rhs.f_[1] + f_[11] * rhs.f_[2]
7313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[15] * rhs.f_[3];
7413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
7513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[4] = f_[0] * rhs.f_[4] + f_[4] * rhs.f_[5] + f_[8] * rhs.f_[6]
7613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[12] * rhs.f_[7];
7713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[5] = f_[1] * rhs.f_[4] + f_[5] * rhs.f_[5] + f_[9] * rhs.f_[6]
7813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[13] * rhs.f_[7];
7913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[6] = f_[2] * rhs.f_[4] + f_[6] * rhs.f_[5] + f_[10] * rhs.f_[6]
8013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[14] * rhs.f_[7];
8113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[7] = f_[3] * rhs.f_[4] + f_[7] * rhs.f_[5] + f_[11] * rhs.f_[6]
8213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[15] * rhs.f_[7];
8313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
8413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[8] = f_[0] * rhs.f_[8] + f_[4] * rhs.f_[9] + f_[8] * rhs.f_[10]
8513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[12] * rhs.f_[11];
8613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[9] = f_[1] * rhs.f_[8] + f_[5] * rhs.f_[9] + f_[9] * rhs.f_[10]
8713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[13] * rhs.f_[11];
8813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[10] = f_[2] * rhs.f_[8] + f_[6] * rhs.f_[9] + f_[10] * rhs.f_[10]
8913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[14] * rhs.f_[11];
9013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[11] = f_[3] * rhs.f_[8] + f_[7] * rhs.f_[9] + f_[11] * rhs.f_[10]
9113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[15] * rhs.f_[11];
9213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
9313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[12] = f_[0] * rhs.f_[12] + f_[4] * rhs.f_[13] + f_[8] * rhs.f_[14]
9413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[12] * rhs.f_[15];
9513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[13] = f_[1] * rhs.f_[12] + f_[5] * rhs.f_[13] + f_[9] * rhs.f_[14]
9613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[13] * rhs.f_[15];
9713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[14] = f_[2] * rhs.f_[12] + f_[6] * rhs.f_[13] + f_[10] * rhs.f_[14]
9813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[14] * rhs.f_[15];
9913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[15] = f_[3] * rhs.f_[12] + f_[7] * rhs.f_[13] + f_[11] * rhs.f_[14]
10013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda            + f_[15] * rhs.f_[15];
10113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
10213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return ret;
10313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
10413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
10513754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaVec4 Mat4::operator*( const Vec4& rhs ) const
10613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
10713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Vec4 ret;
10813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.x_ = rhs.x_ * f_[0] + rhs.y_ * f_[4] + rhs.z_ * f_[8] + rhs.w_ * f_[12];
10913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.y_ = rhs.x_ * f_[1] + rhs.y_ * f_[5] + rhs.z_ * f_[9] + rhs.w_ * f_[13];
11013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.z_ = rhs.x_ * f_[2] + rhs.y_ * f_[6] + rhs.z_ * f_[10] + rhs.w_ * f_[14];
11113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.w_ = rhs.x_ * f_[3] + rhs.y_ * f_[7] + rhs.z_ * f_[11] + rhs.w_ * f_[15];
11213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return ret;
11313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
11413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
11513754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::Inverse()
11613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
11713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 ret;
11813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float det_1;
11913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float pos = 0;
12013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float neg = 0;
12113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float temp;
12213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
12313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    temp = f_[0] * f_[5] * f_[10];
12413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    if( temp >= 0 )
12513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        pos += temp;
12613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    else
12713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        neg += temp;
12813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    temp = f_[4] * f_[9] * f_[2];
12913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    if( temp >= 0 )
13013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        pos += temp;
13113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    else
13213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        neg += temp;
13313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    temp = f_[8] * f_[1] * f_[6];
13413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    if( temp >= 0 )
13513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        pos += temp;
13613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    else
13713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        neg += temp;
13813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    temp = -f_[8] * f_[5] * f_[2];
13913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    if( temp >= 0 )
14013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        pos += temp;
14113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    else
14213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        neg += temp;
14313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    temp = -f_[4] * f_[1] * f_[10];
14413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    if( temp >= 0 )
14513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        pos += temp;
14613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    else
14713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        neg += temp;
14813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    temp = -f_[0] * f_[9] * f_[6];
14913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    if( temp >= 0 )
15013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        pos += temp;
15113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    else
15213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        neg += temp;
15313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    det_1 = pos + neg;
15413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
15513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    if( det_1 == 0.0 )
15613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    {
15713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        //Error
15813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    }
15913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    else
16013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    {
16113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        det_1 = 1.0f / det_1;
16213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[0] = (f_[5] * f_[10] - f_[9] * f_[6]) * det_1;
16313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[1] = -(f_[1] * f_[10] - f_[9] * f_[2]) * det_1;
16413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[2] = (f_[1] * f_[6] - f_[5] * f_[2]) * det_1;
16513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[4] = -(f_[4] * f_[10] - f_[8] * f_[6]) * det_1;
16613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[5] = (f_[0] * f_[10] - f_[8] * f_[2]) * det_1;
16713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[6] = -(f_[0] * f_[6] - f_[4] * f_[2]) * det_1;
16813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[8] = (f_[4] * f_[9] - f_[8] * f_[5]) * det_1;
16913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[9] = -(f_[0] * f_[9] - f_[8] * f_[1]) * det_1;
17013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[10] = (f_[0] * f_[5] - f_[4] * f_[1]) * det_1;
17113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
17213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        /* Calculate -C * inverse(A) */
17313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[12] = -(f_[12] * ret.f_[0] + f_[13] * ret.f_[4] + f_[14] * ret.f_[8]);
17413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[13] = -(f_[12] * ret.f_[1] + f_[13] * ret.f_[5] + f_[14] * ret.f_[9]);
17513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[14] = -(f_[12] * ret.f_[2] + f_[13] * ret.f_[6] + f_[14] * ret.f_[10]);
17613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
17713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[3] = 0.0f;
17813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[7] = 0.0f;
17913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[11] = 0.0f;
18013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda        ret.f_[15] = 1.0f;
18113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    }
18213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
18313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    *this = ret;
18413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return *this;
18513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
18613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
18713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
18813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda// Misc
18913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda//--------------------------------------------------------------------------------
19013754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::RotationX( const float fAngle )
19113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
19213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 ret;
19313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float fCosine, fSine;
19413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
19513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    fCosine = cosf( fAngle );
19613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    fSine = sinf( fAngle );
19713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
19813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[0] = 1.0f;
19913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[4] = 0.0f;
20013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[8] = 0.0f;
20113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[12] = 0.0f;
20213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[1] = 0.0f;
20313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[5] = fCosine;
20413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[9] = fSine;
20513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[13] = 0.0f;
20613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[2] = 0.0f;
20713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[6] = -fSine;
20813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[10] = fCosine;
20913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[14] = 0.0f;
21013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[3] = 0.0f;
21113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[7] = 0.0f;
21213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[11] = 0.0f;
21313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[15] = 1.0f;
21413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return ret;
21513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
21613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
21713754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::RotationY( const float fAngle )
21813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
21913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 ret;
22013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float fCosine, fSine;
22113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
22213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    fCosine = cosf( fAngle );
22313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    fSine = sinf( fAngle );
22413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
22513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[0] = fCosine;
22613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[4] = 0.0f;
22713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[8] = -fSine;
22813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[12] = 0.0f;
22913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[1] = 0.0f;
23013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[5] = 1.0f;
23113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[9] = 0.0f;
23213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[13] = 0.0f;
23313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[2] = fSine;
23413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[6] = 0.0f;
23513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[10] = fCosine;
23613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[14] = 0.0f;
23713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[3] = 0.0f;
23813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[7] = 0.0f;
23913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[11] = 0.0f;
24013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[15] = 1.0f;
24113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return ret;
24213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
24313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
24413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
24513754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::RotationZ( const float fAngle )
24613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
24713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 ret;
24813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float fCosine, fSine;
24913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
25013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    fCosine = cosf( fAngle );
25113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    fSine = sinf( fAngle );
25213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
25313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[0] = fCosine;
25413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[4] = fSine;
25513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[8] = 0.0f;
25613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[12] = 0.0f;
25713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[1] = -fSine;
25813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[5] = fCosine;
25913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[9] = 0.0f;
26013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[13] = 0.0f;
26113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[2] = 0.0f;
26213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[6] = 0.0f;
26313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[10] = 1.0f;
26413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[14] = 0.0f;
26513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[3] = 0.0f;
26613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[7] = 0.0f;
26713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[11] = 0.0f;
26813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[15] = 1.0f;
26913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return ret;
27013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
27113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
27213754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::Translation( const float fX, const float fY, const float fZ )
27313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
27413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 ret;
27513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[0] = 1.0f;
27613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[4] = 0.0f;
27713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[8] = 0.0f;
27813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[12] = fX;
27913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[1] = 0.0f;
28013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[5] = 1.0f;
28113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[9] = 0.0f;
28213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[13] = fY;
28313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[2] = 0.0f;
28413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[6] = 0.0f;
28513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[10] = 1.0f;
28613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[14] = fZ;
28713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[3] = 0.0f;
28813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[7] = 0.0f;
28913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[11] = 0.0f;
29013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[15] = 1.0f;
29113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return ret;
29213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
29313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
29413754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::Translation( const Vec3 vec )
29513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
29613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 ret;
29713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[0] = 1.0f;
29813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[4] = 0.0f;
29913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[8] = 0.0f;
30013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[12] = vec.x_;
30113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[1] = 0.0f;
30213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[5] = 1.0f;
30313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[9] = 0.0f;
30413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[13] = vec.y_;
30513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[2] = 0.0f;
30613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[6] = 0.0f;
30713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[10] = 1.0f;
30813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[14] = vec.z_;
30913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[3] = 0.0f;
31013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[7] = 0.0f;
31113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[11] = 0.0f;
31213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    ret.f_[15] = 1.0f;
31313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return ret;
31413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
31513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
31613754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::Perspective( float width, float height, float nearPlane, float farPlane )
31713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
31813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float n2 = 2.0f * nearPlane;
31913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    float rcpnmf = 1.f / (nearPlane - farPlane);
32013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
32113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 result;
32213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[0] = n2 / width;
32313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[4] = 0;
32413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[8] = 0;
32513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[12] = 0;
32613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[1] = 0;
32713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[5] = n2 / height;
32813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[9] = 0;
32913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[13] = 0;
33013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[2] = 0;
33113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[6] = 0;
33213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[10] = (farPlane + nearPlane) * rcpnmf;
33313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[14] = farPlane * rcpnmf * n2;
33413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[3] = 0;
33513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[7] = 0;
33613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[11] = -1.0;
33713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[15] = 0;
33813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
33913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return result;
34013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
34113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
34213754d53c3305ba55930408ab991c1fad9b492d9Hak MatsudaMat4 Mat4::LookAt( const Vec3& vec_eye, const Vec3& vec_at, const Vec3& vec_up )
34313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda{
34413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Vec3 vec_forward, vec_up_norm, vec_side;
34513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    Mat4 result;
34613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
34713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    vec_forward.x_ = vec_eye.x_ - vec_at.x_;
34813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    vec_forward.y_ = vec_eye.y_ - vec_at.y_;
34913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    vec_forward.z_ = vec_eye.z_ - vec_at.z_;
35013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
35113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    vec_forward.Normalize();
35213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    vec_up_norm = vec_up;
35313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    vec_up_norm.Normalize();
35413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    vec_side = vec_up_norm.Cross( vec_forward );
35513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    vec_up_norm = vec_forward.Cross( vec_side );
35613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
35713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[0] = vec_side.x_;
35813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[4] = vec_side.y_;
35913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[8] = vec_side.z_;
36013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[12] = 0;
36113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[1] = vec_up_norm.x_;
36213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[5] = vec_up_norm.y_;
36313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[9] = vec_up_norm.z_;
36413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[13] = 0;
36513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[2] = vec_forward.x_;
36613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[6] = vec_forward.y_;
36713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[10] = vec_forward.z_;
36813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[14] = 0;
36913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[3] = 0;
37013754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[7] = 0;
37113754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[11] = 0;
37213754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.f_[15] = 1.0;
37313754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
37413754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    result.PostTranslate( -vec_eye.x_, -vec_eye.y_, -vec_eye.z_ );
37513754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda    return result;
37613754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda}
37713754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
37813754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda} //namespace ndkHelper
37913754d53c3305ba55930408ab991c1fad9b492d9Hak Matsuda
380