gltest.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib GL Test Source
  4. // Copyright (C) 2000-2002 by Denton Woods
  5. // Last modified: 05/17/2002 <--Y2K Compliant! =]
  6. //
  7. // Filename: examples/gl example/gl example.c
  8. //
  9. // Description: Sample implementation of an OpenGL image viewer.
  10. //
  11. //-----------------------------------------------------------------------------
  12. //
  13. // We use FreeGlut in Windows, because it's more stable.
  14. //
  15. #ifdef _WIN32
  16. #include <GL/freeglut.h>
  17. #else
  18. #include <GL/glut.h>
  19. #endif
  20. #ifdef _DEBUG
  21. #define IL_DEBUG
  22. #endif//_DEBUG
  23. #define ILUT_USE_OPENGL
  24. #include <IL/il.h>
  25. #include <IL/ilu.h>
  26. #include <IL/ilut.h>
  27. #include "gltest.h"
  28. #include <math.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. //
  33. // Easier to do this than change the project's settings in MSVC++. (I'm lazy!)
  34. //
  35. #ifdef _MSC_VER
  36. #pragma comment(lib, "freeglut.lib")
  37. // Prevent the console window from popping up.
  38. //#pragma comment(linker, "/entry:mainCRTStartup")
  39. //#pragma comment(linker, "/subsystem:windows")
  40. #endif
  41. char *FileName;
  42. ILuint Width, Height, window;
  43. void HandleDevILErrors ()
  44. {
  45. ILenum error = ilGetError ();
  46. if (error != IL_NO_ERROR) {
  47. do {
  48. printf ("\n\n%s\n", iluErrorString (error));
  49. } while ((error = ilGetError ()));
  50. exit (1);
  51. }
  52. }
  53. extern int main(int argc, char** argv);
  54. int main(int argc, char** argv)
  55. {
  56. // No filename is specified on the command-line.
  57. if (argc < 2) {
  58. printf ("Please run as:\n\nDevIL_testGL image_filename\n");
  59. return 1;
  60. }
  61. FileName = argv[1]; // Set filename equal to the first argument.
  62. //
  63. // Check if the shared lib's version matches the executable's version.
  64. //
  65. //
  66. // fixed to get the right numbers from the right library call...
  67. //
  68. if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
  69. iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
  70. ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION) {
  71. printf ("DevIL library is out of date! Please upgrade\n");
  72. return 2;
  73. }
  74. // Needed to initialize DevIL.
  75. ilInit ();
  76. iluInit();
  77. // GL cannot use palettes anyway, so convert early.
  78. ilEnable (IL_CONV_PAL);
  79. // Gets rid of dithering on some nVidia-based cards.
  80. ilutEnable (ILUT_OPENGL_CONV);
  81. // Generate the main image name to use.
  82. ilGenImages (1, &ImgId);
  83. // Bind this image name.
  84. ilBindImage (ImgId);
  85. // Loads the image specified by File into the ImgId image.
  86. if (!ilLoadImage (FileName)) {
  87. HandleDevILErrors ();
  88. }
  89. // Make sure the window is in the same proportions as the image.
  90. // Generate the appropriate width x height less than or equal to MAX_X x MAX_Y.
  91. // Instead of just clipping Width x Height to MAX_X x MAX_Y, we scale to
  92. // an appropriate size, so the image isn't stretched/squished.
  93. Width = ilGetInteger (IL_IMAGE_WIDTH);
  94. Height = ilGetInteger (IL_IMAGE_HEIGHT);
  95. if (Width > 0) { // Don't want a divide by 0...
  96. if (Width > MAX_X) {
  97. Width = MAX_X;
  98. Height = (ILuint)(MAX_X / (ILfloat)ilGetInteger(IL_IMAGE_WIDTH) * Height);
  99. }
  100. }
  101. if (Height > 0) { // Don't want a divide by 0...
  102. if (Height > MAX_Y) {
  103. Height = MAX_Y;
  104. Width = (ILuint)(MAX_Y / (ILfloat)ilGetInteger(IL_IMAGE_HEIGHT) * Width);
  105. }
  106. }
  107. HandleDevILErrors ();
  108. // Standard glut initializations.
  109. glutInit (&argc, argv); // Standard glut initialization.
  110. glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE);
  111. glutInitWindowPosition (100, 100);
  112. glutInitWindowSize (Width, Height);
  113. window = glutCreateWindow("Developer's Image Library (DevIL) Test");
  114. ilutInit();
  115. glutDisplayFunc (DisplayFunc);
  116. glutKeyboardFunc (KeyboardFunc);
  117. // Goes into our setup function.
  118. if (Setup() == IL_FALSE)
  119. return 1;
  120. // Enter the main (Free)GLUT processing loop
  121. glutMainLoop();
  122. // Clean up any loose ends.
  123. CleanUp();
  124. return 0;
  125. }
  126. //
  127. // Standard glut resize function.
  128. //
  129. void ResizeFunc(int NewWidth, int NewHeight)
  130. {
  131. glMatrixMode (GL_PROJECTION);
  132. glLoadIdentity ();
  133. glViewport (0, 0, NewWidth, NewHeight);
  134. glOrtho (0, Width, Height, 0, -1, 1);
  135. }
  136. //
  137. // Standard glut display function.
  138. //
  139. void DisplayFunc()
  140. {
  141. glClear(GL_COLOR_BUFFER_BIT); // Clear the colour buffer.
  142. // Texture a quad with our image that fills the entire window.
  143. glBindTexture (GL_TEXTURE_2D, TexID);
  144. glBegin (GL_QUADS);
  145. /*glTexCoord2f (0.0f, 0.0f); glVertex3i (0, 0, 0);
  146. glTexCoord2f (1.0f, 0.0f); glVertex3i (Width, 0, 0);
  147. glTexCoord2f (1.0f, 1.0f); glVertex3i (Width, Height, 0);
  148. glTexCoord2f (0.0f, 1.0f); glVertex3i (0, Height, 0);*/
  149. glTexCoord2f (0.0f, 1.0f); glVertex3i (0, 0, 0);
  150. glTexCoord2f (1.0f, 1.0f); glVertex3i (Width, 0, 0);
  151. glTexCoord2f (1.0f, 0.0f); glVertex3i (Width, Height, 0);
  152. glTexCoord2f (0.0f, 0.0f); glVertex3i (0, Height, 0);
  153. glEnd();
  154. glutSwapBuffers(); // We use double buffering, so swap the buffers.
  155. }
  156. //
  157. // Standard glut idle function
  158. //
  159. void IdleFunc()
  160. {
  161. glutShowWindow ();
  162. glutPostRedisplay ();
  163. }
  164. //
  165. // Any keypress closes the window - standard glut keypress function.
  166. //
  167. void KeyboardFunc(unsigned char cChar, int nMouseX, int nMouseY)
  168. {
  169. CleanUp(); // Clean up OpenGL.
  170. glutDestroyWindow(window); // Destroy the window.
  171. #ifndef _WIN32
  172. // Must use this with regular glut, since it never returns control to main().
  173. exit(0);
  174. #endif
  175. }
  176. //
  177. // Setup OpenGL to use our image.
  178. //
  179. ILboolean Setup()
  180. {
  181. glEnable (GL_TEXTURE_2D); // Enable texturing.
  182. glMatrixMode (GL_PROJECTION); // We want to use the projection matrix.
  183. glLoadIdentity (); // Loads the identity matrix into the current matrix.
  184. // Lets ILUT know to use its OpenGL functions.
  185. ilutRenderer (ILUT_OPENGL);
  186. // Goes through all steps of sending the image to OpenGL.
  187. TexID = ilutGLBindTexImage();
  188. // We're done with our image, so we go ahead and delete it.
  189. ilDeleteImages(1, &ImgId);
  190. glOrtho(0, Width, Height, 0, -1, 1); // Set up an orthographic projection with OpenGL.
  191. return IL_TRUE;
  192. }
  193. //
  194. // Cleans up any loose ends.
  195. //
  196. void CleanUp()
  197. {
  198. if (!bCleaned) // Can only delete the texture once.
  199. glDeleteTextures (1, &TexID); // Delete our OpenGL texture.
  200. bCleaned = IL_TRUE; // Want to make sure we only delete it once.
  201. }