"""Teste de profundidade e descarte de faces traseiras. Aula 07 — Leitura de Objetos e Superfícies Ocultas. Teclas: D depth test, C culling, I inverte uma face, R reinicia, setas rotação, Esc fecha. """ from __future__ import annotations import sys from PySide6.QtCore import Qt from PySide6.QtGui import QSurfaceFormat from PySide6.QtOpenGLWidgets import QOpenGLWidget from PySide6.QtWidgets import QApplication from OpenGL.GL import * from OpenGL.GLU import gluLookAt, gluPerspective VERTICES = [ (0.0, 1.5, 0.0), (-1.0, 0.0, -1.0), (1.0, 0.0, -1.0), (1.0, 0.0, 1.0), (-1.0, 0.0, 1.0), ] FACES = [(0, 2, 1), (0, 3, 2), (0, 4, 3), (0, 1, 4), (1, 2, 3), (1, 3, 4)] CORES = [ (0.95, 0.45, 0.25), (0.25, 0.70, 0.95), (0.55, 0.80, 0.30), (0.80, 0.40, 0.85), (0.95, 0.75, 0.25), (0.25, 0.75, 0.65), ] class Visibilidade(QOpenGLWidget): def __init__(self, parent=None): super().__init__(parent) self.reiniciar() self.setFocusPolicy(Qt.FocusPolicy.StrongFocus) def reiniciar(self): self.depth, self.culling = True, False self.inverter_face = False self.rot_x, self.rot_y = -15.0, 35.0 def initializeGL(self) -> None: glClearColor(0.08, 0.09, 0.12, 1.0) def resizeGL(self, w: int, h: int) -> None: dpr = self.devicePixelRatioF() w, h = max(1, round(w * dpr)), max(1, round(h * dpr)) glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(50.0, w / float(h), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) def paintGL(self) -> None: (glEnable if self.depth else glDisable)(GL_DEPTH_TEST) (glEnable if self.culling else glDisable)(GL_CULL_FACE) glCullFace(GL_BACK) glFrontFace(GL_CCW) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() gluLookAt(0.0, 2.2, 5.5, 0.0, 0.6, 0.0, 0.0, 1.0, 0.0) glRotatef(self.rot_x, 1.0, 0.0, 0.0) glRotatef(self.rot_y, 0.0, 1.0, 0.0) glBegin(GL_TRIANGLES) for numero, (face, cor) in enumerate(zip(FACES, CORES)): if self.inverter_face and numero == 2: face = (face[0], face[2], face[1]) glColor3f(*cor) for indice in face: glVertex3f(*VERTICES[indice]) glEnd() self.setWindowTitle( f"D depth={'on' if self.depth else 'off'} | " f"C culling={'on' if self.culling else 'off'} | I face invertida={self.inverter_face} | R reinicia" ) def keyPressEvent(self, event) -> None: tecla = event.key() if tecla == Qt.Key.Key_Escape: self.close() return if tecla == Qt.Key.Key_D: self.depth = not self.depth elif tecla == Qt.Key.Key_R: self.reiniciar() elif tecla == Qt.Key.Key_I: self.inverter_face = not self.inverter_face elif tecla == Qt.Key.Key_C: self.culling = not self.culling elif tecla == Qt.Key.Key_Left: self.rot_y -= 5.0 elif tecla == Qt.Key.Key_Right: self.rot_y += 5.0 elif tecla == Qt.Key.Key_Up: self.rot_x -= 5.0 elif tecla == Qt.Key.Key_Down: self.rot_x += 5.0 else: return self.update() def main() -> int: fmt = QSurfaceFormat() fmt.setVersion(2, 1) fmt.setProfile(QSurfaceFormat.OpenGLContextProfile.NoProfile) fmt.setDepthBufferSize(24) QSurfaceFormat.setDefaultFormat(fmt) app = QApplication(sys.argv) janela = Visibilidade() janela.resize(800, 600) janela.show() return app.exec() if __name__ == "__main__": raise SystemExit(main())