-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_Primitives.py
More file actions
149 lines (105 loc) · 4.39 KB
/
01_Primitives.py
File metadata and controls
149 lines (105 loc) · 4.39 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from OpenGL.GL import *
from OpenGL.GLU import *
import sys
from PyQt5.QtWidgets import QOpenGLWidget, QApplication, QMainWindow, QLabel, QLineEdit, QVBoxLayout, QWidget
from PyQt5.QtWidgets import QGroupBox, QGridLayout, QSlider, QComboBox, QPushButton
from PyQt5.QtCore import *
from PyQt5.QtGui import QPainter, QPen
import numpy as np
PRIMITIVES = ['GL_POINTS', 'GL_LINES', 'GL_LINE_STRIP', 'GL_LINE_LOOP',
'GL_TRIANGLES', 'GL_TRIANGLE_STRIP', 'GL_TRIANGLE_FAN',
'GL_QUADS', 'GL_POLYGON']
PRIMITIVE_VALUES = [ GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP,
GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN,
GL_QUADS, GL_POLYGON]
selected = 0
POINTS = [[0,0], [10, 10], [100, 50]]
class MyGLWidget(QOpenGLWidget):
def __init__(self, parent=None):
super(MyGLWidget, self).__init__(parent)
self.colors = []
self.colors.append(np.array([0.0, 0.0, 0.0]))
self.colors.append(np.array([0.0, 0.0, 0.0]))
self.colors.append(np.array([0.0, 0.0, 0.0]))
def initializeGL(self):
# OpenGL 그리기를 수행하기 전에 각종 상태값을 초기화
glClearColor(0.8, 0.8, 0.6, 1.0)
def resizeGL(self, width, height):
# 카메라의 투영 특성을 여기서 설정
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, 350, 300, 0, -1, 1)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
# 색과 프리미티브를 이용한 객체 그리기
glColor3f(1,0,0)
glBegin(PRIMITIVE_VALUES[selected])
for i in range(len(POINTS)):
glVertex2fv(POINTS[i])
glEnd()
# 그려진 프레임버퍼를 화면으로 송출
glFlush()
class MyWindow(QMainWindow):
def __init__(self, title = ''):
QMainWindow.__init__(self) # call the init for the parent class
self.setWindowTitle(title)
### GUI 설정
central_widget = QWidget()
self.setCentralWidget(central_widget)
gui_layout = QVBoxLayout() # CentralWidget에 사용될 수직 나열 레이아웃
# 배치될 것들 - GL Window + Control
central_widget.setLayout(gui_layout)
self.glWidget = MyGLWidget() # OpenGL Widget
gui_layout.addWidget(self.glWidget)
self.controlGroup = QGroupBox('Vertex and Primitives')
gui_layout.addWidget(self.controlGroup)
control_layout = QVBoxLayout()
self.controlGroup.setLayout(control_layout)
primitive_selection = QComboBox()
for i in range(len(PRIMITIVES)):
primitive_selection.addItem(PRIMITIVES[i])
#ComboBox에 기능 연결
primitive_selection.currentIndexChanged.connect(self.selectPrimitive)
reset_button = QPushButton('reset vertices', self)
reset_button.clicked.connect(self.resetPoints)
control_layout.addWidget(primitive_selection)
control_layout.addWidget(reset_button)
self.canvas = Drawer(parent=self)
gui_layout.addWidget(self.canvas)
def selectPrimitive(self, text):
global selected
selected = int(text)
self.glWidget.update()
def resetPoints(self, btn):
global POINTS
POINTS = []
self.glWidget.update()
self.canvas.update()
class Drawer(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.parent = parent
self.painter = QPainter()
def paintEvent(self, event):
global POINTS
self.painter.begin(self)
self.painter.setPen(QPen(Qt.blue, 3))
for i in range(len(POINTS)):
self.painter.drawPoint(POINTS[i][0], POINTS[i][1])
for i in range(len(POINTS)-1):
self.painter.drawLine(POINTS[i][0], POINTS[i][1], POINTS[i+1][0], POINTS[i+1][1])
self.painter.end()
def mousePressEvent(self, event):
POINTS.append([event.x(), event.y()])
self.parent.glWidget.update()
self.update()
def main(argv = []):
app = QApplication(argv)
window = MyWindow('Primitives')
window.setFixedSize(400, 800)
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv)