first commit

This commit is contained in:
kostroksen3 2017-08-10 17:53:55 +02:00
commit 52c163021c
18 changed files with 22945 additions and 0 deletions

0
README.md Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

30
camera.py Normal file
View File

@ -0,0 +1,30 @@
from OpenGL.GLU import *
import math
class Camera:
def __init__(self,x,y,z):
self.x=x
self.y=y
self.z=z
self.tx=0.0
self.ty=0.0
self.tz=0.0
self.pitch=0.0
self.roll=0.0
def move(self,x,y,z):
self.x+=x
self.y+=y
self.z+=z
def moveTo(self,x,y,z):
self.x=x
self.y=y
self.z=z
def rotate(self,x,y):
self.roll+=x
self.pitch+=y
self.tx=math.cos(self.roll)*math.sin(self.pitch)
self.ty=math.cos(self.pitch)
self.tz=math.sin(self.roll)*math.sin(self.pitch)
def look(self):
gluLookAt(self.x,self.y,self.z,self.x+self.tx,self.y+self.ty,self.z+self.tz,0,1,0)
def getPos(self):
return self.x,self.y,self.z

BIN
camera.pyc Normal file

Binary file not shown.

13
cube.mtl Normal file
View File

@ -0,0 +1,13 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd fasfas.png

2576
cube.obj Normal file

File diff suppressed because it is too large Load Diff

12
cube2.mtl Normal file
View File

@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

26
cube2.obj Normal file
View File

@ -0,0 +1,26 @@
# Blender v2.76 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube2.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -0.000000 -0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
usemtl Material
s off
f 1//1 2//1 3//1 4//1
f 5//2 8//2 7//2 6//2
f 1//3 5//3 6//3 2//3
f 2//4 6//4 7//4 3//4
f 3//5 7//5 8//5 4//5
f 5//6 1//6 4//6 8//6

BIN
fasfas.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

20061
get-pip.py Normal file

File diff suppressed because it is too large Load Diff

93
main.py Normal file
View File

