• Advertisement
Sign in to follow this  

How am i getting an access violation??

This topic is 4321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
	}

Share this post


Link to post
Share on other sites
Advertisement
You are probably reading beyond the limits of "pxls" when you call glTexImage2D

Share this post


Link to post
Share on other sites
how can that be so when all "pxls" is is just what gets returned by image->getpixels() ??

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
to answer the 'no texture problem' if the result of your image->getwidth() returns 256, then you subtract 1, then your useing non-power-of-2 textures, and that usually results in no texture being displaying, on anything but brand new hardware. I can't say why your getting the access violation tho...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement