"""Primitiva GL_TRIANGLES — dois triângulos independentes. Aula 02 — Visualização 2D: primitivas gráficas. Exemplo em Python com PySide6 e PyOpenGL (pipeline fixed-function). Execute com: python triangulos2d.py Pontos de atenção: * ``GL_TRIANGLES`` consome os vértices de **três em três**: v0,v1,v2 formam o primeiro triângulo e v3,v4,v5 o segundo. Vértices sobrando (1 ou 2 no fim) são simplesmente descartados. * ``glColor3f`` é **estado corrente**: vale para todos os vértices emitidos depois dela, até a próxima chamada. Por isso uma única chamada antes de cada trio pinta o triângulo inteiro de cor sólida. Experimente: troque ``GL_TRIANGLES`` por ``GL_TRIANGLE_STRIP`` e por ``GL_TRIANGLE_FAN`` mantendo os mesmos 6 vértices — o número de triângulos gerados muda (2, 4 e 4, respectivamente). """ from __future__ import annotations import sys from PySide6.QtCore import Qt from PySide6.QtGui import QSurfaceFormat from PySide6.QtWidgets import QApplication from PySide6.QtOpenGLWidgets import QOpenGLWidget from OpenGL.GL import * from OpenGL.GLU import gluOrtho2D class Triangulos2D(QOpenGLWidget): """Desenha dois triângulos com GL_TRIANGLES.""" def initializeGL(self) -> None: glClearColor(0.10, 0.11, 0.13, 1.0) def resizeGL(self, w: int, h: int) -> None: w, h = max(1, w), max(1, h) glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() if w <= h: gluOrtho2D(-50.0, 50.0, -50.0 * h / w, 50.0 * h / w) else: gluOrtho2D(-50.0 * w / h, 50.0 * w / h, -50.0, 50.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def paintGL(self) -> None: glClear(GL_COLOR_BUFFER_BIT) glBegin(GL_TRIANGLES) glColor3f(0.95, 0.30, 0.30) # 1o. triângulo, vermelho glVertex2f(-35.0, -14.0) # v0 glVertex2f(-21.0, 14.0) # v1 glVertex2f( -7.0, -14.0) # v2 glColor3f(0.35, 0.55, 0.95) # 2o. triângulo, azul glVertex2f( 7.0, 14.0) # v3 glVertex2f( 21.0, -14.0) # v4 glVertex2f( 35.0, 14.0) # v5 glEnd() def keyPressEvent(self, event) -> None: if event.key() == Qt.Key.Key_Escape: self.close() def main() -> int: fmt = QSurfaceFormat() fmt.setVersion(2, 1) fmt.setProfile(QSurfaceFormat.OpenGLContextProfile.NoProfile) fmt.setDepthBufferSize(0) QSurfaceFormat.setDefaultFormat(fmt) app = QApplication(sys.argv) window = Triangulos2D() window.setWindowTitle("Primitivas 2D — GL_TRIANGLES") window.resize(600, 400) window.show() return app.exec() if __name__ == "__main__": raise SystemExit(main())