@ -0,0 +1,93 @@
import pygame
from pygame.locals import *
import glfw
import math
import time
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.arrays import vbo
import numpy as np
from ctypes import *
from camera import *
from obj_loader import *
from shaders import *
def main():
pygame.init()
display = (800,600)
pygame.display.set_mode(display, OPENGL|DOUBLEBUF)
i=0
running=True
cam=Camera(0,0,0)
vsh=open("shader.vsh")
fsh=open("shader.fsh")
shader=Shader(vsh,fsh)
rel=np.array([0,0])
dpi=200.0
rel2=np.array([0,0])
pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
glMatrixMode(GL_PROJECTION)
gluPerspective(60,800.0/600.0,0.1,1000.0)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glLightfv(GL_LIGHT0,GL_DIFFUSE,(1.0,1.0,1.0,1.0))
glLightfv(GL_LIGHT0,GL_AMBIENT,(0.2,0.2,0.2,1.0))
obj=Object("cube.obj",(0.0,0.0,-2.0),True )
while running:
ticks=pygame.time.get_ticks()
pressed=pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False
glClearColor(0.0,0.0,0.0,1.0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
shader.use()
cam.rotate(rel[0]/dpi,rel[1]/dpi)
cam.look()
obj.draw()
glLightfv(GL_LIGHT0,GL_POSITION,cam.getPos())
glPopMatrix()
pygame.display.flip()
rel=np.array(pygame.mouse.get_rel())
pygame.mouse.set_pos(display[0]/2, display[1]/2)
rel2=np.array(pygame.mouse.get_rel())
ticks=pygame.time.get_ticks()-ticks
if ticks>0:
fps=math.floor(1000000.0/(ticks*1000.0))
else:
fps=1000000.0
print(fps)
dt=ticks/1000.0
mx,my=math.sin(cam.roll),math.cos(cam.roll)
if pressed[pygame.K_d]:
cam.move(-mx*dt,0,my*dt)
if pressed[pygame.K_a]:
cam.move(mx*dt,0,-my*dt)
if pressed[pygame.K_w]:
cam.move(my*dt,0,mx*dt)
if pressed[pygame.K_s]:
cam.move(-my*dt,0,-mx*dt)
if pressed[pygame.K_SPACE]:
cam.move(0,dt,0)
if pressed[pygame.K_LCTRL]:
cam.move(0,-dt,0)
if pressed[pygame.K_ESCAPE]:
running=False
pygame.quit()
quit()
main()

118
obj_loader.py Normal file
View File

@ -0,0 +1,118 @@
from OpenGL.GL import *
from ctypes import *
import numpy as np
from math import *
class Object:
file=None
vertices=[]
normals=[]
faces=[]
materials=[]
lists=[]
r=[0,0,0]
texcoords=[]
def __init__(self,filename,pos,smoothnormals):
self.smoothnormals=smoothnormals
self.pos=pos
self.file=open(filename, 'r')
for line in self.file:
l=line.split()
if len(l)>0:
command=l[0]
words=l[1:]
if command=='v':
self.vertices.append((float(words[0]),float(words[1]),float(words[2])))
elif command=='vn':
self.normals.append((float(words[0]),float(words[1]),float(words[2])))
elif command=='vt':
self.texcoords.append((float(words[0]),float(words[1])))
elif command=='f':
if len(words)==3:
self.faces.append((int(words[0].split("/",2)[2])-1,\
int(words[0].split("/",2)[0])-1,\
int(words[1].split("/",2)[0])-1,\
int(words[2].split("/",2)[0])-1))
elif len(words)==4:
self.faces.append((int(words[0].split("/",2)[2])-1,\
int(words[0].split("/",2)[0])-1,\
int(words[1].split("/",2)[0])-1,\
int(words[2].split("/",2)[0])-1,\
int(words[3].split("/",2)[0])-1))
else:
print("Only Quads and Triangles are supported")
elif command=="mtllib":
filen=words[0]
f=open(filen, 'r')
for lin in f:
li=lin.split()
if len(li)>=1:
com=li[0]
other=li[1:]
if com=="newmtl":
name=other
else:
continue
self.file.close()
if self.smoothnormals:
nn=[]
for i in range(len(self.vertices)):
nn.append(None)
for i in range(len(self.vertices)):
vecX=0.0
vecY=0.0
vecZ=0.0
num=0
for face in self.faces:
for vertex in face[1:]:
if vertex==i:
vecX+=self.normals[face[0]][0]
vecY+=self.normals[face[0]][1]
vecZ+=self.normals[face[0]][2]
num+=1
if num>0:
vecX/=num
vecY/=num
vecZ/=num
d=sqrt(vecX*vecX+vecY*vecY+vecZ*vecZ)
if d>0:
vecX/=d
vecY/=d
vecZ/=d
nn[i]=(vecX,vecY,vecZ)
self.normals=nn
self.num=glGenLists(1)
glNewList(self.num,GL_COMPILE)
for face in self.faces:
if len(face)==5:
glBegin(GL_QUADS)
glNormal3fv(self.normals[face[1]])
glVertex3fv(self.vertices[face[1]])
glNormal3fv(self.normals[face[2]])
glVertex3fv(self.vertices[face[2]])
glNormal3fv(self.normals[face[3]])
glVertex3fv(self.vertices[face[3]])
glNormal3fv(self.normals[face[4]])
glVertex3fv(self.vertices[face[4]])
glEnd()
else:
glBegin(GL_TRIANGLES)
glNormal3fv(self.normals[face[1]])
glVertex3fv(self.vertices[face[1]])
glNormal3fv(self.normals[face[2]])
glVertex3fv(self.vertices[face[2]])
glNormal3fv(self.normals[face[3]])
glVertex3fv(self.vertices[face[3]])
glEnd()
glEndList()
def draw(self):
glPushMatrix()
glRotatef(self.r[0],1,0,0)
glRotatef(self.r[1],0,1,0)
glRotatef(self.r[2],0,0,1)
glTranslatef(self.pos[0],self.pos[1],self.pos[2])
glCallList(self.num)
glPopMatrix()
def rotate(self,x,y,z):
self.r[0]+=x
self.r[1]+=y
self.r[2]+=z

BIN
obj_loader.pyc Normal file

Binary file not shown.

2
shader.fsh Normal file
View File

@ -0,0 +1,2 @@
void main() {
}

2
shader.vsh Normal file
View File

@ -0,0 +1,2 @@
void main() {
}

12
shaders.py Normal file
View File

@ -0,0 +1,12 @@
from OpenGL.GL import shaders
from OpenGL.GL import *
class Shader:
def __init__(self,vertexShader,fragmentShader):
self.program=glCreateProgram()
self.vertexShader=shaders.compileShader(vertexShader,GL_VERTEX_SHADER)
glAttachShader(self.program,self.vertexShader)
self.fragmentShader=shaders.compileShader(fragmentShader,GL_FRAGMENT_SHADER)
glAttachShader(self.program,self.fragmentShader)
glLinkProgram(self.program)
def use(self):
glUseProgram(self.program)