"""Atividade — octaedro como malha triangular indexada. Aula 07 — Leitura de Objetos e Superfícies Ocultas. Execute com: python octaedro_base.py """ from __future__ import annotations import math 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, 1.4, 0), (0, -1.4, 0), (-1, 0, 0), (0, 0, -1), (1, 0, 0), (0, 0, 1)] # TODO 1: oito triplas de índices, anti-horárias vistas de fora. FACES = [] 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), (0.65, 0.55, 0.90), (0.85, 0.60, 0.35)] def normal_face(face): # TODO 2: produto vetorial normalizado (b-a) x (c-a). return (0.0, 0.0, 0.0) class Octaedro(QOpenGLWidget): def __init__(self, parent=None): super().__init__(parent) self.angulo = 25.0 self.setWindowTitle("Octaedro | setas: rotação | Esc: sair") self.setFocusPolicy(Qt.FocusPolicy.StrongFocus) def initializeGL(self): glClearColor(0.08, 0.09, 0.12, 1.0) # TODO 3: habilite GL_DEPTH_TEST e GL_CULL_FACE. glCullFace(GL_BACK) glFrontFace(GL_CCW) def resizeGL(self, w, h): 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): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity(); gluLookAt(0, 1.8, 5, 0, 0, 0, 0, 1, 0) glRotatef(self.angulo, 0, 1, 0) # TODO 4: substitua os pontos pelo desenho de FACES em GL_TRIANGLES, # usando CORES[numero] para cada face. Não ative iluminação. glPointSize(8.0) glColor3f(1.0, 0.8, 0.2) glBegin(GL_POINTS) for vertice in VERTICES: glVertex3f(*vertice) glEnd() def keyPressEvent(self, event): if event.key() == Qt.Key.Key_Escape: self.close(); return if event.key() == Qt.Key.Key_Left: self.angulo -= 5 elif event.key() == Qt.Key.Key_Right: self.angulo += 5 else: return self.update() def main(): fmt = QSurfaceFormat(); fmt.setVersion(2, 1) fmt.setProfile(QSurfaceFormat.OpenGLContextProfile.NoProfile); fmt.setDepthBufferSize(24) QSurfaceFormat.setDefaultFormat(fmt) app = QApplication(sys.argv); janela = Octaedro(); janela.resize(800, 600); janela.show() return app.exec() if __name__ == "__main__": raise SystemExit(main())