4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Daniel Genrich
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/space_view3d/drawvolume.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_view3d_types.h"
43 #include "BLI_blenlib.h"
45 #include "BLI_editVert.h"
46 #include "BLI_edgehash.h"
48 #include "BLI_utildefines.h"
50 #include "BKE_curve.h"
51 #include "BKE_constraint.h" // for the get_constraint_target function
52 #include "BKE_DerivedMesh.h"
53 #include "BKE_deform.h"
54 #include "BKE_displist.h"
55 #include "BKE_effect.h"
57 #include "BKE_global.h"
58 #include "BKE_image.h"
60 #include "BKE_lattice.h"
62 #include "BKE_material.h"
63 #include "BKE_mball.h"
64 #include "BKE_modifier.h"
65 #include "BKE_object.h"
66 #include "BKE_paint.h"
67 #include "BKE_particle.h"
68 #include "BKE_property.h"
69 #include "BKE_smoke.h"
71 #include "smoke_API.h"
75 #include "GPU_extensions.h"
83 #include "view3d_intern.h" // own include
92 static LARGE_INTEGER liFrequency;
93 static LARGE_INTEGER liStartTime;
94 static LARGE_INTEGER liCurrentTime;
96 static void tstart ( void )
98 QueryPerformanceFrequency ( &liFrequency );
99 QueryPerformanceCounter ( &liStartTime );
101 static void tend ( void )
103 QueryPerformanceCounter ( &liCurrentTime );
105 static double tval( void )
107 return ((double)( (liCurrentTime.QuadPart - liStartTime.QuadPart)* (double)1000.0/(double)liFrequency.QuadPart ));
110 #include <sys/time.h>
111 static struct timeval _tstart, _tend;
112 static struct timezone tz;
113 static void tstart ( void )
115 gettimeofday ( &_tstart, &tz );
117 static void tend ( void )
119 gettimeofday ( &_tend,&tz );
125 t1 = ( double ) _tstart.tv_sec*1000 + ( double ) _tstart.tv_usec/ ( 1000 );
126 t2 = ( double ) _tend.tv_sec*1000 + ( double ) _tend.tv_usec/ ( 1000 );
134 static int intersect_edges(float *points, float a, float b, float c, float d, float edges[12][2][3])
140 for (i=0; i<12; i++) {
141 t = -(a*edges[i][0][0] + b*edges[i][0][1] + c*edges[i][0][2] + d)
142 / (a*edges[i][1][0] + b*edges[i][1][1] + c*edges[i][1][2]);
144 points[numpoints * 3 + 0] = edges[i][0][0] + edges[i][1][0]*t;
145 points[numpoints * 3 + 1] = edges[i][0][1] + edges[i][1][1]*t;
146 points[numpoints * 3 + 2] = edges[i][0][2] + edges[i][1][2]*t;
153 static int convex(float *p0, float *up, float *a, float *b)
155 // Vec3 va = a-p0, vb = b-p0;
156 float va[3], vb[3], tmp[3];
157 sub_v3_v3v3(va, a, p0);
158 sub_v3_v3v3(vb, b, p0);
159 cross_v3_v3v3(tmp, va, vb);
160 return dot_v3v3(up, tmp) >= 0;
163 // copied from gpu_extension.c
164 static int is_pow2(int n)
166 return ((n)&(n-1))==0;
169 static int larger_pow2(int n)
180 void draw_volume(ARegion *ar, GPUTexture *tex, float *min, float *max, int res[3], float dx, GPUTexture *tex_shadow)
182 RegionView3D *rv3d= ar->regiondata;
185 int i, j, n, good_index;
187 float *points = NULL;
189 float cor[3] = {1.,1.,1.};
190 int gl_depth = 0, gl_blend = 0;
192 /* draw slices of smoke is adapted from c++ code authored by: Johannes Schmid and Ingemar Rask, 2006, johnny@grob.org */
194 {1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, {1.0f, -1.0f, 1.0f},
195 {1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}, {1.0f, -1.0f, -1.0f}
198 // edges have the form edges[n][0][xyz] + t*edges[n][1][xyz]
199 float edges[12][2][3] = {
200 {{1.0f, 1.0f, -1.0f}, {0.0f, 0.0f, 2.0f}},
201 {{-1.0f, 1.0f, -1.0f}, {0.0f, 0.0f, 2.0f}},
202 {{-1.0f, -1.0f, -1.0f}, {0.0f, 0.0f, 2.0f}},
203 {{1.0f, -1.0f, -1.0f}, {0.0f, 0.0f, 2.0f}},
205 {{1.0f, -1.0f, 1.0f}, {0.0f, 2.0f, 0.0f}},
206 {{-1.0f, -1.0f, 1.0f}, {0.0f, 2.0f, 0.0f}},
207 {{-1.0f, -1.0f, -1.0f}, {0.0f, 2.0f, 0.0f}},
208 {{1.0f, -1.0f, -1.0f}, {0.0f, 2.0f, 0.0f}},
210 {{-1.0f, 1.0f, 1.0f}, {2.0f, 0.0f, 0.0f}},
211 {{-1.0f, -1.0f, 1.0f}, {2.0f, 0.0f, 0.0f}},
212 {{-1.0f, -1.0f, -1.0f}, {2.0f, 0.0f, 0.0f}},
213 {{-1.0f, 1.0f, -1.0f}, {2.0f, 0.0f, 0.0f}}
216 /* Fragment program to calculate the view3d of smoke */
217 /* using 2 textures, density and shadow */
218 const char *text = "!!ARBfp1.0\n"
219 "PARAM dx = program.local[0];\n"
220 "PARAM darkness = program.local[1];\n"
221 "PARAM f = {1.442695041, 1.442695041, 1.442695041, 0.01};\n"
222 "TEMP temp, shadow, value;\n"
223 "TEX temp, fragment.texcoord[0], texture[0], 3D;\n"
224 "TEX shadow, fragment.texcoord[0], texture[1], 3D;\n"
225 "MUL value, temp, darkness;\n"
226 "MUL value, value, dx;\n"
227 "MUL value, value, f;\n"
228 "EX2 temp, -value.r;\n"
229 "SUB temp.a, 1.0, temp.r;\n"
230 "MUL temp.r, temp.r, shadow.r;\n"
231 "MUL temp.g, temp.g, shadow.r;\n"
232 "MUL temp.b, temp.b, shadow.r;\n"
233 "MOV result.color, temp;\n"
241 printf("Could not allocate 3D texture for 3D View smoke drawing.\n");
247 VECSUB(size, max, min);
283 copy_v3_v3(edges[0][0], cv[4]); // maxx, maxy, minz
284 copy_v3_v3(edges[1][0], cv[5]); // minx, maxy, minz
285 copy_v3_v3(edges[2][0], cv[6]); // minx, miny, minz
286 copy_v3_v3(edges[3][0], cv[7]); // maxx, miny, minz
288 copy_v3_v3(edges[4][0], cv[3]); // maxx, miny, maxz
289 copy_v3_v3(edges[5][0], cv[2]); // minx, miny, maxz
290 copy_v3_v3(edges[6][0], cv[6]); // minx, miny, minz
291 copy_v3_v3(edges[7][0], cv[7]); // maxx, miny, minz
293 copy_v3_v3(edges[8][0], cv[1]); // minx, maxy, maxz
294 copy_v3_v3(edges[9][0], cv[2]); // minx, miny, maxz
295 copy_v3_v3(edges[10][0], cv[6]); // minx, miny, minz
296 copy_v3_v3(edges[11][0], cv[5]); // minx, maxy, minz
298 // printf("size x: %f, y: %f, z: %f\n", size[0], size[1], size[2]);
299 // printf("min[2]: %f, max[2]: %f\n", min[2], max[2]);
301 edges[0][1][2] = size[2];
302 edges[1][1][2] = size[2];
303 edges[2][1][2] = size[2];
304 edges[3][1][2] = size[2];
306 edges[4][1][1] = size[1];
307 edges[5][1][1] = size[1];
308 edges[6][1][1] = size[1];
309 edges[7][1][1] = size[1];
311 edges[8][1][0] = size[0];
312 edges[9][1][0] = size[0];
313 edges[10][1][0] = size[0];
314 edges[11][1][0] = size[0];
316 glGetBooleanv(GL_BLEND, (GLboolean *)&gl_blend);
317 glGetBooleanv(GL_DEPTH_TEST, (GLboolean *)&gl_depth);
319 glLoadMatrixf(rv3d->viewmat);
320 // glMultMatrixf(ob->obmat);
322 glDepthMask(GL_FALSE);
323 glDisable(GL_DEPTH_TEST);
325 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
328 printf("Viewinv:\n");
329 printf("%f, %f, %f\n", rv3d->viewinv[0][0], rv3d->viewinv[0][1], rv3d->viewinv[0][2]);
330 printf("%f, %f, %f\n", rv3d->viewinv[1][0], rv3d->viewinv[1][1], rv3d->viewinv[1][2]);
331 printf("%f, %f, %f\n", rv3d->viewinv[2][0], rv3d->viewinv[2][1], rv3d->viewinv[2][2]);
335 copy_v3_v3(viewnormal, rv3d->viewinv[2]);
336 normalize_v3(viewnormal);
338 // find cube vertex that is closest to the viewer
339 for (i=0; i<8; i++) {
342 x = cv[i][0] - viewnormal[0];
343 y = cv[i][1] - viewnormal[1];
344 z = cv[i][2] - viewnormal[2];
346 if ((x>=min[0])&&(x<=max[0])
347 &&(y>=min[1])&&(y<=max[1])
348 &&(z>=min[2])&&(z<=max[2])) {
354 /* fallback, avoid using buffer over-run */
358 // printf("i: %d\n", i);
359 // printf("point %f, %f, %f\n", cv[i][0], cv[i][1], cv[i][2]);
361 if (GL_TRUE == glewIsSupported("GL_ARB_fragment_program"))
363 glEnable(GL_FRAGMENT_PROGRAM_ARB);
364 glGenProgramsARB(1, &prog);
366 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, prog);
367 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(text), text);
370 glProgramLocalParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 0, dx, dx, dx, 1.0);
371 // custom parameter for smoke style (higher = thicker)
372 glProgramLocalParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 1, 7.0, 7.0, 7.0, 1.0);
375 printf("Your gfx card does not support 3D View smoke drawing.\n");
377 GPU_texture_bind(tex, 0);
379 GPU_texture_bind(tex_shadow, 1);
381 printf("No volume shadow\n");
383 if (!GPU_non_power_of_two_support()) {
384 cor[0] = (float)res[0]/(float)larger_pow2(res[0]);
385 cor[1] = (float)res[1]/(float)larger_pow2(res[1]);
386 cor[2] = (float)res[2]/(float)larger_pow2(res[2]);
389 // our slices are defined by the plane equation a*x + b*y +c*z + d = 0
390 // (a,b,c), the plane normal, are given by viewdir
391 // d is the parameter along the view direction. the first d is given by
392 // inserting previously found vertex into the plane equation
393 d0 = (viewnormal[0]*cv[i][0] + viewnormal[1]*cv[i][1] + viewnormal[2]*cv[i][2]);
394 ds = (ABS(viewnormal[0])*size[0] + ABS(viewnormal[1])*size[1] + ABS(viewnormal[2])*size[2]);
395 dd = 0.05; // ds/512.0f;
399 // printf("d0: %f, dd: %f, ds: %f\n\n", d0, dd, ds);
401 points = MEM_callocN(sizeof(float)*12*3, "smoke_points_preview");
405 float tmp_point[3], tmp_point2[3];
410 copy_v3_v3(tmp_point, viewnormal);
411 mul_v3_fl(tmp_point, -dd*((ds/dd)-(float)n));
412 add_v3_v3v3(tmp_point2, cv[good_index], tmp_point);
413 d = dot_v3v3(tmp_point2, viewnormal);
415 // printf("my d: %f\n", d);
417 // intersect_edges returns the intersection points of all cube edges with
418 // the given plane that lie within the cube
419 numpoints = intersect_edges(points, viewnormal[0], viewnormal[1], viewnormal[2], -d, edges);
421 // printf("points: %d\n", numpoints);
424 copy_v3_v3(p0, points);
426 // sort points to get a convex polygon
427 for(i = 1; i < numpoints - 1; i++)
429 for(j = i + 1; j < numpoints; j++)
431 if(!convex(p0, viewnormal, &points[j * 3], &points[i * 3]))
434 copy_v3_v3(tmp2, &points[j * 3]);
435 copy_v3_v3(&points[j * 3], &points[i * 3]);
436 copy_v3_v3(&points[i * 3], tmp2);
441 // printf("numpoints: %d\n", numpoints);
443 glColor3f(1.0, 1.0, 1.0);
444 for (i = 0; i < numpoints; i++) {
445 glTexCoord3d((points[i * 3 + 0] - min[0] )*cor[0]/size[0], (points[i * 3 + 1] - min[1])*cor[1]/size[1], (points[i * 3 + 2] - min[2])*cor[2]/size[2]);
446 glVertex3f(points[i * 3 + 0], points[i * 3 + 1], points[i * 3 + 2]);
454 // printf ( "Draw Time: %f\n",( float ) tval() );
457 GPU_texture_unbind(tex_shadow);
458 GPU_texture_unbind(tex);
460 if(GLEW_ARB_fragment_program)
462 glDisable(GL_FRAGMENT_PROGRAM_ARB);
463 glDeleteProgramsARB(1, &prog);
473 glEnable(GL_DEPTH_TEST);
474 glDepthMask(GL_TRUE);