Advertisement

Need a hand with BMP loading and textures

Started by July 31, 2004 04:27 PM
3 comments, last by CaptainJester 20 years, 4 months ago
Hi everyone... I have a problem with my texture procedures. I`ve made a Flag Effect program but now I want to texture my flag. I took a bit of code from NeHe`s tutorials, the rest was written by me. I dont know what`s wrong. When insert the LoadTex() function to the WinMain function the flag becomes dark-red even though i didn`t choose glTexCoord!!!!! I`m really confused, so please help anyone. I will paste the code now :) : void LoadBitmap(char *filename,bmp *f) { FILE *fptr; char temp; fptr = fopen(filename,"rb"); fread(&f->fileH,sizeof(BITMAPFILEHEADER),1,fptr); fread(&f->infoH,sizeof(BITMAPINFOHEADER),1,fptr); fseek(fptr,f->fileH.bfOffBits,SEEK_SET); f->data = (unsigned char*)malloc(f->infoH.biSizeImage); fread(&f->data,f->infoH.biSizeImage,1,fptr); for(unsigned int i=0;i<f->infoH.biSizeImage;i+=3) { temp = f->data; f->data = f->data[i+2]; f->data[i+2] = temp; }; fclose(fptr); return; }; //*********************************** void LoadTex(bmp *f) { glGenTextures(1,&tex); glBindTexture(GL_TEXTURE_2D,tex); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexImage2D(GL_TEXTURE_2D,0, 3,f->infoH.biWidth,f->infoH.biHeight,0,GL_RGB, GL_UNSIGNED_BYTE,f->data); }; //********************************* And now the bmp struct: typedef struct { BITMAPFILEHEADER fileH; BITMAPINFOHEADER infoH; unsigned char *data; } bmp; //******************************** I thought you could use some code from the Render() function : void Render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0f,0.0f,0.0f,1.0f); glLoadIdentity(); glTranslatef(-5.0f,-2.0f,-30.0f); glRotatef(30.0f,1.0f,0.0f,0.0f); glRotatef(-40.0f,0.0f,1.0f,0.0f); glBindTexture(GL_TEXTURE_2D,tex); for(int l=0;l<9;l++) { for(int k=0;k<9;k++) { glBegin(GL_POLYGON); glVertex3f(points[l][k][0],points[l][k][1],points[l][k][2]); glVertex3f(points[l+1][k][0],points[l+1][k][1],points[l+1][k][2]); glVertex3f(points[l+1][k+1][0],points[l+1][k+1][1],points[l+1][k+1][2]); glVertex3f(points[l][k+1][0],points[l][k+1][1],points[l][k+1][2]); glEnd(); }; }; MoveFlag(); if(angle == 360.0f) angle = 0.0f; angle += 0.1f; }; //************************************ And my Init() function witch I call just after i create da` window :) void InitGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0f,0.0f,0.0f,1.0f); glLoadIdentity(); glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glPolygonMode(GL_FRONT,GL_LINE); glPolygonMode(GL_BACK,GL_LINE); SwapBuffers(hDC); }; //********************* Please help anyone ... BIG THX
Well, first I would use DevIL (openil.sourceforge.net) to load images, it's much easier and it supports many, many formats.

As for the code, most looks fine, the only change I would make is to not load your bitmap manually as you are doing in LoadBitmap(), because different editors can save in slightly different formats and all that. Instead, use the method presented in NeHe's article or use DevIL or another library.
- fyhuang [ site ]
Advertisement
But i want to use my procedure!!! I know that different editors can save it differently but in my other program that texture works fine! So i dont know what to do. I really want to have my own procedure for loading BMPs... So maybe somebody could give me email for sending the whole code?
Is the top-left pixel of the texture dark red? Maybe the texture coordinates are being assumed to be (0,0).

Note: I haven't actually read your code, there's too much of it, I'm just hazarding a guess.

~phil
~phil
Since you have enabled texture mapping, you need to specify texture coordinates. If you don't you end up with a dark/black coloured object. You either need to disable texture mapping or specifiy texture coordinates to fix your problem.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement