Pyglet 3-D's rendering is putting extra pixels in an image

Viewed 50

I have Python 3.8.2, and I'm trying to create my own minecraft armor renderer, and I've run into a problem that I just can't wrap my head around (Render). Obviously I don't want the top of the shoulder to be floating above the shoulder, but it isn't anything to do with the texture (An Example Texture which I got directly by saving the pyglet texture).

How do I fix this and how can I avoid this in the future? If you need me to give more information or more specific information, I can update this and respond. (I'm new to pyglet, so criticism and advice welcome.)

My two armor layers: (Armor Layer 1, Armor Layer 2)

The code I'm worried about:

from pyglet.window import key
import math, os, shutil
from PIL import Image, ImageOps

class Model():

    def get_tex(self,file):
        tex = pyglet.image.load(file).get_texture()
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        return pyglet.graphics.TextureGroup(tex)

    def __init__(self,pos = (0,0,-1),size = (16,16,16),bottom='ancient_debris_top.png',top='ancient_debris_top.png',back='ancient_debris_side.png',front='ancient_debris_side.png',left='ancient_debris_side.png',right='ancient_debris_side.png'):

        self.top = self.get_tex(top)
        self.bottom = self.get_tex(bottom)
        self.back = self.get_tex(back)
        self.front = self.get_tex(front)
        self.left = self.get_tex(left)
        self.right = self.get_tex(right)

        self.batch = pyglet.graphics.Batch()

        tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))

        x,y,z = pos[0]*4, pos[1]*4, pos[2]*4
        X,Y,Z = x+size[0]/4,y+size[1]/4,z+size[2]/4

        self.batch.add(4,GL_QUADS,self.left,('v3f',(x,y,z, x,y,Z, x,Y,Z, x,Y,z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.right,('v3f',(X,y,Z, X,y,z, X,Y,z, X,Y,Z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.bottom,('v3f',(x,y,z, X,y,z, X,y,Z, x,y,Z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.top,('v3f',(x,Y,Z, X,Y,Z, X,Y,z, x,Y,z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.back,('v3f',(X,y,z, x,y,z, x,Y,z, X,Y,z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.front,('v3f',(x,y,Z, X,y,Z, X,Y,Z, x,Y,Z, )),tex_coords)


    def draw(self):
        self.batch.draw()

class Window(pyglet.window.Window):

    def push(self,pos,rot): glPushMatrix(); glRotatef(-rot[0],1,0,0); glRotatef(-rot[1],0,1,0); glTranslatef(-pos[0],-pos[1],-pos[2],)
    def Projection(self): glMatrixMode(GL_PROJECTION); glLoadIdentity()
    def Model(self): glMatrixMode(GL_MODELVIEW); glLoadIdentity()
    def set2d(self): self.Projection(); gluOrtho2D(0,self.width,0,self.height); self.Model()
    def set3d(self): self.Projection(); gluPerspective(70,self.width/self.height,0.05,1000); self.Model()

    def setLock(self,state): self.lock = state; self.set_exclusive_mouse(state)
    lock = False; mouse_lock = property(lambda self:self.lock,setLock)

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.set_minimum_size(300,200)
        self.keys = key.KeyStateHandler()
        self.push_handlers(self.keys)
        pyglet.clock.schedule(self.update)

        self.model = Model(pos=(0,1,-1),size=(8,8,8),front='head/head_front.png',back='head/head_back.png',top='head/head_top.png',bottom='head/head_bottom.png',left='head/head_left.png',right='head/head_right.png')
        self.model2 = Model(pos=(0,0.375,-0.875),size=(8,12,4),front='chest/chest_front.png',back='chest/chest_back.png',top='chest/chest_top.png',bottom='chest/chest_bottom.png',left='chest/chest_left.png',right='chest/chest_right.png')
        self.model3 = Model(pos=(0.5,0.375,-0.875),size=(4,12,4),front='shoulder_right/shoulder_right_front.png',back='shoulder_right/shoulder_right_back.png',top='shoulder_right/shoulder_right_top.png',bottom='shoulder_right/shoulder_right_bottom.png',left='shoulder_right/shoulder_right_left.png',right='shoulder_right/shoulder_right_right.png')
        self.model4 = Model(pos=(-0.25,0.375,-0.875),size=(4,12,4),front='shoulder_left/shoulder_left_front.png',back='shoulder_left/shoulder_left_back.png',top='shoulder_left/shoulder_left_top.png',bottom='shoulder_left/shoulder_left_bottom.png',left='shoulder_left/shoulder_left_left.png',right='shoulder_left/shoulder_left_right.png')
        self.player = Player((0.5,1.5,1.5),(-30,0))

    def on_mouse_motion(self,x,y,dx,dy):
        if self.mouse_lock: self.player.mouse_motion(dx,dy)

    def on_key_press(self,KEY,MOD):
        if KEY == key.ESCAPE:
            self.close()
            rmallfolders()
        elif KEY == key.E: self.mouse_lock = not self.mouse_lock

    def update(self,dt):
        self.player.update(dt,self.keys)

    def on_draw(self):
        self.clear()
        self.set3d()
        self.push(self.player.pos,self.player.rot)
        self.model.draw()
        self.model2.draw()
        self.model3.draw()
        self.model4.draw()
        glPopMatrix()

My entire code:

from pyglet.window import key
import math, os, shutil
from PIL import Image, ImageOps

class Model():

    def get_tex(self,file):
        tex = pyglet.image.load(file).get_texture()
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        return pyglet.graphics.TextureGroup(tex)

    def __init__(self,pos = (0,0,-1),size = (16,16,16),bottom='ancient_debris_top.png',top='ancient_debris_top.png',back='ancient_debris_side.png',front='ancient_debris_side.png',left='ancient_debris_side.png',right='ancient_debris_side.png'):

        self.top = self.get_tex(top)
        self.bottom = self.get_tex(bottom)
        self.back = self.get_tex(back)
        self.front = self.get_tex(front)
        self.left = self.get_tex(left)
        self.right = self.get_tex(right)

        self.batch = pyglet.graphics.Batch()

        tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))

        x,y,z = pos[0]*4, pos[1]*4, pos[2]*4
        X,Y,Z = x+size[0]/4,y+size[1]/4,z+size[2]/4

        self.batch.add(4,GL_QUADS,self.left,('v3f',(x,y,z, x,y,Z, x,Y,Z, x,Y,z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.right,('v3f',(X,y,Z, X,y,z, X,Y,z, X,Y,Z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.bottom,('v3f',(x,y,z, X,y,z, X,y,Z, x,y,Z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.top,('v3f',(x,Y,Z, X,Y,Z, X,Y,z, x,Y,z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.back,('v3f',(X,y,z, x,y,z, x,Y,z, X,Y,z, )),tex_coords)
        self.batch.add(4,GL_QUADS,self.front,('v3f',(x,y,Z, X,y,Z, X,Y,Z, x,Y,Z, )),tex_coords)


    def draw(self):
        self.batch.draw()



class Player:
    def __init__(self,pos=(0,0,0),rot=(0,0)):
        self.pos = list(pos)
        self.rot = list(rot)

    def mouse_motion(self,dx,dy):
        dx/=8; dy/=8; self.rot[0]+=dy; self.rot[1]-=dx
        if self.rot[0]>90: self.rot[0] = 90
        elif self.rot[0]<-90: self.rot[0] = -90

    def update(self,dt,keys):
        s = dt*10
        rotY = -self.rot[1]/180*math.pi
        dx,dz = s*math.sin(rotY),s*math.cos(rotY)
        if keys[key.W]: self.pos[0]+=dx; self.pos[2]-=dz
        if keys[key.S]: self.pos[0]-=dx; self.pos[2]+=dz
        if keys[key.A]: self.pos[0]-=dz; self.pos[2]-=dx
        if keys[key.D]: self.pos[0]+=dz; self.pos[2]+=dx

        if keys[key.SPACE]: self.pos[1]+=s
        if keys[key.LSHIFT]: self.pos[1]-=s


class Window(pyglet.window.Window):

    def push(self,pos,rot): glPushMatrix(); glRotatef(-rot[0],1,0,0); glRotatef(-rot[1],0,1,0); glTranslatef(-pos[0],-pos[1],-pos[2],)
    def Projection(self): glMatrixMode(GL_PROJECTION); glLoadIdentity()
    def Model(self): glMatrixMode(GL_MODELVIEW); glLoadIdentity()
    def set2d(self): self.Projection(); gluOrtho2D(0,self.width,0,self.height); self.Model()
    def set3d(self): self.Projection(); gluPerspective(70,self.width/self.height,0.05,1000); self.Model()

    def setLock(self,state): self.lock = state; self.set_exclusive_mouse(state)
    lock = False; mouse_lock = property(lambda self:self.lock,setLock)

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.set_minimum_size(300,200)
        self.keys = key.KeyStateHandler()
        self.push_handlers(self.keys)
        pyglet.clock.schedule(self.update)

        self.model = Model(pos=(0,1,-1),size=(8,8,8),front='head/head_front.png',back='head/head_back.png',top='head/head_top.png',bottom='head/head_bottom.png',left='head/head_left.png',right='head/head_right.png')
        self.model2 = Model(pos=(0,0.375,-0.875),size=(8,12,4),front='chest/chest_front.png',back='chest/chest_back.png',top='chest/chest_top.png',bottom='chest/chest_bottom.png',left='chest/chest_left.png',right='chest/chest_right.png')
        self.model3 = Model(pos=(0.5,0.375,-0.875),size=(4,12,4),front='shoulder_right/shoulder_right_front.png',back='shoulder_right/shoulder_right_back.png',top='shoulder_right/shoulder_right_top.png',bottom='shoulder_right/shoulder_right_bottom.png',left='shoulder_right/shoulder_right_left.png',right='shoulder_right/shoulder_right_right.png')
        self.model4 = Model(pos=(-0.25,0.375,-0.875),size=(4,12,4),front='shoulder_left/shoulder_left_front.png',back='shoulder_left/shoulder_left_back.png',top='shoulder_left/shoulder_left_top.png',bottom='shoulder_left/shoulder_left_bottom.png',left='shoulder_left/shoulder_left_left.png',right='shoulder_left/shoulder_left_right.png')
        self.player = Player((0.5,1.5,1.5),(-30,0))

    def on_mouse_motion(self,x,y,dx,dy):
        if self.mouse_lock: self.player.mouse_motion(dx,dy)

    def on_key_press(self,KEY,MOD):
        if KEY == key.ESCAPE:
            self.close()
            rmallfolders()
        elif KEY == key.E: self.mouse_lock = not self.mouse_lock

    def update(self,dt):
        self.player.update(dt,self.keys)

    def on_draw(self):
        self.clear()
        self.set3d()
        self.push(self.player.pos,self.player.rot)
        self.model.draw()
        self.model2.draw()
        self.model3.draw()
        self.model4.draw()
        glPopMatrix()

def readfile1(filename):
    rawimage=Image.open(filename)
    #left, top, right, bottom
    head_top = rawimage.crop((8,0,16,8)); head_left = rawimage.crop((0,8,8,16)); head_front = rawimage.crop((8,8,16,16)); head_right = rawimage.crop((16,8,24,16)); head_back = rawimage.crop((24,8,32,16)); head_bottom=Image.open("empty_bottom_head.png"); os.mkdir("head")
    head_top.save('head/head_top.png'); head_left.save('head/head_left.png'); head_front.save('head/head_front.png'); head_right.save('head/head_right.png'); head_back.save('head/head_back.png'); head_bottom.save('head/head_bottom.png')
    chest_top = Image.open("empty_bottom_head.png"); chest_front = rawimage.crop((20,20,28,32)); chest_left = rawimage.crop((16,20,20,32)); chest_right = rawimage.crop((28,20,32,32)); chest_back = rawimage.crop((32,20,40,32)); chest_bottom = Image.open("empty_bottom_head.png"); os.mkdir("chest")
    chest_top.save('chest/chest_top.png'); chest_left.save('chest/chest_left.png'); chest_front.save('chest/chest_front.png'); chest_right.save('chest/chest_right.png'); chest_back.save('chest/chest_back.png'); chest_bottom.save('chest/chest_bottom.png')
    shoulder_right_top = rawimage.crop((44,16,48,20)); shoulder_right_front = rawimage.crop((44,20,48,32)); shoulder_right_left = rawimage.crop((40,20,44,32)); shoulder_right_right = rawimage.crop((48,20,52,32)); shoulder_right_back = rawimage.crop((52,20,56,32)); shoulder_right_bottom = Image.open("empty_bottom_head.png"); os.mkdir("shoulder_right")
    shoulder_right_top.save('shoulder_right/shoulder_right_top.png'); shoulder_right_left.save('shoulder_right/shoulder_right_left.png'); shoulder_right_front.save('shoulder_right/shoulder_right_front.png'); shoulder_right_right.save('shoulder_right/shoulder_right_right.png'); shoulder_right_back.save('shoulder_right/shoulder_right_back.png'); shoulder_right_bottom.save('shoulder_right/shoulder_right_bottom.png')
    shoulder_left_top = ImageOps.mirror(shoulder_right_top); shoulder_left_bottom = ImageOps.mirror(shoulder_right_bottom); shoulder_left_right = ImageOps.mirror(shoulder_right_left); shoulder_left_left = ImageOps.mirror(shoulder_right_right); shoulder_left_front = ImageOps.mirror(shoulder_right_front); shoulder_left_back = ImageOps.mirror(shoulder_right_back); os.mkdir('shoulder_left')
    shoulder_left_top.save('shoulder_left/shoulder_left_top.png'); shoulder_left_left.save('shoulder_left/shoulder_left_left.png'); shoulder_left_front.save('shoulder_left/shoulder_left_front.png'); shoulder_left_right.save('shoulder_left/shoulder_left_right.png'); shoulder_left_back.save('shoulder_left/shoulder_left_back.png'); shoulder_left_bottom.save('shoulder_left/shoulder_left_bottom.png')    

def rmallfolders():
    folders = ['head','chest','shoulder_right','shoulder_left']
    for i in folders:
        try:
            shutil.rmtree(i)
        except:
            pass

if __name__ == '__main__':
    rmallfolders()
    readfile1('ruby_layer_1.png')
    window = Window(width=854,height=480,caption='3-D Modeller',resizable=True)
    glClearColor(0.5,0.7,1,1)
    glEnable(GL_DEPTH_TEST)
    #glEnable(GL_CULL_FACE)
    pyglet.app.run()

Thanks so much!

0 Answers
Related