Análise e Explicação dos Scripts OpenGL
1. Análise do script trianguloquadrado (1).py:
from [Link] import *
from [Link] import *
from [Link] import *
Essas três bibliotecas são da PyOpenGL:
- GL = comandos gráficos básicos.
- GLUT = utilitários para janelas e eventos.
- GLU = utilitários gráficos (projeções, câmeras).
def myInit():
glClearColor(0.5, 0.7, 1.0, 1.0) # Cor de fundo azul claro
glColor3f(0.2, 0.5, 0.4) # Cor padrão dos objetos
glPointSize(10.0) # Tamanho dos pontos
gluOrtho2D(0, 500, 0, 500) # Área de desenho 2D
def display():
glClear(GL_COLOR_BUFFER_BIT) # Limpa a tela
glBegin(GL_POINTS)
glVertex2f(100, 100)
glVertex2f(300, 200)
glEnd()
glBegin(GL_QUADS)
glVertex2f(100.0, 100.0)
glVertex2f(300.0, 100.0)
glVertex2f(300.0, 200.0)
glVertex2f(100.0, 200.0)
glEnd()
glBegin(GL_TRIANGLE_STRIP)
glVertex2f(100.0, 210.0)
glVertex2f(300.0, 210.0)
glVertex2f(300.0, 310.0)
glEnd()
glFlush()
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(500, 500)
glutInitWindowPosition(100, 100)
glutCreateWindow(b"TRIANGULO QUADRADO")
myInit()
glutDisplayFunc(display)
glutMainLoop()
2. Transformação do script para um veículo 2D visto de lado:
Adicionamos:
- Retângulo como corpo do carro
- Outro retângulo como cabine
- Dois círculos como rodas
Código final com a figura de um carro 2D visto de lado:
[... código incluído na próxima página ...]
from [Link] import *
from [Link] import *
from [Link] import *
import math
def myInit():
glClearColor(0.8, 0.9, 1.0, 1.0)
glColor3f(0.2, 0.5, 0.4)
glPointSize(5.0)
gluOrtho2D(0, 500, 0, 500)
def drawCircle(x, y, radius, segments=100):
glBegin(GL_POLYGON)
for i in range(segments):
angle = 2 * [Link] * i / segments
dx = radius * [Link](angle)
dy = radius * [Link](angle)
glVertex2f(x + dx, y + dy)
glEnd()
def display():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0.2, 0.5, 0.8)
glBegin(GL_QUADS)
glVertex2f(150.0, 200.0)
glVertex2f(350.0, 200.0)
glVertex2f(350.0, 270.0)
glVertex2f(150.0, 270.0)
glEnd()
glColor3f(0.3, 0.6, 0.9)
glBegin(GL_QUADS)
glVertex2f(200.0, 270.0)
glVertex2f(300.0, 270.0)
glVertex2f(280.0, 300.0)
glVertex2f(220.0, 300.0)
glEnd()
glColor3f(0.1, 0.1, 0.1)
drawCircle(180, 180, 20)
drawCircle(320, 180, 20)
glFlush()
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(500, 500)
glutInitWindowPosition(100, 100)
glutCreateWindow(b"CARRO 2D")
myInit()
glutDisplayFunc(display)
glutMainLoop()