1 /* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License 4 * as published by the Free Software Foundation; either version 2 5 * of the License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software Foundation, 14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 15 * 16 * The Original Code is Copyright (C) 2012 Blender Foundation. 17 * All rights reserved. 18 */ 19 20 /** \file 21 * \ingroup gpu 22 */ 23 24 #pragma once 25 26 #include "BLI_sys_types.h" 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif 31 32 struct GPUShader; 33 34 void GPU_matrix_reset(void); /* to Identity transform & empty stack */ 35 36 /* ModelView Matrix (2D or 3D) */ 37 38 void GPU_matrix_push(void); /* TODO: PushCopy vs PushIdentity? */ 39 void GPU_matrix_pop(void); 40 41 void GPU_matrix_identity_set(void); 42 43 void GPU_matrix_scale_1f(float factor); 44 45 /* 3D ModelView Matrix */ 46 47 void GPU_matrix_set(const float m[4][4]); 48 void GPU_matrix_mul(const float m[4][4]); 49 50 void GPU_matrix_translate_3f(float x, float y, float z); 51 void GPU_matrix_translate_3fv(const float vec[3]); 52 void GPU_matrix_scale_3f(float x, float y, float z); 53 void GPU_matrix_scale_3fv(const float vec[3]); 54 55 /* Axis of rotation should be a unit vector. */ 56 void GPU_matrix_rotate_3f(float deg, float x, float y, float z); 57 /* Axis of rotation should be a unit vector. */ 58 void GPU_matrix_rotate_3fv(float deg, const float axis[3]); 59 60 void GPU_matrix_rotate_axis(float deg, char axis); /* TODO: enum for axis? */ 61 62 void GPU_matrix_look_at(float eyeX, 63 float eyeY, 64 float eyeZ, 65 float centerX, 66 float centerY, 67 float centerZ, 68 float upX, 69 float upY, 70 float upZ); 71 /* TODO: variant that takes eye[3], center[3], up[3] */ 72 73 /* 2D ModelView Matrix */ 74 75 void GPU_matrix_translate_2f(float x, float y); 76 void GPU_matrix_translate_2fv(const float vec[2]); 77 void GPU_matrix_scale_2f(float x, float y); 78 void GPU_matrix_scale_2fv(const float vec[2]); 79 void GPU_matrix_rotate_2d(float deg); 80 81 /* Projection Matrix (2D or 3D) */ 82 83 void GPU_matrix_push_projection(void); 84 void GPU_matrix_pop_projection(void); 85 86 /* 3D Projection Matrix */ 87 88 void GPU_matrix_identity_projection_set(void); 89 void GPU_matrix_projection_set(const float m[4][4]); 90 91 void GPU_matrix_ortho_set(float left, float right, float bottom, float top, float near, float far); 92 void GPU_matrix_ortho_set_z(float near, float far); 93 94 void GPU_matrix_frustum_set( 95 float left, float right, float bottom, float top, float near, float far); 96 void GPU_matrix_perspective_set(float fovy, float aspect, float near, float far); 97 98 /* 3D Projection between Window and World Space */ 99 100 struct GPUMatrixUnproject_Precalc { 101 float model_inverted[4][4]; 102 float view[4]; 103 bool is_persp; 104 /** 105 * Result of #projmat_dimensions_db. 106 * Using double precision here is important as far clipping ranges 107 * can cause divide-by-zero when using float, see: T66937. 108 */ 109 struct { 110 double xmin, xmax; 111 double ymin, ymax; 112 double zmin, zmax; 113 } dims; 114 }; 115 116 bool GPU_matrix_unproject_precalc(struct GPUMatrixUnproject_Precalc *unproj_precalc, 117 const float model[4][4], 118 const float proj[4][4], 119 const int view[4]); 120 121 void GPU_matrix_project(const float world[3], 122 const float model[4][4], 123 const float proj[4][4], 124 const int view[4], 125 float r_win[3]); 126 127 bool GPU_matrix_unproject(const float win[3], 128 const float model[4][4], 129 const float proj[4][4], 130 const int view[4], 131 float r_world[3]); 132 133 void GPU_matrix_unproject_with_precalc(const struct GPUMatrixUnproject_Precalc *unproj_precalc, 134 const float win[3], 135 float r_world[3]); 136 137 /* 2D Projection Matrix */ 138 139 void GPU_matrix_ortho_2d_set(float left, float right, float bottom, float top); 140 141 /* functions to get matrix values */ 142 const float (*GPU_matrix_model_view_get(float m[4][4]))[4]; 143 const float (*GPU_matrix_projection_get(float m[4][4]))[4]; 144 const float (*GPU_matrix_model_view_projection_get(float m[4][4]))[4]; 145 146 const float (*GPU_matrix_normal_get(float m[3][3]))[3]; 147 const float (*GPU_matrix_normal_inverse_get(float m[3][3]))[3]; 148 149 /* set uniform values for currently bound shader */ 150 void GPU_matrix_bind(struct GPUShader *shader); 151 bool GPU_matrix_dirty_get(void); /* since last bind */ 152 153 /* own working polygon offset */ 154 float GPU_polygon_offset_calc(const float (*winmat)[4], float viewdist, float dist); 155 void GPU_polygon_offset(float viewdist, float dist); 156 157 /* Python API needs to be able to inspect the stack so errors raise exceptions 158 * instead of crashing. */ 159 #ifdef USE_GPU_PY_MATRIX_API 160 int GPU_matrix_stack_level_get_model_view(void); 161 int GPU_matrix_stack_level_get_projection(void); 162 /* static assert ensures this doesn't change! */ 163 # define GPU_PY_MATRIX_STACK_LEN 31 164 #endif /* USE_GPU_PY_MATRIX_API */ 165 166 #ifdef __cplusplus 167 } 168 #endif 169 170 #ifndef SUPPRESS_GENERIC_MATRIX_API 171 172 # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) 173 # define _GPU_MAT3_CONST_CAST(x) \ 174 (_Generic((x), \ 175 void *: (const float (*)[3])(x), \ 176 float *: (const float (*)[3])(x), \ 177 float [9]: (const float (*)[3])(x), \ 178 float (*)[4]: (const float (*)[3])(x), \ 179 float [4][4]: (const float (*)[3])(x), \ 180 const void *: (const float (*)[3])(x), \ 181 const float *: (const float (*)[3])(x), \ 182 const float [9]: (const float (*)[3])(x), \ 183 const float (*)[3]: (const float (*)[3])(x), \ 184 const float [3][3]: (const float (*)[3])(x)) \ 185 ) 186 # define _GPU_MAT3_CAST(x) \ 187 (_Generic((x), \ 188 void *: (float (*)[3])(x), \ 189 float *: (float (*)[3])(x), \ 190 float [9]: (float (*)[3])(x), \ 191 float (*)[3]: (float (*)[3])(x), \ 192 float [3][3]: (float (*)[3])(x)) \ 193 ) 194 # define _GPU_MAT4_CONST_CAST(x) \ 195 (_Generic((x), \ 196 void *: (const float (*)[4])(x), \ 197 float *: (const float (*)[4])(x), \ 198 float [16]: (const float (*)[4])(x), \ 199 float (*)[4]: (const float (*)[4])(x), \ 200 float [4][4]: (const float (*)[4])(x), \ 201 const void *: (const float (*)[4])(x), \ 202 const float *: (const float (*)[4])(x), \ 203 const float [16]: (const float (*)[4])(x), \ 204 const float (*)[4]: (const float (*)[4])(x), \ 205 const float [4][4]: (const float (*)[4])(x)) \ 206 ) 207 # define _GPU_MAT4_CAST(x) \ 208 (_Generic((x), \ 209 void *: (float (*)[4])(x), \ 210 float *: (float (*)[4])(x), \ 211 float [16]: (float (*)[4])(x), \ 212 float (*)[4]: (float (*)[4])(x), \ 213 float [4][4]: (float (*)[4])(x)) \ 214 ) 215 # else 216 # define _GPU_MAT3_CONST_CAST(x) (const float(*)[3])(x) 217 # define _GPU_MAT3_CAST(x) (float(*)[3])(x) 218 # define _GPU_MAT4_CONST_CAST(x) (const float(*)[4])(x) 219 # define _GPU_MAT4_CAST(x) (float(*)[4])(x) 220 # endif /* C11 */ 221 222 /* make matrix inputs generic, to avoid warnings */ 223 # define GPU_matrix_mul(x) GPU_matrix_mul(_GPU_MAT4_CONST_CAST(x)) 224 # define GPU_matrix_set(x) GPU_matrix_set(_GPU_MAT4_CONST_CAST(x)) 225 # define GPU_matrix_projection_set(x) GPU_matrix_projection_set(_GPU_MAT4_CONST_CAST(x)) 226 # define GPU_matrix_model_view_get(x) GPU_matrix_model_view_get(_GPU_MAT4_CAST(x)) 227 # define GPU_matrix_projection_get(x) GPU_matrix_projection_get(_GPU_MAT4_CAST(x)) 228 # define GPU_matrix_model_view_projection_get(x) \ 229 GPU_matrix_model_view_projection_get(_GPU_MAT4_CAST(x)) 230 # define GPU_matrix_normal_get(x) GPU_matrix_normal_get(_GPU_MAT3_CAST(x)) 231 # define GPU_matrix_normal_inverse_get(x) GPU_matrix_normal_inverse_get(_GPU_MAT3_CAST(x)) 232 #endif /* SUPPRESS_GENERIC_MATRIX_API */ 233 234 /* Not part of the GPU_matrix API, 235 * however we need to check these limits in code that calls into these API's. */ 236 #define GPU_MATRIX_ORTHO_CLIP_NEAR_DEFAULT (-100) 237 #define GPU_MATRIX_ORTHO_CLIP_FAR_DEFAULT (100) 238