Sample_SimpleOpenGL.c 11 KB

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