-
Notifications
You must be signed in to change notification settings - Fork 0
/
EquiView360.py
93 lines (83 loc) · 3.25 KB
/
EquiView360.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtOpenGL import QGLWidget
from PIL import Image
class GLWidget(QGLWidget):
def __init__(self, parent):
super().__init__(parent)
self.image = Image.open("example.jpg")
self.image_width, self.image_height = self.image.size
self.yaw = 0
self.pitch = 0
self.prev_dx = 0
self.prev_dy = 0
self.fov = 90
self.moving = False
def initializeGL(self):
glEnable(GL_TEXTURE_2D)
self.texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.texture)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, self.image_width, self.image_height, 0, GL_RGB, GL_UNSIGNED_BYTE, self.image.tobytes())
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
self.sphere = gluNewQuadric()
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90, self.width()/self.height(), 0.1, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glRotatef(self.pitch, 1, 0, 0)
glRotatef(self.yaw, 0, 1, 0)
glRotatef(90, 1, 0, 0)
glRotatef(-90, 0, 0, 1)
gluQuadricTexture(self.sphere, True)
gluSphere(self.sphere, 1, 100, 100)
glPopMatrix()
def resizeGL(self, width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(self.fov, self.width()/self.height(), 0.1, 1000)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.mouse_x, self.mouse_y = event.pos().x(), event.pos().y()
self.moving = True
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.moving = False
def mouseMoveEvent(self, event):
if self.moving:
dx = event.pos().x() - self.mouse_x
dy = event.pos().y() - self.mouse_y
dx *= 0.1
dy *= 0.1
self.yaw -= dx
self.pitch -= dy
self.pitch = min(max(self.pitch, -90), 90)
self.mouse_x, self.mouse_y = event.pos().x(), event.pos().y()
self.update()
def wheelEvent(self, event):
delta = event.angleDelta().y()
self.fov -= delta * 0.1
self.fov = max(30, min(self.fov, 90))
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(self.fov, self.width()/self.height(), 0.1, 1000)
self.update()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Equirectangular 360° Viewer")
self.setWindowIcon(QtGui.QIcon("icon.png"))
self.gl_widget = GLWidget(self)
self.setCentralWidget(self.gl_widget)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.setGeometry(0, 0, 1080, 720)
window.show()
app.exec_()