4 Replies - 1768 Views - Last Post: 08 July 2010 - 09:13 PM Rate Topic: -----

#1 matio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-September 09

opengl texture problem

Posted 27 June 2010 - 03:22 AM

Hi everyone, I'm trying to load an image into pyopengl:
#lib/openglframework/surface.py
import pygame

from OpenGL.GL import *
from OpenGL.GLU import *

from misc import *

class Surface(object):
    """
    Surface class -- displays a 2d surface with OpenGL
    """
    def __init__(self, x, y, surface, z=0, clip=None):
        self.x = x
        self.y = y
        image = surface
        pygame.image.save(image, "surface2.bmp")
        self.width = self.height = 0
        if clip == None:
            self.width, self.height = image.get_rect().size
        else:
            self.width = clip[0]
            self.height = clip[1]
        self.widthp2 = next_power_of_2(self.width)
        self.heightp2 = next_power_of_2(self.height)
        self.backimage = pygame.Surface((self.widthp2, self.heightp2))
        self.backimage.blit(image, (0,0))
        self.backimage = self.backimage.convert_alpha()        
        self.blit()
        self.widthfrac = int(float(self.width)/self.widthp2)
        self.heightfrac = int(float(self.height)/self.heightp2)
        self.z = z
        pygame.image.save(self.backimage, "surface3.bmp")
      
    def blit(self, surface=None, x=None, y=None):  
        if surface != None and x != None and y != None:
            self.backimage.blit(surface, (x,y))
        texture_data = pygame.image.tostring(self.backimage, 'RGBA', 1)
        self.__texture_id = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, self.__texture_id)
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR )
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR )     
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
        
        glTexImage2D( GL_TEXTURE_2D,
                      0,
                      GL_RGBA,
                      self.widthp2,
                      self.heightp2,
                      0,
                      GL_RGBA,
                      GL_UNSIGNED_BYTE,
                      texture_data) 
                              
    def rotate(self, degrees, x, y, z):
        glBindTexture(GL_TEXTURE_2D, self.__texture_id)
        glRotate(degrees, x, y, z)
        
    def scale(self, x, y, z):
        glBindTexture(GL_TEXTURE_2D, self.__texture_id)
        glScalef(x,y,z)
        
    def draw(self):       
        glBindTexture(GL_TEXTURE_2D, self.__texture_id)
        
        glBegin(GL_QUADS)
        glColor(1,1,1,0)
        glTexCoord2f(0, 1-self.heightfrac)
        glVertex3f(self.x-self.width/2, self.y-self.height/2, self.z)

        glTexCoord2f(self.widthfrac, 1-self.heightfrac)
        glVertex3f(self.x+self.width/2, self.y-self.height/2, self.z)

        glTexCoord2f(self.widthfrac, 1)
        glVertex3f(self.x+self.width/2, self.y+self.height/2, self.z)

        glTexCoord2f(0, 1)
        glVertex3f(self.x-self.width/2, self.y+self.height/2, self.z)

        glEnd()        
        
    def __del__(self):
        glDeleteTextures(self.__texture_id)



When I draw this it appears as a grey box, not as the image/surface. Does anyone know why?

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: opengl texture problem

#2 matio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-September 09

Re: opengl texture problem

Posted 27 June 2010 - 10:19 AM

        glTexImage2D( GL_TEXTURE_2D,
                      0,
                      GL_RGBA,
                      self.widthp2,
                      self.heightp2,
                      0,
                      GL_RGBA,
                      GL_UNSIGNED_BYTE,
                      texture_data)



if I change RGBA to RGB the square goes blue...
Was This Post Helpful? 0
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 181
  • View blog
  • Posts: 2,642
  • Joined: 30-December 07

Re: opengl texture problem

Posted 27 June 2010 - 11:49 AM

Perhaps this is a dumb question but shouldn't you be using pygame.image.load instead of pygame.image.save?
Was This Post Helpful? 0
  • +
  • -

#4 matio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-September 09

Re: opengl texture problem

Posted 28 June 2010 - 10:17 AM

The save lines were to make sure the problem was with opengl and not pygame, the saved images appear fine while on the screen it is still solid colour ;)
Was This Post Helpful? 0
  • +
  • -

#5 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 181
  • View blog
  • Posts: 2,642
  • Joined: 30-December 07

Re: opengl texture problem

Posted 08 July 2010 - 09:13 PM

Have you gotten this to work yet? I'm sorry but I'm not very familiar with pygame so I am unable to offer much help beyond pointing out semi-obvious things in the code you posted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1