PDA

View Full Version : extremely confused about texturing!



02-11-2004, 02:51 PM
there are a number of tutorials detailing how to map moving bitmaps onto 3d shapes with rotation etc. All I want to do is map one 256x256 texture to a flat 2d square. Using GLUT. Loading the image from file (namely background2.bmp) Could someone please put me out of my misery and post some simple code that would do the above. Thats all it has to do. No animation or anything. Simply display the bitmap in a glut window. PLEASE! (I am actually using visual studio and the bmp is loaded as a resource named IDB_BACKGROUND2 but the code can simply load from file. C++ preferable but pure C fine! anything would be greatly appriciated. (except links to over complicated tutorials. i am a serious novice trying to do a Uni project!)

nexusone
02-11-2004, 02:59 PM
nehe.gamedev.net has lot's of examples of loading a texture to a quad.

02-11-2004, 03:06 PM
they dont appear to use glut. I understand the concept of texturing the sqaure once i've loaded the bitmap. its just loading the bitmap i dont understand. The main example i have looked at is lesson 38 and tried to copy that. but there is a line which uses hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),MAKE INTRESOURCE(Texture[loop]), IMAGE_BITMAP, 0,
0, LR_CREATEDIBSECTION); that i dont understand. It says LR_CREATEDIBSECTION creates a bitmap without the colour information. But i want the colour information...may be this is why my sqaure appears white?

Nychold
02-11-2004, 03:11 PM
This is the basic process:




// .. snip ..
void init(void)
{
// Set up your OpenGL settings

glEnable(GL_TEXTURE_2D);
glGenTextures(1,texName); // texName is an usigned int
glBindTexture(GL_TEXTURE_2D,texName);

// Bilinear filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);

// Now, load your texture
// I'm assuming the variable name is texPtr
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,256,256,0,GL_ RGBA,GL_UNSIGNED_BYTE,texImage);
}
// .. snip ..
void display()
{
// Prerender stuff
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0); glVertex3f(-1.0,-1.0,0.0);
glTexCoord2f(1.0,0.0); glVertex3f( 1.0,-1.0,0.0);
glTexCoord2f(1.0,1.0); glVertex3f( 1.0, 1.0,0.0);
glTexCoord2f(0.0,1.0); glVertex3f(-1.0, 1.0,0.0);
glEnd();
glFlush();
// Finished.
}


[This message has been edited by Nychold (edited 02-11-2004).]

02-11-2004, 04:16 PM
the following is the code i have which includes all the stuff you wrote, but my square is still white and doesnt show the texture. any idea what could be wrong?

(C++ info for C coders..!! Code executes in the order: constructor ie.Graphics::Graphics() (which then calls loadBackgroundTexture()) That is one off initilasation. drawBackground() is then repeatedly called)

Code:

//#include all the right includes!

class Graphics{
private:
struct object // Create A Structure Called Object
{
int tex; // Integer Used To Select Texture
float x; // X Position
float y; // Y Position
float z; // Z Position
};

GLuint texture[1]; // Storage For 1 Texture
object background; //create object 'background that will hold' background BMP

HBITMAP hBMP; // Handle Of The Bitmap
BITMAP BMP; // Bitmap Structure

public:
Graphics(){}; //default - currently does nothing
Graphics(int argc,char* argv[]); //overloaded constructor
~Graphics() {}; //destructor - currently does nothing

void loadBackgroundTexture(void);
void drawBackground(void);
};


Graphics::Graphics(int argc,char* argv[]){
glutInit(&argc,argv); //start glut
loadBackgroundTexture();
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); //double buffered for animation. RGB colour mode
glutInitWindowSize(300,300); //window size
glutInitWindowPosition(300,150); //window position
glutCreateWindow("PicassoTalk! J.Trevan : 2k4"); //create window
glClearColor(0.0f ,0.0f ,0.0f ,0.0f); //clear colour = black
glMatrixMode(GL_PROJECTION); //select projection mode
glLoadIdentity(); //reset projection view
gluPerspective(45.0f,(GLfloat)300/(GLfloat)300,0.1f,100.0f); //perspective
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glClearDepth (1.0f); // Depth Buffer Setup
glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
glDisable(GL_DEPTH_TEST); // Disable Depth Testing
glShadeModel (GL_SMOOTH); // Select Smooth Shading
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
}


