Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GPU_matrix.h « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e073263f352b8b10f1e6837396ea22a1ebd9f6c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2012 Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup gpu
 */

#pragma once

#include "BLI_sys_types.h"

#ifdef __cplusplus
extern "C" {
#endif

struct GPUShader;

void GPU_matrix_reset(void); /* to Identity transform & empty stack */

/* ModelView Matrix (2D or 3D) */

void GPU_matrix_push(void); /* TODO: PushCopy vs PushIdentity? */
void GPU_matrix_pop(void);

void GPU_matrix_identity_set(void);

void GPU_matrix_scale_1f(float factor);

/* 3D ModelView Matrix */

void GPU_matrix_set(const float m[4][4]);
void GPU_matrix_mul(const float m[4][4]);

void GPU_matrix_translate_3f(float x, float y, float z);
void GPU_matrix_translate_3fv(const float vec[3]);
void GPU_matrix_scale_3f(float x, float y, float z);
void GPU_matrix_scale_3fv(const float vec[3]);

/* Axis of rotation should be a unit vector. */
void GPU_matrix_rotate_3f(float deg, float x, float y, float z);
/* Axis of rotation should be a unit vector. */
void GPU_matrix_rotate_3fv(float deg, const float axis[3]);

void GPU_matrix_rotate_axis(float deg, char axis); /* TODO: enum for axis? */

void GPU_matrix_look_at(float eyeX,
                        float eyeY,
                        float eyeZ,
                        float centerX,
                        float centerY,
                        float centerZ,
                        float upX,
                        float upY,
                        float upZ);
/* TODO: variant that takes eye[3], center[3], up[3] */

/* 2D ModelView Matrix */

void GPU_matrix_translate_2f(float x, float y);
void GPU_matrix_translate_2fv(const float vec[2]);
void GPU_matrix_scale_2f(float x, float y);
void GPU_matrix_scale_2fv(const float vec[2]);
void GPU_matrix_rotate_2d(float deg);

/* Projection Matrix (2D or 3D) */

void GPU_matrix_push_projection(void);
void GPU_matrix_pop_projection(void);

/* 3D Projection Matrix */

void GPU_matrix_identity_projection_set(void);
void GPU_matrix_projection_set(const float m[4][4]);

void GPU_matrix_ortho_set(float left, float right, float bottom, float top, float near, float far);
void GPU_matrix_ortho_set_z(float near, float far);

void GPU_matrix_frustum_set(
    float left, float right, float bottom, float top, float near, float far);
void GPU_matrix_perspective_set(float fovy, float aspect, float near, float far);

/* 3D Projection between Window and World Space */

struct GPUMatrixUnproject_Precalc {
  float model_inverted[4][4];
  float view[4];
  bool is_persp;
  /**
   * Result of #projmat_dimensions_db.
   * Using double precision here is important as far clipping ranges
   * can cause divide-by-zero when using float, see: T66937.
   */
  struct {
    double xmin, xmax;
    double ymin, ymax;
    double zmin, zmax;
  } dims;
};

bool GPU_matrix_unproject_precalc(struct GPUMatrixUnproject_Precalc *unproj_precalc,
                                  const float model[4][4],
                                  const float proj[4][4],
                                  const int view[4]);

void GPU_matrix_project_3fv(const float world[3],
                            const float model[4][4],
                            const float proj[4][4],
                            const int view[4],
                            float r_win[3]);

void GPU_matrix_project_2fv(const float world[3],
                            const float model[4][4],
                            const float proj[4][4],
                            const int view[4],
                            float r_win[2]);

bool GPU_matrix_unproject_3fv(const float win[3],
                              const float model[4][4],
                              const float proj[4][4],
                              const int view[4],
                              float r_world[3]);

void GPU_matrix_unproject_3fv_with_precalc(const struct GPUMatrixUnproject_Precalc *unproj_precalc,
                                           const float win[3],
                                           float r_world[3]);

/* 2D Projection Matrix */

void GPU_matrix_ortho_2d_set(float left, float right, float bottom, float top);

/* functions to get matrix values */
const float (*GPU_matrix_model_view_get(float m[4][4]))[4];
const float (*GPU_matrix_projection_get(float m[4][4]))[4];
const float (*GPU_matrix_model_view_projection_get(float m[4][4]))[4];

const float (*GPU_matrix_normal_get(float m[3][3]))[3];
const float (*GPU_matrix_normal_inverse_get(float m[3][3]))[3];

/* set uniform values for currently bound shader */
void GPU_matrix_bind(struct GPUShader *shader);
bool GPU_matrix_dirty_get(void); /* since last bind */

/* own working polygon offset */
float GPU_polygon_offset_calc(const float (*winmat)[4], float viewdist, float dist);
void GPU_polygon_offset(float viewdist, float dist);

/* Python API needs to be able to inspect the stack so errors raise exceptions
 * instead of crashing. */
#ifdef USE_GPU_PY_MATRIX_API
int GPU_matrix_stack_level_get_model_view(void);
int GPU_matrix_stack_level_get_projection(void);
/* static assert ensures this doesn't change! */
#  define GPU_PY_MATRIX_STACK_LEN 31
#endif /* USE_GPU_PY_MATRIX_API */

#ifdef __cplusplus
}
#endif

