src/render/SDL_d3dmath.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Mar 2014 17:19:19 -0700
changeset 8599 532cd234e923
child 8764 3d5ef7548688
permissions -rw-r--r--
Fixed D3D9 initialization on Windows 8, which doesn't have D3DX
slouken@8599
     1
/*
slouken@8599
     2
  Simple DirectMedia Layer
slouken@8599
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@8599
     4
slouken@8599
     5
  This software is provided 'as-is', without any express or implied
slouken@8599
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@8599
     7
  arising from the use of this software.
slouken@8599
     8
slouken@8599
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@8599
    10
  including commercial applications, and to alter it and redistribute it
slouken@8599
    11
  freely, subject to the following restrictions:
slouken@8599
    12
slouken@8599
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@8599
    14
     claim that you wrote the original software. If you use this software
slouken@8599
    15
     in a product, an acknowledgment in the product documentation would be
slouken@8599
    16
     appreciated but is not required.
slouken@8599
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@8599
    18
     misrepresented as being the original software.
slouken@8599
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@8599
    20
*/
slouken@8599
    21
#include "../SDL_internal.h"
slouken@8599
    22
#include "SDL_stdinc.h"
slouken@8599
    23
slouken@8599
    24
#include "SDL_d3dmath.h"
slouken@8599
    25
slouken@8599
    26
/* Direct3D matrix math functions */
slouken@8599
    27
slouken@8599
    28
Float4X4 MatrixIdentity()
slouken@8599
    29
{
slouken@8599
    30
    Float4X4 m;
slouken@8599
    31
    SDL_zero(m);
slouken@8599
    32
    m._11 = 1.0f;
slouken@8599
    33
    m._22 = 1.0f;
slouken@8599
    34
    m._33 = 1.0f;
slouken@8599
    35
    m._44 = 1.0f;
slouken@8599
    36
    return m;
slouken@8599
    37
}
slouken@8599
    38
slouken@8599
    39
Float4X4 MatrixMultiply(Float4X4 M1, Float4X4 M2)
slouken@8599
    40
{
slouken@8599
    41
    Float4X4 m;
slouken@8599
    42
    m._11 = M1._11 * M2._11 + M1._12 * M2._21 + M1._13 * M2._31 + M1._14 * M2._41;
slouken@8599
    43
    m._12 = M1._11 * M2._12 + M1._12 * M2._22 + M1._13 * M2._32 + M1._14 * M2._42;
slouken@8599
    44
    m._13 = M1._11 * M2._13 + M1._12 * M2._23 + M1._13 * M2._33 + M1._14 * M2._43;
slouken@8599
    45
    m._14 = M1._11 * M2._14 + M1._12 * M2._24 + M1._13 * M2._34 + M1._14 * M2._44;
slouken@8599
    46
    m._21 = M1._21 * M2._11 + M1._22 * M2._21 + M1._23 * M2._31 + M1._24 * M2._41;
slouken@8599
    47
    m._22 = M1._21 * M2._12 + M1._22 * M2._22 + M1._23 * M2._32 + M1._24 * M2._42;
slouken@8599
    48
    m._23 = M1._21 * M2._13 + M1._22 * M2._23 + M1._23 * M2._33 + M1._24 * M2._43;
slouken@8599
    49
    m._24 = M1._21 * M2._14 + M1._22 * M2._24 + M1._23 * M2._34 + M1._24 * M2._44;
slouken@8599
    50
    m._31 = M1._31 * M2._11 + M1._32 * M2._21 + M1._33 * M2._31 + M1._34 * M2._41;
slouken@8599
    51
    m._32 = M1._31 * M2._12 + M1._32 * M2._22 + M1._33 * M2._32 + M1._34 * M2._42;
slouken@8599
    52
    m._33 = M1._31 * M2._13 + M1._32 * M2._23 + M1._33 * M2._33 + M1._34 * M2._43;
slouken@8599
    53
    m._34 = M1._31 * M2._14 + M1._32 * M2._24 + M1._33 * M2._34 + M1._34 * M2._44;
slouken@8599
    54
    m._41 = M1._41 * M2._11 + M1._42 * M2._21 + M1._43 * M2._31 + M1._44 * M2._41;
slouken@8599
    55
    m._42 = M1._41 * M2._12 + M1._42 * M2._22 + M1._43 * M2._32 + M1._44 * M2._42;
slouken@8599
    56
    m._43 = M1._41 * M2._13 + M1._42 * M2._23 + M1._43 * M2._33 + M1._44 * M2._43;
slouken@8599
    57
    m._44 = M1._41 * M2._14 + M1._42 * M2._24 + M1._43 * M2._34 + M1._44 * M2._44;
slouken@8599
    58
    return m;
slouken@8599
    59
}
slouken@8599
    60
slouken@8599
    61
Float4X4 MatrixScaling(float x, float y, float z)
slouken@8599
    62
{
slouken@8599
    63
    Float4X4 m;
slouken@8599
    64
    SDL_zero(m);
slouken@8599
    65
    m._11 = x;
slouken@8599
    66
    m._22 = y;
slouken@8599
    67
    m._33 = z;
slouken@8599
    68
    m._44 = 1.0f;
slouken@8599
    69
    return m;
slouken@8599
    70
}
slouken@8599
    71
slouken@8599
    72
Float4X4 MatrixTranslation(float x, float y, float z)
slouken@8599
    73
{
slouken@8599
    74
    Float4X4 m;
slouken@8599
    75
    SDL_zero(m);
slouken@8599
    76
    m._11 = 1.0f;
slouken@8599
    77
    m._22 = 1.0f;
slouken@8599
    78
    m._33 = 1.0f;
slouken@8599
    79
    m._44 = 1.0f;
slouken@8599
    80
    m._41 = x;
slouken@8599
    81
    m._42 = y;
slouken@8599
    82
    m._43 = z;
slouken@8599
    83
    return m;
slouken@8599
    84
}
slouken@8599
    85
slouken@8599
    86
Float4X4 MatrixRotationX(float r)
slouken@8599
    87
{
slouken@8599
    88
    float sinR = SDL_sinf(r);
slouken@8599
    89
    float cosR = SDL_cosf(r);
slouken@8599
    90
    Float4X4 m;
slouken@8599
    91
    SDL_zero(m);
slouken@8599
    92
    m._11 = 1.0f;
slouken@8599
    93
    m._22 = cosR;
slouken@8599
    94
    m._23 = sinR;
slouken@8599
    95
    m._32 = -sinR;
slouken@8599
    96
    m._33 = cosR;
slouken@8599
    97
    m._44 = 1.0f;
slouken@8599
    98
    return m;
slouken@8599
    99
}
slouken@8599
   100
slouken@8599
   101
Float4X4 MatrixRotationY(float r)
slouken@8599
   102
{
slouken@8599
   103
    float sinR = SDL_sinf(r);
slouken@8599
   104
    float cosR = SDL_cosf(r);
slouken@8599
   105
    Float4X4 m;
slouken@8599
   106
    SDL_zero(m);
slouken@8599
   107
    m._11 = cosR;
slouken@8599
   108
    m._13 = -sinR;
slouken@8599
   109
    m._22 = 1.0f;
slouken@8599
   110
    m._31 = sinR;
slouken@8599
   111
    m._33 = cosR;
slouken@8599
   112
    m._44 = 1.0f;
slouken@8599
   113
    return m;
slouken@8599
   114
}
slouken@8599
   115
slouken@8599
   116
Float4X4 MatrixRotationZ(float r)
slouken@8599
   117
{
slouken@8599
   118
    float sinR = SDL_sinf(r);
slouken@8599
   119
    float cosR = SDL_cosf(r);
slouken@8599
   120
    Float4X4 m;
slouken@8599
   121
    SDL_zero(m);
slouken@8599
   122
    m._11 = cosR;
slouken@8599
   123
    m._12 = sinR;
slouken@8599
   124
    m._21 = -sinR;
slouken@8599
   125
    m._22 = cosR;
slouken@8599
   126
    m._33 = 1.0f;
slouken@8599
   127
    m._44 = 1.0f;
slouken@8599
   128
    return m;
slouken@8599
   129
}
slouken@8599
   130
slouken@8599
   131
/* vi: set ts=4 sw=4 expandtab: */