def loadSphere(): """ Return the geometry of a sphere. The sphere data is loaded from 'sphere.obj' which is part of the repo. :return: (vert, uv, rgb) """ # Assemble path to the model. p = os.path.dirname(os.path.abspath(__file__)) p = os.path.join(p, '..', 'viewer', 'models', 'sphere') fname = os.path.join(p, 'sphere.obj') # Import the model geometry. print(' Importing <{}>... '.format(fname), end='', flush=True) mesh = model_import.loadModelAll(fname) # The model may contain several sub-models. Each one has a set of vertices, # UV- and texture maps. The following code simply flattens the three lists # of lists into just three lists. vert = np.array(mesh['vertices']).flatten() uv = np.array(mesh['UV']).flatten() rgb = np.array(mesh['RGB']).flatten() # Ensure the data has the correct format. vert = 0.5 * np.array(vert) uv = np.array(uv, np.float32) rgb = np.array(rgb, np.uint8) # Return the geometry. return vert, uv, rgb
def loadModel(fname): """ Load 3D model from ``fname`` and return the vertices, UV, and RGB arrays. """ # Load the model. print(" Importing <{}>... ".format(fname), end="", flush=True) mesh = model_import.loadModelAll(fname) # The model may contain several sub-models. Each one has a set of vertices, # UV- and texture maps. The following code simply flattens the three lists # of lists into just three lists. vert = np.array(mesh["vertices"]).flatten() uv = np.array(mesh["UV"]).flatten() rgb = np.array(mesh["RGB"]).flatten() return vert, uv, rgb
def loadModel(fname): """ Load 3D model from ``fname`` and return the vertices, UV, and RGB arrays. """ # Load the model. print(' Importing <{}>... '.format(fname), end='', flush=True) mesh = model_import.loadModelAll(fname) # The model may contain several sub-models. Each one has a set of vertices, # UV- and texture maps. The following code simply flattens the three lists # of lists into just three lists. vert = np.array(mesh['vertices']).flatten() uv = np.array(mesh['UV']) rgb = np.array(mesh['RGB']) print('done') return vert, uv, rgb
def loadGeometry(self): # Backup the latest state variables because we will download new ones # from Azrael shortly. self.oldSVs = self.newSVs # Get latest SV values. with util.Timeit('viewer.getV') as timeit: ret = self.client.getObjectStates(None) if not ret.ok: print('Could not retrieve the state variables -- Abort') self.close() # Remove all *None* entries (means Azrael does not know about them; # should be impossible but just to be sure). self.newSVs = {} for objID in ret.data: if ret.data[objID] is None: self._removeObjectData(objID) self.newSVs[objID] = ret.data[objID] # Remove all objects from the local scene for which Azrael did not # provid SV data. for objID in self.oldSVs: # Ignore the player object. if (objID in self.newSVs) or (objID == self.player_id): continue # Delete all fragment textures. for frag in self.textureBuffer[objID].values(): gl.glDeleteTextures(frag) # Delete the corresponding entries in our meta variables. # gl.glDeleteBuffers(2, [1, 2]) self._removeObjectData(objID) # The previous loop removed objects that do not exist anymore in # Azrael. This loop adds objects that now exist in Azrael but not yet # in our scene. for objID in list(self.newSVs.keys()): # Do not add anything if it is the player object itself. if objID == self.player_id: continue # Skip the object if we already have its model, unless the model # changed. if (objID in self.oldSVs) and not self.hasGeometryChanged(objID): continue # Download the latest geometry for this object; skip it if the # object does not exist (anymore). ret = self.client.getFragments([objID]) if not ret.ok or ret.data is None or ret.data[objID] is None: self._removeObjectData(objID) continue # Fetch fragment model from Azrael and pass it to the GPU. base_url = 'http://{}:{}'.format(self.addr_clerk, self.port_webapi) for fragID, frag_data in ret.data[objID].items(): if frag_data['fragtype'] == 'RAW': url = base_url + frag_data['url_frag'] + '/model.json' frag = requests.get(url).content if len(frag) == 0: self._removeObjectData(objID) break frag = json.loads(frag.decode('utf8')) frag = getFragMetaRaw( frag['vert'], frag['uv'], frag['rgb'], frag['width'], frag['height'] ) elif frag_data['fragtype'] == 'DAE': url = base_url + frag_data['url_frag'] + '/' + fragID frag = requests.get(url).content if len(frag) == 0: self._removeObjectData(objID) break with tempfile.TemporaryDirectory() as tmpdir: open('model.dae', 'wb').write(frag) mesh = model_import.loadModelAll('model.dae') # The model may contain several sub-models. Each one has a # set of vertices, UV- and texture maps. The following code # simply flattens the three list-of-lists into three plain # lists. vert = np.array(mesh['vertices']).flatten() uv = np.array(mesh['UV']).flatten() rgb = np.array(mesh['RGB']).flatten() width = height = None # Ensure the data has the correct format. vert = np.array(vert) uv = np.array(uv, np.float32) rgb = np.array(rgb, np.uint8) frag = getFragMetaRaw(vert, uv, rgb, width, height) elif frag_data['fragtype'] == '3JS_V3': # Model files in 3JS format. These are stored in a main # JSON file plus (optional) texture files. Find the JSON # file and log an error if there is not exactly one. fnames = [_ for _ in frag_data['files'] if _.lower().endswith('.json')] if len(fnames) != 1: print('{} possible 3JS model candidates'.format(len(fnames))) break # Download the model. url = base_url + frag_data['url_frag'] + '/' + fnames[0] frag = requests.get(url).content if len(frag) == 0: self._removeObjectData(objID) break frag = json.loads(frag.decode('utf8')) vert, uv, rgb = demolib.load3JSModel(frag) # Download a texture file (if the model has one). fnames = [_ for _ in frag_data['files'] if _.lower().endswith('.jpg')] if len(fnames) > 0: print('found texture') url = base_url + frag_data['url_frag'] + '/' + fnames[0] texture = requests.get(url).content assert len(texture) > 0 with tempfile.TemporaryDirectory() as tmpdir: open('texture.jpg', 'wb').write(texture) img = PIL.Image.open(fnames[0]) width, height = img.size img = np.array(img) rgb = np.rollaxis(np.flipud(img), 1).flatten() print('imported texture {}'.format(url)) del img del url, texture del fnames frag = getFragMetaRaw(vert, uv, rgb, width, height) else: continue self.upload2GPU(objID, fragID, frag) # Only draw visible triangles for this fragment. gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LESS)
def loadGeometry(self): # Backup the latest state variables because we will download new ones # from Azrael shortly. self.oldSVs = self.newSVs # Get latest SV values. with util.Timeit('viewer.getV') as timeit: ret = self.client.getObjectStates(None) if not ret.ok: print('Could not retrieve the state variables -- Abort') self.close() # Remove all *None* entries (means Azrael does not know about them; # should be impossible but just to be sure). self.newSVs = {} for objID in ret.data: if ret.data[objID] is None: self._removeObjectData(objID) self.newSVs[objID] = ret.data[objID] # Remove all objects from the local scene for which Azrael did not # provid SV data. for objID in self.oldSVs: # Ignore the player object. if (objID in self.newSVs) or (objID == self.player_id): continue # Delete all fragment textures. for frag in self.textureBuffer[objID].values(): gl.glDeleteTextures(frag) # Delete the corresponding entries in our meta variables. # gl.glDeleteBuffers(2, [1, 2]) self._removeObjectData(objID) # The previous loop removed objects that do not exist anymore in # Azrael. This loop adds objects that now exist in Azrael but not yet # in our scene. for objID in list(self.newSVs.keys()): # Do not add anything if it is the player object itself. if objID == self.player_id: continue # Skip the object if we already have its model, unless the model # changed. if (objID in self.oldSVs) and not self.hasGeometryChanged(objID): continue # Download the latest geometry for this object; skip it if the # object does not exist (anymore). ret = self.client.getFragments([objID]) if not ret.ok or ret.data is None or ret.data[objID] is None: self._removeObjectData(objID) continue # Fetch fragment model from Azrael and pass it to the GPU. base_url = 'http://{}:{}'.format(self.addr_clerk, self.port_webapi) for fragID, frag_data in ret.data[objID].items(): if frag_data['fragtype'] == 'RAW': url = base_url + frag_data['url_frag'] + '/model.json' frag = requests.get(url).content if len(frag) == 0: self._removeObjectData(objID) break frag = json.loads(frag.decode('utf8')) frag = getFragMetaRaw(frag['vert'], frag['uv'], frag['rgb'], frag['width'], frag['height']) elif frag_data['fragtype'] == 'DAE': url = base_url + frag_data['url_frag'] + '/' + fragID frag = requests.get(url).content if len(frag) == 0: self._removeObjectData(objID) break with tempfile.TemporaryDirectory() as tmpdir: open('model.dae', 'wb').write(frag) mesh = model_import.loadModelAll('model.dae') # The model may contain several sub-models. Each one has a # set of vertices, UV- and texture maps. The following code # simply flattens the three list-of-lists into three plain # lists. vert = np.array(mesh['vertices']).flatten() uv = np.array(mesh['UV']).flatten() rgb = np.array(mesh['RGB']).flatten() width = height = None # Ensure the data has the correct format. vert = np.array(vert) uv = np.array(uv, np.float32) rgb = np.array(rgb, np.uint8) frag = getFragMetaRaw(vert, uv, rgb, width, height) elif frag_data['fragtype'] == '3JS_V3': # Model files in 3JS format. These are stored in a main # JSON file plus (optional) texture files. Find the JSON # file and log an error if there is not exactly one. fnames = [ _ for _ in frag_data['files'] if _.lower().endswith('.json') ] if len(fnames) != 1: print('{} possible 3JS model candidates'.format( len(fnames))) break # Download the model. url = base_url + frag_data['url_frag'] + '/' + fnames[0] frag = requests.get(url).content if len(frag) == 0: self._removeObjectData(objID) break frag = json.loads(frag.decode('utf8')) vert, uv, rgb = demolib.load3JSModel(frag) # Download a texture file (if the model has one). fnames = [ _ for _ in frag_data['files'] if _.lower().endswith('.jpg') ] if len(fnames) > 0: print('found texture') url = base_url + frag_data['url_frag'] + '/' + fnames[0] texture = requests.get(url).content assert len(texture) > 0 with tempfile.TemporaryDirectory() as tmpdir: open('texture.jpg', 'wb').write(texture) img = PIL.Image.open(fnames[0]) width, height = img.size img = np.array(img) rgb = np.rollaxis(np.flipud(img), 1).flatten() print('imported texture {}'.format(url)) del img del url, texture del fnames frag = getFragMetaRaw(vert, uv, rgb, width, height) else: continue self.upload2GPU(objID, fragID, frag) # Only draw visible triangles for this fragment. gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LESS)
def loadGeometry(self): # Backup the latest state variables because we will download new ones # from Azrael shortly. self.oldSVs = self.newSVs # Get latest SV values. with util.Timeit('viewer.getV') as timeit: ret = self.client.getObjectStates(None) if not ret.ok: print('Could not retrieve the state variables -- Abort') self.close() # Remove all *None* entries (means Azrael does not know about them; # should be impossible but just to be sure). self.newSVs = {} for objID in ret.data: if ret.data[objID] is None: self._removeObjectData(objID) self.newSVs[objID] = ret.data[objID] # Remove all objects from the local scene for which Azrael did not # provid SV data. for objID in self.oldSVs: # Ignore the player object. if (objID in self.newSVs) or (objID == self.player_id): continue # Delete all fragment textures. for frag in self.textureBuffer[objID].values(): gl.glDeleteTextures(frag) # Delete the corresponding entries in our meta variables. # gl.glDeleteBuffers(2, [1, 2]) self._removeObjectData(objID) # The previous loop removed objects that do not exist anymore in # Azrael. This loop adds objects that now exist in Azrael but not yet # in our scene. for objID in list(self.newSVs.keys()): # Do not add anything if it is the player object itself. if objID == self.player_id: continue # Skip the object if we already have its model, unless the model # changed. if (objID in self.oldSVs) and not self.hasGeometryChanged(objID): continue # Download the latest geometry for this object; skip it if the # object does not exist (anymore). ret = self.client.getFragments([objID]) if not ret.ok or ret.data is None or ret.data[objID] is None: self._removeObjectData(objID) continue # Fetch fragment model from Azrael and pass it to the GPU. base_url = 'http://{}:{}'.format(self.ip, config.port_webserver) for fragID, frag_data in ret.data[objID].items(): if frag_data['fragtype'] == 'RAW': url = base_url + frag_data['url_frag'] + '/model.json' frag = urllib.request.urlopen(url).readall() if len(frag) == 0: self._removeObjectData(objID) break frag = json.loads(frag.decode('utf8')) frag = getFragMeta('RAW', FragRaw(**frag)) elif frag_data['fragtype'] == 'DAE': url = base_url + frag_data['url_frag'] + '/' + fragID frag = urllib.request.urlopen(url).readall() if len(frag) == 0: self._removeObjectData(objID) break with tempfile.TemporaryDirectory() as tmpdir: open('model.dae', 'wb').write(frag) mesh = model_import.loadModelAll('model.dae') # The model may contain several sub-models. Each one has a # set of vertices, UV- and texture maps. The following code # simply flattens the three list-of-lists into three plain # lists. vert = np.array(mesh['vertices']).flatten() uv = np.array(mesh['UV']).flatten() rgb = np.array(mesh['RGB']).flatten() # Ensure the data has the correct format. vert = np.array(vert) uv = np.array(uv, np.float32) rgb = np.array(rgb, np.uint8) frag = getFragMeta('RAW', FragRaw(vert, uv, rgb)) else: continue self.upload2GPU(objID, fragID, frag) # Only draw visible triangles for this fragment. gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LESS)