bsipic.cpp
上传用户:yunyi28
上传日期:2021-04-25
资源大小:6978k
文件大小:9k
源码类别:

分形几何

开发平台:

Visual C++

  1. #include "math.h"
  2. #include "bsipic.h"
  3. #include "stdio.h"
  4. #include "Tree.h"
  5. #include <list>
  6. #include "Data.h"
  7. #define  PI 3.14159
  8. float rtri;
  9. float rquad;
  10. GLfloat xrot;
  11. GLfloat yrot;
  12. GLfloat zyot;
  13. bsipic::bsipic(void)
  14. {
  15. }
  16. bsipic::~bsipic(void)
  17. {
  18. }
  19. int bsipic::Pbmp()
  20. {
  21. glEnable(GL_TEXTURE_2D);
  22. glBindTexture(GL_TEXTURE_2D,texture[0]);
  23. // glShadeModel(GL_SMOOTH);
  24. glClearColor(0.0f,0.0f,0.0f,0.0f);
  25. glClearDepth(1.0f);
  26. glEnable(GL_DEPTH_TEST);
  27. glDepthFunc(GL_LEQUAL);
  28. glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
  29. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  30. glLoadIdentity();
  31. glTranslatef(1.5f,0.0f,-7.0f);
  32. glRotatef(rquad,1.0f,0.0f,0.0f);
  33. glBegin(GL_QUADS);
  34. // glColor3f(0.0f,1.0f,0.0f);
  35. glTexCoord2f(0.0f,0.0f);
  36. glVertex3f(1.0f,1.0f,-1.0f);
  37. glTexCoord2f(1.0f,0.0f);
  38. glVertex3f(-1.0f,1.0f,-1.0f);//上
  39. glTexCoord2f(1.0f,1.0f);
  40. glVertex3f(-1.0f,1.0f,1.0f);
  41. glTexCoord2f(0.0f,1.0f);
  42. glVertex3f(1.0f,1.0f,1.0f);
  43. glTexCoord2f(0.0f,0.0f);
  44. glVertex3f(1.0f,-1.0f,1.0f);
  45. glTexCoord2f(1.0f,0.0f);
  46. glVertex3f(-1.0f,-1.0f,1.0f);//下
  47. glTexCoord2f(1.0f,1.0f);
  48. glVertex3f(-1.0f,-1.0f,-1.0f);
  49. glTexCoord2f(0.0f,1.0f);
  50. glVertex3f(1.0f,-1.0f,-1.0f);
  51. glTexCoord2f(0.0f,0.0f);
  52. glVertex3f(1.0f,1.0f,1.0f);
  53. glTexCoord2f(0.5f,0.0f);
  54. glVertex3f(-1.0f,1.0f,1.0f);//前
  55. glTexCoord2f(0.5f,1.0f);
  56. glVertex3f(-1.0f,-1.0f,1.0f);
  57. glTexCoord2f(0.0f,1.0f);
  58. glVertex3f(1.0f,-1.0f,1.0f);
  59. glTexCoord2f(0.0f,0.0f);
  60. glVertex3f(1.0f,-1.0f,-1.0f);
  61. glTexCoord2f(1.0f,0.0f);
  62. glVertex3f(-1.0f,-1.0f,-1.0f);//后
  63. glTexCoord2f(1.0f,1.0f);
  64. glVertex3f(-1.0f,1.0f,-1.0f);
  65. glTexCoord2f(0.0f,1.0f);
  66. glVertex3f(1.0f,1.0f,-1.0f);
  67. glTexCoord2f(0.0f,0.0f);
  68. glVertex3f(-1.0f,1.0f,1.0f);
  69. glTexCoord2f(1.0f,0.0f);
  70. glVertex3f(-1.0f,1.0f,-1.0f);//左
  71. glTexCoord2f(1.0f,1.0f);
  72. glVertex3f(-1.0f,-1.0f,-1.0f);
  73. glTexCoord2f(0.0f,1.0f);
  74. glVertex3f(-1.0f,-1.0f,1.0f);
  75. glTexCoord2f(0.0f,0.0f);
  76. glVertex3f(1.0f,1.0f,-1.0f);
  77. glTexCoord2f(1.0f,0.0f);
  78. glVertex3f(1.0f,1.0f,1.0f);//右
  79. glTexCoord2f(1.0f,1.0f);
  80. glVertex3f(1.0f,-1.0f,1.0f);
  81. glTexCoord2f(0.0f,1.0f);
  82. glVertex3f(1.0f,-1.0f,-1.0f);
  83. glEnd();
  84. rtri+=0.2f;
  85. rquad-=0.15f;
  86. return TRUE;
  87. }
  88. void bsipic::Park()
  89. {
  90. glColor3f(0,0,0);
  91. glPushMatrix();
  92. glTranslatef(-5,-4,-13);
  93. glBegin(GL_TRIANGLE_FAN);
  94. glVertex3f(0,0,0.0f);
  95. for(int i=0;i<=360;i+=5)
  96. {
  97. float p=(float)(i*3.14/180);
  98. glVertex3f((float)sin(p),(float)cos(p),0.0f);
  99. }
  100. glEnd();
  101. glPopMatrix();
  102. }
  103. void bsipic::airplane(float x,float y,float z,int r)
  104. {
  105. glPushMatrix();
  106. glTranslatef(x,y,z);
  107. glRotatef(-r,0.0,1.0,0.0);
  108. glColor3f(1.0,1.0,1.0);
  109. //螺旋桨
  110. glColor3f(0,1,0);
  111. auxSolidBox(1.6,0.3,0.05);
  112. //机头
  113. glColor3f(0.1,0.3,0.4);
  114. glTranslatef(0.0,0.0,-0.5);
  115. auxSolidSphere(0.5);
  116. //机身
  117. glColor3f(0.7,0.2,0.4);
  118. glRotatef(90,1.0,0.0,0.0);
  119. glTranslatef(0.0f,-1.0f,0);
  120. auxSolidCylinder(.4f,2.0);
  121. //机尾
  122. glColor3f(0.1,0.5,0.7);
  123. glRotatef(-270,1.0,0.0,0.0);
  124. glTranslatef(0.0f,-0.0f,1.0f);
  125. auxSolidCone(.4,1.5);
  126. //后翼
  127. glColor3f(1,1,1);
  128. glTranslatef(0.0f,-0.5f,1.2f);
  129. auxSolidBox(3,0.05f,0.5f);
  130. glTranslatef(0.0f,-0.0f,0.0f);
  131. auxSolidBox(0.05,1.0f,0.60f);
  132. //前翼
  133. glColor3f(0.3,0.4,0.4);
  134. glTranslatef(0.0f,0.7f,-1.9f);
  135. auxSolidBox(8,0.05f,0.7f);
  136. glPopMatrix();
  137. }
  138. void bsipic::TriAndQuad()
  139. {
  140. glClearColor(0.0f,0.0f,0.0f,0.0f);
  141. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  142. glLoadIdentity();
  143. glTranslatef(-1.5f,0.0f,-6.0f);
  144. glRotatef(rtri,0.0f,1.0f,0.0f);
  145. glBegin(GL_TRIANGLES);
  146. glColor3f(1.0f,0.0f,0.0f);
  147. glVertex3f(0.0f,1.0f,0.0f);
  148. glColor3f(0.0f,1.0f,0.0f);
  149. glVertex3f(1.0f,0.0f,0.0f);
  150. glColor3f(0.0f,0.0f,1.0f);
  151. glVertex3f(-1.0f,0.0f,0.0f);
  152. glEnd();
  153. glLoadIdentity();//重置模型观察矩阵
  154. glTranslatef(0.0f,0.0f,-7.0f);
  155. glRotatef(rquad,1.0f,0.0f,0.0f);
  156. glColor3f(0.5f,0.5f,1.0f);
  157. glBegin(GL_QUADS);
  158. glVertex3f(-1.0f,1.0f,0.0f);
  159. glVertex3f(1.0f,1.0f,0.0f);
  160. glVertex3f(1.0f,-1.0f,0.0f);
  161. glVertex3f(-1.0f,-1.0f,0.0f);
  162. glEnd();
  163. rtri+=0.2f;
  164. rquad-=0.15f;
  165. }
  166. void bsipic::TriAndQuad_3D()
  167. {
  168. glClearColor(0.0f,0.0f,0.0f,0.0f);
  169. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  170. glLoadIdentity();
  171. glTranslatef(-1.5f,0.0f,-6.0f);
  172. glRotatef(rtri,0.0f,1.0f,0.0f);
  173. glBegin(GL_TRIANGLES);
  174. glColor3f(1.0f,0.0f,0.0f);
  175. glVertex3f(0.0f,1.0f,0.0f);
  176. glColor3f(0.0f,1.0f,0.0f);
  177. glVertex3f(-1.0f,-1.0f,1.0f);
  178. glColor3f(0.0f,0.0f,1.0f);
  179. glVertex3f(1.0f,-1.0f,1.0f);
  180. glColor3f(1.0f,0.0f,0.0f);
  181. glVertex3f(0.0f,1.0f,0.0f);
  182. glColor3f(0.0f,0.0f,1.0f);
  183. glVertex3f(1.0f,-1.0f,1.0f);
  184. glColor3f(0.0f,1.0f,0.0f);
  185. glVertex3f(0.0f,-1.0f,-1.0f);
  186. glColor3f(1.0f,0.0f,0.0f);
  187. glVertex3f(0.0f,1.0f,0.0f);
  188. glColor3f(0.0f,1.0f,-1.0f);
  189. glVertex3f(0.0f,-1.0f,-1.0f);
  190. glColor3f(0.0f,0.0f,1.0f);
  191. glVertex3f(-1.0f,-1.0f,1.0f);
  192. glColor3f(1.0f,0.0f,0.0f);
  193. glVertex3f(0.0f,-1.0f,-1.0f);
  194. glColor3f(0.0f,0.0f,1.0f);
  195. glVertex3f(1.0f,-1.0f,1.0f);
  196. glColor3f(0.0f,1.0f,0.0f);
  197. glVertex3f(-1.0f,-1.0f,1.0f);
  198. glEnd();
  199. glLoadIdentity();//重置模型观察矩阵
  200. glTranslatef(1.5f,0.0f,-7.0f);
  201. glRotatef(rquad,1.0f,0.0f,0.0f);
  202. glBegin(GL_QUADS);
  203. glColor3f(0.0f,1.0f,0.0f);
  204. glVertex3f(1.0f,1.0f,-1.0f);
  205. glVertex3f(-1.0f,1.0f,-1.0f);
  206. glVertex3f(-1.0f,1.0f,1.0f);
  207. glVertex3f(1.0f,1.0f,1.0f);
  208. glColor3f(1.0f,0.5f,0.0f);
  209. glVertex3f(1.0f,-1.0f,1.0f);
  210. glVertex3f(-1.0f,-1.0f,1.0f);
  211. glVertex3f(-1.0f,-1.0f,-1.0f);
  212. glVertex3f(1.0f,-1.0f,-1.0f);
  213. glColor3f(1.0f,0.0f,0.0f);
  214. glVertex3f(1.0f,1.0f,1.0f);
  215. glVertex3f(-1.0f,1.0f,1.0f);
  216. glVertex3f(-1.0f,-1.0f,1.0f);
  217. glVertex3f(1.0f,-1.0f,1.0f);
  218. glColor3f(1.0f,1.0f,0.0f);
  219. glVertex3f(1.0f,-1.0f,-1.0f);
  220. glVertex3f(-1.0f,-1.0f,-1.0f);
  221. glVertex3f(-1.0f,1.0f,-1.0f);
  222. glVertex3f(1.0f,1.0f,-1.0f);
  223. glColor3f(0.0f,0.0f,1.0f);
  224. glVertex3f(-1.0f,1.0f,1.0f);
  225. glVertex3f(-1.0f,1.0f,-1.0f);
  226. glVertex3f(-1.0f,-1.0f,-1.0f);
  227. glVertex3f(-1.0f,-1.0f,1.0f);
  228. glColor3f(1.0f,0.0f,1.0f);
  229. glVertex3f(1.0f,1.0f,-1.0f);
  230. glVertex3f(1.0f,1.0f,1.0f);
  231. glVertex3f(1.0f,-1.0f,1.0f);
  232. glVertex3f(1.0f,-1.0f,-1.0f);
  233. glEnd();
  234. ;
  235. rtri+=0.2f;
  236. rquad-=0.15f;
  237. }
  238. void bsipic::StencilQuads()
  239. {
  240. // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |  GL_STENCIL_BUFFER_BIT);
  241. glPushMatrix();
  242. glColor3f(1.0f,1.0f,0.0f);
  243. glEnable(GL_STENCIL_TEST);
  244. glStencilFunc(GL_ALWAYS , 5 ,1);
  245. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  246. glLoadIdentity();
  247. glTranslatef(0.0f,2.0f,-90.0f);
  248. glBegin(GL_QUADS);
  249. glVertex3f(-10.0f,10.0f,0.0f);
  250. glVertex3f(10.0f,10.0f,0.0f);
  251. glVertex3f(10.0f,-10.0f,0.0f);
  252. glVertex3f(-10.0f,-10.0f,0.0f);
  253. glEnd();
  254. glDisable(GL_STENCIL_TEST);
  255. glPopMatrix();
  256. }
  257. void bsipic::Trees()
  258. {
  259. glEnable(GL_TEXTURE_2D);
  260. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
  261. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |  GL_STENCIL_BUFFER_BIT); // Clear Screen And Depth Buffer
  262. glClearColor(1.0,1.0,1.0,0.5);
  263. glClearDepth(-50.0f);
  264. glEnable(GL_BLEND);
  265. glDisable(GL_DEPTH_TEST);
  266. glLoadIdentity();
  267. gluLookAt(
  268. 0.0f,0.0f,200.0f,
  269. 0.0f,0.0f,0.0f,
  270. 0.0f,1.0f,0.0f);
  271. //设置观察点,目标点,向上方向
  272. glTranslatef(0.0f,-50.0f,-10.0f);
  273. glRotatef(rquad,0.0f,1.0f,0.0f);
  274. rquad-=pause*0.15f;
  275. list<Tree>::iterator i = trees.begin();
  276. int numtriangles = 0;
  277. // StencilQuads();
  278. // glEnable(GL_STENCIL_TEST);
  279. glStencilFunc(GL_EQUAL ,7 ,1);
  280. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  281. while(i!=trees.end())  //end()函数什么意思
  282. {
  283. numtriangles+=i->RenderTree();
  284. i++;
  285. }
  286. // numtriangles+=i->RenderTree();
  287. // glDisable(GL_STENCIL_TEST);
  288. glDisable(GL_BLEND);
  289. glEnable(GL_DEPTH_TEST);
  290. }
  291. void bsipic::IsPlane()
  292. {
  293. Vertex pt[4];
  294. float x = 0.0f,y = 0.0f,z = 0.0f;
  295. // float sqrt2s = sqrt(2.0f);
  296. float salpha = sin(45.0/180*PI);
  297. float calpha = cos(45.0/180*PI);
  298. float cbeta = cos(45.0/180*PI);
  299. float sbeta = sin(45.0/180*PI);
  300. float sqrt3s = sqrt(2.0f);
  301. float sqrt2s = 4;
  302. pt[0].x = x;
  303. pt[0].y = y;
  304. pt[0].z = z;
  305. pt[2].x = x+sqrt2s*salpha*cbeta;
  306. pt[2].y = y+sqrt2s*calpha;
  307. pt[2].z = z+sqrt2s*salpha*sbeta;
  308. pt[1].x = x+sqrt2s*salpha*cos((0)* PI /180)/2;//(cbeta*sqrt2+sbeta*sqrt2)/2;
  309. pt[1].y = y/2+pt[2].y/2;
  310. pt[1].z = z+sqrt2s*salpha*sin((0)* PI /180)/2;//(sbeta*sqrt2-cbeta*sqrt2)/2;
  311. pt[3].x = x+sqrt2s*salpha*cos((90)* PI /180)/2;//(cbeta*sqrt2-sbeta*sqrt2)/2;
  312. pt[3].y = pt[1].y;
  313. pt[3].z = z+sqrt2s*salpha*sin((90)* PI /180)/2;//(sbeta*sqrt2+cbeta*sqrt2)/2;
  314. glEnable(GL_TEXTURE_2D);
  315. glBindTexture(GL_TEXTURE_2D,texture[0]);
  316. // glShadeModel(GL_SMOOTH);
  317. glClearColor(0.0f,0.0f,0.0f,0.0f);
  318. // glClearDepth(1.0f);
  319. // glEnable(GL_DEPTH_TEST);
  320. // glDepthFunc(GL_LEQUAL);
  321. glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
  322. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  323. glLoadIdentity();
  324. gluLookAt(5.0f,0.0f,5.0f,0.0f,sqrt2s/2,0.0f,0.0f,1.0f,0.0f);
  325. glTranslatef(0.0f,0.0f,0.0f);
  326. glBegin(GL_QUADS);
  327. glTexCoord2f(0.0f,0.0f);
  328. glVertex3f(pt[0].x,pt[0].y,pt[0].z);
  329. glTexCoord2f(0.0f,1.0f);
  330. glVertex3f(pt[3].x,pt[3].y,pt[3].z);
  331. glTexCoord2f(1.0f,1.0f);
  332. glVertex3f(pt[2].x,pt[2].y,pt[2].z);
  333. glTexCoord2f(1.0f,0.0f);
  334. glVertex3f(pt[1].x,pt[1].y,pt[1].z);//上
  335. glEnd();
  336. }