Jump to content
  • Advertisement
Sign in to follow this  
broady

DevIL - Look at this code please

This topic is 3684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, after 2-3 days of study i am able to use libs. After 1 day of pain with libpng i went for the DevIL lib. I like it since it's openGL-style. I added the include and lib folders to my project options search directories, linked correctly and added the DLLs in wyndows/system. The code is not working. Here it is, pretty simple (it's a sort of porting of the well khnown tutorial about DevIL and SDL):
#include all...

using namespace std;

static GLuint image;
static ILuint texid;
ILboolean success;



void init(void)
{

     if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
  {
    cout<<"version error";
  }

    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    ilGenImages(1, &texid); /* Generation of one image name */
    ilBindImage(texid); /* Binding of image name */

    cout<<ilGetError()<<"\n";
    cout<<iluErrorString(ilGetError()); //no error till here
    success = ilLoadImage("image.jpg"); /* Loading of image "image.jpg" */

      if (success) /* If no error occured: */
  {
      printf("success");
    success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); /* Convert every colour component into
      unsigned byte. If your image contains alpha channel you can replace IL_RGB with IL_RGBA */
    if (!success)
    {
      /* Error occured */
      printf("conversion failed");
    }
    glGenTextures(1, ℑ); /* Texture name generation */
    glBindTexture(GL_TEXTURE_2D, image); /* Binding of texture name */
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
    ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
    ilGetData()); /* Texture specification */
  }
  else
  {
    /* Error occured */
    printf("image loading failed");
  }

  ilDeleteImages(1, &texid);


}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    glBindTexture(GL_TEXTURE_2D, image);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
    glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
    glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
    glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
    glEnd();
    glFlush();
    glDisable(GL_TEXTURE_2D);
}


int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize(250, 250);
   glutInitWindowPosition(100, 100);
   glutCreateWindow(argv[0]);
   init();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);

   ilInit();
   iluInit();
   ilutRenderer(ILUT_OPENGL);//must be done after opengl is initialized

   glutMainLoop();
   return 0;
}
It can be compiled without errors. When i go to run it i have the hated vista error message. It says nothing about the problem. Debugging here and there i saw the problem is ilLoadImage(). I can't look at the error since after this function call i got a crash and no more code is executed. Debugger says to be a segmentation error. I have this kind of errors when i.e. i try to store data in a too small array. Here but i dont touch an array, Devil should take care of space allocation. Any suggestion?

Share this post


Link to post
Share on other sites
Advertisement
[ source] tags are your friend...

You're calling DevIL functions without calling the appropriate initializer functions first. Move ilInit(), iluInit(), and ilutRenderer(ILUT_OPENGL) to the top of main() (although, as your comment points out, put the OpenGL init functions before ilutRenderer). That should solve your (immediate) problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by EmrldDrgn
[ source] tags are your friend...

You're calling DevIL functions without calling the appropriate initializer functions first. Move ilInit(), iluInit(), and ilutRenderer(ILUT_OPENGL) to the top of main() (although, as your comment points out, put the OpenGL init functions before ilutRenderer). That should solve your (immediate) problem.


yeah sorry u are right, i am used to use the tab and they dont work here.
I am trying to solve with ur suggestions. Ty a lot ED

Share this post


Link to post
Share on other sites
Dear EmrldDrgn,

i feel so stupid :D Yeah moving the DevIL inizializzations before calling the function solved my crash problems.

An other question: i have to put the image in minGW/bin folder or the image wont be loaded. How can i say him to load the images from /projectdir/Data/image.***?

Thanks very much mate

Share this post


Link to post
Share on other sites
Btw, what's this character: 'ℑ'? You use it in the call to glGenTextures().
Also, quite a colorful mix of cout, printf(), C commenting, C++ commenting and documenting the obvious. Hope that's only testing code ;)

DevIL will simply forward the file names to the file system calls, so it's going to use the current working directory. Either configure your debugger to explicitely launch the application in the directory of your choice.

Otherwise, find out which directory the .exe is in (on Windows, you can use GetModuleFileName()) and use that to build absolute paths when loading your files.

Share this post


Link to post
Share on other sites
Quote:
Original post by Cygon
Otherwise, find out which directory the .exe is in (on Windows, you can use GetModuleFileName()) and use that to build absolute paths when loading your files.


which won't work if you've configured your debugger to use a different working directory. GetCurrentDirectory() [getcwd in linux] is probably more suitable, that or define an environment var (i.e. $MY_ASSETS_DIR).

Share this post


Link to post
Share on other sites
Thanks for the interest guys

Quote:

Either configure your debugger to explicitely launch the application in the directory of your choice.


How can i do this?
Bro

Share this post


Link to post
Share on other sites
Quote:
Original post by broady
Thanks for the interest guys

Quote:

Either configure your debugger to explicitely launch the application in the directory of your choice.


How can i do this?
Bro

It would be different in each IDE. What are you using?

In some projects I have put in a little class which constructs itself at the starting of the application and sets the working directory to the executables path. This code works for Windows.

// set_working_dir.cpp
#include <windows.h>
#include <shlwapi.h>
// link with shlwapi.lib

class wd_helper
{
public:
wd_helper (void)
{
// get full executable filename of the application
char filename[MAX_PATH];
GetModuleFilename (NULL, filename, MAX_PATH);

// remove the filename part of the path to leave directory
PathRemoveFileSpec (filename);

// set the working directory
SetCurrentDirectory (filename);
}
};

// having a global instance of wd_helper means its constructed before the main() or WinMain() entry point is called
wd_helper g_WdHelper;

This way, no matter how the program is launched, the working directory can always be relied on to be the same directory as the executable.

Windows can often cause problems with links or start menu 'pins' not correctly setting the working directory and thus your application would not be able to find relative data files it uses since the working directory is not the correct folder that your relative paths rely on.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!