[关闭]
@Pigmon 2016-12-01T12:06:27.000000Z 字数 23307 阅读 127

图形学参考大作业2

上课


如果不知道怎么整合到cgcourse-examples里

老师的Cgcourse-examples环境的配置方法 (pdf)
链接:http://pan.baidu.com/s/1jI2ToUE 密码:yxea

拼凑大作业指南+参考大作业1指南
https://www.zybuluo.com/Pigmon/note/582224

说明

参考大作业2,即整合Nehe Lesson10 场景漫游 + 环境贴图的那个。

修改建议:

  • 改场景贴图,地板,天花板分别是场景模型的前2个,和第3-4个三角形
  • 修改场景地形,在world.txt里面
  • 加几个光源
  • 加天空盒,或者背景贴图
  • 把手动移动环境球,变成自动按顺序走动

后面是程序和 world.txt。world.txt选好存放位置后,不要忘了修改程序里的路径
如果程序执行闪退看看是不是贴图或world.txt的路径不对。

程序

仔细看中文注释
我也不会,所以很粗糙

  1. /*
  2. -----------------------------------------------------------------------
  3. This source file is part of "cgcourse-examples"
  4. (Examples for Computer Graphics Course of CCCE GUCAS.)
  5. Copyright (C) 2011 Xue Jian (jian.xue.cn@gmail.com)
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. -----------------------------------------------------------------------
  17. */
  18. /*
  19. NeHe (nehe.gamedev.net) OpenGL tutorial series
  20. GLUT port.in 2001 by milix (milix_gr@hotmail.com)
  21. Most comments are from the original tutorials found in NeHe.
  22. For VC++ users, create a Win32 Console project and link
  23. the program with glut32.lib, glu32.lib, opengl32.lib
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h> // Standard C/C++ Input-Output
  27. #include <math.h> // Math Functions (NEW)
  28. #include <windows.h> // Standard Header For MSWindows Applications
  29. #include <gl/glut.h> // The GL Utility Toolkit (GLUT) Header
  30. #include <GL/glext.h>
  31. #include <iostream>
  32. #include "glinc.h"
  33. #include "trackball.h"
  34. #include "quaternion.h"
  35. #include "camera.h"
  36. #include "FreeImage.h"
  37. #include "mitkBMPReader.h"
  38. #include "mitkVolume.h"
  39. #include "mitkPLYReader.h"
  40. #include "mitkTriangleMesh.h"
  41. void display();
  42. // -----------------------------------------------------------------------
  43. // Nehe
  44. // -----------------------------------------------------------------------
  45. #define TEXTURES_NUM 3 // We Have 3 Textures
  46. #define PI_OVER_180 0.0174532925f
  47. typedef struct _VERTEX {
  48. float x, y, z;
  49. float u, v;
  50. } VERTEX;
  51. typedef struct _TRIANGLE {
  52. VERTEX vertex[3];
  53. } TRIANGLE;
  54. typedef struct _SECTOR {
  55. int numtriangles;
  56. TRIANGLE* triangle;
  57. } SECTOR;
  58. // Global Variables
  59. bool g_gamemode; // GLUT GameMode ON/OFF
  60. bool g_fullscreen; // Fullscreen Mode ON/OFF (When g_gamemode Is OFF)
  61. bool g_blend = false; // Blending ON/OFF
  62. GLfloat g_xpos = 0.f; // Position In X-Axis
  63. GLfloat g_zpos = 0.f; // Position In Z-Axis
  64. GLfloat g_yrot = 0.0f; // Y Rotation
  65. GLfloat g_xspeed = 0.0f; // X Rotation Speed
  66. GLfloat g_yspeed = 0.0f; // Y Rotation Speed
  67. GLfloat g_z = 2.0f; // Depth Into The Screen
  68. GLfloat g_heading = 0.0f;
  69. GLfloat g_walkbias = 0.0f; // Gives The Illusion That We Are Walking (NEW)
  70. GLfloat g_walkbiasangle = 0.0f; // Gives The Illusion That We Are Walking (NEW)
  71. GLfloat g_lookupdown = 5.0f; // Look Position In The Z-Axis (NEW)
  72. //GLuint g_filter = 1; // Which Filter To Use
  73. GLuint g_texid[TEXTURES_NUM]; // Our Textures' Id List
  74. SECTOR g_sector1; // Our Model Goes Here:
  75. bool g_key[255]; // Lookup Table For Key's State (NEW)
  76. float g_step_speed = 0.001f;
  77. float g_rot_speed = 0.05f;
  78. int g_window_size = 500;
  79. // -----------------------------------------------------------------------
  80. // cg course
  81. // -----------------------------------------------------------------------
  82. PFNGLGENFRAMEBUFFERSPROC glGenFramebuffers;
  83. PFNGLBINDFRAMEBUFFERPROC glBindFramebuffer;
  84. PFNGLDELETEFRAMEBUFFERSPROC glDeleteFramebuffers;
  85. PFNGLFRAMEBUFFERRENDERBUFFERPROC glFramebufferRenderbuffer;
  86. PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D;
  87. PFNGLCHECKFRAMEBUFFERSTATUSPROC glCheckFramebufferStatus;
  88. PFNGLGENRENDERBUFFERSPROC glGenRenderbuffers;
  89. PFNGLBINDRENDERBUFFERPROC glBindRenderbuffer;
  90. PFNGLDELETERENDERBUFFERSPROC glDeleteRenderbuffers;
  91. PFNGLRENDERBUFFERSTORAGEPROC glRenderbufferStorage;
  92. PFNGLGENERATEMIPMAPEXTPROC glGenerateMipmap;
  93. GLenum g_face_target[6] = {
  94. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  95. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  96. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  97. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  98. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  99. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
  100. };
  101. GLuint g_fbo; // Our handle to the FBO
  102. GLuint g_depth_buffer; // Our handle to the depth render buffer
  103. GLsizei g_cm_width;
  104. GLsizei g_cm_height;
  105. GLuint g_cm_texture_id[3];
  106. GLuint g_texture_id[6];
  107. float g_light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  108. float g_light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  109. float g_light_position[] = { 0.0f, 0.0f, 10.0f, 1.0f };
  110. float g_light_direction[] = { 0.0f, 0.0f, -1.0f, 0.0f };
  111. float g_light_rot = 0.0f;
  112. float g_light_rot_speed = 2.0f;
  113. float g_light_rot2 = 0.0f;
  114. float g_light_rot_speed2 = 6.0f;
  115. float g_material_ambient[] = { 1.0f, 0.57f, 0.04f, 1.0f };
  116. float g_material_diffuse[] = { 1.0f, 0.57f, 0.04f, 1.0f };
  117. float g_material_specular[] = { 0.33f, 0.33f, 0.52f, 1.0f };
  118. float g_material_emission[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  119. float g_material_specular_power = 80.0f;
  120. int g_texgen_mode = GL_REFLECTION_MAP;
  121. int g_tex_wrap = GL_CLAMP_TO_EDGE;
  122. unsigned int g_filter = 2; // Which Filter To Use
  123. Cameraf g_cam;
  124. Vectorf g_objpos(0.0f, 0.0f, 4.0f);
  125. Vectorf g_campos(0.0f, 0.0f, 5.0f);
  126. // -----------------------------------------------------------------------
  127. // Nehe
  128. // -----------------------------------------------------------------------
  129. FIBITMAP* load_image(char const *filename, int flag = 0)
  130. {
  131. FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename, 0);
  132. if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename);
  133. if (fif == FIF_UNKNOWN) return false;
  134. return FreeImage_Load(fif, filename, flag);
  135. }
  136. void readstr(FILE* f, char* string)
  137. {
  138. do {
  139. fgets(string, 255, f);
  140. } while ((string[0] == '/') || (string[0] == '\n'));
  141. }
  142. /// 通过world.txt的内容来构建场景
  143. void setupWorld()
  144. {
  145. float x, y, z, u, v;
  146. int numtriangles;
  147. FILE *filein;
  148. char oneline[255];
  149. filein = fopen("data/test2/world.txt", "rt"); // File To Load World Data From
  150. readstr(filein, oneline);
  151. sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
  152. g_sector1.triangle = new TRIANGLE[numtriangles];
  153. g_sector1.numtriangles = numtriangles;
  154. for (int loop = 0; loop < numtriangles; loop++) {
  155. for (int vert = 0; vert < 3; vert++) {
  156. readstr(filein, oneline);
  157. sscanf(oneline, "%f %f %f %f %f %f %f", &x, &y, &z, &u, &v);
  158. g_sector1.triangle[loop].vertex[vert].x = x * 10.0f;
  159. g_sector1.triangle[loop].vertex[vert].y = y * 10.0f;
  160. g_sector1.triangle[loop].vertex[vert].z = z * 10.0f;
  161. g_sector1.triangle[loop].vertex[vert].u = u;
  162. g_sector1.triangle[loop].vertex[vert].v = v;
  163. }
  164. }
  165. fclose(filein);
  166. return;
  167. }
  168. bool load_nehe_textures()
  169. {
  170. FIBITMAP *tdib = load_image("data/test2/rockwall.tga");
  171. if (!tdib) return false;
  172. bool status(false);
  173. unsigned int bpp = FreeImage_GetBPP(tdib);
  174. FIBITMAP *dib = tdib;
  175. if (bpp != 24) dib = FreeImage_ConvertTo24Bits(tdib);
  176. BYTE *bits = FreeImage_GetBits(dib);
  177. unsigned int width = FreeImage_GetWidth(dib);
  178. unsigned int height = FreeImage_GetHeight(dib);
  179. GLenum format = FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR ? GL_BGR : GL_RGB;
  180. RGBQUAD *pal = FreeImage_GetPalette(dib);
  181. if (bits != 0 && width>0 && height>0) {
  182. status = true; // Set The Status To TRUE
  183. glGenTextures(TEXTURES_NUM, g_texid); // Create Three Textures
  184. // Create Nearest Filtered Texture
  185. glBindTexture(GL_TEXTURE_2D, g_texid[0]);
  186. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  187. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  188. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, bits);
  189. // Create Linear Filtered Texture
  190. glBindTexture(GL_TEXTURE_2D, g_texid[1]);
  191. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  192. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  193. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, bits);
  194. // Create MipMapped Texture
  195. glBindTexture(GL_TEXTURE_2D, g_texid[2]);
  196. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  197. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  198. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, bits);
  199. }
  200. if (bpp != 24) FreeImage_Unload(dib);
  201. FreeImage_Unload(tdib);
  202. return status; // Return The Status
  203. }
  204. // Our Keyboard Handler For Special Keys (Like Arrow Keys And Function Keys)
  205. void special_keys(int a_keys, int x, int y)
  206. {
  207. switch (a_keys) {
  208. case GLUT_KEY_F1:
  209. // We Can Switch Between Windowed Mode And Fullscreen Mode Only
  210. if (!g_gamemode) {
  211. g_fullscreen = !g_fullscreen; // Toggle g_fullscreen Flag
  212. if (g_fullscreen) glutFullScreen(); // We Went In Fullscreen Mode
  213. else glutReshapeWindow(500, 500); // We Went In Windowed Mode
  214. }
  215. break;
  216. default:
  217. g_key[a_keys] = true;
  218. break;
  219. }
  220. }
  221. // Our Keyboard Handler For Special Key Releases.
  222. void special_keys_up(int key, int x, int y)
  223. {
  224. g_key[key] = false;
  225. }
  226. // Our Game Function. Check The User Input And Performs The Rendering
  227. void game_function()
  228. {
  229. // Process User Input
  230. if (g_key[GLUT_KEY_PAGE_UP]) {
  231. g_z -= 0.02f;
  232. g_lookupdown -= g_rot_speed;
  233. }
  234. if (g_key[GLUT_KEY_PAGE_DOWN]) {
  235. g_z += 0.02f;
  236. g_lookupdown += g_rot_speed;
  237. }
  238. if (g_key[GLUT_KEY_UP]) {
  239. g_xpos -= (float)sin(g_heading*PI_OVER_180) * g_step_speed;
  240. g_zpos -= (float)cos(g_heading*PI_OVER_180) * g_step_speed;
  241. //if (g_walkbiasangle >= 359.0f) g_walkbiasangle = 0.0f;
  242. //else g_walkbiasangle += 10;
  243. //g_walkbias = (float)sin(g_walkbiasangle*PI_OVER_180) / 20.0f;
  244. }
  245. if (g_key[GLUT_KEY_DOWN]) {
  246. g_xpos += (float)sin(g_heading*PI_OVER_180) * g_step_speed;
  247. g_zpos += (float)cos(g_heading*PI_OVER_180) * g_step_speed;
  248. //if (g_walkbiasangle <= 1.0f) g_walkbiasangle = 359.0f;
  249. //else g_walkbiasangle -= 10;
  250. //g_walkbias = (float)sin(g_walkbiasangle*PI_OVER_180) / 20.0f;
  251. }
  252. if (g_key[GLUT_KEY_RIGHT]) {
  253. g_heading -= g_rot_speed;
  254. g_yrot = g_heading;
  255. }
  256. if (g_key[GLUT_KEY_LEFT]) {
  257. g_heading += g_rot_speed;
  258. g_yrot = g_heading;
  259. }
  260. // Do The Rendering
  261. display();
  262. }
  263. // -----------------------------------------------------------------------
  264. // cg course
  265. // -----------------------------------------------------------------------
  266. void set_material()
  267. {
  268. GLenum face = GL_FRONT_AND_BACK;
  269. //glMaterialfv(face, GL_AMBIENT, g_material_ambient);
  270. glMaterialfv(face, GL_AMBIENT, g_material_diffuse);
  271. glMaterialfv(face, GL_DIFFUSE, g_material_diffuse);
  272. glMaterialfv(face, GL_SPECULAR, g_material_specular);
  273. glMaterialfv(face, GL_EMISSION, g_material_emission);
  274. glMaterialf(face, GL_SHININESS, g_material_specular_power);
  275. }
  276. bool is_extension_supported(const char *extension)
  277. {
  278. const GLubyte *extensions = NULL;
  279. const GLubyte *start;
  280. GLubyte *where, *terminator;
  281. /* Extension names should not have spaces. */
  282. where = (GLubyte *)strchr(extension, ' ');
  283. if (where || *extension == '\0') return false;
  284. extensions = glGetString(GL_EXTENSIONS);
  285. /* It takes a bit of care to be fool-proof about parsing the
  286. OpenGL extensions string. Don't be fooled by sub-strings,
  287. etc. */
  288. start = extensions;
  289. for (;;)
  290. {
  291. where = (GLubyte *)strstr((const char *)start, extension);
  292. if (!where)
  293. break;
  294. terminator = where + strlen(extension);
  295. if (where == start || *(where - 1) == ' ')
  296. if (*terminator == ' ' || *terminator == '\0')
  297. return true;
  298. start = terminator;
  299. }
  300. return false;
  301. }
  302. bool init_extfuncs()
  303. {
  304. if (!is_extension_supported("GL_EXT_framebuffer_object")) return false;
  305. glGenFramebuffers = (PFNGLGENFRAMEBUFFERSEXTPROC)wglGetProcAddress("glGenFramebuffersEXT");
  306. glBindFramebuffer = (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
  307. glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)wglGetProcAddress("glDeleteFramebuffersEXT");
  308. glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC)wglGetProcAddress("glFramebufferRenderbufferEXT");
  309. glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)wglGetProcAddress("glFramebufferTexture2DEXT");
  310. glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)wglGetProcAddress("glCheckFramebufferStatusEXT");
  311. glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)wglGetProcAddress("glGenRenderbuffersEXT");
  312. glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)wglGetProcAddress("glBindRenderbufferEXT");
  313. glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)wglGetProcAddress("glDeleteRenderbuffersEXT");
  314. glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)wglGetProcAddress("glRenderbufferStorageEXT");
  315. glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)wglGetProcAddress("glGenerateMipmapEXT");
  316. if (glGenFramebuffers == 0
  317. || glBindFramebuffer == 0
  318. || glDeleteFramebuffers == 0
  319. || glFramebufferRenderbuffer == 0
  320. || glFramebufferTexture2D == 0
  321. || glCheckFramebufferStatus == 0
  322. || glGenRenderbuffers == 0
  323. || glBindRenderbuffer == 0
  324. || glDeleteRenderbuffers == 0
  325. || glRenderbufferStorage == 0
  326. || glGenerateMipmap == 0)
  327. {
  328. std::cout << "glGenFramebuffers: " << glGenFramebuffers << std::endl
  329. << "glBindFramebuffer: " << glBindFramebuffer << std::endl
  330. << "glDeleteFramebuffers " << glDeleteFramebuffers << std::endl
  331. << "glFramebufferRenderbuffer " << glFramebufferRenderbuffer << std::endl
  332. << "glFramebufferTexture2D: " << glFramebufferTexture2D << std::endl
  333. << "glCheckFramebufferStatus: " << glCheckFramebufferStatus << std::endl
  334. << "glGenRenderbuffers: " << glGenRenderbuffers << std::endl
  335. << "glBindRenderbuffer: " << glBindRenderbuffer << std::endl
  336. << "glDeleteRenderbuffers: " << glDeleteRenderbuffers << std::endl
  337. << "glRenderbufferStorage: " << glRenderbufferStorage << std::endl
  338. << "glGenerateMipmap: " << glGenerateMipmap << std::endl;
  339. return false;
  340. }
  341. return true;
  342. }
  343. bool init_fbo()
  344. {
  345. // Setup our FBO
  346. glGenFramebuffers(1, &g_fbo);
  347. glBindFramebuffer(GL_FRAMEBUFFER, g_fbo);
  348. // Create the render buffer for depth
  349. glGenRenderbuffers(1, &g_depth_buffer);
  350. glBindRenderbuffer(GL_RENDERBUFFER, g_depth_buffer);
  351. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, g_cm_height, g_cm_height);
  352. // Attach the depth render buffer to the FBO as it's depth attachment
  353. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, g_depth_buffer);
  354. glBindFramebuffer(GL_FRAMEBUFFER, 0); // Unbind the FBO for now
  355. return true;
  356. }
  357. void update_texgen()
  358. {
  359. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, g_texgen_mode);
  360. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, g_texgen_mode);
  361. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, g_texgen_mode);
  362. }
  363. void update_wrap()
  364. {
  365. glBindTexture(GL_TEXTURE_CUBE_MAP, g_cm_texture_id[g_filter]);
  366. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, g_tex_wrap);
  367. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, g_tex_wrap);
  368. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, g_tex_wrap);
  369. }
  370. // ----------------------------------------------------------------------
  371. /// 袁胜:其实这个函数就是初始化cubemap的6个贴图,我也不知道怎么简化掉,
  372. /// 所以就只留了一个cm_top.bmp
  373. /// 你可以把这个路径改成你项目的路径,只拷贝这个cm_top
  374. /// 如果能把整个这个函数简化掉更好了
  375. bool load_cube_map_textures()
  376. {
  377. mitkBMPReader *reader = new mitkBMPReader();
  378. reader->AddFileName("data/dyncubemap/cm_top.bmp");
  379. reader->AddFileName("data/dyncubemap/cm_top.bmp");
  380. reader->AddFileName("data/dyncubemap/cm_top.bmp");
  381. reader->AddFileName("data/dyncubemap/cm_top.bmp");
  382. reader->AddFileName("data/dyncubemap/cm_top.bmp");
  383. reader->AddFileName("data/dyncubemap/cm_top.bmp");
  384. bool success = reader->Run();
  385. if (success)
  386. {
  387. mitkVolume *teximgs = reader->GetOutput();
  388. g_cm_width = teximgs->GetWidth();
  389. g_cm_height = teximgs->GetHeight();
  390. glGenTextures(3, g_cm_texture_id);
  391. for (int loop = 0; loop<6; loop++)
  392. {
  393. // Create Nearest Filtered Texture
  394. glBindTexture(GL_TEXTURE_CUBE_MAP, g_cm_texture_id[0]);
  395. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  396. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  397. glTexImage2D(g_face_target[loop], 0, GL_RGB, g_cm_width, g_cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, teximgs->GetSliceData(loop));
  398. // Create Linear Filtered Texture
  399. glBindTexture(GL_TEXTURE_CUBE_MAP, g_cm_texture_id[1]);
  400. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  401. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  402. glTexImage2D(g_face_target[loop], 0, GL_RGB, g_cm_width, g_cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, teximgs->GetSliceData(loop));
  403. // Create MipMapped Texture
  404. glBindTexture(GL_TEXTURE_CUBE_MAP, g_cm_texture_id[2]);
  405. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  406. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  407. gluBuild2DMipmaps(g_face_target[loop], GL_RGB, g_cm_width, g_cm_height, GL_RGB, GL_UNSIGNED_BYTE, teximgs->GetSliceData(loop));
  408. }
  409. teximgs->Delete();
  410. }
  411. reader->Delete();
  412. return success;
  413. }
  414. bool init()
  415. {
  416. if (!load_nehe_textures()) return false;
  417. if (!load_cube_map_textures()) {
  418. std::cout << "init: failed to load cube map textures!" << std::endl;
  419. return false;
  420. }
  421. if (!init_extfuncs()) {
  422. std::cout << "init: failed to initialize ext functions!" << std::endl;
  423. return false;
  424. }
  425. if (!init_fbo()) {
  426. std::cout << "init: failed to initialize frame buffer object!" << std::endl;
  427. return false;
  428. }
  429. update_texgen();
  430. update_wrap();
  431. glEnable(GL_NORMALIZE);
  432. glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Pixel Storage Mode To Byte Alignment
  433. glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
  434. glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Set The Blending Function (NEW)
  435. glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background (CHANGED)
  436. glClearDepth(1.0f); // Depth Buffer Setup
  437. glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
  438. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  439. glShadeModel(GL_SMOOTH); // Enable Smooth Shading
  440. ShowCursor(FALSE); // WiN API - Hides The Mouse Pointer (NEW)
  441. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  442. glLightfv(GL_LIGHT0, GL_AMBIENT, g_light_ambient); // Setup The Ambient Light
  443. glLightfv(GL_LIGHT0, GL_DIFFUSE, g_light_diffuse); // Setup The Diffuse Light
  444. glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 1.0f);
  445. glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 60.0f);
  446. glEnable(GL_LIGHT0); // Enable Light One
  447. setupWorld();
  448. memset(g_key, 0, sizeof(g_key));
  449. return true;
  450. }
  451. // -----------------------------------------------------------------------
  452. /// 绘制整个场景的地方。
  453. /// 整个大作业最核心的部分就是把dyncubemap例子中
  454. /// 动态创建cubemap的函数(create_dynamic_cube_map)
  455. /// 中绘制盒子的函数替换成这个
  456. void draw_scene()
  457. {
  458. GLfloat x_m, y_m, z_m, u_m, v_m;
  459. int numtriangles;
  460. GLfloat xtrans = -g_xpos;
  461. GLfloat ztrans = -g_zpos;
  462. GLfloat ytrans = -g_walkbias - 0.25f;
  463. GLfloat sceneroty = 360.0f - g_yrot;
  464. glRotatef(g_lookupdown, 1.0f, 0, 0);
  465. glRotatef(sceneroty, 0, 1.0f, 0);
  466. glTranslatef(xtrans, ytrans, ztrans);
  467. glBindTexture(GL_TEXTURE_2D, g_texid[g_filter]);
  468. glEnable(GL_TEXTURE_2D);
  469. numtriangles = g_sector1.numtriangles;
  470. // Process Each Triangle
  471. for (int loop_m = 0; loop_m < numtriangles; loop_m++)
  472. {
  473. glBegin(GL_TRIANGLES);
  474. glNormal3f(0.0f, 0.0f, 1.0f);
  475. x_m = g_sector1.triangle[loop_m].vertex[0].x;
  476. y_m = g_sector1.triangle[loop_m].vertex[0].y;
  477. z_m = g_sector1.triangle[loop_m].vertex[0].z;
  478. u_m = g_sector1.triangle[loop_m].vertex[0].u;
  479. v_m = g_sector1.triangle[loop_m].vertex[0].v;
  480. glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
  481. x_m = g_sector1.triangle[loop_m].vertex[1].x;
  482. y_m = g_sector1.triangle[loop_m].vertex[1].y;
  483. z_m = g_sector1.triangle[loop_m].vertex[1].z;
  484. u_m = g_sector1.triangle[loop_m].vertex[1].u;
  485. v_m = g_sector1.triangle[loop_m].vertex[1].v;
  486. glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
  487. x_m = g_sector1.triangle[loop_m].vertex[2].x;
  488. y_m = g_sector1.triangle[loop_m].vertex[2].y;
  489. z_m = g_sector1.triangle[loop_m].vertex[2].z;
  490. u_m = g_sector1.triangle[loop_m].vertex[2].u;
  491. v_m = g_sector1.triangle[loop_m].vertex[2].v;
  492. glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
  493. glEnd();
  494. }
  495. }
  496. /// 动态创建cubemap
  497. void create_dynamic_cube_map()
  498. {
  499. // First we bind the FBO so we can render to it
  500. glBindFramebuffer(GL_FRAMEBUFFER, g_fbo);
  501. glDrawBuffer(GL_COLOR_ATTACHMENT0);
  502. // Save the view port and set it to the size of the texture
  503. glPushAttrib(GL_VIEWPORT_BIT);
  504. glViewport(0, 0, g_cm_width, g_cm_height);
  505. glMatrixMode(GL_PROJECTION);
  506. glPushMatrix();
  507. glLoadIdentity();
  508. //MUST USE 90 degree FOV!
  509. g_cam.Perspective(90.0f, 1.0f, 0.1f, 5000.0f);
  510. glMatrixMode(GL_MODELVIEW);
  511. float facepos[6][6] = {
  512. { 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f },
  513. { -5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f },
  514. { 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 1.0f },
  515. { 0.0f, -5.0f, 0.0f, 0.0f, 0.0f, -1.0f },
  516. { 0.0f, 0.0f, 5.0f, 0.0f, -1.0f, 0.0f },
  517. { 0.0f, 0.0f, -5.0f, 0.0f, -1.0f, 0.0f }
  518. };
  519. glBindTexture(GL_TEXTURE_CUBE_MAP, g_cm_texture_id[g_filter]);
  520. for (int face = 0; face<6; ++face) {
  521. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, g_face_target[face], g_cm_texture_id[g_filter], 0);
  522. Vectorf up(facepos[face][3], facepos[face][4], facepos[face][5]);
  523. g_campos.ele[0] = facepos[face][0];
  524. g_campos.ele[1] = facepos[face][1];
  525. g_campos.ele[2] = facepos[face][2];
  526. g_campos.ele[3] = 1.0f;
  527. g_campos += g_objpos;
  528. g_campos.ele[3] = 1.0f;
  529. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  530. glPushMatrix();
  531. g_cam.LookAt(g_objpos.ele[0], g_objpos.ele[1], g_objpos.ele[2], g_campos.ele[0], g_campos.ele[1], g_campos.ele[2], up.ele[0], up.ele[1], up.ele[2]);
  532. glLightfv(GL_LIGHT0, GL_POSITION, g_light_position); // Position The Light
  533. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, g_light_direction);
  534. glPushMatrix();
  535. draw_scene(); /// 这里是关键
  536. glPopMatrix();
  537. glPopMatrix();
  538. glFlush();
  539. }
  540. if (g_filter == 2) glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  541. // Restore old view port and set rendering back to default frame buffer
  542. glPopAttrib();
  543. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  544. glMatrixMode(GL_PROJECTION);
  545. glPopMatrix();
  546. glMatrixMode(GL_MODELVIEW);
  547. }
  548. void display()
  549. {
  550. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  551. create_dynamic_cube_map();
  552. g_campos.ele[0] = 0.0f;
  553. g_campos.ele[1] = 0.0f;
  554. g_campos.ele[2] = 5.0f;
  555. g_campos.ele[3] = 1.0f;
  556. Vectorf up(0.0f, 1.0f, 0.0f);
  557. g_campos += g_objpos;
  558. g_campos.ele[3] = 1.0f;
  559. g_cam.LookAt(g_campos.ele[0], g_campos.ele[1], g_campos.ele[2], g_objpos.ele[0], g_objpos.ele[1], g_objpos.ele[2], up.ele[0], up.ele[1], up.ele[2]);
  560. glLightfv(GL_LIGHT0, GL_POSITION, g_light_position); // Position The Light
  561. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, g_light_direction);
  562. glPushMatrix();
  563. glDisable(GL_LIGHTING);
  564. g_material_diffuse[0] = 0.9f;
  565. g_material_diffuse[1] = 0.9f;
  566. g_material_diffuse[2] = 0.9f;
  567. set_material();
  568. glEnable(GL_TEXTURE_GEN_S); // Enable Texture Coord Generation For S (NEW)
  569. glEnable(GL_TEXTURE_GEN_T); // Enable Texture Coord Generation For T (NEW)
  570. glEnable(GL_TEXTURE_GEN_R);
  571. glEnable(GL_TEXTURE_CUBE_MAP);
  572. glTranslatef(0.0f, 1.0f, 0.0f);
  573. glutSolidSphere(1, 50, 50);
  574. glPopMatrix();
  575. glDisable(GL_TEXTURE_GEN_S);
  576. glDisable(GL_TEXTURE_GEN_T);
  577. glDisable(GL_TEXTURE_GEN_R);
  578. glDisable(GL_TEXTURE_CUBE_MAP);
  579. glEnable(GL_LIGHTING);
  580. glPushMatrix();
  581. draw_scene();
  582. glPopMatrix();
  583. glutSwapBuffers();
  584. g_light_rot += g_light_rot_speed;
  585. if (g_light_rot > 360.0f) g_light_rot = 0.0f;
  586. g_light_rot2 += g_light_rot_speed2;
  587. if (g_light_rot2 > 360.0f) g_light_rot2 = 0.0f;
  588. }
  589. void reshape(int w, int h)
  590. {
  591. glViewport(0, 0, w, h);
  592. glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
  593. glLoadIdentity(); // Reset The Projection Matrix
  594. // Calculate The Aspect Ratio And Set The Clipping Volume
  595. if (h == 0) h = 1;
  596. gluPerspective(45.0f, (float)w / (float)h, 0.1, 100.0);
  597. glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
  598. glLoadIdentity(); // Reset The Modelview Matrix
  599. }
  600. void keyboard(unsigned char key, int x, int y)
  601. {
  602. switch (key)
  603. {
  604. case 27:
  605. exit(0);
  606. break;
  607. }
  608. }
  609. int main(int argc, char *argv[])
  610. {
  611. glutInit(&argc, argv);
  612. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  613. glutInitWindowSize(500, 500);
  614. glutCreateWindow("参考大作业2");
  615. if (!init()) return 1;
  616. glutKeyboardFunc(keyboard);
  617. glutSpecialFunc(special_keys);
  618. glutSpecialUpFunc(special_keys_up);
  619. glutReshapeFunc(reshape);
  620. glutDisplayFunc(display);
  621. glutIdleFunc(game_function);
  622. glutMainLoop();
  623. return 0;
  624. }

