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