Guest User

Untitled

a guest
Aug 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. OpenGL - Rotate a 'Curve' About the Y-Axis
  2. x'=xcos(theta)-zsin(theta)
  3.  
  4. y'=y
  5.  
  6. z'=xsin(theta)+zcos(theta)
  7.  
  8. // this is in a loop
  9.  
  10. // setup the new angle
  11. double angle = i>0 ? (360/sweepResolutionMod)*i : 0;
  12.  
  13. angle = angle * (M_PI/180);
  14.  
  15. // for each point...
  16. for( int i=0; i<clickedPoints.size(); i++ )
  17. {
  18. // initial point, normalized
  19. GLfloat tempX = (clickedPoints[i].x-250)/250;
  20. GLfloat tempY = (clickedPoints[i].y-250)/250;
  21. GLfloat tempZ = 0.0;
  22.  
  23. // log the initial point
  24. cout << "(" << tempX << ", " << tempY << ", 0.0) by " << angle << " radians = ";
  25.  
  26. // generate the new point
  27. GLfloat newX = (tempX * cos(angle)) - (tempZ * sin(angle));
  28. GLfloat newY = tempY;
  29. GLfloat newZ = (tempX * sin(angle)) - (tempZ * cos(angle));
  30.  
  31. // log the new point
  32. cout << "(" << newX << ", " << newY << ", " << newZ << ")n";
  33.  
  34. // render the new point
  35. glVertex3d(newX, newY, newZ);
  36. }
  37.  
  38. (0.048, -0.296, 0.0) by 0 radians = (0.048, -0.296, 0)
  39. (0.376, -0.508, 0.0) by 0 radians = (0.376, -0.508, 0)
  40. (0.72, -0.204, 0.0) by 0 radians = (0.72, -0.204, 0)
  41. (0.652, 0.176, 0.0) by 0 radians = (0.652, 0.176, 0)
  42. (0.368, 0.504, 0.0) by 0 radians = (0.368, 0.504, 0)
  43.  
  44. (0.048, -0.296, 0.0) by 0.628319 radians = (0.0388328, -0.296, 0.0282137)
  45. (0.376, -0.508, 0.0) by 0.628319 radians = (0.30419, -0.508, 0.221007)
  46. (0.72, -0.204, 0.0) by 0.628319 radians = (0.582492, -0.204, 0.423205)
  47. (0.652, 0.176, 0.0) by 0.628319 radians = (0.527479, 0.176, 0.383236)
  48. (0.368, 0.504, 0.0) by 0.628319 radians = (0.297718, 0.504, 0.216305)
  49.  
  50. (0.048, -0.296, 0.0) by 1.25664 radians = (0.0148328, -0.296, 0.0456507)
  51. (0.376, -0.508, 0.0) by 1.25664 radians = (0.11619, -0.508, 0.357597)
  52. (0.72, -0.204, 0.0) by 1.25664 radians = (0.222492, -0.204, 0.684761)
  53. (0.652, 0.176, 0.0) by 1.25664 radians = (0.201479, 0.176, 0.620089)
  54. (0.368, 0.504, 0.0) by 1.25664 radians = (0.113718, 0.504, 0.349989)
  55.  
  56. ...
  57.  
  58. (0.048, -0.296, 0.0) by 6.28319 radians = (0.048, -0.296, -1.17566e-17)
  59. (0.376, -0.508, 0.0) by 6.28319 radians = (0.376, -0.508, -9.20934e-17)
  60. (0.72, -0.204, 0.0) by 6.28319 radians = (0.72, -0.204, -1.76349e-16)
  61. (0.652, 0.176, 0.0) by 6.28319 radians = (0.652, 0.176, -1.59694e-16)
  62. (0.368, 0.504, 0.0) by 6.28319 radians = (0.368, 0.504, -9.0134e-17)
  63.  
  64. void displayPersp(void)
  65. {
  66. glClear(GL_COLOR_BUFFER_BIT);
  67.  
  68. glMatrixMode (GL_MODELVIEW);
  69. glLoadIdentity ();
  70.  
  71. gluLookAt (-2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);
  72.  
  73. // draw the axis
  74. glBegin(GL_LINES);
  75. // x
  76. glVertex3f(500.0, 0.0, 0.0);
  77. glVertex3f(-500.0, 0.0, 0.0);
  78. // y
  79. glVertex3f(0.0, -500.0, 0.0);
  80. glVertex3f(0.0, 500.0, 0.0);
  81. // z
  82. glVertex3f(0.0, 0.0, -500.0);
  83. glVertex3f(0.0, 0.0, 500.0);
  84.  
  85. glEnd();
  86.  
  87. cout << endl;
  88.  
  89. // loop as many number of times as we are going to draw the points around the Y-Axis
  90. for( int i=0; i<=sweepResolutionMod; i++ )
  91. {
  92. cout << endl;
  93.  
  94. // setup the new angle
  95. double angle = i>0 ? (360/sweepResolutionMod)*i : 0;
  96.  
  97. angle = angle * (M_PI/180);
  98.  
  99. // for each point...
  100. for( int i=0; i<clickedPoints.size(); i++ )
  101. {
  102. GLfloat tempX = (clickedPoints[i].x-250)/250;
  103. GLfloat tempY = (clickedPoints[i].y-250)/250;
  104. GLfloat tempZ = 0.0;
  105.  
  106. cout << "(" << tempX << ", " << tempY << ", 0.0) by " << angle << " degrees = ";
  107.  
  108. GLfloat newX = (tempX * cos(angle)) - (tempZ * sin(angle));
  109. GLfloat newY = tempY;
  110. GLfloat newZ = (tempX * sin(angle)) - (tempZ * cos(angle));
  111.  
  112. cout << "(" << newX << ", " << newY << ", " << newZ << ")n";
  113.  
  114. glVertex3d(newX, newY, newZ);
  115. }
  116.  
  117. // the following was my old solution, using OpenGL's rotate(), but that
  118. // didn't allow me to get back the new point's coordinates.
  119.  
  120. /*
  121. glRotatef(angle, 0.0, 1.0, 0.0);
  122.  
  123. // draw a line?
  124. if( clickedPoints.size() > 1 )
  125. {
  126. glBegin(GL_LINE_STRIP);
  127.  
  128. for(int i=0; i<clickedPoints.size(); i++ )
  129. {
  130. glVertex3f((clickedPoints[i].x-250)/250, (clickedPoints[i].y-250)/250, 0.0);
  131. }
  132.  
  133. glEnd();
  134. }
  135.  
  136. // everyone gets points
  137. glBegin(GL_POINTS);
  138.  
  139. for(int i=0; i<clickedPoints.size(); i++ )
  140. {
  141. glVertex3f((clickedPoints[i].x-250)/250, (clickedPoints[i].y-250)/250, 0.0);
  142. }
  143.  
  144. glEnd();
  145. */
  146. }
  147.  
  148.  
  149. glutSwapBuffers();
  150. }
  151.  
  152. void displayPersp(void)
  153. {
  154. glClear(GL_COLOR_BUFFER_BIT);
  155.  
  156. gluLookAt (-2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);
  157.  
  158. glMatrixMode (GL_MODELVIEW);
  159. glLoadIdentity ();
  160.  
  161. // draw the axis
  162. glBegin(GL_LINES);
  163. // x
  164. glVertex3f(500.0, 0.0, 0.0);
  165. glVertex3f(-500.0, 0.0, 0.0);
  166. // y
  167. glVertex3f(0.0, -500.0, 0.0);
  168. glVertex3f(0.0, 500.0, 0.0);
  169. // z
  170. glVertex3f(0.0, 0.0, -500.0);
  171. glVertex3f(0.0, 0.0, 500.0);
  172.  
  173. glEnd();
  174.  
  175. cout << endl;
  176.  
  177. double previousTheta = 0.0;
  178.  
  179. for( int i=0; i<=sweepResolutionMod; i++ )
  180. {
  181. double theta = i>0 ? (360/sweepResolutionMod)*i : 0;
  182.  
  183. theta = theta * (M_PI/180);
  184.  
  185. if( clickedPoints.size() > 1 )
  186. {
  187. // the 'vertical' piece
  188. glBegin(GL_LINE_STRIP);
  189.  
  190. for(int i=0; i<clickedPoints.size(); i++ )
  191. {
  192. // normalize
  193. GLfloat tempX = (clickedPoints[i].x-250)/250;
  194. GLfloat tempY = (clickedPoints[i].y-250)/250;
  195. GLfloat tempZ = 0.0;
  196.  
  197. // new points
  198. GLfloat newX = ( tempX * cos(theta) ) + ( tempZ * sin(theta) );
  199. GLfloat newY = tempY;
  200. GLfloat newZ = ( tempZ * cos(theta) ) - ( tempX * sin(theta) );
  201.  
  202. glVertex3f(newX, newY, newZ);
  203. }
  204.  
  205. glEnd();
  206.  
  207. // the 'horizontal' piece
  208. if( previousTheta != theta )
  209. {
  210. glBegin(GL_LINES);
  211.  
  212. for(int i=0; i<clickedPoints.size(); i++ )
  213. {
  214. // normalize
  215. GLfloat tempX = (clickedPoints[i].x-250)/250;
  216. GLfloat tempY = (clickedPoints[i].y-250)/250;
  217. GLfloat tempZ = 0.0;
  218.  
  219. // new points
  220. GLfloat newX = ( tempX * cos(theta) ) + ( tempZ * sin(theta) );
  221. GLfloat newY = tempY;
  222. GLfloat newZ = ( tempZ * cos(theta) ) - ( tempX * sin(theta) );
  223.  
  224. // previous points
  225. GLfloat previousX = ( tempX * cos(previousTheta) ) + ( tempZ * sin(previousTheta) );
  226. GLfloat previousY = tempY;
  227. GLfloat previousZ = ( tempZ * cos(previousTheta) ) - ( tempX * sin(previousTheta) );
  228.  
  229. // horizontal component
  230. glVertex3f(newX, newY, newZ);
  231. glVertex3f(previousX, previousY, previousZ);
  232. }
  233.  
  234. glEnd();
  235. }
  236. }
  237.  
  238. previousTheta = theta;
  239. }
  240.  
  241. glutSwapBuffers();
  242. }
  243.  
  244. static const double pi = 3.1416;
  245.  
  246. for (int point=0; point<NUM_POINTS; point++) {
  247. glBegin(GL_LINE_STRIP);
  248. for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
  249. double x = cos(theta);
  250. double z = sin(theta);
  251. glVertex3d(points[point][0]*x, points[point][1], -1.0-points[point][0]*z);
  252. }
  253. glEnd();
  254. }
  255.  
  256. for (int point=0; point<NUM_POINTS; point++) {
  257. glBegin(GL_LINE_STRIP);
  258. for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
  259. double x = cos(theta);
  260. double z = sin(theta);
  261. glVertex3d(points[point][0]*x, points[point][1], -1.0 - points[point][0]*z);
  262. }
  263. glEnd();
  264. }
  265.  
  266. for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
  267. glBegin(GL_LINE_STRIP);
  268. for (int point=0; point<NUM_POINTS; point++) {
  269. double x = cos(theta);
  270. double z = sin(theta);
  271. glVertex3d(points[point][0]*x, points[point][1], -1.0 - points[point][0]*z);
  272. }
  273. glEnd();
  274. }
Add Comment
Please, Sign In to add comment