def initializeGL(self): # 사람 오브젝트 부르기 self.person01 = obj.OBJ('object/per_obj.obj', swapyz=True) self.person02 = obj.OBJ('object/per_obj2.obj', swapyz=True) self.object = obj.OBJ('object/floor15.obj', swapyz=True) # gl.glShadeModel(gl.GL_FLAT) gl.glEnable(gl.GL_DEPTH_TEST) # gl.glEnable(gl.GL_CULL_FACE) self.move01 = self.getJSON('test01') self.move02 = self.getJSON('test02') self.move03 = self.getJSON('test03') self.move04 = self.getJSON('test04') self.move05 = self.getJSON('test05') self.move06 = self.getJSON('test06') self.move07 = self.getJSON('test07') self.move08 = self.getJSON('test08') self.move09 = self.getJSON('test09') self.move10 = self.getJSON('test10')
def load_and_draw_model(filename): glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_DEPTH_TEST) glClear(GL_DEPTH_BUFFER_BIT) glMaterialfv(GL_FRONT, GL_AMBIENT, [0, 0, 0, 0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.5, 0.75, 1, 0]) glMaterialfv(GL_FRONT, GL_SHININESS, 0.25 * 128) import objloader obj = objloader.OBJ(filename, swapyz=True) glScale(0.1, 0.1, 0.1) glCallList(obj.gl_list)
def load_and_draw_model(filename): """ 使用 objloader.py,从 .obj 文件中装载模型 假设路径文件夹中存在同名的 .mtl 材料设置文件 """ glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_DEPTH_TEST) glClear(GL_DEPTH_BUFFER_BIT) # 设置模型颜色 glMaterialfv(GL_FRONT, GL_AMBIENT, [0, 0, 0, 0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.5, 0.75, 1.0, 0.0]) glMaterialf(GL_FRONT, GL_SHININESS, 0.25 * 128.0) # 从文件中载入 import objloader obj = objloader.OBJ(filename, swapyz=True) glCallList(obj.gl_list)
def load_and_draw_model(filename): """ Loads a model from an .obj file using objloader.py. Assumes there is a .mtl material file with the same name. """ glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_DEPTH_TEST) glClear(GL_DEPTH_BUFFER_BIT) # set model color glMaterialfv(GL_FRONT, GL_AMBIENT, [0, 0, 0, 0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.5, 0.75, 1.0, 0.0]) glMaterialf(GL_FRONT, GL_SHININESS, 0.25 * 128.0) # load from a file import objloader obj = objloader.OBJ(filename, swapyz=True) glCallList(obj.gl_list)
def load_and_draw_model2(foldername, filename): viewport = (1200, 747) srf = pygame.display.set_mode(viewport, OPENGL | DOUBLEBUF) import objloader # LOAD OBJECT AFTER PYGAME INIT obj = objloader.OBJ(foldername,filename, swapyz=True) clock = pygame.time.Clock() rx, ry = (0, 0) tx, ty = (0, 0) glEnable(GL_BLEND) glBlendFunc(GL_ONE,GL_ZERO ) glDisable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) draw_background('background-1.png') glLightfv(GL_LIGHT0, GL_POSITION, (-40, 200, 100, 0.0)) glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0)) glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0)) glEnable(GL_LIGHT0) glEnable(GL_LIGHTING) glEnable(GL_COLOR_MATERIAL) glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded glMatrixMode(GL_PROJECTION) glLoadIdentity() width, height = viewport gluPerspective(90.0, width / float(height), 1, 100.0) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # RENDER OBJECT glTranslate(-0.4, -1, - 5) glRotate(ry, 1, 0, 0) glRotate(rx, 0, 1, 0) glScale(0.03, 0.03, 0.03) glCallList(obj.gl_list)
def initializeGL(self): self.object = obj.OBJ('object/map.obj', swapyz=True) gl.glShadeModel(gl.GL_FLAT) gl.glEnable(gl.GL_DEPTH_TEST)
import objloader import geometry from progressbar import ProgressBar sqr = lambda x: x * x if False: #fullscreen fs = FULLSCREEN RESOLUTION = (1280, 800) else: RESOLUTION = (800, 600) fs = 0 #### LOADING ############################### cow = objloader.OBJ("models/cow2.obj") def readtetrahedronlist(fn): counter = 0 res = [] with open(fn, "r") as f: for line in f: if counter % 4 == 0: a = eval(line) elif counter % 4 == 1: b = eval(line) elif counter % 4 == 2: c = eval(line) elif counter % 4 == 3: d = eval(line)
img = "../../data/mag_front.jpg" #img = "../../data/mag_perspective.jpg" #img = "../../data/mag_perspective_1.jpg" pkl = img[:-4] + ".pkl" #with open("../../data/ar_camera.pkl", "r") as f: with open(pkl, "r") as f: K = pickle.load(f) Rt = pickle.load(f) setup() glEnable(GL_NORMALIZE) ar.draw_background(img) ar.set_projection_from_camera(K, width, height) ar.set_modelview_from_camera(Rt) """ Load once into memory as this is time-taking """ obj = objloader.OBJ("toyplane.obj") #obj = objloader.OBJ("Sofa_3_3ds.obj") #obj_2 = objloader.OBJ("toyplane.obj") # obj = None # with open("../objloader/toyplane.pkl", 'rb') as input: # obj = cPickle.load(input) # if not obj: # sys.exit() #(scale, degrees, pos) = (0.001486436280241436, [0, -5, 0], [0.09500000000000001, 0, 0.10500000000000002]) degrees = [0, 0, 0] pos = [0, 0, 0] scale = 0.005 ar.draw_furniture(obj, scale) pygame.display.flip()
def main(): # Vídeo de entrada + lista com todos os frames (:Image()) video = Video(SOURCE_VIDEO) frames = video.getFrameList() """ Aruco Markers * Quatro versões do aruco marker são armazenadas: * Uma para cada rotação possível.""" markers = [Image(SOURCE_MARKER)] for n in range(3): markers.append(markers[-1].getRotated90()) camera = Camera(frames) pygame.init() width = 480 height = 640 display = (height, width) screen = pygame.display.set_mode(display, DOUBLEBUF | OPENGL) pygame.display.set_caption("ICV") fx = camera.getIntrinsicMatrix()[0][0] fy = camera.getIntrinsicMatrix()[1][1] fovy = 2 * np.arctan(0.5 * display[1] / fy) * 180 / np.pi #fovy *= 1.1 aspect = (display[0] * fx) / (display[1] * fy) clipStart = 0.1 clipEnd = 150.0 glMatrixMode(GL_PROJECTION) gluPerspective(fovy, aspect, clipStart, clipEnd) #glMatrixMode(GL_MODELVIEW) glEnable(GL_TEXTURE_2D) textureID = glGenTextures(1) pikachu = objloader.OBJ('Pikachu.obj', swapyz=True) for frame in frames: gray = frame.getGrayScale() edge = gray.getEdgeImage() # OpenGL Texture stuff... glEnable(GL_TEXTURE_2D) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, textureID) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 640, 480, 0, GL_RGB, GL_UNSIGNED_BYTE, frame.getSource().tobytes()) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) # Texture background... glDisable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, width, 0, height) drawPlane(height, width) glBindTexture(GL_TEXTURE_2D, 0) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(fovy, aspect, clipStart, clipEnd) glEnable(GL_DEPTH_TEST) for contour in edge.getContours(): for n, marker in enumerate(markers): warped = gray.getWarpedPerspective(contour, marker) # Se o contorno encontrado for igual ao marker, # desenha o contorno com a cor certa. if warped == marker: imagePoints = np.array(contour, dtype="float32") objectPoints = np.array( [[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]], dtype="float32") flag, rvecs, tvecs = cv2.solvePnP( objectPoints, imagePoints, camera.getIntrinsicMatrix(), camera.distance) #, flags=cv2.SOLVEPNP_ITERATIVE) #print(flag, rvecs, tvecs) rotm = cv2.Rodrigues(rvecs)[0] m = np.array( [[rotm[0][0], rotm[0][1], rotm[0][2], tvecs[0]], [rotm[1][0], rotm[1][1], rotm[1][2], tvecs[1]], [rotm[2][0], rotm[2][1], rotm[2][2], tvecs[2]], [0.0, 0.0, 0.0, 1.0]]) m = m * np.array( [[1.0, 1.0, 1.0, 1.0], [-1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, -1.0, -1.0], [1.0, 1.0, 1.0, 1.0]]) m = np.transpose(m) #print(m) # the Object glMatrixMode(GL_MODELVIEW) glLoadIdentity() glPushMatrix() glLoadMatrixd(m) glCallList(pikachu.gl_list) glPopMatrix() break #frame.show(2) quit = False for event in pygame.event.get(): if event.type == pygame.QUIT: quit = True break if quit: break pygame.display.flip() video.release() cv2.destroyAllWindows() pygame.quit()
def main(): # Vídeo de entrada + lista com todos os frames (:Image()) video = Video(SOURCE_VIDEO) frames = video.getFrameList() """ Aruco Markers * Quatro versões do aruco marker são armazenadas: * Uma para cada rotação possível.""" global markers markers = [Image(SOURCE_MARKER)] for n in range(3): markers.append(markers[-1].getRotated90()) #global camera camera = None if CALCULATE_INTRINSIC_MATRIX: camera = Camera(frames) else: camera = Camera( None, np.mat( "306.15951514 0.0 306.99696673; 0.0 255.6431554 250.73583755; 0.0 0.0 1.0" )) pygame.init() display = (640, 480) screen = pygame.display.set_mode(display, DOUBLEBUF | OPENGL) pygame.display.set_caption("ICV") fx = camera.getIntrinsicMatrix()[0][0] fy = camera.getIntrinsicMatrix()[1][1] fovy = 2 * np.arctan(0.5 * display[1] / fy) * 180 / np.pi aspect = (display[0] * fx) / (display[1] * fy) gluPerspective(fovy, aspect, 0.1, 150.0) glMatrixMode(GL_MODELVIEW) glEnable(GL_TEXTURE_2D) pikachu = objloader.OBJ('Pikachu.obj', swapyz=True) bg_id = glGenTextures(1) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, bg_id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) for frame in frames: glClearColor(0.0, 0.0, 0.0, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) """if not videoStore.isOpened(): videoStore.release() return""" videoStore = video.getSource() #ok, frame = videoStore.read() gray = frame.getGrayScale() edge = gray.getEdgeImage() # /* load_background """bg_img = cv2.cvtColor(frame.getSource(), cv2.COLOR_BGR2RGB) bg_img = cv2.flip(bg_img, 0) height, width, channels = bg_img.shape bg_img = np.frombuffer(bg_img.tostring(), dtype=bg_img.dtype) bg_img.shape = (height, width, channels)""" bg_img = frame.getSource().tobytes() #height, width, channels = frame.getShape() width, height = 640, 480 #bg_id = glGenTextures(1) glEnable(GL_TEXTURE_2D) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, bg_id) #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_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, bg_img) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, width, 0, height) #glBindTexture(GL_TEXTURE_2D, bg_id) #glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, bg_img) drawPlane(height, width) """glPushMatrix() glBegin(GL_QUADS) glTexCoord2i(0, 0); glVertex2i(0, 0) glTexCoord2i(1, 0); glVertex2i(width, 0) glTexCoord2i(1, 1); glVertex2i(width, height) glTexCoord2i(0, 1); glVertex2i(0, height) glEnd() glPopMatrix()""" glBindTexture(GL_TEXTURE_2D, 0) # load_background *\ glMatrixMode(GL_PROJECTION) gluPerspective(fovy, aspect, 0.1, 150.0) objp = np.zeros((6 * 7, 3), np.float32) objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2) for contour in edge.getContours(): for n, marker in enumerate(markers): warped = gray.getWarpedPerspective(contour, marker) # Se o contorno encontrado for igual ao marker, # desenha o contorno com a cor certa. if warped == marker: imagePoints = np.array(contour, dtype="float32") objectPoints = np.array( [[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]], dtype="float32") flag, rvecs, tvecs = cv2.solvePnP( objectPoints, imagePoints, camera.getIntrinsicMatrix(), camera.distance) # , flags=cv2.SOLVEPNP_ITERATIVE) # print(flag, rvecs, tvecs) rotm = cv2.Rodrigues(rvecs)[0] m = np.array( [[rotm[0][0], rotm[0][1], rotm[0][2], tvecs[0]], [rotm[1][0], rotm[1][1], rotm[1][2], tvecs[1]], [rotm[2][0], rotm[2][1], rotm[2][2], tvecs[2]], [0.0, 0.0, 0.0, 1.0]]) m = m * np.array( [[1.0, 1.0, 1.0, 1.0], [-1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, -1.0, -1.0], [1.0, 1.0, 1.0, 1.0]]) m = np.transpose(m) # the Object # glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glPushMatrix() glLoadMatrixd(m) # drawObject() # glutWireTeapot(0.5) glCallList(pikachu.gl_list) glPopMatrix() #glutSwapBuffers() quit = False for event in pygame.event.get(): if event.type == pygame.QUIT: quit = True break if quit: break pygame.display.flip() pygame.quit() """fx = camera.getIntrinsicMatrix()[0][0]
def load_and_draw_model2(foldername, filename): viewport = (1200, 747) srf = pygame.display.set_mode(viewport, OPENGL | DOUBLEBUF) import objloader # LOAD OBJECT AFTER PYGAME INIT obj = objloader.OBJ(foldername, filename, swapyz=True) clock = pygame.time.Clock() rx, ry = (0, 0) tx, ty = (0, 0) zpos = 5 rotate = move = False onlyone = True while onlyone: clock.tick(30) for e in pygame.event.get(): if e.type == QUIT: sys.exit() elif e.type == KEYDOWN and e.key == K_ESCAPE: sys.exit() elif e.type == MOUSEBUTTONDOWN: if e.button == 4: zpos = max(1, zpos - 1) elif e.button == 5: zpos += 1 elif e.button == 1: rotate = True elif e.button == 3: move = True elif e.type == MOUSEBUTTONUP: if e.button == 1: rotate = False elif e.button == 3: move = False elif e.type == MOUSEMOTION: i, j = e.rel if rotate: rx += i ry += j if move: tx += i ty -= j glEnable(GL_BLEND) glBlendFunc(GL_ONE, GL_ZERO) glDisable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) draw_background('background-1.png') glLightfv(GL_LIGHT0, GL_POSITION, (-40, 200, 100, 0.0)) glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0)) glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0)) glEnable(GL_LIGHT0) glEnable(GL_LIGHTING) glEnable(GL_COLOR_MATERIAL) glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded glMatrixMode(GL_PROJECTION) glLoadIdentity() width, height = viewport gluPerspective(90.0, width / float(height), 1, 100.0) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # RENDER OBJECT # glTranslate(tx / 20., ty / 20., - zpos) glTranslate(-0.4, -1, -zpos) glRotate(ry, 1, 0, 0) glRotate(rx, 0, 1, 0) glScale(0.03, 0.03, 0.03) glCallList(obj.gl_list) pygame.display.flip()
def loadObjects(self): self.objects = {} self.objects['wheel'] = objloader.OBJ("wheel.obj") self.objects['frame'] = objloader.OBJ("frame.obj") self.objects['rider'] = objloader.OBJ("rider.obj") self.objects['cp'] = objloader.OBJ("cp.obj")