Skip to main content
GameDev.net gamedev.net
🔒 Locked

Having trouble using DevIL for OpenGL textures/DevIL to OpenGL textures?

Started by the_cyberlord Oct 29, 2004 at 11:48 AM 6 replies 2.7k views
Original Post
the_cyberlord
the_cyberlord
How do you use DevIL to work with textures in OpenGL? I'm totally confused about DevIL, can somebody, for example, write a little example of loading the image with DevIL and then drawing a plane with that texture on it? [Edited by - the_cyberlord on October 29, 2004 12:19:16 PM]
Ilici
Ilici
DevIL's API is quite similar to OpenGL's one. It has objects and states just like OpenGL.

here's a sample of my loading code:

        printf("LOAD: Loading image %s\n", fname);	if (ilIsImage(imgID) == IL_FALSE)	{		ilGenImages(1, &imgID);		if (!imgID) 		{			Error("Cannot create image");			return ERR;		}	}	ilBindImage(imgID);	ilLoad(IL_TYPE_UNKNOWN, (char*)fname);	if (ilGetError())	{		sysError("%s", iluErrorString(ilGetError()));		return ERR;	}	return OK;


and this will get you a pointer to the image data:

	ilBindImage(imgID);	return ilGetData();
the_cyberlord
the_cyberlord
yeah, OK. But how do use that loaded file in OpenGL for textures?
pex22
pex22
for OpenGL you can just call GLuint image = ilutGLLoadImage("c:\\image.bmp");
all other functions are GL-related:
glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D,image);	glBegin(GL_QUADS);		glTexCoord2f(0.0,1.0);	glVertex3f(x,height,z);	glTexCoord2f(0.0,0.0);	glVertex3f(x,y,z);	glTexCoord2f(1.0,0.0);	glVertex3f(width,y,z);	glTexCoord2f(1.0,1.0);	glVertex3f(width,height,z);		glEnd();
pex.
the_cyberlord
the_cyberlord
this doesn't work! look, here's the code I used:

	GLvoid DrawGLScene() {		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the sreen & depth buffer		glLoadIdentity();	// View reset command		glPushMatrix();		// View reset command	mouse.calculate_world(-10);	glTranslatef(mouse.world.x,mouse.world.y,mouse.world.z);	//man1.draw(); 	glBindTexture(GL_TEXTURE_2D,image);	glBegin(GL_TRIANGLES);			glTexCoord2f(0,0);		glVertex3f(0,0,0);			glTexCoord2f(0,1);		glVertex3f(0.5,-0.5,0);			glTexCoord2f(1,1);		glVertex3f(-0.5,-0.5,0);	glEnd();		glutSwapBuffers(); //Swap the display buffers		} //Blabla	int main (int argc, char **argv) {		ilInit();		iluInit();		ilutRenderer(ILUT_OPENGL);		image = ilutGLLoadImage("C:/Documents and Settings/The_CyberLord/My Documents/3D/TETRIS/texture.bmp");		glutInit(&argc, argv); //Initialize the GLUT state		glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); //Initialize the display modes		glEnable(GL_DEPTH_TEST); //Enables depth checking		glEnable(GL_LIGHTING);		glEnable(GL_TEXTURE_2D);		//windowcreation		glutInitWindowSize(400, 400); //Set the initial window size		glutInitWindowPosition(0, 0); //window in upper left corner of the screen		window = glutCreateWindow("BASIC OPENGL FRAME");//Open a window with a title		InitGL(400, 400); //Initialize the window		glutIgnoreKeyRepeat(1); //ignore keyrepeats		//Register the GLUT callback functions		glutDisplayFunc(DrawGLScene); //Function that does the OpenGL drawing		glutKeyboardFunc( NormalKey); //Function that processes normal key presses		glutVisibilityFunc(Viz); //The function to control drawing when window visibility has state x		glutTimerFunc(40,Timer,0); //The fps timer function		glutMouseFunc(MouseFunction); //Mouse		glutPassiveMotionFunc(MousePassive); //Mouse movement 		glutMotionFunc(MouseActive); //movement with pressed button		glutKeyboardUpFunc(ReleaseKey);		glutMainLoop(); //The event processing engine		return 1; }
b2b3
b2b3
I think, you have to init opengl and enable TEXTURE_2D before loading image using devil.
So try put your image loading on the end of main() before main loop.
paulc
paulc
You need an active opengl context before you try and do anything gl related, i.e. telling Devil to use the opengl renderer or loading textures directly into an opengl texture object. You could load the textures into Devil then create the textures later, but the code you have should work if you move it to after the glut setup code and before the glutMainLoop line.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.