#ifndef SUPPRESS_GENERIC_MATRIX_API

#  if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
#    define _GPU_MAT3_CONST_CAST(x) \
      (_Generic((x), \
  void *:       (const float (*)[3])(x), \
  float *:      (const float (*)[3])(x), \
  float [9]:    (const float (*)[3])(x), \
  float (*)[4]: (const float (*)[3])(x), \
  float [4][4]: (const float (*)[3])(x), \
  const void *:       (const float (*)[3])(x), \
  const float *:      (const float (*)[3])(x), \
  const float [9]:    (const float (*)[3])(x), \
  const float (*)[3]: (const float (*)[3])(x), \
  const float [3][3]: (const float (*)[3])(x)) \
)
#    define _GPU_MAT3_CAST(x) \
      (_Generic((x), \
  void *:       (float (*)[3])(x), \
  float *:      (float (*)[3])(x), \
  float [9]:    (float (*)[3])(x), \
  float (*)[3]: (float (*)[3])(x), \
  float [3][3]: (float (*)[3])(x)) \
)
#    define _GPU_MAT4_CONST_CAST(x) \
      (_Generic((x), \
  void *:       (const float (*)[4])(x), \
  float *:      (const float (*)[4])(x), \
  float [16]:   (const float (*)[4])(x), \
  float (*)[4]: (const float (*)[4])(x), \
  float [4][4]: (const float (*)[4])(x), \
  const void *:       (const float (*)[4])(x), \
  const float *:      (const float (*)[4])(x), \
  const float [16]:   (const float (*)[4])(x), \
  const float (*)[4]: (const float (*)[4])(x), \
  const float [4][4]: (const float (*)[4])(x)) \
)
#    define _GPU_MAT4_CAST(x) \
      (_Generic((x), \
  void *:       (float (*)[4])(x), \
  float *:      (float (*)[4])(x), \
  float [16]:   (float (*)[4])(x), \
  float (*)[4]: (float (*)[4])(x), \
  float [4][4]: (float (*)[4])(x)) \
)
#  else
#    define _GPU_MAT3_CONST_CAST(x) (const float(*)[3])(x)
#    define _GPU_MAT3_CAST(x) (float(*)[3])(x)
#    define _GPU_MAT4_CONST_CAST(x) (const float(*)[4])(x)
#    define _GPU_MAT4_CAST(x) (float(*)[4])(x)
#  endif /* C11 */

/* make matrix inputs generic, to avoid warnings */
#  define GPU_matrix_mul(x) GPU_matrix_mul(_GPU_MAT4_CONST_CAST(x))
#  define GPU_matrix_set(x) GPU_matrix_set(_GPU_MAT4_CONST_CAST(x))
#  define GPU_matrix_projection_set(x) GPU_matrix_projection_set(_GPU_MAT4_CONST_CAST(x))
#  define GPU_matrix_model_view_get(x) GPU_matrix_model_view_get(_GPU_MAT4_CAST(x))
#  define GPU_matrix_projection_get(x) GPU_matrix_projection_get(_GPU_MAT4_CAST(x))
#  define GPU_matrix_model_view_projection_get(x) \
    GPU_matrix_model_view_projection_get(_GPU_MAT4_CAST(x))
#  define GPU_matrix_normal_get(x) GPU_matrix_normal_get(_GPU_MAT3_CAST(x))
#  define GPU_matrix_normal_inverse_get(x) GPU_matrix_normal_inverse_get(_GPU_MAT3_CAST(x))
#endif /* SUPPRESS_GENERIC_MATRIX_API */

/* Not part of the GPU_matrix API,
 * however we need to check these limits in code that calls into these API's. */
#define GPU_MATRIX_ORTHO_CLIP_NEAR_DEFAULT (-100)
#define GPU_MATRIX_ORTHO_CLIP_FAR_DEFAULT (100)