3dtest.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #ifdef _WIN32
  2. #include <GL/freeglut.h>
  3. #else
  4. #include <GL/glut.h>
  5. #endif
  6. #include <IL/il.h>
  7. #include <IL/ilu.h>
  8. //#define ILUT_USE_OPENGL
  9. #include <IL/ilut.h>
  10. #include "3dtest.h"
  11. #include <math.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <malloc.h>
  16. #ifdef _MSC_VER
  17. #pragma comment(lib, "opengl32.lib")
  18. #pragma comment(lib, "freeglut.lib")
  19. // Prevent the console window from popping up.
  20. #pragma comment(linker, "/entry:mainCRTStartup")
  21. #pragma comment(linker, "/subsystem:windows")
  22. #endif
  23. char *File;
  24. ILint Width, Height, Depth, Window;
  25. ILuint ActiveImage = 0;
  26. int main(int argc, char** argv)
  27. {
  28. //char Test[6] = { 0, 0, 0, 0, 0, 0 };
  29. if (argc < 2) {
  30. //cout << "Please specify a filename." << endl;
  31. return 1;
  32. }
  33. File = argv[1];
  34. if (argc > 2) {
  35. TransFactor = atoi(argv[2]) != 0 ? -atoi(argv[2]) : TransFactor;
  36. }
  37. if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
  38. ilGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
  39. ilGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION) {
  40. //cout << "OpenIL version is different...exiting!" << endl;
  41. return 2;
  42. }
  43. ilInit();
  44. //ilEnable(IL_CONV_PAL);
  45. ilutEnable(ILUT_OPENGL_CONV);
  46. glutInit(&argc, argv);
  47. ilGenImages(1, &ImgId);
  48. ilBindImage(ImgId);
  49. ilLoadImage(File);
  50. // Generate the appropriate width x height less than or equal to MAX_X x MAX_Y.
  51. // Instead of just clipping Width x Height to MAX_X x MAX_Y, we scale to
  52. // an appropriate size, so the image isn't stretched/squished.
  53. Width = ilGetInteger(IL_IMAGE_WIDTH);
  54. Height = ilGetInteger(IL_IMAGE_HEIGHT);
  55. if (Width > 0) { // Don't want a divide by 0...
  56. if (Width > MAX_X) {
  57. Width = MAX_X;
  58. Height = (ILuint)(MAX_X / (ILfloat)ilGetInteger(IL_IMAGE_WIDTH) * Height);
  59. }
  60. }
  61. if (Height > 0) { // Don't want a divide by 0...
  62. if (Height > MAX_Y) {
  63. Height = MAX_Y;
  64. Width = (ILuint)(MAX_Y / (ILfloat)ilGetInteger(IL_IMAGE_HEIGHT) * Width);
  65. }
  66. }
  67. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  68. glutInitWindowPosition(100, 100);
  69. glutInitWindowSize(Width, Height);
  70. Window = glutCreateWindow("Open Image Library (OpenIL) Test");
  71. glutDisplayFunc(DisplayFunc);
  72. glutKeyboardFunc(KeyboardFunc);
  73. glutSpecialFunc(KeySpecialFunc);
  74. if (Setup() == IL_FALSE)
  75. return 1;
  76. // Enter the main (Free)GLUT processing loop
  77. glutMainLoop();
  78. CleanUp();
  79. return 0;
  80. }
  81. void ResizeFunc(int NewWidth, int NewHeight)
  82. {
  83. glMatrixMode(GL_PROJECTION);
  84. glLoadIdentity();
  85. glViewport(0, 0, NewWidth, NewHeight);
  86. //glOrtho(0, Width, 0, Height, -100, 1);
  87. SetPerspective(50.0f);
  88. }
  89. void DisplayFunc()
  90. {
  91. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92. glLoadIdentity();
  93. SetPerspective(50.0f);
  94. glTranslatef(0.0f, 0.0f, TransFactor);
  95. glRotatef(Angle, 0.0f, 1.0f, 0.0f);
  96. glBindTexture(GL_TEXTURE_2D, TexID1);
  97. glBegin(GL_QUADS);
  98. glTexCoord2f(0.0f, 0.0f);
  99. glVertex3i(-Width, -Height, Depth);
  100. glTexCoord2f(1.0f, 0.0f);
  101. glVertex3i(Width, -Height, Depth);
  102. glTexCoord2f(1.0f, 1.0f);
  103. glVertex3i(Width, Height, Depth);
  104. glTexCoord2f(0.0f, 1.0f);
  105. glVertex3i(-Width, Height, Depth);
  106. glEnd();
  107. glBindTexture(GL_TEXTURE_2D, TexID2);
  108. glBegin(GL_QUADS);
  109. glTexCoord2f(0.0f, 0.0f);
  110. glVertex3i(-Width, -Height, Depth);
  111. glTexCoord2f(1.0f, 0.0f);
  112. glVertex3i(-Width, -Height, -Depth);
  113. glTexCoord2f(1.0f, 1.0f);
  114. glVertex3i(-Width, Height, -Depth);
  115. glTexCoord2f(0.0f, 1.0f);
  116. glVertex3i(-Width, Height, Depth);
  117. glEnd();
  118. glBindTexture(GL_TEXTURE_2D, TexID3);
  119. glBegin(GL_QUADS);
  120. glTexCoord2f(0.0f, 0.0f);
  121. glVertex3i(Width, -Height, Depth);
  122. glTexCoord2f(1.0f, 0.0f);
  123. glVertex3i(Width, -Height, -Depth);
  124. glTexCoord2f(1.0f, 1.0f);
  125. glVertex3i(Width, Height, -Depth);
  126. glTexCoord2f(0.0f, 1.0f);
  127. glVertex3i(Width, Height, Depth);
  128. glEnd();
  129. glBindTexture(GL_TEXTURE_2D, TexID4);
  130. glBegin(GL_QUADS);
  131. glTexCoord2f(0.0f, 0.0f);
  132. glVertex3i(-Width, -Height, -Depth);
  133. glTexCoord2f(1.0f, 0.0f);
  134. glVertex3i(Width, -Height, -Depth);
  135. glTexCoord2f(1.0f, 1.0f);
  136. glVertex3i(Width, Height, -Depth);
  137. glTexCoord2f(0.0f, 1.0f);
  138. glVertex3i(-Width, Height, -Depth);
  139. glEnd();
  140. glFlush();
  141. glFinish();
  142. glutSwapBuffers();
  143. }
  144. void IdleFunc()
  145. {
  146. glutShowWindow();
  147. glutPostRedisplay();
  148. }
  149. void KeyboardFunc(unsigned char cChar, int nMouseX, int nMouseY)
  150. {
  151. if (cChar >= '0' && cChar <= '9') {
  152. ActiveImage = cChar - '0';
  153. CleanUp();
  154. GenSides();
  155. return;
  156. }
  157. if (cChar == '+' || cChar == '=') {
  158. ActiveImage++;
  159. CleanUp();
  160. GenSides();
  161. return;
  162. }
  163. if (cChar == '-' || cChar == '_') {
  164. if (ActiveImage == 0)
  165. return;
  166. ActiveImage--;
  167. CleanUp();
  168. GenSides();
  169. return;
  170. }
  171. CleanUp();
  172. glutDestroyWindow(Window);
  173. #ifndef _WIN32
  174. /* Siigron: added exit(), since glutDestroyWindow() doesn't exit the
  175. program with "normal" GLUT */
  176. exit(0);
  177. #endif
  178. }
  179. void KeySpecialFunc(int Key, int x, int y)
  180. {
  181. switch (Key)
  182. {
  183. case GLUT_KEY_UP:
  184. TransFactor += 10.0f;
  185. //glTranslatef(0.0f, 0.0f, 10.0f);
  186. break;
  187. case GLUT_KEY_DOWN:
  188. TransFactor -= 10.0f;
  189. //glTranslatef(0.0f, 0.0f, -10.0f);
  190. break;
  191. case GLUT_KEY_RIGHT:
  192. Angle += 10.0f;
  193. //glRotatef(10.0f, 0.0f, 1.0f, 0.0f);
  194. break;
  195. case GLUT_KEY_LEFT:
  196. Angle -= 10.0f;
  197. //glRotatef(-10.0f, 0.0f, 1.0f, 0.0f);
  198. break;
  199. }
  200. glutPostRedisplay();
  201. return;
  202. }
  203. #define PI 3.14159265
  204. void SetPerspective(float Fov)
  205. {
  206. float fov = (float)tan(Fov * .5f * PI / 180.0f);
  207. float Aspect = 0.0f;
  208. if (Height != 0)
  209. Aspect = Width / (float)Height;
  210. glMatrixMode(GL_PROJECTION);
  211. glLoadIdentity();
  212. glFrustum(-fov * Aspect, fov * Aspect, -fov, fov, 1.0f, 10000.0f);
  213. return;
  214. }
  215. ILboolean Setup()
  216. {
  217. glEnable(GL_DEPTH_TEST);
  218. glDepthFunc(GL_LEQUAL); // or should this be GL_LESS?
  219. glClearDepth(1);
  220. glEnable(GL_TEXTURE_2D);
  221. glMatrixMode(GL_PROJECTION);
  222. glLoadIdentity();
  223. ilutRenderer(ILUT_OPENGL);
  224. if (!GenSides())
  225. return IL_FALSE;
  226. TransFactor += -Depth;
  227. glTranslatef(0.0f, 0.0f, -100.0f);
  228. return IL_TRUE;
  229. }
  230. ILboolean GenSides()
  231. {
  232. ILubyte *Buffer, *Data, Bpp, Bpc;
  233. ILuint TempImage;
  234. ILenum Format, Type;
  235. ILint SizePlane, Bps, c, y, z, i;
  236. ilActiveImage(ActiveImage);
  237. Bpp = ilGetInteger(IL_IMAGE_BPP);
  238. Bpc = ilGetInteger(IL_IMAGE_BPC);
  239. Format = ilGetInteger(IL_IMAGE_FORMAT);
  240. Type = ilGetInteger(IL_IMAGE_TYPE);
  241. // Front
  242. TexID1 = ilutGLBindTexImage();
  243. Width = ilGetInteger(IL_IMAGE_WIDTH);
  244. Height = ilGetInteger(IL_IMAGE_HEIGHT);
  245. Depth = ilGetInteger(IL_IMAGE_DEPTH);
  246. ilGenImages(1, &TempImage);
  247. SizePlane = ilGetInteger(IL_IMAGE_PLANESIZE);
  248. SizePlane = Width * Height * Bpp * Bpc;
  249. Bps = Width * Bpp * Bpc;
  250. Data = ilGetData();
  251. // Left
  252. i = 0;
  253. Buffer = (ILubyte*)malloc(Height * Depth * Bpp * Bpc);
  254. for (y = 0; y < Height; y++) {
  255. for (z = 0; z < Depth; z++) {
  256. for (c = 0; c < Bpp * Bpc; c++) {
  257. Buffer[i++] = Data[z * SizePlane + y * Bps + c];
  258. }
  259. }
  260. }
  261. ilBindImage(TempImage);
  262. ilTexImage(Depth, Height, 1, Bpp, Format, Type, Buffer);
  263. TexID2 = ilutGLBindTexImage();
  264. free(Buffer);
  265. // Right
  266. ilBindImage(ImgId);
  267. ilActiveImage(ActiveImage);
  268. i = 0;
  269. Buffer = (ILubyte*)malloc(Height * Depth * Bpp * Bpc);
  270. for (y = 0; y < Height; y++) {
  271. for (z = 0; z < Depth; z++) {
  272. for (c = 0; c < Bpp * Bpc; c++) {
  273. Buffer[i++] = Data[z * SizePlane + y * Bps + (Width - 1) * Bpp * Bpc + c];
  274. }
  275. }
  276. }
  277. ilBindImage(TempImage);
  278. ilTexImage(Depth, Height, 1, Bpp, Format, Type, Buffer);
  279. TexID3 = ilutGLBindTexImage();
  280. free(Buffer);
  281. // Back
  282. ilBindImage(ImgId);
  283. ilActiveImage(ActiveImage);
  284. Buffer = (ILubyte*)malloc(Width * Height * Bpp * Bpc);
  285. ilCopyPixels(0, 0, Depth-1, Width, Height, 1, Format, Type, Buffer);
  286. ilBindImage(TempImage);
  287. ilTexImage(Width, Height, 1, Bpp, Format, Type, Buffer);
  288. TexID4 = ilutGLBindTexImage();
  289. free(Buffer);
  290. //ilDeleteImages(1, &ImgId);
  291. ilDeleteImages(1, &TempImage);
  292. ilBindImage(ImgId);
  293. return IL_TRUE;
  294. }
  295. void CleanUp()
  296. {
  297. glDeleteTextures(1, &TexID1);
  298. glDeleteTextures(1, &TexID2);
  299. glDeleteTextures(1, &TexID3);
  300. glDeleteTextures(1, &TexID4);
  301. return;
  302. }
  303. void ExitClean()
  304. {
  305. if (!bCleaned) {
  306. glDeleteTextures(1, &TexID1);
  307. glDeleteTextures(1, &TexID2);
  308. glDeleteTextures(1, &TexID3);
  309. glDeleteTextures(1, &TexID4);
  310. ilDeleteImages(1, &ImgId);
  311. }
  312. bCleaned = IL_TRUE;
  313. return;
  314. }