OpenGL cursor not showing correctly?

Started by
7 comments, last by thekingforever 12 years, 8 months ago
Hi there!

So I have drawn a cursor on the screen as a quad using a texture. I switch from perspective to orthog, draw it, and revert back. However, there's a graphics bug with the cursor....

You can see how it looks as opposed to how it's supposed to look. Forgive the black background, that's temporary.

Here's the drawing code: (C++)

[source]//draw the mouse
glBindTexture(GL_TEXTURE_2D, m_mouseTexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(mouseX+0.0, mouseY+0.0, 0.0);

glTexCoord2f(1.0f, 0.0f);
glVertex3f(mouseX+25.0, mouseY+0.0, 0.0);

glTexCoord2f(1.0f, 1.0f);
glVertex3f(mouseX+25.0, mouseY-25.0, 0.0);

glTexCoord2f(0.0f, 1.0f);
glVertex3f(mouseX+0.0, mouseY-25.0, 0.0);
glEnd();[/source]

Thanks for any help. Thought this would be the best place but please move if it's inappropriate.
Advertisement
The problem is not with drawing it, but with how you load it into memory and/or pass the data to OpenGL as a texture.
Thanks for your reply. I am using a modified version of the source code from Chapter 7 "Terrain" from Beginning OpenGL Game Programming. (not sure if I can post it here) What could I have done to cause this when the landscape and the water render fine? They repeat the texture which I think might be the problem, as I copy and pasted the routine the code uses: (I'm pretty sure this amount of code is okay even if the samples are copyright the authors)

image.Load("water.tga");
glGenTextures(1, &m_waterTexture);
glBindTexture(GL_TEXTURE_2D, m_waterTexture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
image.Release();

image.Load("mouse.tga");
glGenTextures(1, &m_mouseTexture);
glBindTexture(GL_TEXTURE_2D, m_mouseTexture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
image.Release();


I'm not sure how to specify the texture to "stretch" to the quad it's been given.

Thanks for any help. I'm new to all of this, perhaps ambitious but rubbish. But that's where we all start. :)
Your cursor image is 25 pixels, and each pixel is 3 bytes. Unless your image loader code pads the image, the rows are not properly byte-aligned. See glPixelStore and GL_UNPACK_ALIGNMENT. Either ensure that the image is properly padded, or that the alignment is changed to 1 byte. It is the typical problem, and the only source of such a problem I can see. Although I'm not entirely sure that the GLU library actually respects the alignment parameter, but I'm fairly sure it does.
Thanks. I'll have a look into it and write back. :)
Aha! Thankyou! :D

I put this in my Init function:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

Now the cursor is aligned. :)

I can now press on with transparency, thankyou very much for your help! (the book, oddly didn't seem to cover this. Or I've missed it)

Your cursor image is 25 pixels, and each pixel is 3 bytes. Unless your image loader code pads the image, the rows are not properly byte-aligned. See glPixelStore and GL_UNPACK_ALIGNMENT. Either ensure that the image is properly padded, or that the alignment is changed to 1 byte. It is the typical problem, and the only source of such a problem I can see. Although I'm not entirely sure that the GLU library actually respects the alignment parameter, but I'm fairly sure it does.


This is correct, but you should also be double-checking that your hardware supports non-power-of-two textures. Most hardware should nowadays, but you'd be surprised to find that there is still hardware out there that doesn't. If you ever release this to the public you may be in for a nasty shock.

Now things get interesting. There is some OpenGL 2.0/2.1 hardware out there that reports support for non-power-of-two textures (as it is required to by that GL_VERSION) but will drop you back to software emulation if you use them. Hello GeForce FX. If you need to run on this class of hardware, the only really safe option is to just use a power-of-two texture instead.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


[quote name='Brother Bob' timestamp='1313514840' post='4849928']
Your cursor image is 25 pixels, and each pixel is 3 bytes. Unless your image loader code pads the image, the rows are not properly byte-aligned. See glPixelStore and GL_UNPACK_ALIGNMENT. Either ensure that the image is properly padded, or that the alignment is changed to 1 byte. It is the typical problem, and the only source of such a problem I can see. Although I'm not entirely sure that the GLU library actually respects the alignment parameter, but I'm fairly sure it does.


This is correct, but you should also be double-checking that your hardware supports non-power-of-two textures. Most hardware should nowadays, but you'd be surprised to find that there is still hardware out there that doesn't. If you ever release this to the public you may be in for a nasty shock.

Now things get interesting. There is some OpenGL 2.0/2.1 hardware out there that reports support for non-power-of-two textures (as it is required to by that GL_VERSION) but will drop you back to software emulation if you use them. Hello GeForce FX. If you need to run on this class of hardware, the only really safe option is to just use a power-of-two texture instead.
[/quote]
glutBuild2DMipmap resizes the image to a power of two size if it isn't already, so while true in general, it is not an issue here.

[quote name='Brother Bob' timestamp='1313514840' post='4849928']
Your cursor image is 25 pixels, and each pixel is 3 bytes. Unless your image loader code pads the image, the rows are not properly byte-aligned. See glPixelStore and GL_UNPACK_ALIGNMENT. Either ensure that the image is properly padded, or that the alignment is changed to 1 byte. It is the typical problem, and the only source of such a problem I can see. Although I'm not entirely sure that the GLU library actually respects the alignment parameter, but I'm fairly sure it does.


This is correct, but you should also be double-checking that your hardware supports non-power-of-two textures. Most hardware should nowadays, but you'd be surprised to find that there is still hardware out there that doesn't. If you ever release this to the public you may be in for a nasty shock.

Now things get interesting. There is some OpenGL 2.0/2.1 hardware out there that reports support for non-power-of-two textures (as it is required to by that GL_VERSION) but will drop you back to software emulation if you use them. Hello GeForce FX. If you need to run on this class of hardware, the only really safe option is to just use a power-of-two texture instead.
[/quote]

Thanks for that information. I have an FX 5900 lying around (blown capacitors but I can fix that) So I'll power it up on that and see how that goes.

This topic is closed to new replies.

Advertisement