PDA

View Full Version : Getting problem with texure map with bmp/JPG file



jenny_wui
10-25-2011, 06:05 AM
Hello, I have just copied the following code to draw a rectangle, them pasted a raw file for texture. Texture is loaded first. Then the rectangle is drawn inside the graphic loop.With raw file i can view the image properly. Now I want to paste some bmp/jpg file, but I can't see the texture. For my coding, I need to upload JPG/bmp file. Could anyone say how the following code be changed to make it work with JPG/bmp file. I also want to know how the width/height affects the texture. Sorry for my poor knowledge. If any one clarifies, it will be highly appreciated.

/************************************************** ******/
GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;

// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;

// allocate buffer
width = 256;
height = 256;
data = (BYTE*)malloc( width * height * 3 );

// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );

// allocate a texture name
glGenTextures( 1, &texture );

// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );

// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// when texture area is small, bilinear filter the closest MIP map
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first MIP map
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
wrap ? GL_REPEAT : GL_CLAMP );

// build our texture MIP maps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,
height, GL_RGB, GL_UNSIGNED_BYTE, data );

// free buffer
free( data );

return texture;

}

/************************************************** ***************************************/
void drawRectangle( ){

// setup texture mapping
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );

glPushMatrix();

glBegin( GL_QUADS );
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
glPopMatrix();
glDisable( GL_TEXTURE_2D );


}

thokra
10-25-2011, 09:12 AM
Do you set up a proper view and projection matrix anywhere? The glVertex2{df} calls default to z = 0.

If you got a projection matrix set up with a near plane at, say, n = 1.0 you won't see the quad unless you transform it accordingly.

Check your projection matrix and add the following call:


glPushMatrix();

// make sure |z| >= n in this scenario
glTranslatef(0.f, 0.f, -1.f);

...

glPopMatrix();


See if that works.

jenny_wui
10-25-2011, 07:53 PM
Hi, thanks for the reply. But my problem is that I can do texturing properly with raw file, but can't do so with bmp/jpg file. Does setting z = -1 solve this problem? Please clarify a bit so that I can understand.Thanks.

Alfonse Reinheart
10-25-2011, 10:05 PM
But my problem is that I can do texturing properly with raw file, but can't do so with bmp/jpg file.

Of course you can't. All you do is blast the file into memory and hand it off to OpenGL, expecting it to know what it is.

RAW files are so called because they are raw. They contain only data. Most files have an actual format, which means that they at least have a header that says what they contain and how big they are.

OpenGL doesn't handle file formats; it only processes image data. So you must extract the image data from the file, based on knowing what the file's format is. Then you can pass that image data to OpenGL.

Generally, people use image loading libraries (http://www.opengl.org/wiki/Image_Libraries) to handle this.

thokra
10-26-2011, 01:33 AM
Try one of the following:

http://openil.sourceforge.net
http://freeimage.sourceforge.net

Both are simple to use and they support what you need.

jenny_wui
10-26-2011, 01:37 AM
Thank u very much. I shall try and then get back to u.

jenny_wui
10-26-2011, 09:27 PM
Hello, I tried to install devil and compile it. But getting lots of problem in compilation. At first I had to rename config.h.in to config.h. After that I found the following problems:

1)fatal error C1083: Cannot open include file: 'png.h': No such file or directory

2) fatal error C1083: Cannot open include file: 'lcms.h': No such file or directory

3) fatal error C1083: Cannot open include file: 'jasper/jasper.h': No such file or directory

4) fatal error C1083: Cannot open include file: 'jpeglib.h': No such file or directory

5)fatal error C1083: Cannot open include file: 'libmng.h': No such file or directory

6) fatal error C1083: Cannot open include file: 'tiffio.h': No such file or directory

7) fatal error C1083: Cannot open include file: 'WMPGlue.h': No such file or directory


Please help me with the solution. I am totally novice in using this.