Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

689 lines
22 KiB

  1. /*
  2. * surfgrid.c - simple test of polygon offset
  3. *
  4. * $Revision: 1.3 $
  5. *
  6. * usage:
  7. * surfgrid [-f]
  8. *
  9. * options:
  10. * -f run on full screen
  11. *
  12. * keys:
  13. * p toggle polygon offset
  14. * m toggle multisampling
  15. * S increase polygon offset factor
  16. * s decrease polygon offset factor
  17. * B increase polygon offset bias
  18. * b decrease polygon offset bias
  19. * g toggle grid drawing
  20. * t toggle surface drawing
  21. * f toggle smooth/flat shading
  22. * n toggle whether to use GL evaluators or GLU nurbs
  23. * u decr number of segments in U direction
  24. * U incr number of segments in U direction
  25. * v decr number of segments in V direction
  26. * V incr number of segments in V direction
  27. * escape quit
  28. */
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <windows.h>
  32. #include <gl\gl.h>
  33. #include <glaux.h>
  34. #define W 600
  35. #define H 600
  36. static GLfloat controlpts[] =
  37. {
  38. 4.0f, 0.0f, 0.0f, 4.0f, 2.0f, 0.0f, 1.0f, 2.0f, 3.0f, 0.0f, 1.0f, 2.0f,
  39. 3.0f, 0.0f, 1.0f, 2.0f, 4.0f, 0.0f, 1.0f, 2.0f, 8.0f, 0.0f, 0.0f, 4.0f,
  40. 8.0f, 0.0f, 0.0f, 4.0f, 4.0f, 0.0f,-1.0f, 2.0f, 3.0f, 0.0f,-1.0f, 2.0f,
  41. 3.0f, 0.0f,-1.0f, 2.0f, 2.0f, 0.0f,-1.0f, 2.0f, 4.0f, 0.0f, 0.0f, 4.0f,
  42. 2.0f,-2.0f, 0.0f, 2.0f, 1.0f,-1.0f, 0.5f, 1.0f, 1.5f,-1.5f, 0.5f, 1.0f,
  43. 1.5f,-1.5f, 0.5f, 1.0f, 2.0f,-2.0f, 0.5f, 1.0f, 4.0f,-4.0f, 0.0f, 2.0f,
  44. 4.0f,-4.0f, 0.0f, 2.0f, 2.0f,-2.0f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1.0f, 1.0f, 0.5f, 1.0f, 2.0f, 2.0f, 0.5f, 1.0f, 4.0f, 4.0f, 0.0f, 2.0f,
  101. 2.0f, 2.0f,-0.5f, 1.0f, 1.0f, 1.0f,-0.5f, 1.0f, 2.0f, 2.0f, 0.0f, 2.0f,
  102. 4.0f, 0.0f, 0.0f, 4.0f, 2.0f, 0.0f, 1.0f, 2.0f, 4.0f, 0.0f, 1.0f, 2.0f,
  103. 8.0f, 0.0f, 0.0f, 4.0f, 4.0f, 0.0f,-1.0f, 2.0f, 2.0f, 0.0f,-1.0f, 2.0f,
  104. 4.0f, 0.0f, 0.0f, 4.0f,
  105. };
  106. /*
  107. * Misc vector op routines.
  108. */
  109. float x_axis[] = { 1.0f, 0.0f, 0.0f };
  110. float y_axis[] = { 0.0f, 1.0f, 0.0f };
  111. float z_axis[] = { 0.0f, 0.0f, 1.0f };
  112. float nx_axis[] = { -1.0f, 0.0f, 0.0f };
  113. float ny_axis[] = { 0.0f, -1.0f, 0.0f };
  114. float nz_axis[] = { 0.0f, 0.0f, -1.0f };
  115. void norm(float v[3])
  116. {
  117. float r;
  118. r = (float)sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
  119. v[0] /= r;
  120. v[1] /= r;
  121. v[2] /= r;
  122. }
  123. float dot(float a[3], float b[3])
  124. {
  125. return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
  126. }
  127. void cross(float v1[3], float v2[3], float result[3])
  128. {
  129. result[0] = v1[1]*v2[2] - v1[2]*v2[1];
  130. result[1] = v1[2]*v2[0] - v1[0]*v2[2];
  131. result[2] = v1[0]*v2[1] - v1[1]*v2[0];
  132. }
  133. float length(float v[3])
  134. {
  135. float r = (float)sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
  136. return r;
  137. }
  138. static long winwidth = W, winheight = H;
  139. GLUnurbsObj *nobj;
  140. GLuint surflist, gridlist;
  141. int useglunurbs = 0;
  142. int smooth = 1;
  143. int tracking = 0;
  144. int showgrid = 1;
  145. int showsurf = 1;
  146. int fullscreen = 0;
  147. int multisampling = 0;
  148. float modelmatrix[16];
  149. float scale = 0.5f;
  150. float bias = 0.002f;
  151. int usegments = 4;
  152. int vsegments = 4;
  153. int spindx, spindy;
  154. int startx, starty;
  155. int curx, cury;
  156. void redraw(void);
  157. void createlists(void);
  158. float torusnurbpts[];
  159. float torusbezierpts[];
  160. void key_n(void)
  161. {
  162. useglunurbs = !useglunurbs;
  163. }
  164. void key_p(void)
  165. {
  166. #ifdef GL_POLYGON_OFFSET_EXT
  167. if (glIsEnabled( GL_POLYGON_OFFSET_EXT ))
  168. {
  169. glDisable( GL_POLYGON_OFFSET_EXT );
  170. printf("disabling polygon offset\n");
  171. }
  172. else
  173. {
  174. glEnable( GL_POLYGON_OFFSET_EXT );
  175. printf("enabling polygon offset\n");
  176. }
  177. #endif
  178. }
  179. void key_m(void)
  180. {
  181. #ifdef GL_MULTISAMPLE_SGIS
  182. if (multisampling)
  183. {
  184. if (glIsEnabled( GL_MULTISAMPLE_SGIS ))
  185. {
  186. glDisable( GL_MULTISAMPLE_SGIS );
  187. }
  188. else
  189. {
  190. glEnable( GL_MULTISAMPLE_SGIS );
  191. }
  192. }
  193. #endif
  194. }
  195. void key_g(void)
  196. {
  197. showgrid = !showgrid;
  198. }
  199. void key_t(void)
  200. {
  201. showsurf = !showsurf;
  202. }
  203. void key_f(void)
  204. {
  205. smooth = !smooth;
  206. if (smooth)
  207. {
  208. glShadeModel( GL_SMOOTH );
  209. }
  210. else
  211. {
  212. glShadeModel( GL_FLAT );
  213. }
  214. }
  215. void key_S(void)
  216. {
  217. scale += 0.1f;
  218. printf( "scale: %8.4f\n", scale);
  219. }
  220. void key_s(void)
  221. {
  222. scale -= 0.1f;
  223. printf( "scale: %8.4f\n", scale);
  224. }
  225. void key_B(void)
  226. {
  227. bias += 0.0001f;
  228. printf( "bias: %8.4f\n", bias);
  229. }
  230. void key_b(void)
  231. {
  232. bias -= 0.0001f;
  233. printf( "bias: %8.4f\n", bias);
  234. }
  235. void key_u(void)
  236. {
  237. usegments = (usegments < 2 ? 1 : usegments-1);
  238. createlists();
  239. }
  240. void key_U(void)
  241. {
  242. usegments++;
  243. createlists();
  244. }
  245. void key_v(void)
  246. {
  247. vsegments = (vsegments < 2 ? 1 : vsegments-1);
  248. createlists();
  249. }
  250. void key_V(void)
  251. {
  252. vsegments++;
  253. createlists();
  254. }
  255. void reshape_proc(GLsizei w, GLsizei h)
  256. {
  257. int size;
  258. winwidth = w;
  259. winheight = h;
  260. size = (winwidth < winheight ? winwidth : winheight);
  261. glViewport((winwidth-size)/2, (winheight-size)/2, size, size);
  262. }
  263. void mouse_ldown(AUX_EVENTREC *mouse)
  264. {
  265. curx = startx = mouse->data[AUX_MOUSEX];
  266. cury = starty = mouse->data[AUX_MOUSEY];
  267. spindx = 0;
  268. spindy = 0;
  269. tracking = 1;
  270. }
  271. void mouse_lup(AUX_EVENTREC *mouse)
  272. {
  273. /*
  274. * If user released the button while moving the mouse, keep
  275. * spinning.
  276. */
  277. if (mouse->data[AUX_MOUSEX] != curx || mouse->data[AUX_MOUSEY] != cury)
  278. {
  279. spindx = mouse->data[AUX_MOUSEX] - curx;
  280. spindy = mouse->data[AUX_MOUSEY] - cury;
  281. }
  282. tracking = 0;
  283. }
  284. void mouse_lloc(AUX_EVENTREC *mouse)
  285. {
  286. if (!tracking)
  287. {
  288. return;
  289. }
  290. curx = mouse->data[AUX_MOUSEX];
  291. cury = mouse->data[AUX_MOUSEY];
  292. if (curx != startx || cury != starty)
  293. {
  294. redraw();
  295. startx = curx;
  296. starty = cury;
  297. }
  298. }
  299. void idle_proc(void)
  300. {
  301. if (!tracking && (spindx != 0 || spindy != 0))
  302. {
  303. redraw();
  304. }
  305. }
  306. void gridmaterials(void)
  307. {
  308. static float front_mat_diffuse[] = { 1.0f, 1.0f, 0.4f, 1.0f };
  309. static float front_mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
  310. static float back_mat_diffuse[] = { 1.0f, 0.0f, 0.0f, 1.0f };
  311. static float back_mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
  312. glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  313. glMaterialfv(GL_FRONT, GL_AMBIENT, front_mat_ambient);
  314. glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  315. glMaterialfv(GL_BACK, GL_AMBIENT, back_mat_ambient);
  316. }
  317. void surfacematerials(void)
  318. {
  319. static float front_mat_diffuse[] = { 0.2f, 0.7f, 0.4f, 1.0f };
  320. static float front_mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
  321. static float back_mat_diffuse[] = { 1.0f, 1.0f, 0.2f, 1.0f };
  322. static float back_mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
  323. glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  324. glMaterialfv(GL_FRONT, GL_AMBIENT, front_mat_ambient);
  325. glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  326. glMaterialfv(GL_BACK, GL_AMBIENT, back_mat_ambient);
  327. }
  328. void init(void)
  329. {
  330. static float ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  331. static float diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  332. static float position[] = { 90.0f, 90.0f, -150.0f, 0.0f };
  333. static float lmodel_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  334. static float lmodel_twoside[] = { (float)GL_TRUE };
  335. glMatrixMode(GL_PROJECTION);
  336. glLoadIdentity();
  337. gluPerspective( 40.0, 1.0, 2.0, 200.0 );
  338. glMatrixMode(GL_MODELVIEW);
  339. glLoadIdentity();
  340. glGetFloatv(GL_MODELVIEW_MATRIX, modelmatrix);
  341. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  342. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  343. glLightfv(GL_LIGHT0, GL_POSITION, position);
  344. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  345. glEnable(GL_LIGHTING);
  346. glEnable(GL_LIGHT0);
  347. glEnable(GL_DEPTH_TEST);
  348. glEnable(GL_AUTO_NORMAL);
  349. #ifdef GL_POLYGON_OFFSET_EXT
  350. glEnable(GL_POLYGON_OFFSET_EXT);
  351. #endif
  352. glFrontFace(GL_CCW);
  353. glEnable( GL_MAP2_VERTEX_4 );
  354. glClearColor(0.25f, 0.25f, 0.5f, 0.0f);
  355. #ifdef GL_POLYGON_OFFSET_EXT
  356. glPolygonOffsetEXT( scale, bias );
  357. #endif
  358. nobj = gluNewNurbsRenderer();
  359. gluNurbsProperty(nobj, GLU_SAMPLING_METHOD, (float)GLU_DOMAIN_DISTANCE );
  360. surflist = glGenLists(1);
  361. gridlist = glGenLists(1);
  362. createlists();
  363. }
  364. void drawmesh(void)
  365. {
  366. int i, j;
  367. float *p;
  368. int up2p = 4;
  369. int uorder = 3, vorder = 3;
  370. int nu = 4, nv = 4;
  371. int vp2p = up2p * uorder * nu;
  372. for (j=0; j < nv; j++) {
  373. for (i=0; i < nu; i++) {
  374. p = torusbezierpts + (j * vp2p * vorder) + (i * up2p * uorder);
  375. #ifdef GL_POLYGON_OFFSET_EXT
  376. glPolygonOffsetEXT( scale, bias );
  377. #endif
  378. glMap2f( GL_MAP2_VERTEX_4, 0.0f, 1.0f, up2p, 3, 0.0f, 1.0f, vp2p, 3,
  379. (void*)p );
  380. if (showsurf) {
  381. surfacematerials();
  382. glEvalMesh2( GL_FILL, 0, usegments, 0, vsegments );
  383. }
  384. if (showgrid) {
  385. gridmaterials();
  386. glEvalMesh2( GL_LINE, 0, usegments, 0, vsegments );
  387. }
  388. }
  389. }
  390. }
  391. void redraw(void)
  392. {
  393. static int i=0;
  394. int dx, dy;
  395. float v[3], rot[3];
  396. float len, ang;
  397. static GLuint vcount;
  398. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  399. glColor3f( 1.0f, 0.0f, 0.0f);
  400. if (tracking) {
  401. dx = curx - startx;
  402. dy = cury - starty;
  403. } else {
  404. dx = spindx;
  405. dy = spindy;
  406. }
  407. if (dx || dy) {
  408. dy = -dy;
  409. v[0] = (float)dx;
  410. v[1] = (float)dy;
  411. v[2] = 0.0f;
  412. len = length(v);
  413. ang = -len / 600 * 360;
  414. norm( v );
  415. cross( v, z_axis, rot );
  416. /*
  417. ** This is certainly not recommended for programs that care
  418. ** about performance or numerical stability: we concatenate
  419. ** the rotation onto the current modelview matrix and read the
  420. ** matrix back, thus saving ourselves from writing our own
  421. ** matrix manipulation routines.
  422. */
  423. glLoadIdentity();
  424. glRotatef(ang, rot[0], rot[1], rot[2]);
  425. glMultMatrixf(modelmatrix);
  426. glGetFloatv(GL_MODELVIEW_MATRIX, modelmatrix);
  427. }
  428. glLoadIdentity();
  429. glTranslatef( 0.0f, 0.0f, -10.0f );
  430. glMultMatrixf(modelmatrix);
  431. if (useglunurbs) {
  432. if (showsurf) glCallList(surflist);
  433. if (showgrid) glCallList(gridlist);
  434. } else {
  435. glMapGrid2f( usegments, 0.0f, 1.0f, vsegments, 0.0f, 1.0f );
  436. drawmesh();
  437. }
  438. auxSwapBuffers();
  439. }
  440. static void usage(void)
  441. {
  442. printf("usage: surfgrid [-f]\n");
  443. exit(-1);
  444. }
  445. /*
  446. * pass the name of the desired extension.
  447. * this will return 1 if the extension is supported, 0 otherwise.
  448. */
  449. int
  450. getextension(char *e)
  451. {
  452. const GLubyte *s;
  453. s = glGetString(GL_EXTENSIONS);
  454. if (!s)
  455. return 0;
  456. if (*s == '\0')
  457. return 0;
  458. return (strstr(s,e) == 0) ? 0 : 1;
  459. }
  460. int __cdecl main(int argc, char **argv)
  461. {
  462. int i;
  463. for (i=1; i<argc; i++) {
  464. if (argv[i][0] == '-') {
  465. switch (argv[i][1]) {
  466. case 'f':
  467. fullscreen = 1;
  468. break;
  469. default:
  470. usage();
  471. break;
  472. }
  473. } else {
  474. usage();
  475. }
  476. }
  477. #ifdef GL_POLYGON_OFFSET_EXT
  478. if (!getextension("GL_EXT_polygon_offset"))
  479. {
  480. printf("Warning: GL_EXT_polygon_offset not supported on this "
  481. "machine.. trying anyway\n");
  482. }
  483. #endif
  484. auxInitPosition(50, 50, winwidth, winheight);
  485. auxInitDisplayMode(AUX_RGB | AUX_DOUBLE | AUX_DEPTH16);
  486. auxInitWindow("SurfGrid");
  487. auxReshapeFunc(reshape_proc);
  488. auxIdleFunc(idle_proc);
  489. auxKeyFunc(AUX_n, key_n);
  490. auxKeyFunc(AUX_p, key_p);
  491. auxKeyFunc(AUX_m, key_m);
  492. auxKeyFunc(AUX_g, key_g);
  493. auxKeyFunc(AUX_t, key_t);
  494. auxKeyFunc(AUX_f, key_f);
  495. auxKeyFunc(AUX_S, key_S);
  496. auxKeyFunc(AUX_s, key_s);
  497. auxKeyFunc(AUX_B, key_B);
  498. auxKeyFunc(AUX_b, key_b);
  499. auxKeyFunc(AUX_u, key_u);
  500. auxKeyFunc(AUX_U, key_U);
  501. auxKeyFunc(AUX_v, key_v);
  502. auxKeyFunc(AUX_V, key_V);
  503. auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, mouse_ldown);
  504. auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEUP, mouse_lup);
  505. auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSELOC, mouse_lloc);
  506. init();
  507. auxMainLoop(redraw);
  508. return 0;
  509. }
  510. /****************************************************************************/
  511. float circleknots[] = { 0.0f, 0.0f, 0.0f, 0.25f, 0.50f, 0.50f, 0.75f, 1.0f,
  512. 1.0f, 1.0f };
  513. void createlists(void)
  514. {
  515. gluNurbsProperty(nobj, GLU_U_STEP, (usegments-1)*4.0f );
  516. gluNurbsProperty(nobj, GLU_V_STEP, (vsegments-1)*4.0f );
  517. gluNurbsProperty(nobj, GLU_DISPLAY_MODE, (float)GLU_FILL);
  518. glNewList(surflist, GL_COMPILE);
  519. surfacematerials();
  520. gluBeginSurface(nobj);
  521. gluNurbsSurface(nobj, 10, circleknots, 10, circleknots,
  522. 4, 28, torusnurbpts, 3, 3, GL_MAP2_VERTEX_4);
  523. gluEndSurface(nobj);
  524. glEndList();
  525. gluNurbsProperty(nobj, GLU_DISPLAY_MODE, (float)GLU_OUTLINE_POLYGON);
  526. glNewList(gridlist, GL_COMPILE);
  527. gridmaterials();
  528. gluBeginSurface(nobj);
  529. gluNurbsSurface(nobj, 10, circleknots, 10, circleknots,
  530. 4, 28, torusnurbpts, 3, 3, GL_MAP2_VERTEX_4);
  531. gluEndSurface(nobj);
  532. glEndList();
  533. }
  534. /****************************************************************************/
  535. /*
  536. * Control points of the torus in Bezier form. Can be rendered
  537. * using OpenGL evaluators.
  538. */
  539. static GLfloat torusbezierpts[] = {
  540. 4.0f, 0.0f, 0.0f, 4.0f, 2.0f, 0.0f, 1.0f, 2.0f, 3.0f, 0.0f, 1.0f, 2.0f,
  541. 3.0f, 0.0f, 1.0f, 2.0f, 4.0f, 0.0f, 1.0f, 2.0f, 8.0f, 0.0f, 0.0f, 4.0f,
  542. 8.0f, 0.0f, 0.0f, 4.0f, 4.0f, 0.0f,-1.0f, 2.0f, 3.0f, 0.0f,-1.0f, 2.0f,
  543. 3.0f, 0.0f,-1.0f, 2.0f, 2.0f, 0.0f,-1.0f, 2.0f, 4.0f, 0.0f, 0.0f, 4.0f,
  544. 2.0f,-2.0f, 0.0f, 2.0f, 1.0f,-1.0f, 0.5f, 1.0f, 1.5f,-1.5f, 0.5f, 1.0f,
  545. 1.5f,-1.5f, 0.5f, 1.0f, 2.0f,-2.0f, 0.5f, 1.0f, 4.0f,-4.0f, 0.0f, 2.0f,
  546. 4.0f,-4.0f, 0.0f, 2.0f, 2.0f,-2.0f,-0.5f, 1.0f, 1.5f,-1.5f,-0.5f, 1.0f,
  547. 1.5f,-1.5f,-0.5f, 1.0f, 1.0f,-1.0f,-0.5f, 1.0f, 2.0f,-2.0f, 0.0f, 2.0f,
  548. 0.0f,-2.0f, 0.0f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0.5f, 1.0f, 0.0f, 1.0f,-0.5f, 1.0f, 0.0f, 2.0f, 0.0f, 2.0f,
  576. 0.0f, 2.0f, 0.0f, 2.0f, 0.0f, 1.0f, 0.5f, 1.0f, 0.0f, 1.5f, 0.5f, 1.0f,
  577. 0.0f, 1.5f, 0.5f, 1.0f, 0.0f, 2.0f, 0.5f, 1.0f, 0.0f, 4.0f, 0.0f, 2.0f,
  578. 0.0f, 4.0f, 0.0f, 2.0f, 0.0f, 2.0f,-0.5f, 1.0f, 0.0f, 1.5f,-0.5f, 1.0f,
  579. 0.0f, 1.5f,-0.5f, 1.0f, 0.0f, 1.0f,-0.5f, 1.0f, 0.0f, 2.0f, 0.0f, 2.0f,
  580. 2.0f, 2.0f, 0.0f, 2.0f, 1.0f, 1.0f, 0.5f, 1.0f, 1.5f, 1.5f, 0.5f, 1.0f,
  581. 1.5f, 1.5f, 0.5f, 1.0f, 2.0f, 2.0f, 0.5f, 1.0f, 4.0f, 4.0f, 0.0f, 2.0f,
  582. 4.0f, 4.0f, 0.0f, 2.0f, 2.0f, 2.0f,-0.5f, 1.0f, 1.5f, 1.5f,-0.5f, 1.0f,
  583. 1.5f, 1.5f,-0.5f, 1.0f, 1.0f, 1.0f,-0.5f, 1.0f, 2.0f, 2.0f, 0.0f, 2.0f,
  584. 4.0f, 0.0f, 0.0f, 4.0f, 2.0f, 0.0f, 1.0f, 2.0f, 3.0f, 0.0f, 1.0f, 2.0f,
  585. 3.0f, 0.0f, 1.0f, 2.0f, 4.0f, 0.0f, 1.0f, 2.0f, 8.0f, 0.0f, 0.0f, 4.0f,
  586. 8.0f, 0.0f, 0.0f, 4.0f, 4.0f, 0.0f,-1.0f, 2.0f, 3.0f, 0.0f,-1.0f, 2.0f,
  587. 3.0f, 0.0f,-1.0f, 2.0f, 2.0f, 0.0f,-1.0f, 2.0f, 4.0f, 0.0f, 0.0f, 4.0f,
  588. };
  589. /*
  590. * Control points of a torus in NURBS form. Can be rendered using
  591. * the GLU NURBS routines.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