new to textures

Started by
5 comments, last by NIm 15 years, 3 months ago
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()


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)


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*
You miss this:
GlEnable(GL_TEXTURE_2D)

(as far as I remember OpenGL...)
..so we ate rare animals, we spent the night eating rare animals..
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.
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)
..so we ate rare animals, we spent the night eating rare animals..
Thank you. I appreciate you introducing me to the enable function!

This topic is closed to new replies.

Advertisement