81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
import sys
|
|
|
|
from OpenGL.raw.GL.VERSION.GL_1_0 import glClearColor
|
|
from PySide6.QtCore import Qt
|
|
from PySide6.QtOpenGLWidgets import QOpenGLWidget
|
|
from PySide6.QtWidgets import QDoubleSpinBox, QPushButton, QVBoxLayout, QApplication, QWidget
|
|
from OpenGL.GL import *
|
|
from OpenGL.GLUT import *
|
|
|
|
class GLWidget(QOpenGLWidget):
|
|
def __init__(self, parent=None):
|
|
super(GLWidget, self).__init__(parent)
|
|
self.shapes = []
|
|
self.extruded_shapes = []
|
|
|
|
def initializeGL(self):
|
|
glClearColor(1, 1, 1, 1)
|
|
|
|
def paintGL(self):
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
|
glLoadIdentity()
|
|
glColor3f(1, 0, 0)
|
|
|
|
for shape in self.shapes:
|
|
glBegin(GL_LINE_LOOP)
|
|
glVertex2f(shape[0], shape[1]) # Access x and y coordinates of the point
|
|
glEnd()
|
|
|
|
def resizeGL(self, w, h):
|
|
glViewport(0, 0, w, h)
|
|
glMatrixMode(GL_PROJECTION)
|
|
glLoadIdentity()
|
|
glOrtho(0, w, h, 0, -1, 1)
|
|
glMatrixMode(GL_MODELVIEW)
|
|
|
|
def mousePressEvent(self, event):
|
|
if event.button() == Qt.LeftButton:
|
|
pos = event.pos()
|
|
x = int(pos.x())
|
|
y = int(pos.y())
|
|
self.shapes.append((x, y)) # Append coordinate tuple (x, y)
|
|
self.update()
|
|
def extrude_shapes(self, height):
|
|
self.extruded_shapes = []
|
|
for shape in self.shapes:
|
|
extruded_shape = []
|
|
for point in shape:
|
|
extruded_shape.append([point[0], point[1]])
|
|
for point in shape:
|
|
extruded_shape.append([point[0], point[1] + height])
|
|
self.extruded_shapes.append(extruded_shape)
|
|
|
|
class MainWindow(QWidget):
|
|
def __init__(self):
|
|
super(MainWindow, self).__init__()
|
|
self.gl_widget = GLWidget()
|
|
self.height_spin_box = QDoubleSpinBox()
|
|
self.height_spin_box.setRange(0, 100)
|
|
self.height_spin_box.setValue(10)
|
|
self.extrude_button = QPushButton("Extrude")
|
|
self.extrude_button.clicked.connect(self.extrude_shapes)
|
|
|
|
layout = QVBoxLayout()
|
|
layout.addWidget(self.gl_widget)
|
|
layout.addWidget(self.height_spin_box)
|
|
layout.addWidget(self.extrude_button)
|
|
|
|
self.setLayout(layout)
|
|
|
|
def extrude_shapes(self):
|
|
height = self.height_spin_box.value()
|
|
self.gl_widget.extrude_shapes(height)
|
|
self.gl_widget.update()
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
window = MainWindow()
|
|
window.setGeometry(100, 100, 800, 600)
|
|
window.setWindowTitle("Extrude Shapes")
|
|
window.show()
|
|
sys.exit(app.exec_()) |