| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 23 |
#~ Imports |
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
|
|---|
| 26 |
from RBRapier.Renderer.AttributeMgr import AttributeChangeElement |
|---|
| 27 |
from RBRapier.Tools import Vector |
|---|
| 28 |
from OpenGL import GL |
|---|
| 29 |
|
|---|
| 30 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 31 |
#~ Definitions |
|---|
| 32 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 33 |
|
|---|
| 34 |
class LightingModel(object): |
|---|
| 35 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 36 |
#~ Constants / Variables / Etc. |
|---|
| 37 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 38 |
|
|---|
| 39 |
AttributeChange = AttributeChangeElement(GL.GL_LIGHTING_BIT) |
|---|
| 40 |
|
|---|
| 41 |
LocalViewer = 0 |
|---|
| 42 |
TwoSided = 0 |
|---|
| 43 |
ShadeModel = GL.GL_SMOOTH |
|---|
| 44 |
AmbientColor = Vector.ColorVectorProperty('Ambient', (.2, .2, .2, 1.)) |
|---|
| 45 |
|
|---|
| 46 |
# The following is not supported at the moment by PyOpenGL -- no constants! |
|---|
| 47 |
#SeperateSpecular = GL.GL_SINGLE_COLOR # GL.GL_SEPARATE_SPECULAR_COLOR |
|---|
| 48 |
|
|---|
| 49 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 50 |
#~ Public Methods |
|---|
| 51 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 52 |
|
|---|
| 53 |
def __init__(self, **kw): |
|---|
| 54 |
for name,value in kw.iteritems(): |
|---|
| 55 |
setattr(self, name, value) |
|---|
| 56 |
|
|---|
| 57 |
def GLSelect(self, context): |
|---|
| 58 |
context.StateMgr.Enable(GL.GL_LIGHTING) |
|---|
| 59 |
GL.glShadeModel(self.ShadeModel) |
|---|
| 60 |
GL.glLightModel(GL.GL_LIGHT_MODEL_AMBIENT, self.AmbientColor) |
|---|
| 61 |
GL.glLightModel(GL.GL_LIGHT_MODEL_TWO_SIDE, self.TwoSided) |
|---|
| 62 |
GL.glLightModel(GL.GL_LIGHT_MODEL_LOCAL_VIEWER, self.LocalViewer) |
|---|
| 63 |
|
|---|
| 64 |
# The following is not supported at the moment by PyOpenGL -- no constants! |
|---|
| 65 |
#GL.glLightModel(GL.GL_LIGHT_MODEL_COLOR_CONTROL, self.SeperateSpecular) |
|---|
| 66 |
|
|---|
| 67 |
def GLDeselect(self, context): |
|---|
| 68 |
context.StateMgr.Disable(GL.GL_LIGHTING) |
|---|
| 69 |
|
|---|
| 70 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 71 |
|
|---|
| 72 |
class Light(object): |
|---|
| 73 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 74 |
#~ Constants / Variables / Etc. |
|---|
| 75 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 76 |
|
|---|
| 77 |
AttributeChange = AttributeChangeElement(GL.GL_LIGHTING_BIT) |
|---|
| 78 |
|
|---|
| 79 |
LightNumber = None # 0 |
|---|
| 80 |
|
|---|
| 81 |
Ambient = Vector.ColorVectorProperty('Ambient', (0., 0., 0., 1.)) |
|---|
| 82 |
Diffuse = Vector.ColorVectorProperty('Diffuse', (1., 1., 1., 1.)) |
|---|
| 83 |
Specular = Vector.ColorVectorProperty('Specular', (1., 1., 1., 1.)) |
|---|
| 84 |
|
|---|
| 85 |
Position = Vector.Vector4Property('Position', (0., 0., 1., 0.)) |
|---|
| 86 |
|
|---|
| 87 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 88 |
#~ Public Methods |
|---|
| 89 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 90 |
|
|---|
| 91 |
def __init__(self, LightNumber=None, **kw): |
|---|
| 92 |
self.LightNumber = LightNumber |
|---|
| 93 |
for name,value in kw.iteritems(): |
|---|
| 94 |
setattr(self, name, value) |
|---|
| 95 |
|
|---|
| 96 |
def GLSelect(self, context): |
|---|
| 97 |
LightId = GL.GL_LIGHT0 + self.LightNumber |
|---|
| 98 |
context.StateMgr.Enable(LightId) |
|---|
| 99 |
GL.glLightfv(LightId, GL.GL_AMBIENT, self.Ambient) |
|---|
| 100 |
GL.glLightfv(LightId, GL.GL_DIFFUSE, self.Diffuse) |
|---|
| 101 |
GL.glLightfv(LightId, GL.GL_SPECULAR, self.Specular) |
|---|
| 102 |
GL.glLightfv(LightId, GL.GL_POSITION, self.Position) |
|---|
| 103 |
|
|---|
| 104 |
def GLDeselect(self, context): |
|---|
| 105 |
LightId = GL.GL_LIGHT0 + self.LightNumber |
|---|
| 106 |
context.StateMgr.Disable(LightId) |
|---|
| 107 |
|
|---|
| 108 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 109 |
|
|---|
| 110 |
class AttenuatedLightMixin(object): |
|---|
| 111 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 112 |
#~ Constants / Variables / Etc. |
|---|
| 113 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 114 |
|
|---|
| 115 |
Attenuation = Vector.Vector3Property('Attenuation', (1., 0., 0.)) |
|---|
| 116 |
|
|---|
| 117 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 118 |
#~ Public Methods |
|---|
| 119 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 120 |
|
|---|
| 121 |
def GLSelect(self, context): |
|---|
| 122 |
self.__super.GLSelect(context) |
|---|
| 123 |
const,linear,quadratic = self.Attenuation |
|---|
| 124 |
GL.glLightf(LightId, GL.GL_CONSTANT_ATTENUATION, const) |
|---|
| 125 |
GL.glLightf(LightId, GL.GL_LINEAR_ATTENUATION, linear) |
|---|
| 126 |
GL.glLightf(LightId, GL.GL_QUADRATIC_ATTENUATION, quadratic) |
|---|
| 127 |
|
|---|
| 128 |
AttenuatedLightMixin._AttenuatedLightMixin__super = super(AttenuatedLightMixin) |
|---|
| 129 |
|
|---|
| 130 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 131 |
|
|---|
| 132 |
class SpotLightMixin(object): |
|---|
| 133 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 134 |
#~ Constants / Variables / Etc. |
|---|
| 135 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 136 |
|
|---|
| 137 |
SpotDirection = Vector.UnitVector3Property('SpotDirection', (0., 0., -1.)) |
|---|
| 138 |
SpotExponent = 0.0 |
|---|
| 139 |
SpotCutoff = 180.0 |
|---|
| 140 |
|
|---|
| 141 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 142 |
#~ Public Methods |
|---|
| 143 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 144 |
|
|---|
| 145 |
def GLSelect(self, context): |
|---|
| 146 |
self.__super.GLSelect(context) |
|---|
| 147 |
LightId = GL.GL_LIGHT0 + self.LightNumber |
|---|
| 148 |
GL.glLightfv(LightId, GL.GL_SPOT_DIRECTION, self.SpotDirection) |
|---|
| 149 |
GL.glLightf(LightId, GL.GL_SPOT_EXPONENT, self.SpotExponent) |
|---|
| 150 |
GL.glLightf(LightId, GL.GL_SPOT_CUTOFF, self.SpotCutoff) |
|---|
| 151 |
|
|---|
| 152 |
SpotLightMixin._SpotLightMixin__super = super(SpotLightMixin) |
|---|
| 153 |
|
|---|
| 154 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 155 |
|
|---|
| 156 |
class AttenuatedLight(AttenuatedLightMixin, Light): pass |
|---|
| 157 |
class SpotLight(SpotLightMixin, Light): pass |
|---|
| 158 |
class AttenuatedSpotLight(SpotLightMixin, AttenuatedLightMixin, Light): pass |
|---|
| 159 |
|
|---|