Guest User

Untitled

a guest
Jan 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. //********************************************
  2. // lab3.cpp
  3. //********************************************
  4.  
  5.  
  6.  
  7. #include "stdafx.h"
  8. #include "Tool.h"
  9.  
  10. #include "ToolDoc.h"
  11. #include "RenderView.h"
  12. #include <math.h>
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. #ifndef M_PI
  21. #define M_PI 3.141592654
  22. #endif
  23.  
  24.  
  25. void CRenderView::drawSphere(double r)
  26. {
  27.  
  28. float no_mat[] = {0.0f, 0.0f, 0.0f, 1.0f};
  29. float mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
  30. float mat_ambient_color[] = {0.8f, 0.8f, 0.2f, 1.0f};
  31. float mat_diffuse[] = {0.1f, 0.5f, 0.8f, 1.0f};
  32. float no_shininess = 0.0f;
  33. float mat_emission[] = {0.3f, 0.2f, 0.2f, 0.0f};
  34. glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  35. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  36.  
  37. if(m_Highlight)
  38. {
  39. // your codes for highlight here
  40. } else {
  41. glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  42. glMaterialf(GL_FRONT, GL_SHININESS, no_shininess);
  43. }
  44.  
  45. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  46.  
  47. int i,j;
  48. int n = 20;
  49. for(i=0;i<n;i++)
  50. for(j=0;j<2*n;j++)
  51. if(m_Smooth)
  52. {
  53. glBegin(GL_POLYGON);
  54.  
  55. // the normal of each vertex is actaully its own coordinates normalized for a sphere
  56.  
  57. // your normal here
  58. glVertex3d(r*sin(i*M_PI/n)*cos(j*M_PI/n),r*cos(i*M_PI/n)*cos(j*M_PI/n),r*sin(j*M_PI/n));
  59. // your normal here
  60. glVertex3d(r*sin((i+1)*M_PI/n)*cos(j*M_PI/n),r*cos((i+1)*M_PI/n)*cos(j*M_PI/n),r*sin(j*M_PI/n));
  61. // your normal here
  62. glVertex3d(r*sin((i+1)*M_PI/n)*cos((j+1)*M_PI/n),r*cos((i+1)*M_PI/n)*cos((j+1)*M_PI/n),r*sin((j+1)*M_PI/n));
  63. // your normal here
  64. glVertex3d(r*sin(i*M_PI/n)*cos((j+1)*M_PI/n),r*cos(i*M_PI/n)*cos((j+1)*M_PI/n),r*sin((j+1)*M_PI/n));
  65. glEnd();
  66. } else {
  67. glBegin(GL_POLYGON);
  68. // Explanation: the normal of the whole polygon is the coordinate of the center of the polygon for a sphere
  69. glNormal3d(sin((i+0.5)*M_PI/n)*cos((j+0.5)*M_PI/n),cos((i+0.5)*M_PI/n)*cos((j+0.5)*M_PI/n),sin((j+0.5)*M_PI/n));
  70. glVertex3d(r*sin(i*M_PI/n)*cos(j*M_PI/n),r*cos(i*M_PI/n)*cos(j*M_PI/n),r*sin(j*M_PI/n));
  71. glVertex3d(r*sin((i+1)*M_PI/n)*cos(j*M_PI/n),r*cos((i+1)*M_PI/n)*cos(j*M_PI/n),r*sin(j*M_PI/n));
  72. glVertex3d(r*sin((i+1)*M_PI/n)*cos((j+1)*M_PI/n),r*cos((i+1)*M_PI/n)*cos((j+1)*M_PI/n),r*sin((j+1)*M_PI/n));
  73. glVertex3d(r*sin(i*M_PI/n)*cos((j+1)*M_PI/n),r*cos(i*M_PI/n)*cos((j+1)*M_PI/n),r*sin((j+1)*M_PI/n));
  74. glEnd();
  75. }
  76.  
  77. }
  78.  
  79.  
  80. void CRenderView::drawMyCreation()
  81. {
  82. // your composite obejct here
  83. }
  84.  
  85. void CRenderView::drawScene()
  86. {
  87.  
  88. switch(obj_no) {
  89. case 0:
  90. drawSphere();
  91. break;
  92. case 1:
  93. // your primitive objects here
  94. break;
  95. case 2:
  96. drawMyCreation();
  97. break;
  98. case 3:
  99.  
  100. // your another compsite object here
  101. break;
  102. default:
  103. break;
  104. };
  105. }
Add Comment
Please, Sign In to add comment