| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- #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, "freeglut.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 | GLUT_DEPTH);
- 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;
- }
- glutPostRedisplay();
- 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;
- }
|