diff --git a/source/blender/blenlib/BLI_arithb.h b/source/blender/blenlib/BLI_arithb.h index 9ed23bc32b6..4d277cf98e1 100644 --- a/source/blender/blenlib/BLI_arithb.h +++ b/source/blender/blenlib/BLI_arithb.h @@ -258,6 +258,7 @@ void Vec2Addf(float *v, float *v1, float *v2); void Vec2Subf(float *v, float *v1, float *v2); void Vec2Copyf(float *v1, float *v2); +void AxisAngleToQuat(float *q, float *axis, float angle); void vectoquat(float *vec, short axis, short upflag, float *q); float VecAngle2(float *v1, float *v2); @@ -269,6 +270,8 @@ float NormalizedVecAngle2_2D(float *v1, float *v2); void euler_rot(float *beul, float ang, char axis); +void NormalShortToFloat(float *out, short *in); +void NormalFloatToShort(short *out, float *in); float DistVL2Dfl(float *v1, float *v2, float *v3); float PdistVL2Dfl(float *v1, float *v2, float *v3); @@ -372,6 +375,7 @@ void tubemap(float x, float y, float z, float *u, float *v); void spheremap(float x, float y, float z, float *u, float *v); int LineIntersectsTriangle(float p1[3], float p2[3], float v0[3], float v1[3], float v2[3], float *lambda, float *uv); +int RayIntersectsTriangle(float p1[3], float d[3], float v0[3], float v1[3], float v2[3], float *lambda, float *uv); int SweepingSphereIntersectsTriangleUV(float p1[3], float p2[3], float radius, float v0[3], float v1[3], float v2[3], float *lambda, float *ipoint); int AxialLineIntersectsTriangle(int axis, float co1[3], float co2[3], float v0[3], float v1[3], float v2[3], float *lambda); int AabbIntersectAabb(float min1[3], float max1[3], float min2[3], float max2[3]); diff --git a/source/blender/blenlib/intern/arithb.c b/source/blender/blenlib/intern/arithb.c index 48a149f4b3a..322a9e6fd02 100644 --- a/source/blender/blenlib/intern/arithb.c +++ b/source/blender/blenlib/intern/arithb.c @@ -1335,6 +1335,22 @@ void NormalQuat(float *q) } } +void AxisAngleToQuat(float *q, float *axis, float angle) +{ + float nor[3]; + float si; + + VecCopyf(nor, axis); + Normalize(nor); + + angle /= 2; + si = (float)sin(angle); + q[0] = (float)cos(angle); + q[1] = nor[0] * si; + q[2] = nor[1] * si; + q[3] = nor[2] * si; +} + void vectoquat(float *vec, short axis, short upflag, float *q) { float q2[4], nor[3], *fp, mat[3][3], angle, si, co, x2, y2, z2, len1; @@ -2258,6 +2274,20 @@ double Sqrt3d(double d) else return exp(log(d)/3); } +void NormalShortToFloat(float *out, short *in) +{ + out[0] = in[0] / 32767.0; + out[1] = in[1] / 32767.0; + out[2] = in[2] / 32767.0; +} + +void NormalFloatToShort(short *out, float *in) +{ + out[0] = (short)(in[0] * 32767.0); + out[1] = (short)(in[1] * 32767.0); + out[2] = (short)(in[2] * 32767.0); +} + /* distance v1 to line v2-v3 */ /* using Hesse formula, NO LINE PIECE! */ float DistVL2Dfl( float *v1, float *v2, float *v3) { @@ -3671,6 +3701,43 @@ int LineIntersectsTriangle(float p1[3], float p2[3], float v0[3], float v1[3], f return 1; } +/* moved from effect.c + test if the ray starting at p1 going in d direction intersects the triangle v0..v2 + return non zero if it does +*/ +int RayIntersectsTriangle(float p1[3], float d[3], float v0[3], float v1[3], float v2[3], float *lambda, float *uv) +{ + float p[3], s[3], e1[3], e2[3], q[3]; + float a, f, u, v; + + VecSubf(e1, v1, v0); + VecSubf(e2, v2, v0); + + Crossf(p, d, e2); + a = Inpf(e1, p); + if ((a > -0.000001) && (a < 0.000001)) return 0; + f = 1.0f/a; + + VecSubf(s, p1, v0); + + Crossf(q, s, e1); + *lambda = f * Inpf(e2, q); + if ((*lambda < 0.0)) return 0; + + u = f * Inpf(s, p); + if ((u < 0.0)||(u > 1.0)) return 0; + + v = f * Inpf(d, q); + if ((v < 0.0)||((u + v) > 1.0)) return 0; + + if(uv) { + uv[0]= u; + uv[1]= v; + } + + return 1; +} + /* Adapted from the paper by Kasper Fauerby */ /* "Improved Collision detection and Response" */ int SweepingSphereIntersectsTriangleUV(float p1[3], float p2[3], float radius, float v0[3], float v1[3], float v2[3], float *lambda, float *ipoint) diff --git a/source/blender/include/BSE_view.h b/source/blender/include/BSE_view.h index 4b334fdd959..f7afce958f5 100644 --- a/source/blender/include/BSE_view.h +++ b/source/blender/include/BSE_view.h @@ -66,6 +66,8 @@ void project_int(float *vec, int *adr); void project_int_noclip(float *vec, int *adr); void project_float(float *vec, float *adr); void project_float_noclip(float *vec, float *adr); +void viewray(short mval[2], float ray_start[3], float ray_normal[3]); +void viewline(short mval[2], float ray_start[3], float ray_end[3]); int boundbox_clip(float obmat[][4], struct BoundBox *bb); void fdrawline(float x1, float y1, float x2, float y2); diff --git a/source/blender/src/editparticle.c b/source/blender/src/editparticle.c index 95a4abe1f9d..f420d46c827 100644 --- a/source/blender/src/editparticle.c +++ b/source/blender/src/editparticle.c @@ -2273,9 +2273,9 @@ static void brush_add(Object *ob, ParticleSystem *psys, short *mval, short numbe ParticleEditSettings *pset= PE_settings(); ParticleEdit *edit = psys->edit; int i, k, n = 0, totpart = psys->totpart; + short mco[2]; short dmx = 0, dmy = 0; - short mx = mval[0] - curarea->winx / 2, my = mval[1] - curarea->winy / 2; - float co1[3], co2[3], vec[4], min_d, imat[4][4]; + float co1[3], co2[3], min_d, imat[4][4]; float framestep, timestep = psys_get_timestep(psys->part); short size = pset->brush[PE_BRUSH_ADD].size; short size2 = size*size; @@ -2302,35 +2302,9 @@ static void brush_add(Object *ob, ParticleSystem *psys, short *mval, short numbe } } - /* create intersection coordinates in view Z direction at mouse coordinates */ - /* Thanks to who ever wrote the "Mouse Location 3D Space" tutorial in "Blender 3D: Blending Into Python/Cookbook". */ - if(G.vd->persp != V3D_ORTHO){ - vec[0]= (2.0f*(mx+dmx)/curarea->winx); - vec[1]= (2.0f*(my+dmy)/curarea->winy); - vec[2]= -1.0f; - vec[3]= 1.0f; - - Mat4MulVec4fl(G.vd->persinv, vec); - VecMulf(vec, 1.0f/vec[3]); - - VECCOPY(co1, G.vd->viewinv[3]); - VECSUB(vec, vec, co1); - Normalize(vec); - - VECADDFAC(co1, G.vd->viewinv[3], vec, G.vd->near); - VECADDFAC(co2, G.vd->viewinv[3], vec, G.vd->far); - } - else { - vec[0] = 2.0f*(mx+dmx)/curarea->winx; - vec[1] = 2.0f*(my+dmy)/curarea->winy; - vec[2] = 0.0f; - vec[3] = 1.0f; - - Mat4MulVec4fl(G.vd->persinv,vec); - - VECADDFAC(co1,vec,G.vd->viewinv[2],1000.0f); - VECADDFAC(co2,vec,G.vd->viewinv[2],-1000.0f); - } + mco[0] = mval[0] + dmx; + mco[1] = mval[1] + dmy; + viewline(mco, co1, co2); Mat4MulVecfl(imat,co1); Mat4MulVecfl(imat,co2); diff --git a/source/blender/src/view.c b/source/blender/src/view.c index f53bcb3a9f7..835aeb9bb30 100644 --- a/source/blender/src/view.c +++ b/source/blender/src/view.c @@ -144,6 +144,48 @@ void persp(int a) } } +/* create intersection ray in view Z direction at mouse coordinates */ +void viewray(short mval[2], float ray_start[3], float ray_normal[3]) +{ + float ray_end[3]; + viewline(mval, ray_start, ray_end); + VecSubf(ray_normal, ray_end, ray_start); + Normalize(ray_normal); +} + +/* create intersection coordinates in view Z direction at mouse coordinates */ +void viewline(short mval[2], float ray_start[3], float ray_end[3]) +{ + float vec[3]; + + if(G.vd->persp != V3D_ORTHO){ + vec[0]= 2.0f * mval[0] / curarea->winx - 1; + vec[1]= 2.0f * mval[1] / curarea->winy - 1; + vec[2]= -1.0f; + vec[3]= 1.0f; + + Mat4MulVec4fl(G.vd->persinv, vec); + VecMulf(vec, 1.0f / vec[3]); + + VECCOPY(ray_start, G.vd->viewinv[3]); + VECSUB(vec, vec, ray_start); + Normalize(vec); + + VECADDFAC(ray_start, G.vd->viewinv[3], vec, G.vd->near); + VECADDFAC(ray_end, G.vd->viewinv[3], vec, G.vd->far); + } + else { + vec[0] = 2.0f * mval[0] / curarea->winx - 1; + vec[1] = 2.0f * mval[1] / curarea->winy - 1; + vec[2] = 0.0f; + vec[3] = 1.0f; + + Mat4MulVec4fl(G.vd->persinv, vec); + + VECADDFAC(ray_start, vec, G.vd->viewinv[2], 1000.0f); + VECADDFAC(ray_end, vec, G.vd->viewinv[2], -1000.0f); + } +} void initgrabz(float x, float y, float z) {