PyOpenGL : 3D temps réel
Sauter à la navigation
Sauter à la recherche
Avec EduPython3
Les librairies nécessaires sont incluses dans la distribution :
import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import *
Avec EduPython antérieur
- Installer d'abord pyopengl avec Conda
- Sur EduPython : Outil->outil->Installation d'un nouveau module #1 (conda) pyopengl
Plus généralement
Un exemple intéressant :
https://stackoverflow.com/questions/56643470/how-can-i-make-a-sphere-in-python-using-pyopengl-and-pygame
Et un programme de base affichant quelques boules avec une syntaxe simplifiée : affSphere(x,y,z) Fichier:Exemple de base 3D Python.pdf
import pygame from pygame.locals import * from OpenGL.GL import * #Installer d'abord pyopengl avec Conda from OpenGL.GLU import * #Sur EduPython : Outil->outil->Installation d'un nouveau module #1 (conda) pyopengl import math from math import * import numpy as np # pygame.init() display = (800, 600) scree = pygame.display.set_mode(display, DOUBLEBUF | OPENGL) glEnable(GL_DEPTH_TEST) sphere = gluNewQuadric() #Create new sphere glMatrixMode(GL_PROJECTION) gluPerspective(45, (display[0]/display[1]), 0.1, 50.0) #Bloc ajouté depuis https://stackoverflow.com/questions/42986754/pyopengl-sphere-with-texture glClearColor(0., 0., 0., 1.) glShadeModel(GL_SMOOTH) glEnable(GL_CULL_FACE) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) lightZeroPosition = [10., 4., 10., 1.] lightZeroColor = [0.8, 1.0, 0.8, 1.0] glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition) glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor) glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1) glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05) glEnable(GL_LIGHT0) # glMatrixMode(GL_MODELVIEW) gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1) viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX) glLoadIdentity() # def affSphereM(M,r): "Affiche Sphère à partir d'un point à trois coordonnées" affSphere(M.x, M.y, M.z) # def affSphere(x,y,z,r): "Affiche une sphère centrée sur (x,y,z) de rayon r " "et de couleur définie par trois nombres de [0;1]" glPushMatrix() glTranslatef(x,y,z) #Move to the place gluSphere(sphere, r, 16, 8) #Draw sphere glPopMatrix() # # La boucle principale # t=0 #Le temps qui s'écoule run=True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN: run = False keypress = pygame.key.get_pressed() # init model view matrix glLoadIdentity() # init the view matrix glPushMatrix() glLoadIdentity() # Bouger l'écran avec les touches qsd etc... if keypress[pygame.K_w]: glTranslatef(0,0,0.1) if keypress[pygame.K_s]: glTranslatef(0,0,-0.1) if keypress[pygame.K_d]: glTranslatef(-0.1,0,0) if keypress[pygame.K_a]: glTranslatef(0.1,0,0) # multiply the current matrix by the get the new view matrix and store the final vie matrix glMultMatrixf(viewMatrix) viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX) # apply view matrix glPopMatrix() glMultMatrixf(viewMatrix) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #Clear the screen t+=0.01 #Le temps qui s'écoule entre chaque image # #C'est ici que l'on affiche les sphères # affSphere(0,0,0,0.5) affSphere(0,0,1+t,0.4) affSphere(0,1,0,0.4) affSphere(1,0,0,0.4) # pygame.display.flip() #Update the screen pygame.time.wait(10) pygame.quit()