Sample_SimpleOpenGL.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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.h"
  15. #include "aiPostProcess.h"
  16. #include "aiScene.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. void set_float4(float f[4], float a, float b, float c, float d)
  81. {
  82. f[0] = a;
  83. f[1] = b;
  84. f[2] = c;
  85. f[3] = d;
  86. }
  87. // ----------------------------------------------------------------------------
  88. void apply_material(const struct aiMaterial *mtl)
  89. {
  90. float c[4];
  91. GLenum fill_mode;
  92. int ret1, ret2;
  93. struct aiColor4D diffuse;
  94. struct aiColor4D specular;
  95. struct aiColor4D ambient;
  96. struct aiColor4D emission;
  97. float shininess, strength;
  98. int two_sided;
  99. int wireframe;
  100. int max;
  101. set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f);
  102. if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
  103. color4_to_float4(&diffuse, c);
  104. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c);
  105. set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
  106. if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular))
  107. color4_to_float4(&specular, c);
  108. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c);
  109. set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f);
  110. if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient))
  111. color4_to_float4(&ambient, c);
  112. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, c);
  113. set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
  114. if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission))
  115. color4_to_float4(&emission, c);
  116. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, c);
  117. max = 1;
  118. ret1 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max);
  119. max = 1;
  120. ret2 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &max);
  121. if((ret1 == AI_SUCCESS) && (ret2 == AI_SUCCESS))
  122. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess * strength);
  123. else {
  124. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);
  125. set_float4(c, 0.0f, 0.0f, 0.0f, 0.0f);
  126. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c);
  127. }
  128. max = 1;
  129. if(AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_ENABLE_WIREFRAME, &wireframe, &max))
  130. fill_mode = wireframe ? GL_LINE : GL_FILL;
  131. else
  132. fill_mode = GL_FILL;
  133. glPolygonMode(GL_FRONT_AND_BACK, fill_mode);
  134. max = 1;
  135. if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && two_sided)
  136. glEnable(GL_CULL_FACE);
  137. else
  138. glDisable(GL_CULL_FACE);
  139. }
  140. // ----------------------------------------------------------------------------
  141. // Can't send color down as a pointer to aiColor4D because AI colors are ABGR.
  142. void Color4f(const struct aiColor4D *color)
  143. {
  144. glColor4f(color->r, color->g, color->b, color->a);
  145. }
  146. // ----------------------------------------------------------------------------
  147. void recursive_render (const struct aiScene *sc, const struct aiNode* nd)
  148. {
  149. 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. if(mesh->mColors[0] != NULL) {
  166. glEnable(GL_COLOR_MATERIAL);
  167. } else {
  168. glDisable(GL_COLOR_MATERIAL);
  169. }
  170. for (t = 0; t < mesh->mNumFaces; ++t) {
  171. const struct aiFace* face = &mesh->mFaces[t];
  172. GLenum face_mode;
  173. switch(face->mNumIndices) {
  174. case 1: face_mode = GL_POINTS; break;
  175. case 2: face_mode = GL_LINES; break;
  176. case 3: face_mode = GL_TRIANGLES; break;
  177. default: face_mode = GL_POLYGON; break;
  178. }
  179. glBegin(face_mode);
  180. for(i = 0; i < face->mNumIndices; i++) {
  181. int index = face->mIndices[i];
  182. if(mesh->mColors[0] != NULL)
  183. Color4f(&mesh->mColors[0][index]);
  184. if(mesh->mNormals != NULL)
  185. glNormal3fv(&mesh->mNormals[index].x);
  186. glVertex3fv(&mesh->mVertices[index].x);
  187. }
  188. glEnd();
  189. }
  190. }
  191. // draw all children
  192. for (n = 0; n < nd->mNumChildren; ++n) {
  193. recursive_render(sc, nd->mChildren[n]);
  194. }
  195. glPopMatrix();
  196. }
  197. // ----------------------------------------------------------------------------
  198. void do_motion (void)
  199. {
  200. static GLint prev_time = 0;
  201. int time = glutGet(GLUT_ELAPSED_TIME);
  202. angle += (time-prev_time)*0.01;
  203. prev_time = time;
  204. glutPostRedisplay ();
  205. }
  206. // ----------------------------------------------------------------------------
  207. void display(void)
  208. {
  209. float tmp;
  210. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  211. glMatrixMode(GL_MODELVIEW);
  212. glLoadIdentity();
  213. gluLookAt(0.f,0.f,3.f,0.f,0.f,-5.f,0.f,1.f,0.f);
  214. // rotate it around the y axis
  215. glRotatef(angle,0.f,1.f,0.f);
  216. // scale the whole asset to fit into our view frustum
  217. tmp = scene_max.x-scene_min.x;
  218. tmp = aisgl_max(scene_max.y - scene_min.y,tmp);
  219. tmp = aisgl_max(scene_max.z - scene_min.z,tmp);
  220. tmp = 1.f / tmp;
  221. glScalef(tmp, tmp, tmp);
  222. // center the model
  223. glTranslatef( -scene_center.x, -scene_center.y, -scene_center.z );
  224. // if the display list has not been made yet, create a new one and
  225. // fill it with scene contents
  226. if(scene_list == 0) {
  227. scene_list = glGenLists(1);
  228. glNewList(scene_list, GL_COMPILE);
  229. // now begin at the root node of the imported data and traverse
  230. // the scenegraph by multiplying subsequent local transforms
  231. // together on GL's matrix stack.
  232. recursive_render(scene, scene->mRootNode);
  233. glEndList();
  234. }
  235. glCallList(scene_list);
  236. glutSwapBuffers();
  237. do_motion();
  238. }
  239. // ----------------------------------------------------------------------------
  240. int loadasset (const char* path)
  241. {
  242. // we are taking one of the postprocessing presets to avoid
  243. // writing 20 single postprocessing flags here.
  244. scene = aiImportFile(path,aiProcessPreset_TargetRealtime_Quality);
  245. if (scene) {
  246. get_bounding_box(&scene_min,&scene_max);
  247. scene_center.x = (scene_min.x + scene_max.x) / 2.0f;
  248. scene_center.y = (scene_min.y + scene_max.y) / 2.0f;
  249. scene_center.z = (scene_min.z + scene_max.z) / 2.0f;
  250. return 0;
  251. }
  252. return 1;
  253. }
  254. // ----------------------------------------------------------------------------
  255. int main(int argc, char **argv)
  256. {
  257. struct aiLogStream stream;
  258. glutInitWindowSize(900,600);
  259. glutInitWindowPosition(100,100);
  260. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  261. glutInit(&argc, argv);
  262. glutCreateWindow("Assimp - Very simple OpenGL sample");
  263. glutDisplayFunc(display);
  264. glutReshapeFunc(reshape);
  265. // get a handle to the predefined STDOUT log stream and attach
  266. // it to the logging system. It will be active for all further
  267. // calls to aiImportFile(Ex) and aiApplyPostProcessing.
  268. stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
  269. aiAttachLogStream(&stream);
  270. // ... exactly the same, but this stream will now write the
  271. // log file to assimp_log.txt
  272. stream = aiGetPredefinedLogStream(aiDefaultLogStream_FILE,"assimp_log.txt");
  273. aiAttachLogStream(&stream);
  274. if( 0 != loadasset( argc >= 2 ? argv[1] : "../../test/models/X/dwarf.x")) {
  275. if( argc != 1 || 0 != loadasset( "../../../../test/models/X/dwarf.x")) {
  276. return -1;
  277. }
  278. }
  279. glClearColor(0.1f,0.1f,0.1f,1.f);
  280. glEnable(GL_LIGHTING);
  281. glEnable(GL_LIGHT0); // Uses default lighting parameters
  282. glEnable(GL_DEPTH_TEST);
  283. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  284. glEnable(GL_NORMALIZE);
  285. // XXX docs say all polygons are emitted CCW, but tests show that some aren't.
  286. if(getenv("MODEL_IS_BROKEN"))
  287. glFrontFace(GL_CW);
  288. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  289. glutGet(GLUT_ELAPSED_TIME);
  290. glutMainLoop();
  291. // cleanup - calling 'aiReleaseImport' is important, as the library
  292. // keeps internal resources until the scene is freed again. Not
  293. // doing so can cause severe resource leaking.
  294. aiReleaseImport(scene);
  295. // We added a log stream to the library, it's our job to disable it
  296. // again. This will definitely release the last resources allocated
  297. // by Assimp.
  298. aiDetachAllLogStreams();
  299. return 0;
  300. }