PDA

View Full Version : Texture not working properly



blessman11
02-27-2011, 08:46 AM
I've managed to load my bitmap image successfully but it won't cannot be displayed correctly on the texture.

the link below is an example of my error http://image83.webshots.com/183/2/67/36/2830267360105911122ZIybHb_fs.jpg
http://image83.webshots.com/183/2/67/36/2830267360105911122ZIybHb_fs.jpg

whereas this is the image, I could load up into open gl
http://image87.webshots.com/87/5/3/79/2144503790105911122LlWmci_ph.jpg
http://image87.webshots.com/87/5/3/79/2144503790105911122LlWmci_ph.jpg

mycode is below.

Particularly , if you could look at "myinit" and "display" function, thats where the source of my error could be.

Dont worry about the long function at the bottom, its for reading bitmap images



#include <windows.h>
#include <math.h>
#include<glut.h>
#include<iostream>
using namespace std;


GLubyte *
ReadBitmap(const char *filename,
BITMAPINFO **info);



//Initialising for image load
GLubyte *pixels; // "Image tile" pixel data
BITMAPINFO *info; // Spaceship Bitmap information



void myinit(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

pixels = ReadBitmap("tile1.bmp", &amp;info);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3,info->bmiHeader.biWidth /*checkImageWidth*/,
info->bmiHeader.biHeight /*checkImageHeight*/, 0, GL_RGB, GL_UNSIGNED_BYTE,
pixels/*&amp;checkImage[0][0][0]*/);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
}


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 0.5); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(0.5, 0.0); glVertex3f(0.0, -1.0, 0.0);

glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0,-1.41421);
glEnd();
glFlush();
glutSwapBuffers();
}







//void myReshape(int w, int h)
//{
// // on reshape and on startup, keep the viewport to be the entire size of the window
// glViewport(0,0,(GLsizei)w, (GLsizei) h);
// glMatrixMode(GL_PROJECTION);//centre of screen = origin
// glLoadIdentity();
//
// //keep our logical coordinate system constant
// gluOrtho2D(0.0,600.0,0.0,337.0);
// glMatrixMode(GL_PROJECTION); /*glMatrixMode(GL_MODELVIEW);*/ //conner of screen = origin
// glLoadIdentity();
//}

void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.6);
}



int main()
{
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition (500, 500);
glutCreateWindow("texturing");
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
}










/* read bitmap information. Returns the final bitmap pixel values */
GLubyte *
ReadBitmap(const char *filename,
BITMAPINFO **info)
{
FILE *fp; /* Open file pointer */
GLubyte *pixels; /* Bitmap pixel bits */
int imgsize; /* Size of bitmap image*/
int infosize; /* Size of header information */
BITMAPFILEHEADER header; /* File header */


// Try opening the file; use "rb" mode to read a binary file.
if ((fp = fopen(filename, "rb")) == NULL)
{cout<<"Rb mode not working"<<endl; return (NULL);}

// Read the file header
if (fread(&amp;header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
{
// Couldn't read the file header
fclose(fp);
cout<<"couldn't read the file header"<<endl; return (NULL);
}

if (header.bfType != 'MB') /* Check for BM reversed... */
{
// Not a bitmap file
fclose(fp);
cout<<"not a bitmap file"<<endl;return (NULL);
}

infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
{
fclose(fp);
return (NULL);
}

if (fread(*info, 1, infosize, fp) < infosize)
{
free(*info);
fclose(fp);
return (NULL);
}

/* Now that we have all the header info read in, allocate memory for *
* the bitmap and read it in...
*/
imgsize = (*info)->bmiHeader.biSizeImage;
// sometimes imagesize is not set in files
if (imgsize == 0)
imgsize = ((*info)->bmiHeader.biWidth *
(*info)->bmiHeader.biBitCount + 7) / 8 *
abs((*info)->bmiHeader.biHeight);

if ((pixels = (unsigned char *)malloc(imgsize)) == NULL)
{
free(*info);
fclose(fp);
return (NULL);
}

if (fread(pixels, 1, imgsize, fp) < imgsize)
{
free(*info);
free(pixels);
fclose(fp);
return (NULL);
}

fclose(fp);
return (pixels);
}

McLeary
02-27-2011, 02:20 PM
I'm not really sure about it, but I think you gonna need set the texture parameters first and than load the texture. At least, all the examples I saw do so.



glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

pixels = ReadBitmap("tile1.bmp", &amp;info);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

glTexImage2D(GL_TEXTURE_2D, 0, 3,info->bmiHeader.biWidth /*checkImageWidth*/, info->bmiHeader.biHeight /*checkImageHeight*/, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels/*&amp;checkImage[0][0][0]*/);

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);


Another thing, where is you call to glGenTexture() and glBindTexture() ?

blessman11
02-27-2011, 03:05 PM
Oh, sorry there, I didn't notice how they images were not visible. Anyway top add further description, my texture looks pixelated and the colours are wrong

remdul
02-27-2011, 04:40 PM
To fix the 'pixelated' appearance, enable linear texture filtering. Instead of GL_NEAREST, use GL_LINEAR.

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

If you will be rendering 3d models with these textures, also consider mipmapping. See OpenGL Wiki: http://www.opengl.org/wiki/Texture .

As for the other prolbem, it seems you are loading textures from BMP images. IIRC, BMPs have the pixel colors stored as BGR, rather than RGB. So, in the call to glTexImage2D, replace GL_RGB with GL_BGR.

I should also note that BMP images have padded pixels for each row if the image width is not dividable by 4, which can be source problems. Also keep in mind that BMP image data is stored 'upside down', however, OpenGL does the same internally so generally this should not be a problem.

For beginners I would recommend the TGA image format over BMP as it has a much simpler structure and is easier to read and write. FYI: http://gpwiki.org/index.php/TGA .

blessman11
03-01-2011, 10:52 AM
does TGA support transparency

ZbuffeR
03-01-2011, 12:56 PM
Yes, TGA have support for RGBA.