Jump to content
  • Advertisement
Sign in to follow this  
NIm

OpenGL new to textures

This topic is 3557 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

So I'm just trying to get things to work, as I'm playing with textures and learning openGl. I'm using python, pygame, and pyopengl. It seems that I just am not getting a texture. I've read a number of tutorials, googled, and can't seem to pin this bug down. I'm probably failing to initialize something. Heres my loading code. I know that the image is getting loaded because sprite data has a length of 40 some odd kilobytes. Length and width of the image are correctly handled. If I load a long, skinny image with this code, I get a long skinny quad. This is becauseof the last two lines of the first listing.

heres my loading code
mm = dude()
spritesurf = pygame.image.load("soldier.png")
spritedata = pygame.image.tostring(spritesurf,"RGBA")
print len(spritedata)
spritewidth = spritesurf.get_width()
spriteheight = spritesurf.get_height()
mm.sprite = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, mm.sprite)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spritewidth,spriteheight,0,GL_RGBA,GL_UNSIGNED_BYTE, spritedata)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spritewidth,spriteheight,0,GL_RGBA,GL_UNSIGNED_BYTE, spritedata)
mm.spritewidth = spritewidth * mm.radius / min(spritewidth,spriteheight)
mm.spriteheight = spriteheight * mm.radius / min(spritewidth,spriteheight)

This is the relevant section of my rendering code:
    glBindTexture(GL_TEXTURE_2D, mm.sprite)
    glBegin(GL_QUADS)
    glTexCoord2f(0,0);  glVertex(-mm.spritewidth,-mm.spriteheight ,0)
    glTexCoord2f(1,0);  glVertex( mm.spritewidth,-mm.spriteheight ,0)
    glTexCoord2f(1,1);  glVertex( mm.spritewidth, mm.spriteheight ,0)
    glTexCoord2f(0,1);  glVertex(-mm.spritewidth, mm.spriteheight ,0)
    glEnd()


Share this post


Link to post
Share on other sites
Advertisement
You have 2 calls to glTexImage in the loading section, try adding these two after loading and binding the texture for the first time

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

and try this after binding the texture for rendering

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)


Share this post


Link to post
Share on other sites
Thank you for your reply. These calls seem to have no effect. I still get the right sized rectangle, drawn with the last color that I used, instead of the texture. Is there perhaps something special I must do because the image has an alpha channel? *goes off to research those calls*

Share this post


Link to post
Share on other sites
Hey that kinda worked! rating++ I say kinda because it messed up all my color information(draws black for everything- lucky I have a white background!). I'm going to fiddle with it, and see if I can make it work.

EDIT: putting the glEneable and glDisable tags around the drawing code lets me draw with colors for everything else, but I still got a black quad where my alpha channel should be.

Share this post


Link to post
Share on other sites
That's because you also have to enableAlpha blending I think

GlEnable(GL_BLEND)
then you need to set a blend function to define the way your alpha and color channels are used. For example:
GlBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)+

ps: also, disalbe Alpha Testing! The two things don't like each other usually.
GlDisable(GL_ALPHA_TEST)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!