How am i getting an access violation??

Started by
9 comments, last by AlgorithmX2 17 years, 12 months ago
Im loading an image and trying to specify the various parameters but when I get to the TexImage2d call I get an access violation. When debugging im able to see the values that are getting returned in my assignment statements but I dont understand how I can get an access violation. Can anyone spot my error??

image = OpenImage("c:/sun.jpg");
	if(!image)
	{
		MessageBoxA(NULL,(LPCSTR)"Cannot load image into memory!!", "File not found", MB_OK);  
		exit(1);
	}
	else
	{

		int w = image->getWidth();
		int h = image->getHeight();
		const void *pxls = image->getPixels();

		glGenTextures(1, &texId);
		glBindTexture(GL_TEXTURE_2D, texId);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pxls);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
		glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
		glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glEnable(GL_TEXTURE_2D);

		//delete image;
		//image = NULL;
	}

www.lefthandinteractive.net
Advertisement
You are probably reading beyond the limits of "pxls" when you call glTexImage2D
how can that be so when all "pxls" is is just what gets returned by image->getpixels() ??
www.lefthandinteractive.net
With bitmaps, the last pixel that can be accessed is (width - 1) or (height - 1). I have no idea if this is your problem, but its worth a try. Try changing your params to (h - 1) and (w - 1).
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
I don't know what image library you are using, but it's quite possible that the stride of your texture (the size of one row) is not a multiple of four. OpenGL's default unpack alignment is set to four, so if the texture is not aligned as well, OpenGL will try reading past the end of the buffer and you will get an access violation. Normally this only happens if your texture width is not a power-of-two size.

Try calling: glPixelStorei(GL_UNPACK_ALIGNMENT, 1); and see if that fixes it.

Failing that, I would suspect a bug in the image loading. What library are you using?
are you loading a non-power of two image?
This space for rent.
no the size of the image is 256 by 256. I called it with w-1 and h-1 and it didnt complain but now im not seeing the image when i bind it. I have automatic texture coordinates turned on so im not sure why i cant see anything. Should I turn off lighting??
www.lefthandinteractive.net
depending on what parameters you pass to glTexImage, openGL expects thepixel buffer to be in a particular format, so:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pxls);

the two parameter before pxls, specify what format pxls is in:

GL_RGB --> red, green and blue compoenent
GL_UNSIGNED_BYTE --> one byte per component,

so each pixel is supposed to take 3 bytes. if you have an 8bit bitmap, each pixel is only one byte (and yo uneed to look at the paletted to figure out what each color means)

Best Regards

-Kevin
Close this Gamedev account, I have outgrown Gamedev.
Quote:Original post by kRogue
if you have an 8bit bitmap, each pixel is only one byte (and yo uneed to look at the paletted to figure out what each color means)

Well, he's loading a JPEG, so it can only be 24-bit RGB or 8-bit intensity. You don't see too many 8-bit intensity JPEGs floating around these days...

Just for the heck of it, try this instead:
glTexImage2D(GL_TEXTURE_2D, 0, 3, image->getWidth(), image->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image->getPixels());

This is copied right out from my engine, and it works just fine. :)
Use a debugger. Look at which call the access violation occurs and check that the parameters are correct. Its faster than speculating what could possibly be wrong.

For this kind of error I find valgrind extremely usefull since it can tell you exactly what you are trying to access when the violation takes place. Unfortunately for most people here, its linux/bsd only (AFAIK).

This topic is closed to new replies.

Advertisement