|
| 1 | +/* |
| 2 | + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * This code is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms of the GNU General Public License version 2 only, as |
| 6 | + * published by the Free Software Foundation. Codename One designates this |
| 7 | + * particular file as subject to the "Classpath" exception as provided |
| 8 | + * by Oracle in the LICENSE file that accompanied this code. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Codename One through http://www.codenameone.com/ if you |
| 21 | + * need additional information or have any questions. |
| 22 | + */ |
| 23 | + |
| 24 | +#ifdef CN1_USE_METAL |
| 25 | +#import "CN1METALTransform.h" |
| 26 | +@import simd; |
| 27 | + |
| 28 | +// Global matrix state |
| 29 | +simd_float4x4 CN1_Metal_ProjectionMatrix; |
| 30 | +simd_float4x4 CN1_Metal_ModelViewMatrix; |
| 31 | +simd_float4x4 CN1_Metal_TransformMatrix; |
| 32 | + |
| 33 | +int CN1_Metal_ProjectionMatrixVersion = 0; |
| 34 | +int CN1_Metal_ModelViewMatrixVersion = 0; |
| 35 | +int CN1_Metal_TransformMatrixVersion = 0; |
| 36 | + |
| 37 | +// Matrix stack for nested transforms (max depth 32) |
| 38 | +#define MAX_MATRIX_STACK_DEPTH 32 |
| 39 | +static simd_float4x4 matrixStack[MAX_MATRIX_STACK_DEPTH]; |
| 40 | +static int matrixStackTop = -1; |
| 41 | + |
| 42 | +void CN1_Metal_InitMatrices(int framebufferWidth, int framebufferHeight) { |
| 43 | + // Create orthographic projection for 2D UI rendering |
| 44 | + // Origin at top-left (0,0), Y increases downward (UIKit convention) |
| 45 | + CN1_Metal_ProjectionMatrix = CN1_Metal_MakeOrtho( |
| 46 | + 0, framebufferWidth, |
| 47 | + framebufferHeight, 0, // Flipped Y for UIKit coordinates |
| 48 | + -1, 1 |
| 49 | + ); |
| 50 | + CN1_Metal_ProjectionMatrixVersion++; |
| 51 | + |
| 52 | + // Initialize model-view and transform to identity |
| 53 | + CN1_Metal_ModelViewMatrix = matrix_identity_float4x4; |
| 54 | + CN1_Metal_ModelViewMatrixVersion++; |
| 55 | + |
| 56 | + CN1_Metal_TransformMatrix = matrix_identity_float4x4; |
| 57 | + CN1_Metal_TransformMatrixVersion++; |
| 58 | + |
| 59 | + // Reset matrix stack |
| 60 | + matrixStackTop = -1; |
| 61 | +} |
| 62 | + |
| 63 | +void CN1_Metal_SetProjectionMatrix(simd_float4x4 matrix) { |
| 64 | + CN1_Metal_ProjectionMatrix = matrix; |
| 65 | + CN1_Metal_ProjectionMatrixVersion++; |
| 66 | +} |
| 67 | + |
| 68 | +void CN1_Metal_SetModelViewMatrix(simd_float4x4 matrix) { |
| 69 | + CN1_Metal_ModelViewMatrix = matrix; |
| 70 | + CN1_Metal_ModelViewMatrixVersion++; |
| 71 | +} |
| 72 | + |
| 73 | +void CN1_Metal_SetTransformMatrix(simd_float4x4 matrix) { |
| 74 | + CN1_Metal_TransformMatrix = matrix; |
| 75 | + CN1_Metal_TransformMatrixVersion++; |
| 76 | +} |
| 77 | + |
| 78 | +simd_float4x4 CN1_Metal_GetMVPMatrix(void) { |
| 79 | + // Combine matrices: Projection * ModelView * Transform |
| 80 | + return simd_mul(CN1_Metal_ProjectionMatrix, |
| 81 | + simd_mul(CN1_Metal_ModelViewMatrix, |
| 82 | + CN1_Metal_TransformMatrix)); |
| 83 | +} |
| 84 | + |
| 85 | +void CN1_Metal_Translate(float x, float y, float z) { |
| 86 | + simd_float4x4 translation = CN1_Metal_MakeTranslation(x, y, z); |
| 87 | + CN1_Metal_TransformMatrix = simd_mul(CN1_Metal_TransformMatrix, translation); |
| 88 | + CN1_Metal_TransformMatrixVersion++; |
| 89 | +} |
| 90 | + |
| 91 | +void CN1_Metal_Scale(float x, float y, float z) { |
| 92 | + simd_float4x4 scale = CN1_Metal_MakeScale(x, y, z); |
| 93 | + CN1_Metal_TransformMatrix = simd_mul(CN1_Metal_TransformMatrix, scale); |
| 94 | + CN1_Metal_TransformMatrixVersion++; |
| 95 | +} |
| 96 | + |
| 97 | +void CN1_Metal_Rotate(float angle, float x, float y, float z) { |
| 98 | + simd_float4x4 rotation = CN1_Metal_MakeRotation(angle, x, y, z); |
| 99 | + CN1_Metal_TransformMatrix = simd_mul(CN1_Metal_TransformMatrix, rotation); |
| 100 | + CN1_Metal_TransformMatrixVersion++; |
| 101 | +} |
| 102 | + |
| 103 | +void CN1_Metal_PushMatrix(void) { |
| 104 | + if (matrixStackTop < MAX_MATRIX_STACK_DEPTH - 1) { |
| 105 | + matrixStackTop++; |
| 106 | + matrixStack[matrixStackTop] = CN1_Metal_TransformMatrix; |
| 107 | + } else { |
| 108 | + NSLog(@"CN1METALTransform: Matrix stack overflow!"); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void CN1_Metal_PopMatrix(void) { |
| 113 | + if (matrixStackTop >= 0) { |
| 114 | + CN1_Metal_TransformMatrix = matrixStack[matrixStackTop]; |
| 115 | + matrixStackTop--; |
| 116 | + CN1_Metal_TransformMatrixVersion++; |
| 117 | + } else { |
| 118 | + NSLog(@"CN1METALTransform: Matrix stack underflow!"); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +void CN1_Metal_LoadIdentity(void) { |
| 123 | + CN1_Metal_TransformMatrix = matrix_identity_float4x4; |
| 124 | + CN1_Metal_TransformMatrixVersion++; |
| 125 | +} |
| 126 | + |
| 127 | +// Helper: Create orthographic projection matrix |
| 128 | +simd_float4x4 CN1_Metal_MakeOrtho(float left, float right, float bottom, float top, float near, float far) { |
| 129 | + simd_float4x4 m = matrix_identity_float4x4; |
| 130 | + m.columns[0][0] = 2.0f / (right - left); |
| 131 | + m.columns[1][1] = 2.0f / (top - bottom); |
| 132 | + m.columns[2][2] = -2.0f / (far - near); |
| 133 | + m.columns[3][0] = -(right + left) / (right - left); |
| 134 | + m.columns[3][1] = -(top + bottom) / (top - bottom); |
| 135 | + m.columns[3][2] = -(far + near) / (far - near); |
| 136 | + m.columns[3][3] = 1.0f; |
| 137 | + return m; |
| 138 | +} |
| 139 | + |
| 140 | +// Helper: Create translation matrix |
| 141 | +simd_float4x4 CN1_Metal_MakeTranslation(float x, float y, float z) { |
| 142 | + simd_float4x4 m = matrix_identity_float4x4; |
| 143 | + m.columns[3][0] = x; |
| 144 | + m.columns[3][1] = y; |
| 145 | + m.columns[3][2] = z; |
| 146 | + return m; |
| 147 | +} |
| 148 | + |
| 149 | +// Helper: Create scale matrix |
| 150 | +simd_float4x4 CN1_Metal_MakeScale(float x, float y, float z) { |
| 151 | + simd_float4x4 m = matrix_identity_float4x4; |
| 152 | + m.columns[0][0] = x; |
| 153 | + m.columns[1][1] = y; |
| 154 | + m.columns[2][2] = z; |
| 155 | + return m; |
| 156 | +} |
| 157 | + |
| 158 | +// Helper: Create rotation matrix around arbitrary axis |
| 159 | +simd_float4x4 CN1_Metal_MakeRotation(float angleRadians, float x, float y, float z) { |
| 160 | + // Normalize axis |
| 161 | + float length = sqrtf(x * x + y * y + z * z); |
| 162 | + if (length == 0) { |
| 163 | + return matrix_identity_float4x4; |
| 164 | + } |
| 165 | + x /= length; |
| 166 | + y /= length; |
| 167 | + z /= length; |
| 168 | + |
| 169 | + float c = cosf(angleRadians); |
| 170 | + float s = sinf(angleRadians); |
| 171 | + float t = 1.0f - c; |
| 172 | + |
| 173 | + simd_float4x4 m = matrix_identity_float4x4; |
| 174 | + m.columns[0][0] = t * x * x + c; |
| 175 | + m.columns[0][1] = t * x * y + s * z; |
| 176 | + m.columns[0][2] = t * x * z - s * y; |
| 177 | + |
| 178 | + m.columns[1][0] = t * x * y - s * z; |
| 179 | + m.columns[1][1] = t * y * y + c; |
| 180 | + m.columns[1][2] = t * y * z + s * x; |
| 181 | + |
| 182 | + m.columns[2][0] = t * x * z + s * y; |
| 183 | + m.columns[2][1] = t * y * z - s * x; |
| 184 | + m.columns[2][2] = t * z * z + c; |
| 185 | + |
| 186 | + return m; |
| 187 | +} |
| 188 | + |
| 189 | +#endif // CN1_USE_METAL |
0 commit comments