【问题标题】:PyOpenGL Transformation BlurPyOpenGL 变换模糊
【发布时间】:2014-11-11 21:10:24
【问题描述】:

我想知道如何使 openGL 不“模糊”放大的纹理,因为似乎模糊设置为转换的默认值。纹理是一个 POT png 文件。用于定义纹理并将其放在屏幕上的代码是这样的:

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()

我配置openGL如下:

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)

在屏幕上放任何东西之前,我也会调用这个方法:

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)

我正在使用 PyOpenGL 3.0.2

【问题讨论】:

  • GL_NEAREST 而不是 GL_LINEAR?
  • 谢谢!这解决了问题!

标签: opengl python-3.x resize pygame pyopengl


【解决方案1】:

在您的glTexParameteri() 调用中使用GL_NEAREST

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

【讨论】:

    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 2017-04-19
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2014-04-24
    • 2016-01-18
    相关资源
    最近更新 更多