make lighting example more OO
This commit is contained in:
parent
c1bf0bdbd8
commit
968d095b0c
7 changed files with 32734 additions and 71 deletions
22251
examples/shaders/resources/models/barracks.obj
Normal file
22251
examples/shaders/resources/models/barracks.obj
Normal file
File diff suppressed because it is too large
Load diff
BIN
examples/shaders/resources/models/barracks_diffuse.png
Normal file
BIN
examples/shaders/resources/models/barracks_diffuse.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 363 KiB |
5116
examples/shaders/resources/models/church.obj
Normal file
5116
examples/shaders/resources/models/church.obj
Normal file
File diff suppressed because it is too large
Load diff
BIN
examples/shaders/resources/models/church_diffuse.png
Normal file
BIN
examples/shaders/resources/models/church_diffuse.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 350 KiB |
5316
examples/shaders/resources/models/watermill.obj
Normal file
5316
examples/shaders/resources/models/watermill.obj
Normal file
File diff suppressed because it is too large
Load diff
BIN
examples/shaders/resources/models/watermill_diffuse.png
Normal file
BIN
examples/shaders/resources/models/watermill_diffuse.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 437 KiB |
|
@ -31,8 +31,7 @@ from enum import Enum
|
||||||
from typing import Any
|
from typing import Any
|
||||||
import math
|
import math
|
||||||
|
|
||||||
MAX_LIGHTS = 4 #// Max dynamic lights supported by shader
|
|
||||||
lightsCount = 0
|
|
||||||
|
|
||||||
|
|
||||||
def MatrixRotateX(angle):
|
def MatrixRotateX(angle):
|
||||||
|
@ -110,24 +109,8 @@ def MatrixMultiply(left, right):
|
||||||
#// Types and Structures Definition
|
#// Types and Structures Definition
|
||||||
#//----------------------------------------------------------------------------------
|
#//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
#// Light data
|
MAX_LIGHTS = 4 #// Max dynamic lights supported by shader
|
||||||
@dataclass
|
lightsCount = 0
|
||||||
class Light:
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
type: Any
|
|
||||||
position: Any
|
|
||||||
target: Any
|
|
||||||
color: Any
|
|
||||||
enabled: Any
|
|
||||||
|
|
||||||
#// Shader locations
|
|
||||||
enabledLoc: Any
|
|
||||||
typeLoc: Any
|
|
||||||
posLoc: Any
|
|
||||||
targetLoc: Any
|
|
||||||
colorLoc: Any
|
|
||||||
|
|
||||||
|
|
||||||
#// Light type
|
#// Light type
|
||||||
|
|
||||||
|
@ -135,65 +118,60 @@ LIGHT_DIRECTIONAL=0
|
||||||
LIGHT_POINT=1
|
LIGHT_POINT=1
|
||||||
|
|
||||||
|
|
||||||
#// Create a light and get shader locations
|
class Light:
|
||||||
def CreateLight(type, position, target, color, shader):
|
def __init__(self, type, position, target, color, shader):
|
||||||
global lightsCount
|
global lightsCount
|
||||||
light = Light()
|
if lightsCount >= MAX_LIGHTS:
|
||||||
|
raise Exception("Too many lights")
|
||||||
|
self.enabled = True
|
||||||
if lightsCount < MAX_LIGHTS:
|
self.type = type
|
||||||
light.enabled = True
|
self.position = position
|
||||||
light.type = type
|
self.target = target
|
||||||
light.position = position
|
self.color = color
|
||||||
light.target = target
|
self.shader = shader
|
||||||
light.color = color
|
|
||||||
|
|
||||||
#// TODO: Below code doesn't look good to me,
|
#// TODO: Below code doesn't look good to me,
|
||||||
# // it assumes a specific shader naming and structure
|
# // it assumes a specific shader naming and structure
|
||||||
# // Probably this implementation could be improved
|
# // Probably this implementation could be improved
|
||||||
enabledName = f"lights[{lightsCount}].enabled"
|
self.enabledName = f"lights[{lightsCount}].enabled"
|
||||||
typeName = f"lights[{lightsCount}].type"
|
self.typeName = f"lights[{lightsCount}].type"
|
||||||
posName = f"lights[{lightsCount}].position"
|
self.posName = f"lights[{lightsCount}].position"
|
||||||
targetName = f"lights[{lightsCount}].target"
|
self.targetName = f"lights[{lightsCount}].target"
|
||||||
colorName = f"lights[{lightsCount}].color"
|
self.colorName = f"lights[{lightsCount}].color"
|
||||||
# enabledName = '0' + str(lightsCount)
|
# enabledName = '0' + str(lightsCount)
|
||||||
# typeName = '0' + str(lightsCount)
|
# typeName = '0' + str(lightsCount)
|
||||||
# posName = '0' + str(lightsCount)
|
# posName = '0' + str(lightsCount)
|
||||||
# targetName = '0' + str(lightsCount)
|
# targetName = '0' + str(lightsCount)
|
||||||
# colorName = '0' + str(lightsCount)
|
# colorName = '0' + str(lightsCount)
|
||||||
|
|
||||||
light.enabledLoc = GetShaderLocation(shader, enabledName.encode('utf-8'))
|
self.enabledLoc = GetShaderLocation(shader, self.enabledName.encode('utf-8'))
|
||||||
light.typeLoc = GetShaderLocation(shader, typeName.encode('utf-8'))
|
self.typeLoc = GetShaderLocation(shader, self.typeName.encode('utf-8'))
|
||||||
light.posLoc = GetShaderLocation(shader, posName.encode('utf-8'))
|
self.posLoc = GetShaderLocation(shader, self.posName.encode('utf-8'))
|
||||||
light.targetLoc = GetShaderLocation(shader, targetName.encode('utf-8'))
|
self.targetLoc = GetShaderLocation(shader, self.targetName.encode('utf-8'))
|
||||||
light.colorLoc = GetShaderLocation(shader, colorName.encode('utf-8'))
|
self.colorLoc = GetShaderLocation(shader, self.colorName.encode('utf-8'))
|
||||||
|
|
||||||
UpdateLightValues(shader, light)
|
self.UpdateLightValues()
|
||||||
|
|
||||||
lightsCount+=1
|
|
||||||
|
|
||||||
|
|
||||||
return light
|
|
||||||
|
|
||||||
|
lightsCount += 1
|
||||||
|
|
||||||
#// Send light properties to shader
|
#// Send light properties to shader
|
||||||
# // NOTE: Light shader locations should be available
|
# // NOTE: Light shader locations should be available
|
||||||
def UpdateLightValues(shader, light):
|
def UpdateLightValues(self):
|
||||||
#// Send to shader light enabled state and type
|
#// Send to shader light enabled state and type
|
||||||
SetShaderValue(shader, light.enabledLoc, ffi.new("int *",light.enabled), UNIFORM_INT)
|
SetShaderValue(shader, self.enabledLoc, ffi.new("int *",self.enabled), UNIFORM_INT)
|
||||||
SetShaderValue(shader, light.typeLoc, ffi.new("int *",light.type), UNIFORM_INT)
|
SetShaderValue(shader, self.typeLoc, ffi.new("int *",self.type), UNIFORM_INT)
|
||||||
|
|
||||||
#// Send to shader light position values
|
#// Send to shader light position values
|
||||||
position = [ light.position.x, light.position.y, light.position.z]
|
position = [ self.position.x, self.position.y, self.position.z]
|
||||||
SetShaderValue(shader, light.posLoc, ffi.new("struct Vector3 *",position), UNIFORM_VEC3)
|
SetShaderValue(shader, self.posLoc, ffi.new("struct Vector3 *",position), UNIFORM_VEC3)
|
||||||
|
|
||||||
#// Send to shader light target position values
|
#// Send to shader light target position values
|
||||||
target =[ light.target.x, light.target.y, light.target.z ]
|
target =[ self.target.x, self.target.y, self.target.z ]
|
||||||
SetShaderValue(shader, light.targetLoc, ffi.new("struct Vector3 *",target), UNIFORM_VEC3)
|
SetShaderValue(shader, self.targetLoc, ffi.new("struct Vector3 *",target), UNIFORM_VEC3)
|
||||||
|
|
||||||
#// Send to shader light color values
|
#// Send to shader light color values
|
||||||
color = [light.color[0]/255.0, light.color[1]/255.0, light.color[2]/255.0, light.color[3]/255.0]
|
color = [self.color[0]/255.0, self.color[1]/255.0, self.color[2]/255.0, self.color[3]/255.0]
|
||||||
SetShaderValue(shader, light.colorLoc, ffi.new("struct Vector4 *",color), UNIFORM_VEC4)
|
SetShaderValue(shader, self.colorLoc, ffi.new("struct Vector4 *",color), UNIFORM_VEC4)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -253,10 +231,11 @@ modelC.materials[0].shader = shader
|
||||||
|
|
||||||
#// Using 4 point lights, white, red, green and blue
|
#// Using 4 point lights, white, red, green and blue
|
||||||
lights = [0] * 4
|
lights = [0] * 4
|
||||||
lights[0] = CreateLight(LIGHT_POINT, ffi.new("struct Vector3 *",[ 4, 2, 4 ]), Vector3Zero(), WHITE, shader)
|
#lights[0] = Light(LIGHT_POINT, ffi.new("struct Vector3 *",[ 400, 400, 400 ]), Vector3Zero(), WHITE, shader)
|
||||||
lights[1] = CreateLight(LIGHT_POINT, ffi.new("struct Vector3 *",[4, 2, 4 ]), Vector3Zero(), RED, shader)
|
lights[0] = Light(LIGHT_POINT, ffi.new("struct Vector3 *",[ 4, 2, 4 ]), Vector3Zero(), WHITE, shader)
|
||||||
lights[2] = CreateLight(LIGHT_POINT, ffi.new("struct Vector3 *",[ 0, 4, 2 ]), Vector3Zero(), GREEN, shader)
|
lights[1] = Light(LIGHT_POINT, ffi.new("struct Vector3 *",[4, 2, 4 ]), Vector3Zero(), RED, shader)
|
||||||
lights[3] = CreateLight(LIGHT_POINT, ffi.new("struct Vector3 *",[ 0, 4, 2 ]), Vector3Zero(), BLUE, shader)
|
lights[2] = Light(LIGHT_POINT, ffi.new("struct Vector3 *",[ 0, 4, 2 ]), Vector3Zero(), GREEN, shader)
|
||||||
|
lights[3] = Light(LIGHT_POINT, ffi.new("struct Vector3 *",[ 0, 4, 2 ]), Vector3Zero(), BLUE, shader)
|
||||||
|
|
||||||
SetCameraMode(camera, CAMERA_ORBITAL) #// Set an orbital camera mode
|
SetCameraMode(camera, CAMERA_ORBITAL) #// Set an orbital camera mode
|
||||||
|
|
||||||
|
@ -285,10 +264,11 @@ while not WindowShouldClose(): #// Detect window close button or ESC
|
||||||
lights[3].position.y = math.cos(-angle*0.35)*4.0
|
lights[3].position.y = math.cos(-angle*0.35)*4.0
|
||||||
lights[3].position.z = math.sin(-angle*0.35)*4.0
|
lights[3].position.z = math.sin(-angle*0.35)*4.0
|
||||||
|
|
||||||
UpdateLightValues(shader, lights[0])
|
lights[0].UpdateLightValues()
|
||||||
UpdateLightValues(shader, lights[1])
|
lights[1].UpdateLightValues()
|
||||||
UpdateLightValues(shader, lights[2])
|
lights[2].UpdateLightValues()
|
||||||
UpdateLightValues(shader, lights[3])
|
lights[3].UpdateLightValues()
|
||||||
|
|
||||||
|
|
||||||
#// Rotate the torus
|
#// Rotate the torus
|
||||||
|
|
||||||
|
|
Reference in a new issue