Blender  V2.93
overlay_edit_text.c
Go to the documentation of this file.
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  * Copyright 2019, Blender Foundation.
17  */
18 
23 #include "DRW_render.h"
24 
25 #include "BKE_font.h"
26 
27 #include "DNA_curve_types.h"
28 
29 #include "overlay_private.h"
30 
32 {
33  OVERLAY_PassList *psl = vedata->psl;
34  OVERLAY_PrivateData *pd = vedata->stl->pd;
35  const DRWContextState *draw_ctx = DRW_context_state_get();
36  View3D *v3d = draw_ctx->v3d;
37  DRWShadingGroup *grp;
38  GPUShader *sh;
40 
44 
45  /* Run Twice for in-front passes. */
46  for (int i = 0; i < 2; i++) {
50 
52  pd->edit_text_wire_grp[i] = grp = DRW_shgroup_create(sh, psl->edit_text_wire_ps[i]);
54  }
55  {
58 
61 
62  DRW_shgroup_uniform_vec4(grp, "color", pd->edit_text.overlay_color, 1);
63 
65  pd->clipping_state;
67  }
68  {
69  /* Create view which will render everything (hopefully) behind the text geometry. */
70  DRWView *default_view = (DRWView *)DRW_view_default_get();
71  pd->view_edit_text = DRW_view_create_with_zoffset(default_view, draw_ctx->rv3d, -5.0f);
72  }
73 }
74 
75 /* Use 2D quad corners to create a matrix that set
76  * a [-1..1] quad at the right position. */
77 static void v2_quad_corners_to_mat4(const float corners[4][2], float r_mat[4][4])
78 {
79  unit_m4(r_mat);
80  sub_v2_v2v2(r_mat[0], corners[1], corners[0]);
81  sub_v2_v2v2(r_mat[1], corners[3], corners[0]);
82  mul_v2_fl(r_mat[0], 0.5f);
83  mul_v2_fl(r_mat[1], 0.5f);
84  copy_v2_v2(r_mat[3], corners[0]);
85  add_v2_v2(r_mat[3], r_mat[0]);
86  add_v2_v2(r_mat[3], r_mat[1]);
87 }
88 
90 {
91  OVERLAY_PrivateData *pd = vedata->stl->pd;
92  const Curve *cu = ob->data;
93  EditFont *ef = cu->editfont;
94  float final_mat[4][4], box[4][2];
95  struct GPUBatch *geom = DRW_cache_quad_get();
96 
97  for (int i = 0; i < ef->selboxes_len; i++) {
98  EditFontSelBox *sb = &ef->selboxes[i];
99 
100  float selboxw;
101  if (i + 1 != ef->selboxes_len) {
102  if (ef->selboxes[i + 1].y == sb->y) {
103  selboxw = ef->selboxes[i + 1].x - sb->x;
104  }
105  else {
106  selboxw = sb->w;
107  }
108  }
109  else {
110  selboxw = sb->w;
111  }
112  /* NOTE: v2_quad_corners_to_mat4 don't need the 3rd corner. */
113  if (sb->rot == 0.0f) {
114  copy_v2_fl2(box[0], sb->x, sb->y);
115  copy_v2_fl2(box[1], sb->x + selboxw, sb->y);
116  copy_v2_fl2(box[3], sb->x, sb->y + sb->h);
117  }
118  else {
119  float mat[2][2];
120  angle_to_mat2(mat, sb->rot);
121  copy_v2_fl2(box[0], sb->x, sb->y);
122  mul_v2_v2fl(box[1], mat[0], selboxw);
123  add_v2_v2(box[1], &sb->x);
124  mul_v2_v2fl(box[3], mat[1], sb->h);
125  add_v2_v2(box[3], &sb->x);
126  }
127  v2_quad_corners_to_mat4(box, final_mat);
128  mul_m4_m4m4(final_mat, ob->obmat, final_mat);
129 
130  DRW_shgroup_call_obmat(pd->edit_text_overlay_grp, geom, final_mat);
131  }
132 }
133 
135 {
136  OVERLAY_PrivateData *pd = vedata->stl->pd;
137  const Curve *cu = ob->data;
138  EditFont *edit_font = cu->editfont;
139  float(*cursor)[2] = edit_font->textcurs;
140  float mat[4][4];
141 
142  v2_quad_corners_to_mat4(cursor, mat);
143  mul_m4_m4m4(mat, ob->obmat, mat);
144 
145  struct GPUBatch *geom = DRW_cache_quad_get();
147 }
148 
150 {
152  const Curve *cu = ob->data;
153 
154  for (int i = 0; i < cu->totbox; i++) {
155  TextBox *tb = &cu->tb[i];
156  const bool is_active = (i == (cu->actbox - 1));
157  float *color = is_active ? G_draw.block.colorActive : G_draw.block.colorWire;
158 
159  if ((tb->w != 0.0f) || (tb->h != 0.0f)) {
160  float vecs[4][3];
161  vecs[0][0] = vecs[1][0] = vecs[2][0] = vecs[3][0] = cu->xof + tb->x;
162  vecs[0][1] = vecs[1][1] = vecs[2][1] = vecs[3][1] = cu->yof + tb->y + cu->fsize_realtime;
163  vecs[0][2] = vecs[1][2] = vecs[2][2] = vecs[3][2] = 0.001;
164 
165  vecs[1][0] += tb->w;
166  vecs[2][0] += tb->w;
167  vecs[2][1] -= tb->h;
168  vecs[3][1] -= tb->h;
169 
170  for (int j = 0; j < 4; j++) {
171  mul_v3_m4v3(vecs[j], ob->obmat, vecs[j]);
172  }
173  for (int j = 0; j < 4; j++) {
174  OVERLAY_extra_line_dashed(cb, vecs[j], vecs[(j + 1) % 4], color);
175  }
176  }
177  }
178 }
179 
181 {
182  OVERLAY_PrivateData *pd = vedata->stl->pd;
183  Curve *cu = ob->data;
184  struct GPUBatch *geom;
185  bool do_in_front = (ob->dtx & OB_DRAW_IN_FRONT) != 0;
186 
187  bool has_surface = (cu->flag & (CU_FRONT | CU_BACK)) || cu->ext1 != 0.0f || cu->ext2 != 0.0f;
188  if ((cu->flag & CU_FAST) || !has_surface) {
189  geom = DRW_cache_text_edge_wire_get(ob);
190  if (geom) {
191  DRW_shgroup_call(pd->edit_text_wire_grp[do_in_front], geom, ob);
192  }
193  }
194  else {
195  /* object mode draws */
196  }
197 
200  edit_text_cache_populate_boxes(vedata, ob);
201 }
202 
204 {
205  OVERLAY_PrivateData *pd = vedata->stl->pd;
206  OVERLAY_PassList *psl = vedata->psl;
207  OVERLAY_FramebufferList *fbl = vedata->fbl;
208 
209  if (DRW_state_is_fbo()) {
211  }
212 
215 
217 
218  /* Alpha blended. */
219  copy_v4_fl4(pd->edit_text.overlay_color, 0.8f, 0.8f, 0.8f, 0.5f);
221 
222  /* Multiply previous result where depth test fail. */
223  copy_v4_fl4(pd->edit_text.overlay_color, 0.0f, 0.0f, 0.0f, 1.0f);
225 }
typedef float(TangentPoint)[2]
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void unit_m4(float m[4][4])
Definition: rct.c:1140
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
void angle_to_mat2(float R[2][2], const float angle)
MINLINE void copy_v2_fl2(float v[2], float x, float y)
MINLINE void copy_v4_fl4(float v[4], float x, float y, float z, float w)
MINLINE void mul_v2_fl(float r[2], float f)
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void add_v2_v2(float r[2], const float a[2])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void mul_v2_v2fl(float r[2], const float a[2], float f)
@ CU_FAST
@ CU_FRONT
@ CU_BACK
@ OB_DRAW_IN_FRONT
@ CURVE_HANDLE_NONE
#define DRW_PASS_INSTANCE_CREATE(pass, original, state)
Definition: DRW_render.h:594
DRWState
Definition: DRW_render.h:312
@ DRW_STATE_BLEND_ALPHA
Definition: DRW_render.h:340
@ DRW_STATE_DEPTH_GREATER_EQUAL
Definition: DRW_render.h:326
@ DRW_STATE_WRITE_DEPTH
Definition: DRW_render.h:314
@ DRW_STATE_WRITE_COLOR
Definition: DRW_render.h:315
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition: DRW_render.h:323
@ DRW_STATE_DEPTH_ALWAYS
Definition: DRW_render.h:321
@ DRW_STATE_BLEND_MUL
Definition: DRW_render.h:345
#define DRW_PASS_CREATE(pass, state)
Definition: DRW_render.h:593
#define DRW_shgroup_call_obmat(shgroup, geom, obmat)
Definition: DRW_render.h:424
#define DRW_shgroup_call(shgroup, geom, ob)
Definition: DRW_render.h:420
GPUBatch
Definition: GPU_batch.h:93
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
struct GPUShader GPUShader
Definition: GPU_shader.h:33
GPUBatch * DRW_cache_quad_get(void)
Definition: draw_cache.c:392
GPUBatch * DRW_cache_text_edge_wire_get(Object *ob)
Definition: draw_cache.c:3074
struct DRW_Global G_draw
Definition: draw_common.c:45
DRWView * DRW_view_create_with_zoffset(const DRWView *parent_view, const RegionView3D *rv3d, float offset)
Definition: draw_common.c:271
bool DRW_state_is_fbo(void)
const DRWContextState * DRW_context_state_get(void)
void DRW_shgroup_uniform_vec4(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
const DRWView * DRW_view_default_get(void)
DRWShadingGroup * DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
void DRW_shgroup_uniform_vec4_copy(DRWShadingGroup *shgroup, const char *name, const float *value)
void DRW_view_set_active(DRWView *view)
void DRW_draw_pass(DRWPass *pass)
static ulong state[N]
static void edit_text_cache_populate_boxes(OVERLAY_Data *vedata, Object *ob)
static void edit_text_cache_populate_select(OVERLAY_Data *vedata, Object *ob)
static void edit_text_cache_populate_cursor(OVERLAY_Data *vedata, Object *ob)
void OVERLAY_edit_text_cache_init(OVERLAY_Data *vedata)
void OVERLAY_edit_text_cache_populate(OVERLAY_Data *vedata, Object *ob)
static void v2_quad_corners_to_mat4(const float corners[4][2], float r_mat[4][4])
void OVERLAY_edit_text_draw(OVERLAY_Data *vedata)
OVERLAY_ExtraCallBuffers * OVERLAY_extra_call_buffer_get(OVERLAY_Data *vedata, Object *ob)
void OVERLAY_extra_line_dashed(OVERLAY_ExtraCallBuffers *cb, const float start[3], const float end[3], const float color[4])
GPUShader * OVERLAY_shader_uniform_color(void)
float xof
float ext1
struct TextBox * tb
struct EditFont * editfont
float fsize_realtime
float yof
float ext2
struct View3D * v3d
Definition: DRW_render.h:742
struct RegionView3D * rv3d
Definition: DRW_render.h:741
GlobalsUboStorage block
Definition: draw_common.h:208
float textcurs[4][2]
Definition: BKE_font.h:52
EditFontSelBox * selboxes
Definition: BKE_font.h:53
int selboxes_len
Definition: BKE_font.h:54
float colorWire[4]
Definition: draw_common.h:41
float colorActive[4]
Definition: draw_common.h:43
OVERLAY_PassList * psl
OVERLAY_StorageList * stl
OVERLAY_FramebufferList * fbl
struct GPUFrameBuffer * overlay_default_fb
DRWPass * edit_text_darken_ps
DRWPass * edit_text_overlay_ps
DRWPass * edit_text_wire_ps[2]
OVERLAY_ShadingData shdata
struct OVERLAY_PrivateData::@240 edit_curve
DRWShadingGroup * edit_text_wire_grp[2]
struct OVERLAY_PrivateData::@241 edit_text
DRWShadingGroup * edit_text_overlay_grp
struct OVERLAY_PrivateData * pd
float obmat[4][4]
void * data
View3DOverlay overlay