Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
63
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //---------------------------------------------------------------------------
  2. //--- tube with sinus screw -------------------------------------------------
  3. //---------------------------------------------------------------------------
  4. // https://stackoverflow.com/a/54050883/2521214
  5. //---------------------------------------------------------------------------
  6. const int ca= 20; // points per slice
  7. const int cb=100; // slices
  8. const float r0= 0.3; // minor screw radius
  9. const float r1= 0.35; // major screw radius
  10. const float l1= 4.0; // tube length
  11. const float nz= 10.0; // screws
  12. //---------------------------------------------------------------------------
  13. vec3 bend_pc; int bend_j0,bend_j1; // just for debug draw
  14. //---------------------------------------------------------------------------
  15. // straight mesh
  16. vec3 pnt0[ca][cb]; // vertex
  17. vec3 nor0[ca][cb]; // normal
  18. vec2 txr0[ca][cb]; // texcoord
  19. vec3 mid0[cb]; // slice center
  20. vec3 dir0[cb]; // slice central axis (normalized)
  21. float len0[cb]; // slice arclength position
  22. //---------------------------------------------------------------------------
  23. // bended mesh
  24. vec3 pnt1[ca][cb]; // vertex
  25. vec3 nor1[ca][cb]; // normal
  26. vec2 txr1[ca][cb]; // texcoord
  27. vec3 mid1[cb]; // slice center
  28. vec3 dir1[cb]; // slice central axis (normalized)
  29. float len1[cb]; // slice arclength position
  30. //---------------------------------------------------------------------------
  31. void obj0_init() // sin screw
  32. {
  33. int i,j,i0,j0;
  34. float a,b,l,da,db,dl,r,s,c,tx,ty;
  35. float dtx=1.0/float(ca-1),dty=1.0/float(cb-1);
  36. vec3 u,v;
  37. // pnt,txr
  38. da=2.0*M_PI/float(ca-1);
  39. db=nz*2.0*M_PI/float(cb);
  40. dl=l1/float(cb);
  41. for (a=0.0,tx=0.0,i=0;i<ca;i++,a+=da,tx+=dtx)
  42. {
  43. s=sin(a);
  44. c=cos(a);
  45. for (l=-0.5*l1,b=0,ty=0.0,j=0;j<cb;j++,b+=db,l+=dl,ty+=dty)
  46. {
  47. r=r0+((r1-r0)*cos(a+b));
  48. pnt0[i][j].x=r*c;
  49. pnt0[i][j].y=r*s;
  50. pnt0[i][j].z=l;
  51. txr0[i][j].x=tx;
  52. txr0[i][j].y=ty;
  53. }
  54. }
  55. // mid,dir
  56. for (l=0.0,j=0;j<cb;j++,l+=dl)
  57. {
  58. mid0[j]=vec3(0.0,0.0, l-(0.5*l1));
  59. dir0[j]=vec3(0.0,0.0,dl);
  60. len0[j]=l;
  61. }
  62. // nor
  63. for (i0=ca-2,i=0;i<ca;i0=i,i++)
  64. for (j0=cb-1,j=0;j<cb;j0=j,j++)
  65. {
  66. u=pnt0[i][j]-pnt0[i0][j];
  67. v=pnt0[i][j]-pnt0[i][j0];
  68. nor0[i][j]=normalize(cross(u,v));
  69. }
  70. }
  71. //---------------------------------------------------------------------------
  72. void obj1_copy() // obj1 = obj0
  73. {
  74. int i,j;
  75. for (i=0;i<ca;i++)
  76. for (j=0;j<cb;j++)
  77. {
  78. pnt1[i][j]=pnt0[i][j];
  79. txr1[i][j]=txr0[i][j];
  80. nor1[i][j]=nor0[i][j];
  81. }
  82. for (j=0;j<cb;j++)
  83. {
  84. mid1[j]=mid0[j];
  85. dir1[j]=dir0[j];
  86. len1[j]=len0[j];
  87. }
  88. }
  89. //---------------------------------------------------------------------------
  90. vec3 rotatex(vec3 p,vec3 p0,float a)
  91. {
  92. vec3 q; p-=p0;
  93. q.z=+(p.z*cos(a))+(p.y*sin(a));
  94. q.y=-(p.z*sin(a))+(p.y*cos(a));
  95. q.x=p.x;
  96. return q+p0;
  97. }
  98. //---------------------------------------------------------------------------
  99. vec3 rotatey(vec3 p,vec3 p0,float a)
  100. {
  101. vec3 q; p-=p0;
  102. q.x=+(p.x*cos(a))+(p.z*sin(a));
  103. q.z=-(p.x*sin(a))+(p.z*cos(a));
  104. q.y=p.y;
  105. return q+p0;
  106. }
  107. //---------------------------------------------------------------------------
  108. vec3 rotatez(vec3 p,vec3 p0,float a)
  109. {
  110. vec3 q; p-=p0;
  111. q.x=+(p.x*cos(a))+(p.y*sin(a));
  112. q.y=-(p.x*sin(a))+(p.y*cos(a));
  113. q.z=p.z;
  114. return q+p0;
  115. }
  116. //---------------------------------------------------------------------------
  117. void obj1_bendx(float l0,float l1,float ang) // [units],[units],[rad] bend obj1 around x axis
  118. {
  119. int i,j,i0,j0,j1;
  120. float a,r,l;
  121. vec3 PC,p,u,v;
  122. vec3 P0,X0,Y0,Z0;
  123. // find start and end of bend
  124. for (j0= 0;(j0<cb)&&(len1[j0]<l0);j0++);
  125. for (j1=j0;(j1<cb)&&(len1[j1]<l1);j1++);
  126. if (j0>cb) return; // no bend
  127. // coordinate system0
  128. P0=mid1[j0];
  129. Z0=normalize(dir1[j0]);
  130. X0=vec3(1.0,0.0,0.0);
  131. Y0=cross(Z0,X0);
  132. X0=cross(Y0,Z0);
  133. // bend center
  134. r=(l1-l0)/ang;
  135. PC=P0-(Y0*r);
  136. r=fabs(r);
  137. // just for debug draw
  138. bend_pc=PC;
  139. bend_j0=j0;
  140. bend_j1=j1;
  141. // bend <l0,l1)
  142. for (j=j0;j<cb;j++)
  143. {
  144. // arc length -> angle [rad] and length correction
  145. if (j<j1)
  146. {
  147. a=ang*(len1[j]-l0)/(l1-l0);
  148. p=Z0*(len1[j]-l0);
  149. }
  150. else{
  151. a=ang;
  152. p=Z0*(l1-l0);
  153. }
  154. // transform system0 -> system1
  155. mid1[j]=rotatex(mid1[j]-p,PC,a);
  156. dir1[j]=rotatex(dir1[j],vec3(0.0,0.0,0.0),a);
  157. for (i=0;i<ca;i++) pnt1[i][j]=rotatex(pnt1[i][j]-p,PC,a);
  158. }
  159. // nor
  160. for (i0=ca-2,i=0;i<ca;i0=i,i++)
  161. for (j0=cb-1,j=0;j<cb;j0=j,j++)
  162. {
  163. u=pnt1[i][j]-pnt1[i0][j];
  164. v=pnt1[i][j]-pnt1[i][j0];
  165. nor1[i][j]=normalize(cross(u,v));
  166. }
  167. }
  168. //---------------------------------------------------------------------------
  169. void obj0_draw()
  170. {
  171. int i,j;
  172. glColor3f(1.0,1.0,1.0);
  173. glEnable(GL_CULL_FACE);
  174. glFrontFace(GL_CW);
  175. for (i=0;i<ca-1;i++)
  176. {
  177. glBegin(GL_QUAD_STRIP);
  178. for (j=0;j<cb;j++)
  179. {
  180. glTexCoord2fv(txr0[i+1][j].dat);
  181. glNormal3fv (nor0[i+1][j].dat);
  182. glVertex3fv (pnt0[i+1][j].dat);
  183. glTexCoord2fv(txr0[i ][j].dat);
  184. glNormal3fv (nor0[i ][j].dat);
  185. glVertex3fv (pnt0[i ][j].dat);
  186. }
  187. glEnd();
  188. }
  189. }
  190. //---------------------------------------------------------------------------
  191. void obj1_draw()
  192. {
  193. int i,j;
  194. glColor3f(1.0,1.0,1.0);
  195. glEnable(GL_CULL_FACE);
  196. glFrontFace(GL_CW);
  197. for (i=0;i<ca-1;i++)
  198. {
  199. glBegin(GL_QUAD_STRIP);
  200. for (j=0;j<cb;j++)
  201. {
  202. glTexCoord2fv(txr1[i+1][j].dat);
  203. glNormal3fv (nor1[i+1][j].dat);
  204. glVertex3fv (pnt1[i+1][j].dat);
  205. glTexCoord2fv(txr1[i ][j].dat);
  206. glNormal3fv (nor1[i ][j].dat);
  207. glVertex3fv (pnt1[i ][j].dat);
  208. }
  209. glEnd();
  210. }
  211. }
  212. //---------------------------------------------------------------------------
  213. //---------------------------------------------------------------------------
  214. //---------------------------------------------------------------------------
Advertisement
RAW Paste Data Copied
Advertisement