Texture Mapping

Started by
13 comments, last by Morgul 18 years, 10 months ago
I'm not very good when it comes to texture mapping. I have tried before, and failed. Can someone please tell me what is wrong with this code?

glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, bmptextures[0]);
glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(32.0f, 128.0f,  1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(240.0f, 128.0f,  1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(240.0f, 336.0f,  1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(32.0f, 336.0f,  1.0f);
glEnd();
Just in case the problem isn't in there, I am using NeHe's bmp loader, however I have changed some things to make it have arguments so that you can choose which file to load and where it goes, etc. If the problem isn't in the above code, ask and I will post the function for loading bitmaps that I have. Thanks in advance
Advertisement
Nothing's wrong with that code you posted. It's to do with something else. Post the code you're using to create the texture. Enclose it in <source> ... </source> tags to put it in a nice source code box. Also, did you remember to call glEnable(GL_TEXTURE_2D)?
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
Alright then...

The bitmap loader (all parts):

AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image{	FILE *File=NULL;									// File Handle	if (!Filename)										// Make Sure A Filename Was Given	{		return NULL;									// If Not Return NULL	}	File=fopen(Filename,"r");							// Check To See If The File Exists	if (File)											// Does The File Exist?	{		fclose(File);									// Close The Handle		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer	}	return NULL;										// If Load Failed Return NULL}int LoadBMP(char *filename, GLuint &texid)									// Load Bitmaps And Convert To Textures{	int Status=FALSE;									// Status Indicator	AUX_RGBImageRec *TextureImage[1];					// Create Storage Space For The Texture	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit	if (TextureImage[0]=LoadBMP(filename))	{		Status=TRUE;									// Set The Status To TRUE		glGenTextures(1, &texid);					// Create The Texture		// Typical Texture Generation Using Data From The Bitmap		glBindTexture(GL_TEXTURE_2D, texid);		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	}	if (TextureImage[0])									// If Texture Exists	{		if (TextureImage[0]->data)							// If Texture Image Exists		{	   	        free(TextureImage[0]->data);                    // Free The Texture Image Memory 		}		free(TextureImage[0]);								// Free The Image Structure	}	return Status;										// Return The Status}

(Spaces are a little screwed on the above code but im not going to fix them)


My initialization of the texture thing:
GLuint	bmptextures[1];


I know that the bmp is in the right place and so on.

Oh yah, for no reason at all but just in case:

if (!LoadBMP("Data/Textures/niobraracross.BMP", bmptextures[0])){	return FALSE;									// If Texture Didn't Load Return FALSE}
do you have GLaux in your linky-thinga-majig?
It is part of the project and I have it here:

#include    <stdlib.h>#include	<windows.h>										// Header File For Windows#include	<stdio.h>										// Header File For Standard Input / Output#include	<stdarg.h>										// Header File For Variable Argument Routines#include	<string.h>										// Header File For String Management#include	<gl\gl.h>										// Header File For The OpenGL32 Library#include	<gl\glu.h>										// Header File For The GLu32 Library#include    <gl\glext.h>#include    "glaux.h"#include    "hdandn.hpp"#include    "definitions.hpp"
Thanks to the nice people on this forum, i now know one thing.....

I believe you need to add "glDisable(GL_TEXTURE_2D);" to the end of your drawing code, otherwise your display will be all messed up.... that problem bothered me for over a week untill i was told to add this code.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
i don't believe you're passing the correct type to your bmp loader.


int LoadBMP(char *filename, GLuint &texid)

GLuint bmptextures[1];

if (!LoadBMP("Data/Textures/niobraracross.BMP", bmptextures[0]))


bmptextures[0] is GLuint and loadBMP is expecting a reference to a GLUint.

try:

if (!LoadBMP("Data/Textures/niobraracross.BMP", &bmptextures[0]))

or just

if (!LoadBMP("Data/Textures/niobraracross.BMP", bmptextures))



I changed the bmp loading code to:

bool LoadBMP(LPTSTR szFileName, GLuint &texid)					// Creates Texture From A Bitmap File{	HBITMAP hBMP;														// Handle Of The Bitmap	BITMAP	BMP;														// Bitmap Structure	glGenTextures(1, &texid);											// Create The Texture	hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );	if (!hBMP)															// Does The Bitmap Exist?		return FALSE;													// If Not Return False	GetObject(hBMP, sizeof(BMP), &BMP);									// Get The Object																		// hBMP:        Handle To Graphics Object																		// sizeof(BMP): Size Of Buffer For Object Information																		// &BMP:        Buffer For Object Information	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);								// Pixel Storage Mode (Word Alignment / 4 Bytes)	// Typical Texture Generation Using Data From The Bitmap	glBindTexture(GL_TEXTURE_2D, texid);								// Bind To The Texture ID	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Min Filter	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Mag Filter	glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);	DeleteObject(hBMP);													// Delete The Object	return TRUE;														// Loading Was Successful}



It still doesn't work. This is more nehe stuff.
Oh and I tried that glDisable thing and it didn't work.
Define "failed". Polygon isn't drawn? Polygon is drawn but not textured? Polygon is drawn but with wrong texture? Polygon is drawn but with corrupted texture?

Some issues that might be causing this problem: Do you have texturing enabled? Do you have a valid OpenGL context at the point where you create the texture? Is your texture Power-Of-Two sized?

Oh, and AP was wrong about the references thing. You don't need to take the address of the variable when using references, only when using pointers (and you should prefer to use references where possible).

Enigma

This topic is closed to new replies.

Advertisement