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.

506 lines
12 KiB

5 years ago
  1. #include "scene.h"
  2. #include "utils.h"
  3. #include "skybox.h"
  4. #include "model.h"
  5. #include "ground.h"
  6. #include "light.h"
  7. #include "camera.h"
  8. GLuint texture;
  9. SkyBox skybox;
  10. Model model;
  11. Ground ground;
  12. DirectionLight light(GL_LIGHT0);
  13. PointLight light1(GL_LIGHT1);
  14. PointLight light2(GL_LIGHT2);
  15. SpotLight light3(GL_LIGHT3);
  16. Camera camera;
  17. void Init() {
  18. glMatrixMode(GL_PROJECTION);
  19. gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
  20. glMatrixMode(GL_MODELVIEW);
  21. glLoadIdentity();
  22. texture = CreateTexture2DFromBMP("Res/earth.bmp");
  23. skybox.Init("Res/");
  24. model.Init("Res/Sphere.obj");
  25. model.mTexture = CreateTexture2DFromBMP("Res/earth.bmp");
  26. light.SetAmbientColor(0.1f, 0.1f, 0.1f, 1.0f);
  27. light.SetDiffuseColor(0.8f, 0.8f, 0.8f, 1.0f);
  28. light.SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
  29. light.SetPosition(0.0f, 1.0f, 0.0f);
  30. light1.SetAmbientColor(0.1f,0.1f,0.1f,1.0f);
  31. light1.SetDiffuseColor(0.8f,0.8f,0.8f,1.0f);
  32. light1.SetSpecularColor(1.0f,1.0f,1.0f,1.0f);
  33. light1.SetPosition(0.0f,0.0f,0.0f);//�ѵ���Դ�ŵ�������λ��
  34. light1.SetConstAttenuation(1.0f);
  35. light1.SetLinearAttenuation(0.2f);
  36. light2.SetAmbientColor(0.1f, 0.1f, 0.1f, 1.0f);
  37. light2.SetDiffuseColor(0.1f, 0.4f, 0.6f, 1.0f);
  38. light2.SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
  39. light2.SetPosition(0.0f, 0.0f, -30.0f);//�ѵ���Դ�ŵ�������λ��
  40. light2.SetConstAttenuation(1.0f);
  41. light2.SetLinearAttenuation(0.2f);
  42. light3.SetAmbientColor(0.1f, 0.1f, 0.1f, 1.0f);
  43. light3.SetDiffuseColor(0.0f, 0.8f, 0.0f, 1.0f);
  44. light3.SetSpecularColor(1.0f,0.0f,0.0f,1.0f);
  45. light3.SetPosition(10.0f,50.0f,-30.0f);
  46. light3.SetDirection(0.0,-1.0f,0.0f);//����
  47. light3.SetExpone(5.0f);//���þ۹��� 5��
  48. light3.SetCutoff(10.0f);//�������䷶Χ 10��
  49. model.SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.0f);
  50. model.SetDiffuseMaterial(0.4f, 0.4f, 0.4f, 1.0f);
  51. model.SetSpecularMaterial(1.0f, 1.0f, 1.0f, 1.0f);
  52. ground.SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.0f);
  53. ground.SetDiffuseMaterial(0.4f, 0.4f, 0.4f, 1.0f);
  54. ground.SetSpecularMaterial(0.0f, 0.0f, 0.0f, 1.0f);
  55. camera.mViewportWidth = 800.0f;
  56. camera.mViewportHeight = 600.0f;
  57. }
  58. void DrawMutipl() {
  59. glClearColor(0, 0, 0, 0);
  60. glClear(GL_COLOR_BUFFER_BIT);
  61. glLoadIdentity();
  62. glPushMatrix();
  63. glTranslatef(0.0f, 0.0f, -5.0f);
  64. static float r = 0;
  65. glPushMatrix();
  66. glRotatef(r, 0.0f, 1.0f, 0.0f);
  67. r += 0.1f;
  68. glBegin(GL_TRIANGLES);
  69. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  70. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  71. glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, -2.5f);
  72. glEnd();
  73. glPopMatrix();
  74. glPopMatrix();
  75. }
  76. void DrawPushMatrix() {
  77. glClearColor(0, 0, 0, 0);
  78. glClear(GL_COLOR_BUFFER_BIT);
  79. glLoadIdentity();//����ģ���ӿھ���Ϊ��λ����
  80. glPushMatrix();
  81. glTranslatef(-1.0f, 0.0f, 0.0f);
  82. glBegin(GL_TRIANGLES);
  83. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  84. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  85. glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, -2.5f);
  86. glEnd();
  87. glPopMatrix();
  88. glPushMatrix();
  89. glTranslatef(1.0f, 0.0f, 0.0f);
  90. glBegin(GL_TRIANGLES);
  91. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  92. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  93. glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, -2.5f);
  94. glEnd();
  95. glPopMatrix();
  96. }
  97. void DrawRoated() {
  98. glClearColor(30.0f / 255.0f, 30.0f / 255.0f, 30.0f / 255.0f, 1.0f);
  99. glClear(GL_COLOR_BUFFER_BIT);
  100. static float r = 0.0f;
  101. glLoadIdentity();
  102. glRotated(r, 0, 0, 1);// ���Ƕȣ�x,y,z��
  103. r += 0.1f;
  104. glBegin(GL_TRIANGLES);
  105. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  106. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  107. glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, -2.5f);
  108. glEnd();
  109. }
  110. void DrawTranslate() {
  111. glClearColor(30.0f / 255.0f, 30.0f / 255.0f, 30.0f / 255.0f, 1.0f);
  112. glClear(GL_COLOR_BUFFER_BIT);
  113. glLoadIdentity();
  114. glTranslatef(0, 0, -2.5f);
  115. glBegin(GL_TRIANGLES);
  116. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, 0.0f);
  117. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, 0.0f);
  118. glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, 0.0f);
  119. glEnd();
  120. }
  121. void DrawPolygon() {
  122. glClearColor(0, 0, 0, 0);
  123. glClear(GL_COLOR_BUFFER_BIT);
  124. glBegin(GL_POLYGON);
  125. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  126. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25, -2.5f);
  127. glColor4ub(0, 255, 0, 255); glVertex3f(1.0f, 0.25f, -2.5f);
  128. glColor4ub(255, 0, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
  129. glColor4ub(0, 255, 0, 255); glVertex3f(-0.1f, 0.1f, -2.5f);
  130. glEnd();
  131. }
  132. void DrawQuadStrip() {
  133. glClearColor(0, 0, 0, 0);
  134. glClear(GL_COLOR_BUFFER_BIT);
  135. glBegin(GL_QUAD_STRIP);
  136. glColor4ub(255, 0, 0, 255); glVertex3f(-0.25f, -0.25f, -2.5f);
  137. glColor4ub(0, 0, 255, 255); glVertex3f(0.25f, -0.25f, -2.5f);
  138. glColor4ub(255, 0, 0, 255); glVertex3f(-0.25f, 0.25f, -2.5f);
  139. glColor4ub(0, 255, 0, 255); glVertex3f(0.25, 0.25, -2.5f);
  140. glColor4ub(255, 0, 0, 255); glVertex3f(-0.25f, 0.5f, -2.5f);
  141. glColor4ub(0, 255, 0, 255); glVertex3f(0.25, 0.5, -2.5f);
  142. glEnd();
  143. }
  144. void DrawQuads() {
  145. glClearColor(0, 0, 0, 0);
  146. glClear(GL_COLOR_BUFFER_BIT);
  147. glBegin(GL_QUADS);
  148. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  149. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  150. glColor4ub(0, 255, 0, 255); glVertex3f(1.0f, 0.5f, -2.5f);
  151. glColor4ub(255, 0, 0, 255); glVertex3f(0.5f, 0.5f, -2.5f);
  152. glEnd();
  153. }
  154. void DrawLineStrip() {
  155. glClearColor(0, 0, 0, 0);
  156. glClear(GL_COLOR_BUFFER_BIT);
  157. glLineWidth(10.0f);
  158. glBegin(GL_LINE_STRIP);
  159. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  160. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  161. glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
  162. glEnd();
  163. }
  164. void DrawLineLoop() {
  165. glClearColor(0, 0, 0, 0);
  166. glClear(GL_COLOR_BUFFER_BIT);
  167. glLineWidth(10.0f);
  168. glBegin(GL_LINE_LOOP);
  169. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  170. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  171. glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
  172. glEnd();
  173. }
  174. void DrawLines() {
  175. glClearColor(0, 0, 0, 0);
  176. glClear(GL_COLOR_BUFFER_BIT);
  177. glLineWidth(10.0f);
  178. glBegin(GL_LINES);
  179. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  180. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  181. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  182. glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
  183. glEnd();
  184. }
  185. void DrawPoints() {
  186. glClearColor(0, 0, 0, 0);
  187. glClear(GL_COLOR_BUFFER_BIT);
  188. glPointSize(10.0f);
  189. glBegin(GL_POINTS);
  190. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  191. glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  192. glEnd();
  193. }
  194. void DrawTriangleFan() {
  195. glClearColor(0, 0, 0, 0);
  196. glClear(GL_COLOR_BUFFER_BIT);
  197. glBegin(GL_TRIANGLE_FAN);
  198. glColor4ub(255, 0, 0, 255); glVertex3f(0.0f, -0.25f, -2.5f);
  199. glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, -0.25f, -2.5f);
  200. glColor4ub(0, 0, 255, 255); glVertex3f(0.4f, 0.0f, -2.5f);
  201. glColor4ub(0, 0, 255, 255); glVertex3f(0.2f, 0.15f, -2.5f);
  202. glColor4ub(0, 0, 255, 255); glVertex3f(0.0f, 0.2f, -2.5f);
  203. glEnd();
  204. }
  205. void DrawTriangleStrip() {
  206. glClearColor(0, 0, 0, 0);
  207. glClear(GL_COLOR_BUFFER_BIT);
  208. glBegin(GL_TRIANGLE_STRIP);
  209. glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
  210. glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25, -2.5f);
  211. glColor4ub(0, 255, 0, 255); glVertex3f(-0.5f, 0.25f, -2.5f);
  212. glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
  213. glEnd();
  214. }
  215. void DrawTriangles() {
  216. glClearColor(0, 0, 0, 0);
  217. glClear(GL_COLOR_BUFFER_BIT);
  218. glBegin(GL_TRIANGLES);
  219. glColor4ub(255, 255, 255, 255);
  220. glVertex3f(-0.2f, -0.2f, -1.5f);
  221. glColor4ub(255, 0, 0, 255);
  222. glVertex3f(0.2f, -0.2f, -1.5f);
  223. glColor4ub(0, 255, 0, 255);
  224. glVertex3f(0.0f, 0.2f, -1.5f);
  225. glEnd();
  226. }
  227. void DrawEnableLight() {
  228. glClearColor(30.0f / 255.0f, 30.0f / 255.0f, 30.0f / 255.0f, 1.0f);
  229. glClear(GL_COLOR_BUFFER_BIT);
  230. glEnable(GL_LIGHTING);
  231. glEnable(GL_LIGHT0);
  232. glEnable(GL_CULL_FACE);//????
  233. float lightPos[] = { 0.0f,1.0f,0.0f,0.0f };
  234. glLightfv(GL_LIGHT0, GL_POSITION, lightPos);//���÷�������2λ��
  235. float whiteColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  236. glLightfv(GL_LIGHT0, GL_AMBIENT, whiteColor);//���û�����
  237. float ambientMat[] = { 0.07f,0.07f, 0.07f, 1.0f };//���������������ʵķ���ϵ��
  238. glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMat);
  239. float diffuseMat[] = { 0.4f,0.4f,0.4f,1.0f };//����������ϵ��
  240. glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMat);
  241. float specularMat[] = { 0.9f,0.9f,0.9f,1.0f };//���þ��淴��ϵ��
  242. glMaterialfv(GL_FRONT, GL_SPECULAR, specularMat);
  243. glBegin(GL_QUADS);
  244. glColor4ub(255, 255, 255, 255);
  245. glVertex3f(-0.5f, -0.2f, -0.5f);
  246. glVertex3f(0.5f, -0.2f, -0.5f);
  247. glVertex3f(0.5f, -0.2f, -1.0f);
  248. glVertex3f(-0.5f, -0.2f, -1.0f);
  249. glEnd();
  250. }
  251. void DrawTexture() {
  252. glClearColor(0.1f, 0.4f, 0.6f, 1.0f);
  253. glClear(GL_COLOR_BUFFER_BIT);
  254. //��������
  255. glEnable(GL_TEXTURE_2D);
  256. //��������
  257. glBindTexture(GL_TEXTURE_2D, texture);
  258. glBegin(GL_QUADS);
  259. glColor4ub(255, 255, 255, 255);
  260. glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.1f, -0.1f, -0.4f);
  261. glTexCoord2f(1.0f, 0.0f); glVertex3f(0.1f, -0.1f, -0.4f);
  262. glTexCoord2f(1.0f, 1.0f); glVertex3f(0.1f, 0.1f, -0.4f);
  263. glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.1f, 0.1f, -0.4f);
  264. glEnd();
  265. }
  266. void DrawDepthBk() {
  267. glClearColor(0, 0, 0, 0);
  268. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//�������Ȼ���������ɫ������
  269. glEnable(GL_DEPTH_TEST);//�������Ȳ���
  270. glBegin(GL_QUADS);
  271. glColor4ub(255, 0, 0, 0);
  272. glVertex3f(-0.1f, -0.1f, -0.4f);
  273. glVertex3f(0.1f, -0.1f, -0.4f);
  274. glVertex3f(0.1f, 0.1f, -0.4f);
  275. glVertex3f(-0.1f, 0.1f, -0.4f);
  276. glEnd();
  277. glBegin(GL_QUADS);
  278. glColor4ub(0, 255, 0, 0);
  279. glVertex3f(-0.1f, -0.1f, -0.6f);
  280. glVertex3f(0.1f, -0.1f, -0.6f);
  281. glVertex3f(0.1f, 0.1f, -0.6f);
  282. glVertex3f(-0.1f, 0.1f, -0.6f);
  283. glEnd();
  284. }
  285. void DrawDepth() {
  286. glClearColor(0, 0, 0, 0);
  287. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//�������Ȼ���������ɫ������
  288. //glEnable(GL_DEPTH_TEST);//�������Ȳ���
  289. GLuint texture2;
  290. texture2 = CreateTexture2DFromBMP("Res/test.bmp");
  291. glEnable(GL_TEXTURE_2D);
  292. glBindTexture(GL_TEXTURE_2D, texture2);
  293. glBegin(GL_QUADS);
  294. glColor4ub(255, 255, 255, 255);
  295. glTexCoord2d(0.0f, 0.0f); glVertex3f(-0.1f, -0.1f, -0.4f);
  296. glTexCoord2d(1.0f, 0.0f); glVertex3f(0.1f, -0.1f, -0.4f);
  297. glTexCoord2d(1.0f, 1.0f); glVertex3f(0.1f, 0.1f, -0.4f);
  298. glTexCoord2d(0.0f, 1.0f); glVertex3f(-0.1f, 0.1f, -0.4f);
  299. glEnd();
  300. glBindTexture(GL_TEXTURE_2D, 0);
  301. glBegin(GL_QUADS);
  302. glColor4ub(0, 255, 0, 0);
  303. glVertex3f(-0.1f, -0.1f, -0.6f);
  304. glVertex3f(0.1f, -0.1f, -0.6f);
  305. glVertex3f(0.1f, 0.1f, -0.6f);
  306. glVertex3f(-0.1f, 0.1f, -0.6f);
  307. glEnd();
  308. }
  309. void DrawSkyBox() {
  310. glClearColor(0, 0, 0, 0);
  311. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//�������Ȼ���������ɫ������
  312. skybox.Draw();
  313. glEnable(GL_DEPTH_TEST);//�������Ȳ���
  314. glBindTexture(GL_TEXTURE_2D, 0);
  315. glBegin(GL_QUADS);
  316. glColor4ub(0, 255, 0, 0);
  317. glVertex3f(-0.1f, -0.1f, -0.6f);
  318. glVertex3f(0.1f, -0.1f, -0.6f);
  319. glVertex3f(0.1f, 0.1f, -0.6f);
  320. glVertex3f(-0.1f, 0.1f, -0.6f);
  321. glEnd();
  322. }
  323. void DrawModel() {
  324. glClearColor(0, 0, 0, 0);
  325. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//�������Ȼ���������ɫ������
  326. light.Enable();
  327. skybox.Draw();
  328. model.Draw();
  329. ground.Draw();
  330. }
  331. void DrawLight() {
  332. glClearColor(0, 0, 0, 0);
  333. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//�������Ȼ���������ɫ������
  334. light1.Enable();
  335. light2.Enable();
  336. light3.Enable();
  337. ground.Draw();
  338. }
  339. void DrawCamera() {
  340. glClearColor(0, 0, 0, 0);
  341. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//�������Ȼ���������ɫ������
  342. float frameTime = GetFrameTime();
  343. camera.Update(frameTime);
  344. light1.Enable();
  345. light1.Update(camera.mPos.x, camera.mPos.y, camera.mPos.z);
  346. light2.Enable();
  347. light2.Update(camera.mPos.x, camera.mPos.y, camera.mPos.z);
  348. light3.Enable();
  349. light3.Update(camera.mPos.x, camera.mPos.y, camera.mPos.z);
  350. ground.Draw();
  351. }
  352. void Draw() {
  353. DrawCamera();
  354. }
  355. void OnKeyDown(char code)
  356. {
  357. switch (code) {
  358. case 'A':
  359. camera.mbMoveLeft = true;
  360. break;
  361. case 'D':
  362. camera.mbMoveRight = true;
  363. break;
  364. case 'W':
  365. camera.mbMoveForward = true;
  366. break;
  367. case 'S':
  368. camera.mbMoveBack = true;
  369. break;
  370. }
  371. }
  372. void OnKeyUp(char code)
  373. {
  374. switch (code) {
  375. case 'A':
  376. camera.mbMoveLeft = false;
  377. break;
  378. case 'D':
  379. camera.mbMoveRight = false;
  380. break;
  381. case 'W':
  382. camera.mbMoveForward = false;
  383. break;
  384. case 'S':
  385. camera.mbMoveBack = false;
  386. break;
  387. }
  388. }
  389. void OnMouseMove(float deltaX, float deltaY)
  390. {
  391. float angleRotateByUp = deltaX / 1000.0f;
  392. float angleRotateByRight = deltaY / 1000.0f;
  393. camera.Yaw(-angleRotateByUp);
  394. camera.Picth(-angleRotateByRight);
  395. }