WM: refactor gestures for use as tools
[blender.git] / source / blender / editors / space_view3d / drawsimdebug.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2014 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Lukas Toenne
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_view3d/drawsimdebug.c
29  *  \ingroup spview3d
30  */
31
32 #include "DNA_scene_types.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_view3d_types.h"
35 #include "DNA_object_types.h"
36
37 #include "BLI_math.h"
38 #include "BLI_utildefines.h"
39 #include "BLI_ghash.h"
40
41 #include "BKE_effect.h"
42
43 #include "view3d_intern.h"
44
45 #include "BIF_gl.h"
46
47
48 static void draw_sim_debug_elements(SimDebugData *debug_data, float imat[4][4])
49 {
50         GHashIterator iter;
51         
52         /**** dots ****/
53         
54         glPointSize(3.0f);
55         glBegin(GL_POINTS);
56         for (BLI_ghashIterator_init(&iter, debug_data->gh); !BLI_ghashIterator_done(&iter); BLI_ghashIterator_step(&iter)) {
57                 SimDebugElement *elem = BLI_ghashIterator_getValue(&iter);
58                 if (elem->type != SIM_DEBUG_ELEM_DOT)
59                         continue;
60                 
61                 glColor3f(elem->color[0], elem->color[1], elem->color[2]);
62                 glVertex3f(elem->v1[0], elem->v1[1], elem->v1[2]);
63         }
64         glEnd();
65         
66         /**** circles ****/
67         
68         {
69                 float circle[16][2] = {
70                     {0.000000, 1.000000}, {0.382683, 0.923880}, {0.707107, 0.707107}, {0.923880, 0.382683},
71                     {1.000000, -0.000000}, {0.923880, -0.382683}, {0.707107, -0.707107}, {0.382683, -0.923880},
72                     {-0.000000, -1.000000}, {-0.382683, -0.923880}, {-0.707107, -0.707107}, {-0.923879, -0.382684},
73                     {-1.000000, 0.000000}, {-0.923879, 0.382684}, {-0.707107, 0.707107}, {-0.382683, 0.923880} };
74                 for (BLI_ghashIterator_init(&iter, debug_data->gh); !BLI_ghashIterator_done(&iter); BLI_ghashIterator_step(&iter)) {
75                         SimDebugElement *elem = BLI_ghashIterator_getValue(&iter);
76                         float radius = elem->v2[0];
77                         float co[3];
78                         int i;
79                         
80                         if (elem->type != SIM_DEBUG_ELEM_CIRCLE)
81                                 continue;
82                         
83                         glColor3f(elem->color[0], elem->color[1], elem->color[2]);
84                         glBegin(GL_LINE_LOOP);
85                         for (i = 0; i < 16; ++i) {
86                                 co[0] = radius * circle[i][0];
87                                 co[1] = radius * circle[i][1];
88                                 co[2] = 0.0f;
89                                 mul_mat3_m4_v3(imat, co);
90                                 add_v3_v3(co, elem->v1);
91                                 
92                                 glVertex3f(co[0], co[1], co[2]);
93                         }
94                         glEnd();
95                 }
96         }
97         
98         /**** lines ****/
99         
100         glBegin(GL_LINES);
101         for (BLI_ghashIterator_init(&iter, debug_data->gh); !BLI_ghashIterator_done(&iter); BLI_ghashIterator_step(&iter)) {
102                 SimDebugElement *elem = BLI_ghashIterator_getValue(&iter);
103                 if (elem->type != SIM_DEBUG_ELEM_LINE)
104                         continue;
105                 
106                 glColor3f(elem->color[0], elem->color[1], elem->color[2]);
107                 glVertex3f(elem->v1[0], elem->v1[1], elem->v1[2]);
108                 glVertex3f(elem->v2[0], elem->v2[1], elem->v2[2]);
109         }
110         glEnd();
111         
112         /**** vectors ****/
113         
114         glPointSize(2.0f);
115         glBegin(GL_POINTS);
116         for (BLI_ghashIterator_init(&iter, debug_data->gh); !BLI_ghashIterator_done(&iter); BLI_ghashIterator_step(&iter)) {
117                 SimDebugElement *elem = BLI_ghashIterator_getValue(&iter);
118                 if (elem->type != SIM_DEBUG_ELEM_VECTOR)
119                         continue;
120                 
121                 glColor3f(elem->color[0], elem->color[1], elem->color[2]);
122                 glVertex3f(elem->v1[0], elem->v1[1], elem->v1[2]);
123         }
124         glEnd();
125         
126         glBegin(GL_LINES);
127         for (BLI_ghashIterator_init(&iter, debug_data->gh); !BLI_ghashIterator_done(&iter); BLI_ghashIterator_step(&iter)) {
128                 SimDebugElement *elem = BLI_ghashIterator_getValue(&iter);
129                 float t[3];
130                 if (elem->type != SIM_DEBUG_ELEM_VECTOR)
131                         continue;
132                 
133                 glColor3f(elem->color[0], elem->color[1], elem->color[2]);
134                 glVertex3f(elem->v1[0], elem->v1[1], elem->v1[2]);
135                 add_v3_v3v3(t, elem->v1, elem->v2);
136                 glVertex3f(t[0], t[1], t[2]);
137         }
138         glEnd();
139         
140         /**** strings ****/
141         
142         for (BLI_ghashIterator_init(&iter, debug_data->gh); !BLI_ghashIterator_done(&iter); BLI_ghashIterator_step(&iter)) {
143                 SimDebugElement *elem = BLI_ghashIterator_getValue(&iter);
144                 if (elem->type != SIM_DEBUG_ELEM_STRING)
145                         continue;
146                 
147                 unsigned char col[4];
148                 rgb_float_to_uchar(col, elem->color);
149                 col[3] = 255;
150                 view3d_cached_text_draw_add(elem->v1, elem->str, strlen(elem->str),
151                                             0, V3D_CACHE_TEXT_GLOBALSPACE, col);
152         }
153 }
154
155 void draw_sim_debug_data(Scene *UNUSED(scene), View3D *v3d, ARegion *ar)
156 {
157         RegionView3D *rv3d = ar->regiondata;
158         /*Object *ob = base->object;*/
159         float imat[4][4];
160         
161         if (!_sim_debug_data)
162                 return;
163         
164         invert_m4_m4(imat, rv3d->viewmatob);
165         
166 //      glDepthMask(GL_FALSE);
167 //      glEnable(GL_BLEND);
168         
169         glPushMatrix();
170         glLoadMatrixf(rv3d->viewmat);
171         
172         view3d_cached_text_draw_begin();
173         draw_sim_debug_elements(_sim_debug_data, imat);
174         view3d_cached_text_draw_end(v3d, ar, false);
175         
176         glPopMatrix();
177         
178 //      glDepthMask(GL_TRUE);
179 //      glDisable(GL_BLEND);
180 }