Uncle Dino
Uncle Dino

Reputation: 864

PyOpenGL(or PyGame) pixel-sized textures

I downloaded a simple sample program for using textures with PyOpenGL, Python & PyGame, but when I tried to replace the original texture with this: pixels.png
(If you don't see that, its a 2x2 pixels square, where all the pixels has different colors)

than it gave me THIS: result.png I DON'T WANT THIS UGLY WINDOW LOGO!!

The code I downloaded:

#!/usr/bin/env python

from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
from pygame.locals import *

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)
            textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
            
            self.texID=glGenTextures(1)
            
            glBindTexture(GL_TEXTURE_2D, self.texID)
            glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
                        textureSurface.get_width(), textureSurface.get_height(),
                        0, GL_RGBA, GL_UNSIGNED_BYTE, textureData )
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
        except:
            print "can't open the texture: %s"%(fileName)
    def __del__(self):
        glDeleteTextures(self.texID)

class Main():
    def resize(self,(width, height)):
        if height==0:
            height=1
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        #gluOrtho2D(-8.0, 8.0, -6.0, 6.0)
        glFrustum(-2,2,-2,2,1,8)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    def init(self):
        #set some basic OpenGL settings and control variables
        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)
        
        self.tutorial_texture=Texture("pixels.png")
        
        self.demandedFps=30.0
        self.done=False
        
        self.x,self.y,self.z=0.0 , 0.0, -4.0
        self.rX,self.rZ=0,0
    def draw(self):
        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)
        
        glPushMatrix()
        
        glTranslatef(self.x, self.y, self.z)

        glRotate(-self.rZ/3,0,0,1)
        glRotate(-self.rX/3,1,0,0)

        glColor4f(1.0, 1.0, 1.0,1.0)
        
        glBindTexture(GL_TEXTURE_2D,self.tutorial_texture.texID)
        
        glBegin(GL_QUADS)
        
        glTexCoord2f(0.0,1.0)
        glVertex3f(-1.0, 1.0,0.0)
        
        glTexCoord2f(1.0,1.0)
        glVertex3f(1.0, 1.0,-1.0)
        
        glTexCoord2f(1.0,0.0)
        glVertex3f(1.0, -1.0,0.0)
        
        glTexCoord2f(0.0,0.0)
        glVertex3f(-1.0, -1.0,1.0)
        
        glEnd()
        glBegin(GL_LINES)
        glColor(1,17,0)
        glVertex(0,0,0)
        glVertex(3,0,0)
        glColor(1,0,1)
        glVertex(0,0,0)
        glVertex(0,3,0)
        glColor(0,1,1)
        glVertex(0,0,0)
        glVertex(0,0,3)
        glEnd()
        glPopMatrix()
        
    def Input(self,fl):
        #mpb=pygame.mouse.get_pressed() # mouse pressed buttons
        kpb=pygame.key.get_pressed() # keyboard pressed buttons
        msh=pygame.mouse.get_rel() # mouse shift
        
        if kpb[K_ESCAPE] or kpb[K_q]:
            self.done=True
            
        if kpb[K_UP]:
            self.y+=0.1
        if kpb[K_DOWN]:
            self.y-=0.1
        
        if kpb[K_RIGHT]:
            self.x+=0.1
        if kpb[K_LEFT]:
            self.x-=0.1
        if fl: self.rZ-=msh[0]/3; self.rX-=msh[1]/3
    def __init__(self):
        glOrtho(0, 800, 0, 600, 0.0, 100.0)
        video_flags = OPENGL|DOUBLEBUF|RESIZABLE
        
        pygame.init()
        pygame.display.set_mode((800,800), video_flags)
        
        pygame.display.set_caption("www.jason.gd")
        
        self.resize((800,800))
        self.init()

        fl=0
        clock = pygame.time.Clock()
        while 1:
            for event in pygame.event.get():
                if event.type == QUIT or self.done:
                    pygame.quit () 
                    break
                if event.type==MOUSEBUTTONDOWN:
                    if event.button==4: self.z+=0.1
                    if event.button==5: self.z-=0.1
                    if event.button==2: fl=1
                if event.type==MOUSEBUTTONUP:
                    if event.button==2: fl=0
                if event.type==VIDEORESIZE: self.resize((event.w,event.h))
            self.Input(fl)
            self.draw()
            
            pygame.display.flip()
            
            #limit fps
            clock.tick(self.demandedFps)

if __name__ == '__main__': Main()

When I tried to use bigger(512x512 px) textures, it worked fine. How can I let the OpenGL to DO NOT mix the pixel borders? Or- the PyGame did this?

Upvotes: 0

Views: 867

Answers (1)

derhass
derhass

Reputation: 45322

What you see here is the GL_LINEAR texture magnification mode (aka "bilinear filtering", which your code explicitely requests) in combination with the default GL_REPEAT texture coordinate repetition at the borders.

I'm not 100% sure which one of the two things you don't want.

You might try changing GL_LINEAR to GL_NEAREST:

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

and/or adding

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE)

Upvotes: 1

Related Questions