wrold.txt

  1. NUMPOLLIES 36
  2. // Floor 1
  3. -3.0 0.0 -3.0 0.0 6.0
  4. -3.0 0.0 3.0 0.0 0.0
  5. 3.0 0.0 3.0 6.0 0.0
  6. -3.0 0.0 -3.0 0.0 6.0
  7. 3.0 0.0 -3.0 6.0 6.0
  8. 3.0 0.0 3.0 6.0 0.0
  9. // Ceiling 1
  10. -3.0 1.0 -3.0 0.0 6.0
  11. -3.0 1.0 3.0 0.0 0.0
  12. 3.0 1.0 3.0 6.0 0.0
  13. -3.0 1.0 -3.0 0.0 6.0
  14. 3.0 1.0 -3.0 6.0 6.0
  15. 3.0 1.0 3.0 6.0 0.0
  16. // A1
  17. -2.0 1.0 -2.0 0.0 1.0
  18. -2.0 0.0 -2.0 0.0 0.0
  19. -0.5 0.0 -2.0 1.5 0.0
  20. -2.0 1.0 -2.0 0.0 1.0
  21. -0.5 1.0 -2.0 1.5 1.0
  22. -0.5 0.0 -2.0 1.5 0.0
  23. // A2
  24. 2.0 1.0 -2.0 2.0 1.0
  25. 2.0 0.0 -2.0 2.0 0.0
  26. 0.5 0.0 -2.0 0.5 0.0
  27. 2.0 1.0 -2.0 2.0 1.0
  28. 0.5 1.0 -2.0 0.5 1.0
  29. 0.5 0.0 -2.0 0.5 0.0
  30. // B1
  31. -2.0 1.0 2.0 2.0 1.0
  32. -2.0 0.0 2.0 2.0 0.0
  33. -0.5 0.0 2.0 0.5 0.0
  34. -2.0 1.0 2.0 2.0 1.0
  35. -0.5 1.0 2.0 0.5 1.0
  36. -0.5 0.0 2.0 0.5 0.0
  37. // B2
  38. 2.0 1.0 2.0 2.0 1.0
  39. 2.0 0.0 2.0 2.0 0.0
  40. 0.5 0.0 2.0 0.5 0.0
  41. 2.0 1.0 2.0 2.0 1.0
  42. 0.5 1.0 2.0 0.5 1.0
  43. 0.5 0.0 2.0 0.5 0.0
  44. // C1
  45. -2.0 1.0 -2.0 0.0 1.0
  46. -2.0 0.0 -2.0 0.0 0.0
  47. -2.0 0.0 -0.5 1.5 0.0
  48. -2.0 1.0 -2.0 0.0 1.0
  49. -2.0 1.0 -0.5 1.5 1.0
  50. -2.0 0.0 -0.5 1.5 0.0
  51. // C2
  52. -2.0 1.0 2.0 2.0 1.0
  53. -2.0 0.0 2.0 2.0 0.0
  54. -2.0 0.0 0.5 0.5 0.0
  55. -2.0 1.0 2.0 2.0 1.0
  56. -2.0 1.0 0.5 0.5 1.0
  57. -2.0 0.0 0.5 0.5 0.0
  58. // D1
  59. 2.0 1.0 -2.0 0.0 1.0
  60. 2.0 0.0 -2.0 0.0 0.0
  61. 2.0 0.0 -0.5 1.5 0.0
  62. 2.0 1.0 -2.0 0.0 1.0
  63. 2.0 1.0 -0.5 1.5 1.0
  64. 2.0 0.0 -0.5 1.5 0.0
  65. // D2
  66. 2.0 1.0 2.0 2.0 1.0
  67. 2.0 0.0 2.0 2.0 0.0
  68. 2.0 0.0 0.5 0.5 0.0
  69. 2.0 1.0 2.0 2.0 1.0
  70. 2.0 1.0 0.5 0.5 1.0
  71. 2.0 0.0 0.5 0.5 0.0
  72. // Upper hallway - L
  73. -0.5 1.0 -3.0 0.0 1.0
  74. -0.5 0.0 -3.0 0.0 0.0
  75. -0.5 0.0 -2.0 1.0 0.0
  76. -0.5 1.0 -3.0 0.0 1.0
  77. -0.5 1.0 -2.0 1.0 1.0
  78. -0.5 0.0 -2.0 1.0 0.0
  79. // Upper hallway - R
  80. 0.5 1.0 -3.0 0.0 1.0
  81. 0.5 0.0 -3.0 0.0 0.0
  82. 0.5 0.0 -2.0 1.0 0.0
  83. 0.5 1.0 -3.0 0.0 1.0
  84. 0.5 1.0 -2.0 1.0 1.0
  85. 0.5 0.0 -2.0 1.0 0.0
  86. // Lower hallway - L
  87. -0.5 1.0 3.0 0.0 1.0
  88. -0.5 0.0 3.0 0.0 0.0
  89. -0.5 0.0 2.0 1.0 0.0
  90. -0.5 1.0 3.0 0.0 1.0
  91. -0.5 1.0 2.0 1.0 1.0
  92. -0.5 0.0 2.0 1.0 0.0
  93. // Lower hallway - R
  94. 0.5 1.0 3.0 0.0 1.0
  95. 0.5 0.0 3.0 0.0 0.0
  96. 0.5 0.0 2.0 1.0 0.0
  97. 0.5 1.0 3.0 0.0 1.0
  98. 0.5 1.0 2.0 1.0 1.0
  99. 0.5 0.0 2.0 1.0 0.0
  100. // Left hallway - Lw
  101. -3.0 1.0 0.5 1.0 1.0
  102. -3.0 0.0 0.5 1.0 0.0
  103. -2.0 0.0 0.5 0.0 0.0
  104. -3.0 1.0 0.5 1.0 1.0
  105. -2.0 1.0 0.5 0.0 1.0
  106. -2.0 0.0 0.5 0.0 0.0
  107. // Left hallway - Hi
  108. -3.0 1.0 -0.5 1.0 1.0
  109. -3.0 0.0 -0.5 1.0 0.0
  110. -2.0 0.0 -0.5 0.0 0.0
  111. -3.0 1.0 -0.5 1.0 1.0
  112. -2.0 1.0 -0.5 0.0 1.0
  113. -2.0 0.0 -0.5 0.0 0.0
  114. // Right hallway - Lw
  115. 3.0 1.0 0.5 1.0 1.0
  116. 3.0 0.0 0.5 1.0 0.0
  117. 2.0 0.0 0.5 0.0 0.0
  118. 3.0 1.0 0.5 1.0 1.0
  119. 2.0 1.0 0.5 0.0 1.0
  120. 2.0 0.0 0.5 0.0 0.0
  121. // Right hallway - Hi
  122. 3.0 1.0 -0.5 1.0 1.0
  123. 3.0 0.0 -0.5 1.0 0.0
  124. 2.0 0.0 -0.5 0.0 0.0
  125. 3.0 1.0 -0.5 1.0 1.0
  126. 2.0 1.0 -0.5 0.0 1.0
  127. 2.0 0.0 -0.5 0.0 0.0
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注