texture mapping

Started by
5 comments, last by padam 14 years, 6 months ago
Hola everyone. I am using glut for creating windows and input handling. Someone suggested me DeviL for loading images, i downloaded the library. I am not able to go further cuz i couldn't find some decent tutorial. Can someone Give me small code snippet to load and bind image in opengl. Please. I also want to know that is Devil a good library for loading textures or is there any other good alternative. Gracias.
http://www.gaanza.com/bloghttp://gaanza.com/games
Advertisement
Here's sample code for DevIL.

ILuint image;ilInit();ilGenImages(1, &image);ilBindImage(image);ilLoadImage("image.jpg");GLuint texID;glGenTextures(1, &texID);glBindTexture(GL_TEXTURE_2D, texID);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_BGR, GL_UNSIGNED_BYTE, ilGetData());ilDeleteImages(1, &image);


I use FreeImage though. Here's sample code for that one.

FBITMAP* bitmap = FreeImage_Load(FIF_JPEG, "image.jpg");GLuint texID;glGenTextures(1, &texID);glBindTexture(GL_TEXTURE_2D, texID);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FreeImage_GetWidth(bitmap), FreeImage_GetHeight(bitmap), 0, GL_BGR, GL_UNSIGNED_BYTE, FreeImage_GetBits(bitmap));FreeImage_Unload(bitmap);
Author Freeworld3Dhttp://www.freeworld3d.org
Gracias. I have written this code to load texture using DeviL. But i am getting error. Please help.
Here is my code:

#include <GL/glut.h>
#include <IL/il.h>

class BoxTest
{
public:
static ILuint texid; /* ILuint is a 32bit unsigned integer.*/
static ILboolean success;
static GLuint image;

static void displayInit();
static void initGL();
static void init();
static void render();
static void update();
static void reshape(int width, int height);
static void input();
};

void BoxTest::displayInit()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);


}

void BoxTest::reshape(int width, int height)
{
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

void BoxTest::initGL()
{
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enables Smooth Shading
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
}

void BoxTest::init()
{
ilInit(); /* Initialization of DevIL */
ilGenImages(1, &texid); /* Generation of one image name */
ilBindImage(texid); /* Binding of image name */
success = ilLoadImage("Crate.bmp"); /* Loading of image "image.jpg" */

glGenTextures(1, ℑ); /* Texture name generation */
}

void BoxTest::update()
{
}

void BoxTest::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

glTranslatef(0.0f,0.0f,-6.0f);
glRotatef(45.0f,1.0f,0.0f,0.0f); // Rotate The Pyramid On It's Y Axis


glBindTexture(GL_TEXTURE_2D, image); // Select Our Texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_BGR, GL_UNSIGNED_BYTE, ilGetData());

glBegin(GL_QUADS); // Draw A Quad

//front
//glColor3f(1.0f,0.0f,0.0f); // Set The Color To Blue One Time Only
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left

//back
/*glColor3f(1.0f,0.0f,0.5f);*/
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right
glVertex3f( 1.0f,-1.0f, -1.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, -1.0f); // Bottom Left

//top
/*glColor3f(0.0f,1.0f,0.0f);*/
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left

//bottom
/*glColor3f(0.0f,0.0f,1.0f);*/
glVertex3f(-1.0f, -1.0f, -1.0f); // Top Left
glVertex3f( 1.0f, -1.0f, -1.0f); // Top Right
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left

//left
/*glColor3f(1.0f,1.0f,0.0f);*/
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, -1.0f); // Bottom Left

//right
/*glColor3f(1.0f,0.5f,0.0f);*/
glVertex3f(1.0f, 1.0f, 1.0f); // Top Left
glVertex3f(1.0f, 1.0f, -1.0f); // Top Right
glVertex3f(1.0f,-1.0f, -1.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left
glEnd(); // Done Drawing The Quad

glutSwapBuffers();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
BoxTest::displayInit();
glutCreateWindow(argv[0]);
BoxTest::initGL();
BoxTest::init();
glutDisplayFunc(BoxTest::render);
glutReshapeFunc(BoxTest::reshape);
glutMainLoop();

return 0;
}


