Created
November 5, 2016 03:43
-
-
Save willamesoares/52495b13138395a2dd42847bbc1e9926 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from OpenGL.GL import * | |
from OpenGL.GLU import * | |
from OpenGL.GLUT import * | |
#string para identificar quando ESC for pressionado | |
ESCAPE = '\033' | |
RETURN = '\r' | |
angle = 45 | |
fAspect = 1 | |
#ID da janela GLUT | |
window = 0 | |
# extra variables | |
ang = 0 | |
angLua = 0 | |
def sol(): | |
glColor3f(1.0, 1.0, 0.0) | |
glutSolidSphere(10, 20, 20) | |
def mercurio(): | |
glColor3f(1.0, 0.0, 0.0) | |
glutSolidSphere(6, 20, 20) | |
def venus(): | |
glColor3f(1.0, 0.5, 0.0) | |
glutSolidSphere(6, 20, 20) | |
def terra(): | |
glColor3f(0.0, 0.0, 1.0) | |
glutSolidSphere(5, 20, 20) | |
def lua(): | |
glColor3f(1.0, 1.0, 1.0) | |
glutSolidSphere(2, 20, 20) | |
def marte(): | |
glColor3f(1.0, 0.5, 1.0) | |
glutSolidSphere(6, 20, 20) | |
def jupiter(): | |
glColor3f(1.0, 0.3, 1.0) | |
glutSolidSphere(6, 20, 20) | |
def saturno(): | |
glColor3f(0.3, 0.5, 0.0) | |
glutSolidSphere(6, 20, 20) | |
def urano(): | |
glColor3f(0.1, 0.1, 1.0) | |
glutSolidSphere(6, 20, 20) | |
def neturno(): | |
glColor3f(1.0, 0.0, 1.0) | |
glutSolidSphere(6, 20, 20) | |
#Desenhar objetos no ambiente | |
def DrawGLScene(): | |
global angLua, ang | |
posicaoLuz = [0.0, 0.0, 0.0, 1.0] | |
glLightfv(GL_LIGHT0, GL_POSITION, posicaoLuz) | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
sol() | |
glPushMatrix() | |
glRotatef(ang*2, 0, 1, 0) | |
glTranslatef(40, 0, 0) | |
mercurio() | |
glPopMatrix() | |
glPushMatrix() | |
glRotatef(ang*1.9, 0, 1, 0) | |
glTranslatef(70, 0, 0) | |
venus() | |
glPopMatrix() | |
glPushMatrix() | |
glRotatef(ang*1.8, 0, 1, 0) | |
glTranslatef(100, 0, 0) | |
terra() | |
glPushMatrix() | |
glRotatef(angLua, 0, 0, 1) | |
glTranslatef(10, 0, 0) | |
lua() | |
glPopMatrix() | |
glPopMatrix() | |
glPushMatrix() | |
glRotatef(ang*1.7, 0, 1, 0) | |
glTranslatef(130, 0, 0) | |
marte() | |
glPopMatrix() | |
glPushMatrix() | |
glRotatef(ang*1.6, 0, 1, 0) | |
glTranslatef(160, 0, 0) | |
jupiter() | |
glPopMatrix() | |
glPushMatrix() | |
glRotatef(ang*1.5, 0, 1, 0) | |
glTranslatef(190, 0, 0) | |
saturno() | |
glPopMatrix() | |
glPushMatrix() | |
glRotatef(ang*1.4, 0, 1, 0) | |
glTranslatef(220, 0, 0) | |
urano() | |
glPopMatrix() | |
glPushMatrix() | |
glRotatef(ang*1.3, 0, 1, 0) | |
glTranslatef(250, 0, 0) | |
neturno() | |
glPopMatrix() | |
glutSwapBuffers() | |
ang += 1 | |
angLua += 1 | |
#Configurar parametros iniciais da janela | |
#chamada logo apos criar a janela | |
def InitGL(width, height): | |
global angle | |
#carregar texturas | |
# LoadTextures(0) | |
# glEnable(GL_TEXTURE_2D) | |
luzAmbiente = [0.2,0.2,0.2,1.0] | |
luzDifusa = [0.7,0.7,0.7,1.0] | |
luzEspecular = [0.0, 0.0, 0.0, 1.0] | |
especularidade = [1.0,1.0,1.0,1.0] | |
especMaterial = 60 | |
#limpar o fundo com cor preta | |
glClearColor(0.0,0.0,0.0,1.0) | |
glShadeModel(GL_SMOOTH) | |
glMaterialfv(GL_FRONT, GL_SPECULAR, especularidade) | |
glMateriali(GL_FRONT, GL_SHININESS, especMaterial) | |
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, luzAmbiente) | |
glLightfv(GL_LIGHT0, GL_AMBIENT, luzAmbiente) | |
glLightfv(GL_LIGHT0, GL_DIFFUSE, luzDifusa) | |
glLightfv(GL_LIGHT0, GL_SPECULAR, luzEspecular) | |
# Habilita a definicao da cor do material a partir da cor corrente | |
glEnable(GL_COLOR_MATERIAL) | |
# Habilita o uso de iluminacao | |
glEnable(GL_LIGHTING) | |
# Habilita a luz de numero 0 | |
glEnable(GL_LIGHT0) | |
# Habilita o depth-buffering | |
glEnable(GL_DEPTH_TEST) | |
angle=45 | |
def EspecificaParametrosVisualizacao(): | |
global angle, fAspect | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(angle, fAspect, 0.4, 500) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
gluLookAt(0,80,200, 0,0,0, 0,1,0) | |
#Redesenhar janela quando ela for redimensionada | |
def ReSizeGLScene(width, height): | |
#prevenir que tamanho seja igual a zero | |
if height == 0: | |
height = 1 | |
fAspect = width/height | |
EspecificaParametrosVisualizacao() | |
#Gerenciar teclas pressionadas | |
def keyPressed(*args): | |
global window | |
#ESCAPE | |
if args[0] == ESCAPE: | |
sys.exit() | |
def tempo(): | |
ang += 1; | |
if ang >= 360: | |
ang = 0 | |
angLua += 3; | |
if angLua >= 360: | |
angLua = 0 | |
glutPostRedisplay() | |
glutTimerFunc(33, tempo, 1) | |
def main(*args): | |
global window | |
#passar argumentos para funcao init | |
#deve ser chamada antes de glutCreateWindow | |
glutInit(sys.argv) | |
#configurar modo de display inicial | |
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) | |
#dimensoes da janela | |
glutInitWindowSize(900, 900) | |
#posicao inicial da janela | |
glutInitWindowPosition(0,0) | |
#criar janela, recuperar ID e configurar titulo da janela | |
window = glutCreateWindow("Sistema Solar") | |
#registrar qual funcao ira desenhar | |
glutDisplayFunc(DrawGLScene) | |
#Funciona como um while loop pra redesenhar o objeto constantemente | |
glutIdleFunc(DrawGLScene) | |
# Funcao a ser chamada quando janela for redimensionada | |
glutReshapeFunc(ReSizeGLScene) | |
#Funcao a ser chamada quando alguma tecla for pressionada | |
glutKeyboardFunc(keyPressed) | |
# timer | |
glutTimerFunc(33, tempo, 1) | |
#inicializar janela | |
InitGL(900,900) | |
#iniciar evento | |
glutMainLoop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment