Created
July 17, 2018 23:10
-
-
Save Sasszem/04fdaccd0112a2cda7bc3a099b27bb2a to your computer and use it in GitHub Desktop.
A PyOpenGL based 3d opbject renderer class with transformations
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
# -*- coding: utf-8 -*- | |
from OpenGL.GL import * | |
from array import array | |
glEnableClientState(GL_VERTEX_ARRAY) | |
class Primitive(): | |
def __init__(self,vert,colors,colors2,drawMode,ind,x=0,y=0,z=0,s=0,rX=0,rY=0,rZ=0,sX=1,sY=1,sZ=1): | |
#Csúcstömbadatok tárolása/Storing vertex-data | |
self.vert=array('f',list(vert)) | |
self.vert=glGenBuffers(1) | |
glBindBuffer(GL_ARRAY_BUFFER,self.vert) | |
#Színtömbadatok/Storing color-data | |
self.colors=array('f',list(colors)) | |
#Indexadatok/Incidies | |
self.ind=array('B',list(ind)) | |
#X-y eltolás/X-Y translation | |
self.x,self.y,self.z=x,y,z | |
#Elforgatás/Rotation | |
self.rX,self.rY,self.rZ=rX,rY,rZ | |
#Másodlagos színtömbadatok(Inaktív 3szög esetén)/Secondary coloring, when triangle is inactive | |
self.colors2=array('f',list(colors2)) | |
#Aktív-inaktív/Active-inactive | |
self.selected=s | |
#glDrawElements DrawMode | |
self.drawM=drawMode | |
#Átméretezés/Scaling | |
self.sX,self.sY,self.sZ=sX,sY,sZ | |
def draw(self): | |
#Ideiglenes projekciós mártix létrehozása/Creating a temporary projection matrix | |
glPushMatrix() | |
#Döntés elsődlege-másodlagos szinezés között/Chosing betveen primary and secondary coloring | |
if self.selected: glColorPointer( 3, GL_FLOAT, 0, self.colors.tostring( ) );cl=len(self.colors)/3 | |
else: glColorPointer( 3, GL_FLOAT, 0, self.colors2.tostring( ) );cl=len(self.colors2)/3 | |
#Csúcstömbadatok feltöltése/Uploading vertexarray | |
glVertexPointer ( 3, GL_FLOAT, 0, self.vert.tostring( ) ) | |
#Eltolás/translation | |
glTranslate(self.x, self.y, self.z) | |
#Elforgatás/Rotation | |
glRotate(self.rX,1,0,0) | |
glRotate(self.rY,0,1,0) | |
glRotate(self.rZ,0,0,1) | |
glScale(self.sX,self.sY,self.sZ) | |
#Kirajzolás/Drawing | |
glDrawElements( self.drawM, len(self.ind), GL_UNSIGNED_BYTE, self.ind.tostring( ) ) | |
#Eredeti projekciós mátrix visszaállítása/Restoring original projection matrix | |
glPopMatrix() | |
def Coords(self,x,y,z=0): | |
self.x+=x | |
self.y+=y | |
self.z+=z | |
def Rotate(self,rX,rY,rZ): #Összes elem elforgatása/Rotate all elements | |
self.rX+=rX | |
self.rY+=rY | |
self.rZ+=rZ | |
def Size(self,sX,sY,sZ): | |
self.sX+=sX | |
self.sY+=sY | |
self.sZ+=sZ | |
#Osztály tárolásra/A Class for storing | |
class ElementsList(): | |
def __init__(self,elements): | |
#Elemeket egy listában tároljuk/We store the elements in a list | |
self.el=list(elements) | |
self.sel=0#Kiválasztot elem/Selected element | |
def __len__(self): | |
#Elemszám/lenght | |
return len(self.el) | |
def __setitem__(self,key,value): | |
#Elemek elérése/Access elements | |
self.el[key]=value | |
def __getitem__(self,key): | |
#Elemek elérése/Access elements | |
return self.el[key] | |
def append(self,value): | |
#Elemek elérése/Access elements | |
self.el.append(value) | |
def select(self,i): | |
#Adott elem kiválasztása/Select an element | |
for item in self.el: item.selected=0 #Összes elem kiválasztásának törlése/Deselect all elements | |
self.el[i].selected=1 #Adott elem kiválasztása/Select element | |
self.sel=i | |
def deselect(self,i): | |
for item in self.el: item.selected=0 #Összes elem kiválasztásának törlése/Deselect all elements | |
self.sel=None | |
def upselect(self): self.select(self.sel+1) | |
def downselect(): self.select(self.sel-1) | |
def draw(self): #Kirajzolás/Drawing | |
for i in self.el: i.draw() | |
def aCoords(self,x,y,z=0): #Aktív elem eltolása | |
for i in self.el: #\ | |
if i.selected==1: # > Aktív elem kiválasztása/Select active element | |
i.x+=x #Eltolás | |
i.y+=y #Translation | |
i.z+=z | |
def Coords(self,x,y,z=0): #Összes elem eltolása/Translate all elements | |
for i in self.el: #Minden elemre/For all elements | |
i.x+=x | |
i.y+=y | |
i.z+=z | |
def aRotate(self,rX,rY,rZ): #Aktív elem elforgatása/Rotate active element | |
for i in self.el: | |
if i.selected==1: | |
i.rX+=rX | |
i.rY+=rY | |
i.rZ+=rZ | |
def Rotate(self,rX,rY,rZ): #Összes elem elforgatása/Rotate all elements | |
for i in self.el: | |
i.rX+=rX | |
i.rY+=rY | |
i.rZ+=rZ | |
def aSize(self,sX,sY,sZ): #Aktív elem átméretezése/Scale active element | |
for i in self.el: | |
if i.selected==1: | |
i.sX+=sX | |
i.sY+=sY | |
i.sZ+=sZ | |
def Size(self,sX,sY,sZ): #Összes elem átméretezése/Scale all elements | |
for i in self.el: | |
i.sX+=sX | |
i.sY+=sY | |
i.sZ+=sZ | |
cVertices = [ -1,-1,1, | |
-1,1,1, | |
1,1,1, | |
1,-1,1, | |
-1,-1,-1, | |
-1,1,-1, | |
1,1,-1, | |
1,-1,-1 ] | |
cColors = [ 0, 0, 0, | |
1, 0, 0, | |
1, 1, 0, | |
0, 1, 0, | |
0, 0, 1, | |
1, 0, 1, | |
1, 1, 1, | |
0, 1, 1] | |
cFaces = [0, 3, 2, 1, | |
2, 3, 7, 6, | |
0, 4, 7, 3, | |
1, 2, 6, 5, | |
4, 5, 6, 7, | |
0, 1, 5, 4 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment