0

Im trying to add 3d texture to my PyopenGL project with pygame. i've used the following code and get an error at the glTexImage3D

def MTL(filename):
    contents = {}
    mtl = None
    for line in open(filename, "r"):
        if line.startswith('#'):
            continue
        values = line.split()
        if not values:
            continue
        if values[0] == 'newmtl':
            mtl = contents[values[1]] = {}
        elif mtl is None:
            raise ValueError, "mtl file doesn't start with newmtl stmt"
        elif values[0] == 'map_Kd':
            # load the texture referred to by this declaration
            mtl[values[0]] = values[1]
            surf = pygame.image.load(mtl['map_Kd'])
            image = pygame.image.tostring(surf, 'RGBA', 1)
            ix, iy = surf.get_rect().size
            texid = mtl['texture_Kd'] = glGenTextures(1)
            glBindTexture(GL_TEXTURE_3D, texid)
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT)
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT)
            glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT)
            glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, ix, 0, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
        else:
            mtl[values[0]] = map(float, values[1:])
    return contents



class OBJ:
    def __init__(self, filename, swapyz=False):
        """Loads a Wavefront OBJ file. """
        self.vertices = []
        self.normals = []
        self.texcoords = []
        self.faces = []
        material = None
        for line in open(filename, "r"):
            if line.startswith('#'): continue
            values = line.split()
            if not values: continue
            if values[0] == 'v':
                v = map(float, values[1:4])
                if swapyz:
                    v = v[0], v[2], v[1]
                self.vertices.append(v)
            elif values[0] == 'vn':
                v = map(float, values[1:4])
                if swapyz:
                    v = v[0], v[2], v[1]
                self.normals.append(v)
            elif values[0] == 'vt':
                self.texcoords.append(map(float, values[1:3]))
            elif values[0] in ('usemtl', 'usemat'):
                material = values[1]
            elif values[0] == 'mtllib':
                self.mtl = MTL(values[1])
            elif values[0] == 'f':
                face = []
                texcoords = []
                norms = []
                for v in values[1:]:
                    w = v.split('/')
                    face.append(int(w[0]))
                    if len(w) >= 2 and len(w[1]) > 0:
                        texcoords.append(int(w[1]))
                    else:
                        texcoords.append(0)
                    if len(w) >= 3 and len(w[2]) > 0:
                        norms.append(int(w[2]))
                    else:
                        norms.append(0)
                self.faces.append((face, norms, texcoords, material))

        self.gl_list = glGenLists(1)
        glNewList(self.gl_list, GL_COMPILE)
        glEnable(GL_TEXTURE_3D)
        glFrontFace(GL_CCW)
        for face in self.faces:
            vertices, normals, texture_coords, material = face

            mtl = self.mtl[material]
            if 'texture_Kd' in mtl:
                # use diffuse texmap
                glBindTexture(GL_TEXTURE_3D, mtl['texture_Kd'])
            else:
                # just use diffuse colour
                glColor(*mtl['Kd'])

            glBegin(GL_POLYGON)
            for i in range(len(vertices)):
                if normals[i] > 0:
                    glNormal3fv(self.normals[normals[i] - 1])
                if texture_coords[i] > 0:
                    glTexCoord3fv(self.texcoords[texture_coords[i] - 1])
                glVertex3fv(self.vertices[vertices[i] - 1])
            glEnd()
        glDisable(GL_TEXTURE_3D)
        glEndList()

in main i just load

   obj = OBJ("ground_NFL.obj", swapyz=True)

and while true:

   glCallList(obj.gl_list)

the error is:

OpenGL.error.GLError: GLError(
err = 1281,
description = 'invalid value',
baseOperation = glTexImage3D,
pyArgs = (
    GL_TEXTURE_3D,
    0,
    GL_RGBA,
    4096,
    4096,
    0,
    0,
    GL_RGBA,
    GL_UNSIGNED_BYTE,
    'NrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\xffNrN\...,
),

i think there is something wrong with the flow. thanks for the help the project uses pygame and pyopengl i've taken the code originally from pygame in the following link https://www.pygame.org/wiki/OBJFileLoader but it only converts 2d images.

Bob Sfog
  • 105
  • 8
  • 1
    Are you sure that what you want is 3 dimensional texture image [`glTexImage3D`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage3D.xhtml)? Isn't it a 2 dimensional texture image [`glTexImage2D`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml). The error message doesn't suit the parameters `...,ix, 0, iy, ...` in `glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, ix, 0, iy, ...` – Rabbid76 Jun 30 '19 at 08:42
  • when i tried to use glTexImage2D the results is the image in 90 degrees glBindTexture(GL_TEXTURE_2D, texid) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) how can i rotate the image to be only on x,0,z? when i put iy in the depth i get an error. – Bob Sfog Jun 30 '19 at 09:01
  • oh the image is 2d. but the texture is 3d. how do i combine them? – Bob Sfog Jun 30 '19 at 09:03
  • i have an obj,mtl, 2d jpg image i want to add texture to my project what am i doing wrong? – Bob Sfog Jun 30 '19 at 09:06
  • 1
    Probably the *"invalid value"* error is caused, because the image size is to large. – Rabbid76 Jun 30 '19 at 09:38
  • #1 I never saw 3D texture used in wavefront obj+mtl its highly likely that you got 2D texture not 3D but without sample we can only guess #2 4096 for 3D texture is huge `...,4096,4096,0,...` is not even 2D !!! for 2D it should be `...,4096,4096,1,...` but not all gfx cards support textures with 4096 resolution #3 `when i tried to use glTexImage2D the results is the image in 90 degrees` what to heck does it mean? #5 see [Access to 3D array in fragment shader](https://stackoverflow.com/a/56072875/2521214) its an example how 3D texture is used ... – Spektre Jul 03 '19 at 08:40

1 Answers1

0

yes i tried to upload a 2D texture, replacing 3d with 2d fixed the issue

Bob Sfog
  • 105
  • 8