PyOpenGL Transformation Blur

308 views Asked by At

I would like to know how to make openGL to not "blur" an upscaled texture, as it seems that the bluring is set to default for transformations. The texure is a POT png file. The code used to define a texture and put it on the screen is this:

class Texture():
    # simple texture class
    # designed for 32 bit png images (with alpha channel)
    def __init__(self,fileName):
        self.texID=0
        self.LoadTexture(fileName)

    def LoadTexture(self,fileName): 
        try:
            textureSurface = pygame.image.load(fileName).convert_alpha()
            textureData = pygame.image.tostring(textureSurface, "RGBA", True)

            self.w, self.h = textureSurface.get_size()

            self.texID=glGenTextures(1)

            glBindTexture(GL_TEXTURE_2D, self.texID)
            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, textureSurface.get_width(), 
                         textureSurface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 
                         textureData )

        except Exception as E:
            print(E)
            print ("can't open the texture: %s"%(fileName))

    def __del__(self):
        glDeleteTextures(self.texID)

    def get_width(self):
        return self.w

    def get_height(self):
        return self.h

def blit(texture, x, y):
    """
    Function that blits a given texture on the screen
    """
    #We put the texture onto the screen
    glBindTexture(GL_TEXTURE_2D, texture.texID)

    #Now we must position the image
    glBegin(GL_QUADS)

    #We calculate each of the points relative to the center of the screen
    top = -y/(HEIGHT//2) + 1.0
    left = x/(WIDTH//2) - 1.0
    right = left + texture.w/(WIDTH//2)
    down = top - texture.h/(HEIGHT//2)

    #We position each point of the image
    glTexCoord2f(0.0, 1.0)
    glVertex2f(left, top)


    glTexCoord2f(1.0,1.0)
    glVertex2f(right, top)

    glTexCoord2f(1.0,0.0)
    glVertex2f(right, down)

    glTexCoord2f(0.0,0.0)
    glVertex2f(left, down)

    glEnd()

I configured openGL as follows:

def ConfigureOpenGL(w, h):
    #glShadeModel(GL_SMOOTH)
    #glClearColor(0.0, 0.0, 0.0, 1.0)
    #glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    #glLoadIdentity()
    #gluOrtho2D(-8.0, 8.0, -6.0, 6.0)
    glMatrixMode(GL_MODELVIEW)

    glLoadIdentity()
    glShadeModel(GL_SMOOTH)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glDisable(GL_DEPTH_TEST)
    glDisable(GL_LIGHTING)
    glDepthFunc(GL_LEQUAL)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glEnable(GL_BLEND)

Surface = pygame.display.set_mode((WIDTH, HEIGHT),  OPENGL|DOUBLEBUF)#|FULLSCREEN)
ConfigureOpenGL(WIDTH, HEIGHT)

Before putting anything in the screen i also call this method:

def OpenGLRender(self):
    """
    Used to prepare the screen to render
    """
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glDisable(GL_LIGHTING)
    glEnable(GL_TEXTURE_2D)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glClearColor(1.0, 1.0, 1.0, 1.0)

I'm using PyOpenGL 3.0.2

1

There are 1 answers

0
genpfault On

Use GL_NEAREST in your glTexParameteri() calls:

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)