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