Note: For just now i am mapping texture to just front face of box.


i am getting error:
error C2664: 'ilLoadImage' : cannot convert parameter 1 from 'const char [10]' to 'const wchar_t *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>e:\sdl_gl\gluttests\gluttests\lesson.cpp(76) : error C2065: 'GL_BGR' : undeclared identifier

Gracias.
http://www.gaanza.com/bloghttp://gaanza.com/games
wchar = sounds like your project is setup for unicode. You need to setup your project using your IDE.

For GL_BGR, you need to include glext.h in your project
http://www.opengl.org/registry/api/glext.h
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
okie i have written the code in c-style now:
#include &lt;GL/glut.h&gt;#include &lt;IL/il.h&gt;ILuint texid; /* ILuint is a 32bit unsigned integer.*/ILboolean success; GLuint image=NULL;void displayInit(){glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(640,480);glutInitWindowPosition(100,100);}void reshape(int width, int height){glMatrixMode(GL_PROJECTION);	// Select The Projection MatrixglLoadIdentity();	 // Reset The Projection Matrix// Calculate The Aspect Ratio Of The WindowgluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);	// Select The Modelview MatrixglLoadIdentity();	 // Reset The Modelview Matrix}void initGL(){glEnable(GL_TEXTURE_2D);	// Enable Texture Mapping ( NEW )glShadeModel(GL_SMOOTH);	// Enables Smooth ShadingglClearColor(0.0,0.0,0.0,0.0);glClearDepth(1.0f);	 // Depth Buffer SetupglEnable(GL_DEPTH_TEST);	// Enables Depth TestingglDepthFunc(GL_LEQUAL);	 // The Type Of Depth Test To DoglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations}void init(){ilInit(); /* Initialization of DevIL */ilGenImages(1, &texid); /* Generation of one image name */ilBindImage(texid); /* Binding of image name */success = ilLoadImage(L"Crate.bmp"); /* Loading of image "image.jpg" */glGenTextures(1, &#8465;); /* Texture name generation */}void update(){}void render(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	 // Clear The Screen And The Depth BufferglLoadIdentity();	 // Reset The ViewglTranslatef(0.0f,0.0f,-6.0f);	glRotatef(45.0f,1.0f,0.0f,0.0f);	 // Rotate The Pyramid On It's Y AxisglBindTexture(GL_TEXTURE_2D, image);	 // Select Our TextureglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, ilGetData());glBegin(GL_QUADS);	 // Draw A Quad//front//glColor3f(1.0f,0.0f,0.0f);	 // Set The Color To Blue One Time OnlyglTexCoord2f(0.0f, 1.0f);glVertex3f(-1.0f, 1.0f, 1.0f);	 // Top LeftglTexCoord2f(1.0f, 0.0f);glVertex3f( 1.0f, 1.0f, 1.0f);	 // Top RightglTexCoord2f(1.0f, 0.0f);glVertex3f( 1.0f,-1.0f, 1.0f);	 // Bottom RightglTexCoord2f(0.0f, 0.0f);glVertex3f(-1.0f,-1.0f, 1.0f);	 // Bottom Left//back/*glColor3f(1.0f,0.0f,0.5f);*/glVertex3f(-1.0f, 1.0f, -1.0f);	 // Top LeftglVertex3f( 1.0f, 1.0f, -1.0f);	 // Top RightglVertex3f( 1.0f,-1.0f, -1.0f);	 // Bottom RightglVertex3f(-1.0f,-1.0f, -1.0f);	 // Bottom Left//top/*glColor3f(0.0f,1.0f,0.0f);*/glVertex3f(-1.0f, 1.0f, -1.0f);	 // Top LeftglVertex3f( 1.0f, 1.0f, -1.0f);	 // Top RightglVertex3f( 1.0f, 1.0f, 1.0f);	 // Bottom RightglVertex3f(-1.0f, 1.0f, 1.0f);	 // Bottom Left//bottom/*glColor3f(0.0f,0.0f,1.0f);*/glVertex3f(-1.0f, -1.0f, -1.0f);	// Top LeftglVertex3f( 1.0f, -1.0f, -1.0f);	// Top RightglVertex3f( 1.0f, -1.0f, 1.0f);	 //Bottom RightglVertex3f(-1.0f, -1.0f, 1.0f);	 // Bottom Left//left/*glColor3f(1.0f,1.0f,0.0f);*/glVertex3f(-1.0f, 1.0f, -1.0f);	 // Top LeftglVertex3f(-1.0f, 1.0f, 1.0f);	 // Top RightglVertex3f(-1.0f,-1.0f, 1.0f);	 // Bottom RightglVertex3f(-1.0f,-1.0f, -1.0f);	 // Bottom Left//right/*glColor3f(1.0f,0.5f,0.0f);*/glVertex3f(1.0f, 1.0f, 1.0f);	 // Top LeftglVertex3f(1.0f, 1.0f, -1.0f);	 // Top RightglVertex3f(1.0f,-1.0f, -1.0f);	 // Bottom RightglVertex3f(-1.0f,-1.0f, 1.0f);	 // Bottom LeftglEnd();	 // Done Drawing The QuadglutSwapBuffers();}int main(int argc, char** argv){glutInit(&argc, argv);displayInit();glutCreateWindow(argv[0]);initGL();init();glutDisplayFunc(render);glutReshapeFunc(reshape);glutMainLoop();return 0;}

