Lately I have been experimenting with OpenGL and right now I am trying to
apply some textures on simple shapes, namely GL_POLYGON
in this case.
The problem is that the result looks kind of fuzzy, vague and I am running out of the possible causes of this problem.
The texture I am trying to use
# -*- coding: UTF-8 -*-
import math
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from PIL import Image
width, height = 640, 480
def init():
glEnable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 0.0)
gl_ortho()
def gl_ortho():
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glViewport(0, 0, width, height)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
im = Image.open('glass_texture.png')
try:
ix, iy, image = im.size[0], im.size[1], im.tobytes("raw", "RGB", 0, -1)
except SystemError:
ix, iy, image = im.size[0], im.size[1], im.tobytes("raw", "RGBX", 0, -1)
texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_id)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage2D(
GL_TEXTURE_2D, 0, 3, ix, iy, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image
)
glEnable(GL_TEXTURE_2D)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glBindTexture(GL_TEXTURE_2D, texture_id)
glPushMatrix()
glBegin(GL_POLYGON)
glTexCoord2f(0.0, 1.0)
glVertex3f(-0.5, 0.5, 0.0)
glTexCoord2f(1.0, 1.0)
glVertex3f(0.5, 0.5, 0.0)
glTexCoord2f(1.0, 0.0)
glVertex3f(0.5, -0.5, 0.0)
glTexCoord2f(0.0, 0.0)
glVertex3f(-0.5, -0.5, 0.0)
glEnd()
glPopMatrix()
glutSwapBuffers()
glFlush()
if __name__ == '__main__':
glutInit()
glutInitWindowSize(width, height)
glutCreateWindow('Textures')
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutDisplayFunc(display)
glutIdleFunc(display)
init()
glutMainLoop()
The result
So I have finally found the root cause of the problem. The line below
im = Image.open('brick_texture.jpg')
should be replaced with
im = Image.open('brick_texture.jpg').convert('RGBA')
and then, a few lines below tobytes
method should be called without any arguments
ix, iy, image = im.size[0], im.size[1], im.tobytes()