Задача: Рисование 3D объекта
Исходник: Рисование 3D объекта, язык: C++ [code #125, hits: 8241]
автор: - [добавлен: 17.05.2006]
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. #include <dos.h>
  7. #define Pi 3.1415926536
  8. enum Action{move,draw};
  9. struct Point3D
  10. {
  11. int x;
  12. int y;
  13. int z;
  14. Action action;
  15. };
  16. // this function initializes graphics mode
  17. // it will work only if you're using Borland C++ compiler & BGI drivers
  18. // if you're using another compiler you should overwrite body of this function
  19. void init_gr(void)
  20. {
  21. /* request autodetection */
  22. int gdriver = DETECT, gmode, errorcode;
  23. /* initialize graphics mode */
  24. initgraph(&gdriver, &gmode, "");
  25. /* read result of initialization */
  26. errorcode = graphresult();
  27. if (errorcode != grOk) /* an error occurred */
  28. {
  29. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  30. printf("Press any key to halt:");
  31. getch();
  32. exit(1); /* return with error code */
  33. }
  34. }
  35. // this function shuts graphics mode down
  36. // it will work only if you're using Borland C++ compiler & BGI drivers
  37. // if you're using another compiler you should overwrite body of this function
  38. void end_gr(void)
  39. {
  40. closegraph();
  41. }
  42. // this function moves CP to (x,y) position
  43. // it will work only if you're using Borland C++ compiler & BGI drivers
  44. // if you're using another compiler you should overwrite body of this function
  45. void MoveTo(int x, int y)
  46. {
  47. moveto(x,y);
  48. }
  49. // this function draws a line to (x,y) position
  50. // it will work only if you're using Borland C++ compiler & BGI drivers
  51. // if you're using another compiler you should overwrite body of this function
  52. void LineTo(int x, int y)
  53. {
  54. lineto(x,y);
  55. }
  56. void draw3Dobject(Point3D *object, int N, float rho, float theta,
  57. float phi, float dist_to_screen, int xshift, int yshift)
  58. {
  59. int x,y;
  60. float xe,ye,ze,costh,sinph,cosph,sinth,v11,v12,v13,v21,v22,v32,v33,v23,v43;
  61. // calculating coefficients
  62. costh=cos(theta);
  63. sinth=sin(theta);
  64. cosph=cos(phi);
  65. sinph=sin(phi);
  66. v11=-sinth; v12=-cosph*costh; v13=-sinph*costh;
  67. v21=costh; v22=-cosph*sinth; v23=-sinph*sinth;
  68. v32=sinph; v33=-cosph;
  69. v43=rho;
  70. for (int i=0;i<N;i++)
  71. {
  72. // calculating eye coordinates
  73. xe=v11*(object+i)->x+v21*(object+i)->y;
  74. ye=v12*(object+i)->x+v22*(object+i)->y+v32*(object+i)->z;
  75. ze=v13*(object+i)->x+v23*(object+i)->y+v33*(object+i)->z+v43;
  76. // calculating screen coordinates
  77. x=dist_to_screen*xe/ze+xshift;
  78. y=dist_to_screen*ye/ze+yshift;
  79. // drawing
  80. if((object+i)->action==move)
  81. MoveTo(x,y);
  82. else
  83. LineTo(x,y);
  84. }
  85. }
  86. int main(void)
  87. {
  88. Point3D thetr[]={{100,100,100,move},
  89. {100,200,100,draw},
  90. {200,100,100,draw},
  91. {100,100,100,draw},
  92. {100,100,200,draw},
  93. {200,100,100,draw},
  94. {100,100,200,move},
  95. {100,200,100,draw}};
  96. int N=sizeof(thetr)/sizeof(Point3D);
  97. float rho=700,theta=Pi/4,phi=Pi/4,dist_to_screen=300;
  98. int xshift=300, yshift=150;
  99. // initializing graphics mode
  100. init_gr();
  101. /* examples */
  102. while (!kbhit())
  103. {
  104. theta+=0.05;
  105. // rotating viewpoint
  106. if (phi>2*Pi) phi-=2*Pi;
  107. setcolor(11);
  108. draw3Dobject(thetr,N,rho,theta,phi,dist_to_screen,xshift,yshift);
  109. setcolor(0);
  110. delay(15);
  111. draw3Dobject(thetr,N,rho,theta,phi,dist_to_screen,xshift,yshift);
  112. }
  113. /* clean up */
  114. getch();
  115. end_gr();
  116. return 0;
  117. }
Пример кода рисования 3D объекта.

//////////////////////////////////////////////////////////////////////////////
//
// Drawing 3D object
// (c) Johna Smith, 1996
//
// Method description:
// Function draw3Dobject recieves the following parameters:
// object - reference to array of points of the drawn object
// N - number of points in this array
// rho \
// phi |- spherical coordinates of point of view
// theta /
// dist_to_screen - distance from viewpoint to screen
// xshift, yshift - picture will be shifted by this values
//
//////////////////////////////////////////////////////////////////////////////

+добавить реализацию