Sample_SimpleOpenGL.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* ----------------------------------------------------------------------------
  2. // Simple sample to prove that Assimp is easy to use with OpenGL.
  3. // It takes a file name as command line parameter, loads it using standard
  4. // settings and displays it.
  5. //
  6. // If you intend to _use_ this code sample in your app, do yourself a favour
  7. // and replace immediate mode calls with VBOs ...
  8. //
  9. // The vc8 solution links against assimp-release-dll_win32 - be sure to
  10. // have this configuration built.
  11. // ----------------------------------------------------------------------------
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <GL/glut.h>
  16. /* assimp include files. These three are usually needed. */
  17. #include <assimp/cimport.h>
  18. #include <assimp/scene.h>
  19. #include <assimp/postprocess.h>
  20. /* the global Assimp scene object */
  21. const struct aiScene* scene = NULL;
  22. GLuint scene_list = 0;
  23. struct aiVector3D scene_min, scene_max, scene_center;
  24. /* current rotation angle */
  25. static float angle = 0.f;
  26. #define aisgl_min(x,y) (x<y?x:y)
  27. #define aisgl_max(x,y) (y>x?y:x)
  28. /* ---------------------------------------------------------------------------- */
  29. void reshape(int width, int height)
  30. {
  31. const double aspectRatio = (float) width / height, fieldOfView = 45.0;
  32. glMatrixMode(GL_PROJECTION);
  33. glLoadIdentity();
  34. gluPerspective(fieldOfView, aspectRatio,
  35. 1.0, 1000.0); /* Znear and Zfar */
  36. glViewport(0, 0, width, height);
  37. }
  38. /* ---------------------------------------------------------------------------- */
  39. void get_bounding_box_for_node (const struct aiNode* nd,
  40. struct aiVector3D* min,
  41. struct aiVector3D* max,
  42. struct aiMatrix4x4* trafo
  43. ){
  44. struct aiMatrix4x4 prev;
  45. unsigned int n = 0, t;
  46. prev = *trafo;
  47. aiMultiplyMatrix4(trafo,&nd->mTransformation);
  48. for (; n < nd->mNumMeshes; ++n) {
  49. const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
  50. for (t = 0; t < mesh->mNumVertices; ++t) {
  51. struct aiVector3D tmp = mesh->mVertices[t];
  52. aiTransformVecByMatrix4(&tmp,trafo);
  53. min->x = aisgl_min(min->x,tmp.x);
  54. min->y = aisgl_min(min->y,tmp.y);
  55. min->z = aisgl_min(min->z,tmp.z);
  56. max->x = aisgl_max(max->x,tmp.x);
  57. max->y = aisgl_max(max->y,tmp.y);
  58. max->z = aisgl_max(max->z,tmp.z);
  59. }
  60. }
  61. for (n = 0; n < nd->mNumChildren; ++n) {
  62. get_bounding_box_for_node(nd->mChildren[n],min,max,trafo);
  63. }
  64. *trafo = prev;
  65. }
  66. /* ---------------------------------------------------------------------------- */
  67. void get_bounding_box (struct aiVector3D* min, struct aiVector3D* max)
  68. {
  69. struct aiMatrix4x4 trafo;
  70. aiIdentityMatrix4(&trafo);
  71. min->x = min->y = min->z = 1e10f;
  72. max->x = max->y = max->z = -1e10f;
  73. get_bounding_box_for_node(scene->mRootNode,min,max,&trafo);
  74. }
  75. /* ---------------------------------------------------------------------------- */
  76. void color4_to_float4(const struct aiColor4D *c, float f[4])
  77. {
  78. f[0] = c->r;
  79. f[1] = c->g;
  80. f[2] = c->b;
  81. f[3] = c->a;
  82. }
  83. /* ---------------------------------------------------------------------------- */
  84. void set_float4(float f[4], float a, float b, float c, float d)
  85. {
  86. f[0] = a;
  87. f[1] = b;
  88. f[2] = c;
  89. f[3] = d;
  90. }
  91. /* ---------------------------------------------------------------------------- */
  92. void apply_material(const struct aiMaterial *mtl)
  93. {
  94. float c[4];
  95. GLenum fill_mode;
  96. int ret1, ret2;
  97. struct aiColor4D diffuse;
  98. struct aiColor4D specular;
  99. struct aiColor4D ambient;
  100. struct aiColor4D emission;
  101. float shininess, strength;
  102. int two_sided;
  103. int wireframe;
  104. unsigned int max;
  105. set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f);
  106. if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
  107. color4_to_float4(&diffuse, c);
  108. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c);
  109. set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
  110. if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular))
  111. color4_to_float4(&specular, c);
  112. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c);
  113. set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f);
  114. if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient))
  115. color4_to_float4(&ambient, c);
  116. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, c);
  117. set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
  118. if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission))
  119. color4_to_float4(&emission, c);
  120. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, c);
  121. max = 1;
  122. ret1 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max);
  123. if(ret1 == AI_SUCCESS) {
  124. max = 1;
  125. ret2 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &max);
  126. if(ret2 == AI_SUCCESS)
  127. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess * strength);
  128. else
  129. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
  130. }
  131. else {
  132. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);
  133. set_float4(c, 0.0f, 0.0f, 0.0f, 0.0f);
  134. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c);
  135. }
  136. max = 1;
  137. if(AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_ENABLE_WIREFRAME, &wireframe, &max))
  138. fill_mode = wireframe ? GL_LINE : GL_FILL;
  139. else
  140. fill_mode = GL_FILL;
  141. glPolygonMode(GL_FRONT_AND_BACK, fill_mode);
  142. max = 1;
  143. if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && two_sided)
  144. glDisable(GL_CULL_FACE);
  145. else
  146. glEnable(GL_CULL_FACE);
  147. }
  148. /* ---------------------------------------------------------------------------- */
  149. void recursive_render (const struct aiScene *sc, const struct aiNode* nd)
  150. {
  151. unsigned int i;
  152. unsigned int n = 0, t;
  153. struct aiMatrix4x4 m = nd->mTransformation;
  154. /* update transform */
  155. aiTransposeMatrix4(&m);
  156. glPushMatrix();
  157. glMultMatrixf((float*)&m);
  158. /* draw all meshes assigned to this node */
  159. for (; n < nd->mNumMeshes; ++n) {
  160. const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
  161. apply_material(sc->mMaterials[mesh->mMaterialIndex]);
  162. if(mesh->mNormals == NULL) {
  163. glDisable(GL_LIGHTING);
  164. } else {
  165. glEnable(GL_LIGHTING);
  166. }
  167. for (t = 0; t < mesh->mNumFaces; ++t) {
  168. const struct aiFace* face = &mesh->mFaces[t];
  169. GLenum face_mode;
  170. switch(face->mNumIndices) {
  171. case 1: face_mode = GL_POINTS; break;
  172. case 2: face_mode = GL_LINES; break;
  173. case 3: face_mode = GL_TRIANGLES; break;
  174. default: face_mode = GL_POLYGON; break;
  175. }
  176. glBegin(face_mode);
  177. for(i = 0; i < face->mNumIndices; i++) {
  178. int index = face->mIndices[i];
  179. if(mesh->mColors[0] != NULL)
  180. glColor4fv((GLfloat*)&mesh->mColors[0][index]);
  181. if(mesh->mNormals != NULL)
  182. glNormal3fv(&mesh->mNormals[index].x);
  183. glVertex3fv(&mesh->mVertices[index].x);
  184. }
  185. glEnd();
  186. }
  187. }
  188. /* draw all children */
  189. for (n = 0; n < nd->mNumChildren; ++n) {
  190. recursive_render(sc, nd->mChildren[n]);
  191. }
  192. glPopMatrix();
  193. }
  194. /* ---------------------------------------------------------------------------- */
  195. void do_motion (void)
  196. {
  197. static GLint prev_time = 0;
  198. static GLint prev_fps_time = 0;
  199. static int frames = 0;
  200. int time = glutGet(GLUT_ELAPSED_TIME);
  201. angle += (time-prev_time)*0.01;
  202. prev_time = time;
  203. frames += 1;
  204. if ((time - prev_fps_time) > 1000) /* update every seconds */
  205. {
  206. int current_fps = frames * 1000 / (time - prev_fps_time);
  207. printf("%d fps\n", current_fps);
  208. frames = 0;
  209. prev_fps_time = time;
  210. }
  211. glutPostRedisplay ();
  212. }
  213. /* ---------------------------------------------------------------------------- */
  214. void display(void)
  215. {
  216. float tmp;
  217. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  218. glMatrixMode(GL_MODELVIEW);
  219. glLoadIdentity();
  220. gluLookAt(0.f,0.f,3.f,0.f,0.f,-5.f,0.f,1.f,0.f);
  221. /* rotate it around the y axis */
  222. glRotatef(angle,0.f,1.f,0.f);
  223. /* scale the whole asset to fit into our view frustum */
  224. tmp = scene_max.x-scene_min.x;
  225. tmp = aisgl_max(scene_max.y - scene_min.y,tmp);
  226. tmp = aisgl_max(scene_max.z - scene_min.z,tmp);
  227. tmp = 1.f / tmp;
  228. glScalef(tmp, tmp, tmp);
  229. /* center the model */
  230. glTranslatef( -scene_center.x, -scene_center.y, -scene_center.z );
  231. /* if the display list has not been made yet, create a new one and
  232. fill it with scene contents */
  233. if(scene_list == 0) {
  234. scene_list = glGenLists(1);
  235. glNewList(scene_list, GL_COMPILE);
  236. /* now begin at the root node of the imported data and traverse
  237. the scenegraph by multiplying subsequent local transforms
  238. together on GL's matrix stack. */
  239. recursive_render(scene, scene->mRootNode);
  240. glEndList();
  241. }
  242. glCallList(scene_list);
  243. glutSwapBuffers();
  244. do_motion();
  245. }
  246. /* ---------------------------------------------------------------------------- */
  247. int loadasset (const char* path)
  248. {
  249. /* we are taking one of the postprocessing presets to avoid
  250. spelling out 20+ single postprocessing flags here. */
  251. scene = aiImportFile(path,aiProcessPreset_TargetRealtime_MaxQuality);
  252. if (scene) {
  253. get_bounding_box(&scene_min,&scene_max);
  254. scene_center.x = (scene_min.x + scene_max.x) / 2.0f;
  255. scene_center.y = (scene_min.y + scene_max.y) / 2.0f;
  256. scene_center.z = (scene_min.z + scene_max.z) / 2.0f;
  257. return 0;
  258. }
  259. return 1;
  260. }
  261. /* ---------------------------------------------------------------------------- */
  262. int main(int argc, char **argv)
  263. {
  264. struct aiLogStream stream;
  265. glutInitWindowSize(900,600);
  266. glutInitWindowPosition(100,100);
  267. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  268. glutInit(&argc, argv);
  269. glutCreateWindow("Assimp - Very simple OpenGL sample");
  270. glutDisplayFunc(display);
  271. glutReshapeFunc(reshape);
  272. /* get a handle to the predefined STDOUT log stream and attach
  273. it to the logging system. It remains active for all further
  274. calls to aiImportFile(Ex) and aiApplyPostProcessing. */
  275. stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
  276. aiAttachLogStream(&stream);
  277. /* ... same procedure, but this stream now writes the
  278. log messages to assimp_log.txt */
  279. stream = aiGetPredefinedLogStream(aiDefaultLogStream_FILE,"assimp_log.txt");
  280. aiAttachLogStream(&stream);
  281. /* the model name can be specified on the command line. If none
  282. is specified, we try to locate one of the more expressive test
  283. models from the repository (/models-nonbsd may be missing in
  284. some distributions so we need a fallback from /models!). */
  285. if( 0 != loadasset( argc >= 2 ? argv[1] : "../../test/models-nonbsd/X/dwarf.x")) {
  286. if( argc != 1 || (0 != loadasset( "../../../../test/models-nonbsd/X/dwarf.x") && 0 != loadasset( "../../test/models/X/Testwuson.X"))) {
  287. return -1;
  288. }
  289. }
  290. glClearColor(0.1f,0.1f,0.1f,1.f);
  291. glEnable(GL_LIGHTING);
  292. glEnable(GL_LIGHT0); /* Uses default lighting parameters */
  293. glEnable(GL_DEPTH_TEST);
  294. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  295. glEnable(GL_NORMALIZE);
  296. /* XXX docs say all polygons are emitted CCW, but tests show that some aren't. */
  297. if(getenv("MODEL_IS_BROKEN"))
  298. glFrontFace(GL_CW);
  299. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  300. glutGet(GLUT_ELAPSED_TIME);
  301. glutMainLoop();
  302. /* cleanup - calling 'aiReleaseImport' is important, as the library
  303. keeps internal resources until the scene is freed again. Not
  304. doing so can cause severe resource leaking. */
  305. aiReleaseImport(scene);
  306. /* We added a log stream to the library, it's our job to disable it
  307. again. This will definitely release the last resources allocated
  308. by Assimp.*/
  309. aiDetachAllLogStreams();
  310. return 0;
  311. }