I think i am having linker problem:
here is the error:
1>Lesson.obj : error LNK2019: unresolved external symbol __imp__ilLoadImage@4 referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>Lesson.obj : error LNK2019: unresolved external symbol __imp__ilBindImage@4 referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>Lesson.obj : error LNK2019: unresolved external symbol __imp__ilGenImages@8 referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>Lesson.obj : error LNK2019: unresolved external symbol __imp__ilInit@0 referenced in function "void __cdecl init(void)" (?init@@YAXXZ)
1>Lesson.obj : error LNK2019: unresolved external symbol __imp__ilGetInteger@4 referenced in function "void __cdecl render(void)" (?render@@YAXXZ)
1>Lesson.obj : error LNK2019: unresolved external symbol __imp__ilGetData@0 referenced in function "void __cdecl render(void)" (?render@@YAXXZ)
1>E:\SDL_GL\GlutTests\Debug\GlutTests.exe : fatal error LNK1120: 6 unresolved externals

I did this steps to setup devil with visual studio 2005:

Tools->Options->Project and solution->VC++ directories. So there i provided the path to include and lib folder.

Then Right-Click my Project->properties->Linker->Input. Then i provided the libs: DevIL.lib ILU.lib ILUT.lib

Note: I had already set up glut.

Help !!

[Edited by - padam on October 2, 2009 8:46:59 AM]
http://www.gaanza.com/bloghttp://gaanza.com/games
Please use source tags when posting long code listings (see the FAQ for how to use them).

Quote:Original post by padam
Right-Click my Project->properties->Linker->Input. Then i provided the libs: DevIL.lib ILU.lib ILUT.lib


Where did you specify them? You're supposed to specify them in the "Additional Dependencies" field.
Quote:Original post by Gage64
Where did you specify them? You're supposed to specify them in the "Additional Dependencies" field.


sorry i'l make sure next time i'l put it within source tag.

Yeah i specified them in additional dependencies. I made a note that the project was set up for glut already, so i just did the same steps to set up devil.

Thanks.
http://www.gaanza.com/bloghttp://gaanza.com/games

This topic is closed to new replies.

Advertisement