void Graphics::loadBackgroundTexture(void){
background.tex = 0; //define texture ie. which bitmap
background.x = 0; //set background position in window
background.y = 0;
background.z = 0;

byte Texture[]={ IDB_BACKGROUND2 }; // The ID of background bitmap to load

glGenTextures(1, texture); // Generate Texture (sizeof(Texture)=1 ID)

//The following loads the bitmap image. MAKEINTRESOURCE converts int to resource number
//ie. which bitmap to load. IMAGE_BITMAP defines that image being loaded is a bitmap. The
//next 2 zero's are the required size of the image, x and y. Want to use the default image
//size so leave as zero. The last parameter (LR_CREATEDIBSECTION) ???!!!!
hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),MAKE INTRESOURCE(Texture[0]), IMAGE_BITMAP, 256,
256, LR_DEFAULTCOLOR);
//hBMP now points to the bitmap data that is loaded by LoadImage( ).

if (hBMP) { // Does The Bitmap Exist?
GetObject(hBMP,sizeof(BMP), &BMP); // Get The Object
// hBMP: Handle To Graphics Object
// sizeof(BMP): Size Of Buffer For Object Information
// Buffer For Object Information

glBindTexture(GL_TEXTURE_2D, texture[0]); // Bind Our Texture

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
DeleteObject(hBMP); // Delete The Bitmap Object
}

else
cerr << "hBMP doesn't point to anything!! Error loading background bitmap!" << endl;

}

void Graphics::drawBackground(void){
glLoadIdentity(); //Reset modelview matrix
glTranslatef(0.0f,0.0f,-10.0f); //into screen

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,256,256,0,GL_ RGBA,GL_UNSIGNED_BYTE,BMP.bmBits);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0); glVertex3f(-1.0,-1.0,0.0);
glTexCoord2f(1.0,0.0); glVertex3f( 1.0,-1.0,0.0);
glTexCoord2f(1.0,1.0); glVertex3f( 1.0, 1.0,0.0);
glTexCoord2f(0.0,1.0); glVertex3f(-1.0, 1.0,0.0);
glEnd();

glLoadIdentity();
glutSwapBuffers();
}

robbinsd
02-11-2004, 04:26 PM
Hi,
I too am an extreme newbie
I experienced the same problem with following some various pieces of tuorials I found around the web and had problems with objects just showing up white. My problem had to do with lighting. can you make your rectangle just show up some color? Make sure you can do that before you attemp textures.

Dave

02-11-2004, 04:36 PM
hi,

yes i can change the colour of my square, i just cant get a texture on it! confused as texturing mapping a square is just changing the colour of the square!!!?! confused!

Nychold
02-11-2004, 05:16 PM
if (hBMP) { // Does The Bitmap Exist?
GetObject(hBMP,sizeof(BMP), &amp;BMP); // Get The Object
// hBMP: Handle To Graphics Object
// sizeof(BMP): Size Of Buffer For Object Information
// Buffer For Object Information

glBindTexture(GL_TEXTURE_2D, texture[0]); // Bind Our Texture

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
DeleteObject(hBMP); // Delete The Bitmap Object
}

Umm, okay. Do not delete the object until the program is about to shut down. So keep your HBITMAP in Graphics, and in your destructor, delete the object. See if that helps.

02-11-2004, 10:10 PM
that didnt work...i guess because the hBMP had already been used to transfer information in the line GetObject(hBMP,sizeof(BMP), &BMP); so therefore it could be deleted before the end of the program as it was no longer required?! next!!!?!!!

02-12-2004, 02:14 AM
Mr.JT, read carefully what said Nychold, especially his first post.

Compare with yours : it not the same order at all.

In english, for texturing to work, you have to :
1) read the texture file using whatever you want, so that you end up with a RGB or RGBA 1-dimensionnal C array of unsigned char values.
2) glEnable(GL_TEXTURE_2D)
3) glGenTextures
4) glBindTexture
5) you can set various glTexParameter/glTexEnv (if you use mipmaps, do not forget to send each level to opengl or use gluBuildMipmaps)
6) ** send the actual texture data to OpenGL **
with glTexImage2D and the C array in parameter.
7) now, you can free/delete the C array.

Once this init is done, each time you want to use this texture you only have to glBindTexture with the 'name' (texture id number). Then set texcoords and vertices. Use glDisable/glEnable(GL_TEXTURE_2D) to switch off/on texturing.

If you want to use several different textures, repeat the steps 3 to 6 to initialize each. And then bind to texture you need.

I hope it is clearer for you now.

Relic
02-12-2004, 03:29 AM
Isn't that the same thread again.
Read my answer in the second one.
You call loadBackGroundTexture before you have created an OpenGL context. All your texture download GL calls are ignored.

02-12-2004, 06:45 AM
Yes sorry it turned out that way...continuing on other thread. thanks