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.
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.
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.
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
20 /** \file \ingroup spview3d
23 #include "DNA_mesh_types.h"
24 #include "DNA_scene_types.h"
25 #include "DNA_object_types.h"
29 #include "BKE_DerivedMesh.h"
30 #include "BKE_global.h"
32 #include "BKE_editmesh.h"
34 #include "DEG_depsgraph.h"
35 #include "DEG_depsgraph_query.h"
38 #include "GPU_shader.h"
39 #include "GPU_immediate.h"
40 #include "GPU_batch.h"
41 #include "GPU_matrix.h"
42 #include "GPU_state.h"
43 #include "GPU_framebuffer.h"
47 #include "UI_resources.h"
49 #include "view3d_intern.h" /* bad level include */
51 #include "../../draw/intern/draw_cache_impl.h" /* bad level include (temporary) */
53 int view3d_effective_drawtype(const struct View3D *v3d)
55 if (v3d->shading.type == OB_RENDER) {
56 return v3d->shading.prev_type;
58 return v3d->shading.type;
61 static bool check_ob_drawface_dot(Scene *sce, View3D *vd, char dt)
63 if ((sce->toolsettings->selectmode & SCE_SELECT_FACE) == 0)
66 if (G.f & G_FLAG_BACKBUFSEL)
69 /* if its drawing textures with zbuf sel, then don't draw dots */
70 if (dt == OB_TEXTURE && vd->shading.type == OB_TEXTURE)
76 /* OpenGL Circle Drawing - Tables for Optimized Drawing Speed */
77 /* 32 values of sin function (still same result!) */
78 #define CIRCLE_RESOL 32
80 static const float sinval[CIRCLE_RESOL] = {
115 /* 32 values of cos function (still same result!) */
116 static const float cosval[CIRCLE_RESOL] = {
151 static void circball_array_fill(float verts[CIRCLE_RESOL][3], const float cent[3], float rad, const float tmat[4][4])
154 float *viter = (float *)verts;
156 mul_v3_v3fl(vx, tmat[0], rad);
157 mul_v3_v3fl(vy, tmat[1], rad);
159 for (unsigned int a = 0; a < CIRCLE_RESOL; a++, viter += 3) {
160 viter[0] = cent[0] + sinval[a] * vx[0] + cosval[a] * vy[0];
161 viter[1] = cent[1] + sinval[a] * vx[1] + cosval[a] * vy[1];
162 viter[2] = cent[2] + sinval[a] * vx[2] + cosval[a] * vy[2];
166 void imm_drawcircball(const float cent[3], float rad, const float tmat[4][4], unsigned pos)
168 float verts[CIRCLE_RESOL][3];
170 circball_array_fill(verts, cent, rad, tmat);
172 immBegin(GPU_PRIM_LINE_LOOP, CIRCLE_RESOL);
173 for (int i = 0; i < CIRCLE_RESOL; ++i) {
174 immVertex3fv(pos, verts[i]);
179 #ifdef VIEW3D_CAMERA_BORDER_HACK
180 unsigned char view3d_camera_border_hack_col[3];
181 bool view3d_camera_border_hack_test = false;
184 /* ***************** BACKBUF SEL (BBS) ********* */
186 /** See #DRW_shgroup_world_clip_planes_from_rv3d, same function for draw manager. */
187 static void bbs_world_clip_planes_from_rv3d(GPUBatch *batch, const float world_clip_planes[6][4])
189 GPU_batch_uniform_4fv_array(batch, "WorldClipPlanes", 6, world_clip_planes[0]);
192 static void bbs_mesh_verts(GPUBatch *batch, int offset, const float world_clip_planes[6][4])
194 GPU_point_size(UI_GetThemeValuef(TH_VERTEX_SIZE));
196 const eGPUShaderConfig shader_cfg = world_clip_planes ? GPU_SHADER_CFG_CLIPPED : GPU_SHADER_CFG_DEFAULT;
197 GPU_batch_program_set_builtin_with_config(batch, GPU_SHADER_3D_FLAT_SELECT_ID, shader_cfg);
198 GPU_batch_uniform_1ui(batch, "offset", offset);
199 if (world_clip_planes != NULL) {
200 bbs_world_clip_planes_from_rv3d(batch, world_clip_planes);
202 GPU_batch_draw(batch);
205 static void bbs_mesh_wire(GPUBatch *batch, int offset, const float world_clip_planes[6][4])
207 GPU_line_width(1.0f);
208 glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
210 const eGPUShaderConfig shader_cfg = world_clip_planes ? GPU_SHADER_CFG_CLIPPED : GPU_SHADER_CFG_DEFAULT;
211 GPU_batch_program_set_builtin_with_config(batch, GPU_SHADER_3D_FLAT_SELECT_ID, shader_cfg);
212 GPU_batch_uniform_1ui(batch, "offset", offset);
213 if (world_clip_planes != NULL) {
214 bbs_world_clip_planes_from_rv3d(batch, world_clip_planes);
216 GPU_batch_draw(batch);
218 glProvokingVertex(GL_LAST_VERTEX_CONVENTION);
221 /* two options, facecolors or black */
222 static void bbs_mesh_face(GPUBatch *batch, const bool use_select, const float world_clip_planes[6][4])
225 const eGPUShaderConfig shader_cfg = world_clip_planes ? GPU_SHADER_CFG_CLIPPED : GPU_SHADER_CFG_DEFAULT;
226 GPU_batch_program_set_builtin_with_config(batch, GPU_SHADER_3D_FLAT_SELECT_ID, shader_cfg);
227 GPU_batch_uniform_1ui(batch, "offset", 1);
228 if (world_clip_planes != NULL) {
229 bbs_world_clip_planes_from_rv3d(batch, world_clip_planes);
231 GPU_batch_draw(batch);
234 const eGPUShaderConfig shader_cfg = world_clip_planes ? GPU_SHADER_CFG_CLIPPED : GPU_SHADER_CFG_DEFAULT;
235 GPU_batch_program_set_builtin_with_config(batch, GPU_SHADER_3D_UNIFORM_SELECT_ID, shader_cfg);
236 GPU_batch_uniform_1ui(batch, "id", 0);
237 if (world_clip_planes != NULL) {
238 bbs_world_clip_planes_from_rv3d(batch, world_clip_planes);
240 GPU_batch_draw(batch);
244 static void bbs_mesh_face_dot(GPUBatch *batch, const float world_clip_planes[6][4])
246 const eGPUShaderConfig shader_cfg = world_clip_planes ? GPU_SHADER_CFG_CLIPPED : GPU_SHADER_CFG_DEFAULT;
247 GPU_batch_program_set_builtin_with_config(batch, GPU_SHADER_3D_FLAT_SELECT_ID, shader_cfg);
248 GPU_batch_uniform_1ui(batch, "offset", 1);
249 if (world_clip_planes != NULL) {
250 bbs_world_clip_planes_from_rv3d(batch, world_clip_planes);
252 GPU_batch_draw(batch);
255 static void bbs_mesh_solid_verts(
256 Depsgraph *UNUSED(depsgraph), Scene *UNUSED(scene), Object *ob, const float world_clip_planes[6][4])
260 GPUBatch *geom_faces = DRW_mesh_batch_cache_get_triangles_with_select_id(me);
261 GPUBatch *geom_verts = DRW_mesh_batch_cache_get_verts_with_select_id(me);
262 DRW_mesh_batch_cache_create_requested(ob, me, NULL, false, true);
264 /* Only draw faces to mask out verts, we don't want their selection ID's. */
265 bbs_mesh_face(geom_faces, false, world_clip_planes);
266 bbs_mesh_verts(geom_verts, 1, world_clip_planes);
268 bm_vertoffs = me->totvert + 1;
271 static void bbs_mesh_solid_faces(Scene *UNUSED(scene), Object *ob, const float world_clip_planes[6][4])
274 Mesh *me_orig = DEG_get_original_object(ob)->data;
276 const bool use_hide = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL);
277 GPUBatch *geom_faces = DRW_mesh_batch_cache_get_triangles_with_select_id(me);
278 DRW_mesh_batch_cache_create_requested(ob, me, NULL, false, use_hide);
280 bbs_mesh_face(geom_faces, true, world_clip_planes);
283 void draw_object_backbufsel(
284 Depsgraph *depsgraph, Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob,
287 ToolSettings *ts = scene->toolsettings;
288 if (select_mode == -1) {
289 select_mode = ts->selectmode;
292 GPU_matrix_mul(ob->obmat);
293 GPU_depth_test(true);
295 const float (*world_clip_planes)[4] = NULL;
296 if (rv3d->rflag & RV3D_CLIPPING) {
297 ED_view3d_clipping_local(rv3d, ob->obmat);
298 world_clip_planes = rv3d->clip_local;
303 if (ob->mode & OB_MODE_EDIT) {
305 BMEditMesh *em = me->edit_btmesh;
306 const bool draw_facedot = check_ob_drawface_dot(scene, v3d, ob->dt);
307 const bool use_faceselect = (select_mode & SCE_SELECT_FACE) != 0;
309 BM_mesh_elem_table_ensure(em->bm, BM_VERT | BM_EDGE | BM_FACE);
311 GPUBatch *geom_faces, *geom_edges, *geom_verts, *geom_facedots;
312 geom_faces = DRW_mesh_batch_cache_get_triangles_with_select_id(me);
313 if (select_mode & SCE_SELECT_EDGE) {
314 geom_edges = DRW_mesh_batch_cache_get_edges_with_select_id(me);
316 if (select_mode & SCE_SELECT_VERTEX) {
317 geom_verts = DRW_mesh_batch_cache_get_verts_with_select_id(me);
320 geom_facedots = DRW_mesh_batch_cache_get_facedots_with_select_id(me);
322 DRW_mesh_batch_cache_create_requested(ob, me, NULL, false, true);
324 bbs_mesh_face(geom_faces, use_faceselect, world_clip_planes);
326 if (use_faceselect && draw_facedot) {
327 bbs_mesh_face_dot(geom_facedots, world_clip_planes);
330 if (select_mode & SCE_SELECT_FACE)
331 bm_solidoffs = 1 + em->bm->totface;
336 ED_view3d_polygon_offset(rv3d, 1.0);
338 /* we draw edges if edge select mode */
339 if (select_mode & SCE_SELECT_EDGE) {
340 bbs_mesh_wire(geom_edges, bm_solidoffs, world_clip_planes);
341 bm_wireoffs = bm_solidoffs + em->bm->totedge;
344 /* `bm_vertoffs` is calculated from `bm_wireoffs`. (otherwise see T53512) */
345 bm_wireoffs = bm_solidoffs;
348 ED_view3d_polygon_offset(rv3d, 1.1);
350 /* we draw verts if vert select mode. */
351 if (select_mode & SCE_SELECT_VERTEX) {
352 bbs_mesh_verts(geom_verts, bm_wireoffs, world_clip_planes);
353 bm_vertoffs = bm_wireoffs + em->bm->totvert;
356 bm_vertoffs = bm_wireoffs;
359 ED_view3d_polygon_offset(rv3d, 0.0);
362 Mesh *me = DEG_get_original_object(ob)->data;
363 if ((me->editflag & ME_EDIT_PAINT_VERT_SEL) &&
364 /* currently vertex select supports weight paint and vertex paint*/
365 ((ob->mode & OB_MODE_WEIGHT_PAINT) || (ob->mode & OB_MODE_VERTEX_PAINT)))
367 bbs_mesh_solid_verts(depsgraph, scene, ob, world_clip_planes);
370 bbs_mesh_solid_faces(scene, ob, world_clip_planes);
379 GPU_matrix_set(rv3d->viewmat);
383 void ED_draw_object_facemap(
384 Depsgraph *depsgraph, Object *ob, const float col[4], const int facemap)
386 /* happens on undo */
387 if (ob->type != OB_MESH || !ob->data) {
393 Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
394 if (ob_eval->runtime.mesh_eval) {
395 me = ob_eval->runtime.mesh_eval;
399 glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);
401 /* Just to create the data to pass to immediate mode, grr! */
402 const int *facemap_data = CustomData_get_layer(&me->pdata, CD_FACEMAP);
404 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
407 const MVert *mvert = me->mvert;
408 const MPoly *mpoly = me->mpoly;
409 const MLoop *mloop = me->mloop;
411 int mpoly_len = me->totpoly;
412 int mloop_len = me->totloop;
414 facemap_data = CustomData_get_layer(&me->pdata, CD_FACEMAP);
416 /* use gawain immediate mode fore now */
417 const int looptris_len = poly_to_tri_count(mpoly_len, mloop_len);
418 const int vbo_len_capacity = looptris_len * 3;
419 int vbo_len_used = 0;
421 GPUVertFormat format_pos = { 0 };
422 const uint pos_id = GPU_vertformat_attr_add(&format_pos, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
424 GPUVertBuf *vbo_pos = GPU_vertbuf_create_with_format(&format_pos);
425 GPU_vertbuf_data_alloc(vbo_pos, vbo_len_capacity);
427 GPUVertBufRaw pos_step;
428 GPU_vertbuf_attr_get_raw_data(vbo_pos, pos_id, &pos_step);
432 if (me->runtime.looptris.array) {
433 MLoopTri *mlt = me->runtime.looptris.array;
434 for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) {
435 if (facemap_data[i] == facemap) {
436 for (int j = 2; j < mp->totloop; j++) {
437 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[0]].v].co);
438 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[1]].v].co);
439 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[2]].v].co);
445 mlt += mp->totloop - 2;
450 /* No tessellation data, fan-fill. */
451 for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) {
452 if (facemap_data[i] == facemap) {
453 const MLoop *ml_start = &mloop[mp->loopstart];
454 const MLoop *ml_a = ml_start + 1;
455 const MLoop *ml_b = ml_start + 2;
456 for (int j = 2; j < mp->totloop; j++) {
457 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_start->v].co);
458 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_a->v].co);
459 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_b->v].co);
469 if (vbo_len_capacity != vbo_len_used) {
470 GPU_vertbuf_data_resize(vbo_pos, vbo_len_used);
473 GPUBatch *draw_batch = GPU_batch_create(GPU_PRIM_TRIS, vbo_pos, NULL);
474 GPU_batch_program_set_builtin(draw_batch, GPU_SHADER_3D_UNIFORM_COLOR);
475 GPU_batch_uniform_4fv(draw_batch, "color", col);
476 GPU_batch_draw(draw_batch);
477 GPU_batch_discard(draw_batch);
478 GPU_vertbuf_discard(vbo_pos);