1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| #!/usr/bin/python
# -*- coding: utf-8 -*-
from math import cos,sin,radians
from OpenGL.GL import *
from OpenGL.GLU import *
class Vertex(object) :
def __init__(self,x=False,y=False,z=False,vertexv=False) :
''' Object from type Vertex representing an vertice '''
if vertexv :
# The coordinates component are given in an vector.
self.wx=vertexv[0]
self.wy=vertexv[1]
self.wz=vertexv[2]
else :
# The coordinates component are given individually.
self.wx=x
self.wy=y
self.wz=z
def get_vertex(self) :
return (self.wx,self.wy,self.wz)
def generate_polygon_on_xy(edges,radius,center,offset=0) :
''' Return an polygon on surface xy:
y
|
____|____x
|
|
from edges sides, from radius radius, with offset offset. '''
if not isinstance(center,Vertex) :
raise TypeError(Vertex)
polygon=[] # Polygon vertice container.
scale=360./edges # Computing of the angle separating 2 points from the polygon.
i=0
while i < edges :
polygon.append(Vertex((radius*cos(radians((scale*i)+offset)))+center.wx,(radius*sin(radians((scale*i)+offset)))+center.wy,center.wz))
i += 1
return polygon
try :
import pygame
from pygame.locals import *
except :
print "pygame required !\nPlease install the pygame module and relaunch the script."
quit()
from time import sleep
def resizeGL(width,height) :
fov_angle=60.0 # Angle of eye view.
z_near=2.0 # Distance from the user from the screen.
z_far=1000.0 # Distance in depth.
glMatrixMode(GL_PROJECTION) # Enable Projection matrix configuration.
#glLoadIdentity()
gluPerspective( fov_angle,
float(width)/float(height),
z_near,
z_far )
glLoadIdentity()
glOrtho( -30.0, # Left coordinates value. ( x_min )
30.0, # Right coordinates value. ( x_max )
-30.0, # Bottom coordinates value. ( y_min )
30.0, # Top coordinates value. ( y_max )
-30.0, # Near coordinates value. ( z_min )
30.0) # Far coordinates value. ( z_max )
glMatrixMode(GL_MODELVIEW) # Enable modelview matrix as current matrix.
def initGL(width,height) :
glClearColor(0.0, 0.0, 0.0, 0.0) # Define clear color [0.0-1.0]
glEnable(GL_DEPTH_TEST) # Enable GL depth functions.
#######################
glShadeModel(GL_SMOOTH) # This configuration setting permit the color gradient effect. contrary to the GL_FLAT argument.
#######################
resizeGL(width,height) # Call to the resize function.
def mainloop() :
''' Display function '''
global red,green,blue,color_high
red=0.0
green=0.0
blue=0.0
color_high='gray'
pygame.key.set_repeat(100, 10)
colors=[(255,255,0),(255,0,0),(255,0,0),(255,255,0),(0,255,0),(0,255,0)]
glColor3ubv((255,255,255))
hexagon=generate_polygon_on_xy(6,20.0,Vertex(0.0,0.0,0.0))
while True :
glClearColor(red,green,blue,0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
i=0
glBegin(GL_POLYGON)
for v in hexagon :
glColor3ubv(colors[i])
glVertex3fv(v.get_vertex())
i += 1
glEnd()
for event in pygame.event.get() :
''' So we can catch user-interaction events in the mainloop. '''
if event.type == QUIT : # The user close the window.
quit()
elif event.type == KEYDOWN : # The user press a keyboard key.
# event.unicode
# event.key
# event.mod
pass
pygame.display.flip()
sleep(0.0125)
def main() :
global screen,width,height
width=800.0
height=600.0
pygame.init() # We initialise the pygame module.
screen=pygame.display.set_mode((int(width),int(height)), # We set the window width and height
HWSURFACE | OPENGL | DOUBLEBUF, # We set flags.
24) # Indicator colors are coded on 24 bits.
initGL(width,height) # Call to initialise function.
mainloop() # We call our display function: mainloop.
if __name__ == "__main__" :
main() |
Partager