Created
April 22, 2018 04:53
-
-
Save jerstlouis/71bd05fb17d4d7158f1d7c361fa21e1a to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from pyecere import * | |
class CubeApp(GuiApplication): | |
def __init__(self, appGlobals): | |
GuiApplication.__init__(self, appGlobals = appGlobals) | |
self.driver = "OpenGL" | |
self.timerResolution = 60 | |
def cube_cycle(self, idle): | |
test3D.updateCube() | |
return True | |
self.cycle = cube_cycle | |
app = CubeApp(appGlobals=globals()) | |
camera = Camera( | |
CameraType.fixed, | |
position = ( 0, 0, -200 ), | |
orientation = Euler ( 0, 0, 0 ), | |
fov = 53) | |
light = Light( | |
#diffuse = white, | |
specular = DefinedColor.white, | |
orientation = Euler ( pitch = 10, yaw = 30 ) ) | |
light2 = Light( | |
diffuse = DefinedColor.white, | |
#specular = DefinedColor.white, | |
orientation = Euler ( pitch = 20, yaw = -30 ) ) | |
class Test3D(Window): | |
def __init__(self): | |
Window.__init__(self, | |
displayDriver = "OpenGL", | |
caption = "Cube", | |
borderStyle = BorderStyle.none, | |
moveable = True, | |
stayOnTop = True, | |
clientSize = ( 400, 400 ), | |
background = 0, | |
opacity = 0, | |
alphaBlend = True) | |
self.lastTime = 0 | |
# self.texture = BitmapResource ( "fractal3.jpg", window = this ) | |
self.texture = BitmapResource ( "knot.png", window = self ) | |
self.material = Material ( diffuse = DefinedColor.white, ambient = DefinedColor.blue, power = 8, opacity = 1.0, flags = MaterialFlags ( translucent = True, doubleSided = True ) ) # specular = red, | |
self.cube = Cube() | |
self.spin = Euler() | |
self.moving = False | |
def test3D_onLoadGraphics(self): | |
self.display.ambient = ColorRGB (0.7, 0.7, 0.7) | |
self.material.baseMap = self.texture.bitmap | |
self.cube.create(self.displaySystem) | |
self.cube.mesh.applyMaterial(self.material) | |
self.cube.mesh.applyTranslucency(self.cube) | |
self.cube.transform = Transform(scaling = ( 100, 100, 100 ), orientation = Euler ( pitch = 20, yaw = -30 ) ) | |
self.cube.updateTransform() | |
return True | |
self.onLoadGraphics = test3D_onLoadGraphics | |
def test3D_onResize(self, w, h): | |
camera.setup(w, h) | |
camera.update() | |
self.onResize = test3D_onResize | |
def test3D_onRedraw(self, surface): | |
surface.clear(ClearType.colorAndDepth) | |
camera.update() | |
self.display.setLight(0, light) | |
self.display.setLight(1, light2) | |
self.display.setCamera(surface, camera) | |
self.display.fogDensity = 0 | |
self.display.drawObject(self.cube) | |
self.display.setCamera(surface, None) | |
self.onRedraw = test3D_onRedraw | |
def test3D_onLeftButtonDown(self, x, y, mods): | |
self.clickTime = getTime() | |
self.capture(); | |
self.startClick = Point ( x, y ) | |
self.moving = True | |
return True; | |
self.onLeftButtonDown = test3D_onLeftButtonDown | |
def test3D_onLeftButtonUp(self, x, y, mods): | |
if self.moving: | |
self.releaseCapture() | |
self.moving = False | |
return True | |
self.onLeftButtonUp = test3D_onLeftButtonUp | |
def test3D_onMouseMove(self, x, y, mods): | |
if self.moving: | |
time = getTime() | |
diffTime = max(time - self.clickTime, 0.01) | |
self.spin.yaw += Degrees ( (x - self.startClick.x) / (25.0 * diffTime) ) | |
self.spin.pitch += Degrees ( (self.startClick.y - y) / (25.0 * diffTime) ) | |
self.startClick = Point ( x, y ) | |
self.clickTime = time | |
return True | |
self.onMouseMove = test3D_onMouseMove | |
def test3D_onKeyHit(self, key, ch): | |
if key == KeyCode.wheelDown or key == KeyCode.wheelUp: | |
w = self.size.w | |
if key == KeyCode.wheelDown: | |
w /= 1.1 | |
elif key == KeyCode.wheelUp: | |
w *= 1.1 | |
self.move(self.position.x + self.size.w / 2 - w / 2, self.position.y + self.size.h / 2 - w / 2, w, w) | |
self.update() | |
return True | |
self.onKeyHit = test3D_onKeyHit | |
def test3D_onKeyDown(self, key, ch): | |
if key == KeyCode.escape: self.destroy(0) | |
return True | |
self.onKeyDown = test3D_onKeyDown | |
def updateCube(self): | |
time = getTime() | |
diffTime = time - self.lastTime if self.lastTime else 0 | |
if self.spin.yaw != 0 or self.spin.pitch != 0: | |
signYaw = 1 | |
signPitch = 1 | |
yaw = self.spin.yaw; pitch = self.spin.pitch | |
t = self.cube.transform | |
orientation = t.orientation | |
tSpin = Euler ( yaw * diffTime, pitch * diffTime, 0 ) | |
thisSpin = Quaternion(tSpin) | |
if yaw < 0: | |
yaw = -yaw | |
signYaw = -1 | |
if pitch < 0: | |
pitch = -pitch | |
signPitch = -1 | |
yaw -= diffTime / 3 * yaw | |
pitch -= diffTime / 3 * pitch | |
if yaw < 0.0001: yaw = 0 | |
if pitch < 0.0001: pitch = 0 | |
self.spin.yaw = yaw * signYaw | |
self.spin.pitch = pitch * signPitch | |
temp = Quaternion() | |
temp.multiply(orientation, thisSpin) | |
orientation.normalize(temp) | |
t.orientation = orientation | |
self.cube.transform = t | |
self.cube.updateTransform() | |
self.update() | |
self.lastTime = time | |
return True | |
test3D = Test3D() | |
app.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment