Sample_SimpleOpenGL.c 11 KB

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