Clone of mesa.
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.

objview.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * .obj file viewer based on "smooth" by Nate Robins, 1997
  3. *
  4. * Brian Paul
  5. * 1 Oct 2009
  6. */
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <assert.h>
  11. #include <stdarg.h>
  12. #include <GL/glew.h>
  13. #include <GL/glut.h>
  14. #include "glm.h"
  15. #include "readtex.h"
  16. #include "skybox.h"
  17. #include "trackball.h"
  18. static char *Model_file = NULL; /* name of the obect file */
  19. static GLMmodel *Model;
  20. static GLfloat Scale = 4.0; /* scaling factor */
  21. static GLboolean Performance = GL_FALSE;
  22. static GLboolean Stats = GL_FALSE;
  23. static GLboolean Animate = GL_TRUE;
  24. static GLuint SkyboxTex;
  25. static GLboolean Skybox = GL_TRUE;
  26. static GLboolean Cull = GL_TRUE;
  27. static GLboolean WireFrame = GL_FALSE;
  28. static GLenum FrontFace = GL_CCW;
  29. static GLfloat Yrot = 0.0;
  30. static GLint WinWidth = 1024, WinHeight = 768;
  31. static GLuint NumInstances = 1;
  32. typedef struct
  33. {
  34. float CurQuat[4];
  35. float Distance;
  36. /* When mouse is moving: */
  37. GLboolean Rotating, Translating;
  38. GLint StartX, StartY;
  39. float StartDistance;
  40. } ViewInfo;
  41. static ViewInfo View;
  42. static void
  43. InitViewInfo(ViewInfo *view)
  44. {
  45. view->Rotating = GL_FALSE;
  46. view->Translating = GL_FALSE;
  47. view->StartX = view->StartY = 0;
  48. view->Distance = 12.0;
  49. view->StartDistance = 0.0;
  50. view->CurQuat[0] = 0.0;
  51. view->CurQuat[1] = 1.0;
  52. view->CurQuat[2] = 0.0;
  53. view->CurQuat[3] = 0.0;
  54. }
  55. /* text: general purpose text routine. draws a string according to
  56. * format in a stroke font at x, y after scaling it by the scale
  57. * specified (scale is in window-space (lower-left origin) pixels).
  58. *
  59. * x - position in x (in window-space)
  60. * y - position in y (in window-space)
  61. * scale - scale in pixels
  62. * format - as in printf()
  63. */
  64. static void
  65. text(GLuint x, GLuint y, GLfloat scale, char* format, ...)
  66. {
  67. va_list args;
  68. char buffer[255], *p;
  69. GLfloat font_scale = 119.05 + 33.33;
  70. va_start(args, format);
  71. vsprintf(buffer, format, args);
  72. va_end(args);
  73. glMatrixMode(GL_PROJECTION);
  74. glPushMatrix();
  75. glLoadIdentity();
  76. gluOrtho2D(0, glutGet(GLUT_WINDOW_WIDTH), 0, glutGet(GLUT_WINDOW_HEIGHT));
  77. glMatrixMode(GL_MODELVIEW);
  78. glPushMatrix();
  79. glLoadIdentity();
  80. glPushAttrib(GL_ENABLE_BIT);
  81. glDisable(GL_LIGHTING);
  82. glDisable(GL_TEXTURE_2D);
  83. glDisable(GL_DEPTH_TEST);
  84. glTranslatef(x, y, 0.0);
  85. glScalef(scale/font_scale, scale/font_scale, scale/font_scale);
  86. for(p = buffer; *p; p++)
  87. glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  88. glPopAttrib();
  89. glPopMatrix();
  90. glMatrixMode(GL_PROJECTION);
  91. glPopMatrix();
  92. glMatrixMode(GL_MODELVIEW);
  93. }
  94. static float
  95. ComputeFPS(void)
  96. {
  97. static double t0 = -1.0;
  98. static int frames = 0;
  99. double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  100. static float fps = 0;
  101. frames++;
  102. if (t0 < 0.0) {
  103. t0 = t;
  104. fps = 0.0;
  105. }
  106. else if (t - t0 >= 4.0) {
  107. fps = (frames / (t - t0) + 0.5);
  108. t0 = t;
  109. frames = 0;
  110. return fps;
  111. }
  112. return 0.0;
  113. }
  114. static void
  115. init_model(void)
  116. {
  117. float objScale;
  118. /* read in the model */
  119. Model = glmReadOBJ(Model_file);
  120. objScale = glmUnitize(Model);
  121. glmFacetNormals(Model);
  122. if (Model->numnormals == 0) {
  123. GLfloat smoothing_angle = 90.0;
  124. printf("Generating normals.\n");
  125. glmVertexNormals(Model, smoothing_angle);
  126. }
  127. glmLoadTextures(Model);
  128. glmReIndex(Model);
  129. glmMakeVBOs(Model);
  130. if (0)
  131. glmPrint(Model);
  132. }
  133. static void
  134. init_skybox(void)
  135. {
  136. SkyboxTex = LoadSkyBoxCubeTexture("alpine_east.rgb",
  137. "alpine_west.rgb",
  138. "alpine_up.rgb",
  139. "alpine_down.rgb",
  140. "alpine_south.rgb",
  141. "alpine_north.rgb");
  142. glmSpecularTexture(Model, SkyboxTex);
  143. }
  144. static void
  145. init_gfx(void)
  146. {
  147. glEnable(GL_DEPTH_TEST);
  148. glEnable(GL_CULL_FACE);
  149. glEnable(GL_NORMALIZE);
  150. glClearColor(0.3, 0.3, 0.9, 0.0);
  151. }
  152. static void
  153. reshape(int width, int height)
  154. {
  155. float ar = 0.5 * (float) width / (float) height;
  156. WinWidth = width;
  157. WinHeight = height;
  158. glViewport(0, 0, width, height);
  159. glMatrixMode(GL_PROJECTION);
  160. glLoadIdentity();
  161. glFrustum(-ar, ar, -0.5, 0.5, 1.0, 300.0);
  162. glMatrixMode(GL_MODELVIEW);
  163. glLoadIdentity();
  164. glTranslatef(0.0, 0.0, -3.0);
  165. }
  166. static void
  167. Idle(void)
  168. {
  169. float q[4];
  170. trackball(q, 100, 0, 99.99, 0);
  171. add_quats(q, View.CurQuat, View.CurQuat);
  172. glutPostRedisplay();
  173. }
  174. static void
  175. display(void)
  176. {
  177. GLfloat rot[4][4];
  178. float fps;
  179. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  180. glPushMatrix();
  181. glTranslatef(0.0, 0.0, -View.Distance);
  182. glRotatef(Yrot, 0, 1, 0);
  183. build_rotmatrix(rot, View.CurQuat);
  184. glMultMatrixf(&rot[0][0]);
  185. glScalef(Scale, Scale, Scale );
  186. glUseProgram(0);
  187. if (Skybox)
  188. DrawSkyBoxCubeTexture(SkyboxTex);
  189. if (WireFrame)
  190. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  191. else
  192. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  193. if (Cull)
  194. glEnable(GL_CULL_FACE);
  195. else
  196. glDisable(GL_CULL_FACE);
  197. if (NumInstances == 1) {
  198. glmDrawVBO(Model);
  199. }
  200. else {
  201. /* draw > 1 instance */
  202. float dr = 360.0 / NumInstances;
  203. float r;
  204. for (r = 0.0; r < 360.0; r += dr) {
  205. glPushMatrix();
  206. glRotatef(r, 0, 1, 0);
  207. glTranslatef(1.4, 0.0, 0.0);
  208. glmDrawVBO(Model);
  209. glPopMatrix();
  210. }
  211. }
  212. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  213. glDisable(GL_CULL_FACE);
  214. glPopMatrix();
  215. if (Stats) {
  216. glColor3f(1.0, 1.0, 1.0);
  217. text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*1), 20, "%s",
  218. Model->pathname);
  219. text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*2), 20, "%d vertices",
  220. Model->numvertices);
  221. text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*3), 20, "%d triangles",
  222. Model->numtriangles);
  223. text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*4), 20, "%d normals",
  224. Model->numnormals);
  225. text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*5), 20, "%d texcoords",
  226. Model->numtexcoords);
  227. text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*6), 20, "%d groups",
  228. Model->numgroups);
  229. text(5, glutGet(GLUT_WINDOW_HEIGHT) - (5+20*7), 20, "%d materials",
  230. Model->nummaterials);
  231. }
  232. glutSwapBuffers();
  233. fps = ComputeFPS();
  234. if (fps)
  235. printf("%f FPS\n", fps);
  236. }
  237. static void
  238. keyboard(unsigned char key, int x, int y)
  239. {
  240. switch (key) {
  241. case 'h':
  242. printf("help\n\n");
  243. printf("a - Toggle animation\n");
  244. printf("d/D - Decrease/Incrase number of models\n");
  245. printf("w - Toggle wireframe/filled\n");
  246. printf("c - Toggle culling\n");
  247. printf("n - Toggle facet/smooth normal\n");
  248. printf("r - Reverse polygon winding\n");
  249. printf("p - Toggle performance indicator\n");
  250. printf("s - Toggle skybox\n");
  251. printf("z/Z - Scale model smaller/larger\n");
  252. printf("i - Show model info/stats\n");
  253. printf("q/escape - Quit\n\n");
  254. break;
  255. case 'a':
  256. Animate = !Animate;
  257. if (Animate)
  258. glutIdleFunc(Idle);
  259. else
  260. glutIdleFunc(NULL);
  261. break;
  262. case 'd':
  263. if (NumInstances > 1)
  264. NumInstances--;
  265. break;
  266. case 'D':
  267. NumInstances++;
  268. break;
  269. case 'i':
  270. Stats = !Stats;
  271. break;
  272. case 'p':
  273. Performance = !Performance;
  274. break;
  275. case 'w':
  276. WireFrame = !WireFrame;
  277. break;
  278. case 'c':
  279. Cull = !Cull;
  280. printf("Polygon culling: %d\n", Cull);
  281. break;
  282. case 'r':
  283. if (FrontFace == GL_CCW)
  284. FrontFace = GL_CW;
  285. else
  286. FrontFace = GL_CCW;
  287. glFrontFace(FrontFace);
  288. printf("Front face:: %s\n", FrontFace == GL_CCW ? "CCW" : "CW");
  289. break;
  290. case 's':
  291. Skybox = !Skybox;
  292. if (Skybox)
  293. glmSpecularTexture(Model, SkyboxTex);
  294. else
  295. glmSpecularTexture(Model, 0);
  296. break;
  297. case 'z':
  298. Scale *= 0.9;
  299. break;
  300. case 'Z':
  301. Scale *= 1.1;
  302. break;
  303. case 'q':
  304. case 27:
  305. exit(0);
  306. break;
  307. }
  308. glutPostRedisplay();
  309. }
  310. static void
  311. menu(int item)
  312. {
  313. keyboard((unsigned char)item, 0, 0);
  314. }
  315. /**
  316. * Handle mouse button.
  317. */
  318. static void
  319. Mouse(int button, int state, int x, int y)
  320. {
  321. if (button == GLUT_LEFT_BUTTON) {
  322. if (state == GLUT_DOWN) {
  323. View.StartX = x;
  324. View.StartY = y;
  325. View.Rotating = GL_TRUE;
  326. }
  327. else if (state == GLUT_UP) {
  328. View.Rotating = GL_FALSE;
  329. }
  330. }
  331. else if (button == GLUT_MIDDLE_BUTTON) {
  332. if (state == GLUT_DOWN) {
  333. View.StartX = x;
  334. View.StartY = y;
  335. View.StartDistance = View.Distance;
  336. View.Translating = GL_TRUE;
  337. }
  338. else if (state == GLUT_UP) {
  339. View.Translating = GL_FALSE;
  340. }
  341. }
  342. }
  343. /**
  344. * Handle mouse motion
  345. */
  346. static void
  347. Motion(int x, int y)
  348. {
  349. int i;
  350. if (View.Rotating) {
  351. float x0 = (2.0 * View.StartX - WinWidth) / WinWidth;
  352. float y0 = (WinHeight - 2.0 * View.StartY) / WinHeight;
  353. float x1 = (2.0 * x - WinWidth) / WinWidth;
  354. float y1 = (WinHeight - 2.0 * y) / WinHeight;
  355. float q[4];
  356. trackball(q, x0, y0, x1, y1);
  357. View.StartX = x;
  358. View.StartY = y;
  359. for (i = 0; i < 1; i++)
  360. add_quats(q, View.CurQuat, View.CurQuat);
  361. glutPostRedisplay();
  362. }
  363. else if (View.Translating) {
  364. float dz = 0.02 * (y - View.StartY);
  365. View.Distance = View.StartDistance + dz;
  366. glutPostRedisplay();
  367. }
  368. }
  369. static void
  370. DoFeatureChecks(void)
  371. {
  372. char *version = (char *) glGetString(GL_VERSION);
  373. if (version[0] == '1') {
  374. /* check for individual extensions */
  375. if (!glutExtensionSupported("GL_ARB_texture_cube_map")) {
  376. printf("Sorry, GL_ARB_texture_cube_map is required.\n");
  377. exit(1);
  378. }
  379. if (!glutExtensionSupported("GL_ARB_vertex_shader")) {
  380. printf("Sorry, GL_ARB_vertex_shader is required.\n");
  381. exit(1);
  382. }
  383. if (!glutExtensionSupported("GL_ARB_fragment_shader")) {
  384. printf("Sorry, GL_ARB_fragment_shader is required.\n");
  385. exit(1);
  386. }
  387. if (!glutExtensionSupported("GL_ARB_vertex_buffer_object")) {
  388. printf("Sorry, GL_ARB_vertex_buffer_object is required.\n");
  389. exit(1);
  390. }
  391. }
  392. }
  393. int
  394. main(int argc, char** argv)
  395. {
  396. glutInitWindowSize(WinWidth, WinHeight);
  397. glutInit(&argc, argv);
  398. if (argc > 1) {
  399. Model_file = argv[1];
  400. }
  401. if (!Model_file) {
  402. fprintf(stderr, "usage: objview file.obj\n");
  403. fprintf(stderr, "(using default bunny.obj)\n");
  404. Model_file = "bunny.obj";
  405. }
  406. glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  407. glutCreateWindow("objview");
  408. glewInit();
  409. DoFeatureChecks();
  410. glutReshapeFunc(reshape);
  411. glutDisplayFunc(display);
  412. glutKeyboardFunc(keyboard);
  413. glutMouseFunc(Mouse);
  414. glutMotionFunc(Motion);
  415. if (Animate)
  416. glutIdleFunc(Idle);
  417. glutCreateMenu(menu);
  418. glutAddMenuEntry("[a] Toggle animate", 'a');
  419. glutAddMenuEntry("[d] Fewer models", 'd');
  420. glutAddMenuEntry("[D] More models", 'D');
  421. glutAddMenuEntry("[w] Toggle wireframe/filled", 'w');
  422. glutAddMenuEntry("[c] Toggle culling on/off", 'c');
  423. glutAddMenuEntry("[r] Reverse polygon winding", 'r');
  424. glutAddMenuEntry("[z] Scale model smaller", 'z');
  425. glutAddMenuEntry("[Z] Scale model larger", 'Z');
  426. glutAddMenuEntry("[p] Toggle performance indicator", 'p');
  427. glutAddMenuEntry("[i] Show model stats", 'i');
  428. glutAddMenuEntry("", 0);
  429. glutAddMenuEntry("[q] Quit", 27);
  430. glutAttachMenu(GLUT_RIGHT_BUTTON);
  431. InitViewInfo(&View);
  432. init_model();
  433. init_skybox();
  434. init_gfx();
  435. glutMainLoop();
  436. return 0;
  437. }