making the shaders_basic_lighting.py and the shaders_fog.py file more python"y" style
This commit is contained in:
parent
1c889888fe
commit
91aa5ae4f9
2 changed files with 70 additions and 77 deletions
|
@ -30,7 +30,6 @@
|
|||
|
||||
import raylib as rl
|
||||
|
||||
|
||||
from raylib.colors import *
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
|
@ -43,12 +42,13 @@ from rlmath import *
|
|||
# lighting system
|
||||
from light_system import *
|
||||
|
||||
#// Initialization
|
||||
#//--------------------------------------------------------------------------------------
|
||||
screenWidth = 1200;
|
||||
screenHeight = 720;
|
||||
# Initialization
|
||||
# --------------------------------------------------------------------------------------
|
||||
screenWidth = 1200
|
||||
screenHeight = 720
|
||||
|
||||
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT| rl.FLAG_WINDOW_RESIZABLE); # Enable Multi Sampling Anti Aliasing 4x (if available)
|
||||
rl.SetConfigFlags(
|
||||
rl.FLAG_MSAA_4X_HINT | rl.FLAG_WINDOW_RESIZABLE); # Enable Multi Sampling Anti Aliasing 4x (if available)
|
||||
rl.InitWindow(screenWidth, screenHeight, b"raylib [shaders] example - basic lighting")
|
||||
|
||||
camera = rl.ffi.new('struct Camera3D *', [
|
||||
|
@ -59,101 +59,98 @@ camera = rl.ffi.new('struct Camera3D *', [
|
|||
rl.CAMERA_PERSPECTIVE
|
||||
])
|
||||
|
||||
#// Load models
|
||||
# Load models
|
||||
modelA = rl.LoadModelFromMesh(rl.GenMeshTorus(0.4, 1.0, 16, 32))
|
||||
modelB = rl.LoadModelFromMesh(rl.GenMeshCube(1.0, 1.0, 1.0))
|
||||
modelC = rl.LoadModelFromMesh(rl.GenMeshSphere(0.5, 32, 32))
|
||||
|
||||
#// Load models texture
|
||||
# Load models texture
|
||||
texture = rl.LoadTexture(b"resources/texel_checker.png")
|
||||
|
||||
#// Assign texture to default model material
|
||||
# Assign texture to default model material
|
||||
modelA.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
|
||||
modelB.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
|
||||
modelC.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
|
||||
|
||||
angle = 6.282;
|
||||
angle = 6.282
|
||||
|
||||
#// Using 4 point lights, white, red, green and blue
|
||||
# Using 4 point lights, white, red, green and blue
|
||||
|
||||
lights0 = Light(LIGHT_POINT, [ 4, 2, 4 ], Vector3Zero(), WHITE)
|
||||
lights1 = Light(LIGHT_POINT, [4, 2, 4 ], Vector3Zero(), RED)
|
||||
lights2 = Light(LIGHT_POINT, [ 0, 4, 2 ], Vector3Zero(), GREEN)
|
||||
lights3 = Light(LIGHT_POINT, [ 0, 4, 2 ], Vector3Zero(), BLUE)
|
||||
lights0 = Light(LIGHT_POINT, [4, 2, 4], Vector3Zero(), WHITE)
|
||||
lights1 = Light(LIGHT_POINT, [4, 2, 4], Vector3Zero(), RED)
|
||||
lights2 = Light(LIGHT_POINT, [0, 4, 2], Vector3Zero(), GREEN)
|
||||
lights3 = Light(LIGHT_POINT, [0, 4, 2], Vector3Zero(), BLUE)
|
||||
|
||||
lightSystem = LightSystem([ 0.2, 0.2, 0.2, 1.0 ], lights0, lights1, lights2, lights3)
|
||||
lightSystem = LightSystem([0.2, 0.2, 0.2, 1.0], lights0, lights1, lights2, lights3)
|
||||
fogD = rl.GetShaderLocation(lightSystem.shader, b'FogDensity')
|
||||
fogDensity = 0.0
|
||||
|
||||
#// All models use the same shader - which lights them
|
||||
# All models use the same shader - which lights them
|
||||
modelA.materials[0].shader = lightSystem.shader
|
||||
modelB.materials[0].shader = lightSystem.shader
|
||||
modelC.materials[0].shader = lightSystem.shader
|
||||
|
||||
rl.SetCameraMode(camera[0], rl.CAMERA_ORBITAL) # Set an orbital camera mode
|
||||
|
||||
rl.SetTargetFPS(60) # // Set our game to run at 60 frames-per-second
|
||||
#//--------------------------------------------------------------------------------------
|
||||
rl.SetTargetFPS(60) # // Set our game to run at 60 frames-per-second
|
||||
# --------------------------------------------------------------------------------------
|
||||
|
||||
#// Main game loop
|
||||
while not rl.WindowShouldClose(): #// Detect window close button or ESC key
|
||||
#// Update
|
||||
#//----------------------------------------------------------------------------------
|
||||
# Main game loop
|
||||
while not rl.WindowShouldClose(): # Detect window close button or ESC key
|
||||
# Update
|
||||
# ----------------------------------------------------------------------------------
|
||||
if rl.IsKeyPressed(rl.KEY_W): lights0.enabled = not lights0.enabled
|
||||
if rl.IsKeyPressed(rl.KEY_R): lights1.enabled = not lights1.enabled
|
||||
if rl.IsKeyPressed(rl.KEY_G): lights2.enabled = not lights2.enabled
|
||||
if rl.IsKeyPressed(rl.KEY_B): lights3.enabled = not lights3.enabled
|
||||
|
||||
rl.UpdateCamera(camera) #// Update camera
|
||||
rl.UpdateCamera(camera) # Update camera
|
||||
|
||||
#// Make the lights do differing orbits
|
||||
# Make the lights do differing orbits
|
||||
angle -= 0.02
|
||||
lights0.position.x = math.cos(angle)*4.0
|
||||
lights0.position.z = math.sin(angle)*4.0
|
||||
lights1.position.x = math.cos(-angle*0.6)*4.0
|
||||
lights1.position.z = math.sin(-angle*0.6)*4.0
|
||||
lights2.position.y = math.cos(angle*0.2)*4.0
|
||||
lights2.position.z = math.sin(angle*0.2)*4.0
|
||||
lights3.position.y = math.cos(-angle*0.35)*4.0
|
||||
lights3.position.z = math.sin(-angle*0.35)*4.0
|
||||
lights0.position.x = math.cos(angle) * 4.0
|
||||
lights0.position.z = math.sin(angle) * 4.0
|
||||
lights1.position.x = math.cos(-angle * 0.6) * 4.0
|
||||
lights1.position.z = math.sin(-angle * 0.6) * 4.0
|
||||
lights2.position.y = math.cos(angle * 0.2) * 4.0
|
||||
lights2.position.z = math.sin(angle * 0.2) * 4.0
|
||||
lights3.position.y = math.cos(-angle * 0.35) * 4.0
|
||||
lights3.position.z = math.sin(-angle * 0.35) * 4.0
|
||||
|
||||
#// Update the light shader with the camera view position
|
||||
# Update the light shader with the camera view position
|
||||
|
||||
lightSystem.update(camera.position)
|
||||
|
||||
|
||||
# ffi.cast('wchar_t', x)
|
||||
# modelA.transform = ffi.cast('Matrix *', MatrixRotateY(angle*1.7))[0]
|
||||
# modelA.transform = MatrixRotateY(angle*1.7)
|
||||
#// Rotate the torus
|
||||
# modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025)[0])[0]
|
||||
# modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012)[0])[0]
|
||||
# ffi.cast('wchar_t', x)
|
||||
# modelA.transform = ffi.cast('Matrix *', MatrixRotateY(angle*1.7))[0]
|
||||
# modelA.transform = MatrixRotateY(angle*1.7)
|
||||
# Rotate the torus
|
||||
# modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025)[0])[0]
|
||||
# modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012)[0])[0]
|
||||
modelA.transform = rl.ffi.cast('Matrix *', MatrixMultiply(modelA.transform, MatrixRotateX(-0.025)))[0]
|
||||
modelA.transform = rl.ffi.cast('Matrix *', MatrixMultiply(modelA.transform, MatrixRotateZ(0.012)))[0]
|
||||
|
||||
if (rl.IsKeyPressed(rl.KEY_F)):
|
||||
rl.ToggleFullscreen()
|
||||
|
||||
# ----------------------------------------------------------------------------------
|
||||
|
||||
#//----------------------------------------------------------------------------------
|
||||
|
||||
#// Draw
|
||||
#//----------------------------------------------------------------------------------
|
||||
# Draw
|
||||
# ----------------------------------------------------------------------------------
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.ClearBackground(RAYWHITE)
|
||||
|
||||
rl.BeginMode3D(camera[0])
|
||||
|
||||
#// Draw the three models
|
||||
rl.DrawModel(modelA, [0,0,0], 1.0, WHITE)
|
||||
rl.DrawModel(modelB, [-1.6,0,0], 1.0, WHITE)
|
||||
rl.DrawModel(modelC, [ 1.6,0,0], 1.0, WHITE)
|
||||
# Draw the three models
|
||||
rl.DrawModel(modelA, [0, 0, 0], 1.0, WHITE)
|
||||
rl.DrawModel(modelB, [-1.6, 0, 0], 1.0, WHITE)
|
||||
rl.DrawModel(modelC, [1.6, 0, 0], 1.0, WHITE)
|
||||
|
||||
#// Draw markers to show where the lights are
|
||||
# Draw markers to show where the lights are
|
||||
lightSystem.draw()
|
||||
|
||||
|
||||
rl.DrawGrid(10, 1.0)
|
||||
|
||||
rl.EndMode3D()
|
||||
|
@ -163,17 +160,17 @@ while not rl.WindowShouldClose(): #// Detect window close button or E
|
|||
rl.DrawText(b"Keys RGB & W toggle lights", 10, 30, 20, DARKGRAY)
|
||||
|
||||
rl.EndDrawing()
|
||||
#//----------------------------------------------------------------------------------
|
||||
# ----------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#// De-Initialization
|
||||
#//--------------------------------------------------------------------------------------
|
||||
rl.UnloadModel(modelA) # // Unload the modelA
|
||||
rl.UnloadModel(modelB) # // Unload the modelB
|
||||
rl.UnloadModel(modelC) # // Unload the modelC
|
||||
# De-Initialization
|
||||
# --------------------------------------------------------------------------------------
|
||||
rl.UnloadModel(modelA) # Unload the modelA
|
||||
rl.UnloadModel(modelB) # Unload the modelB
|
||||
rl.UnloadModel(modelC) # Unload the modelC
|
||||
|
||||
rl.UnloadTexture(texture) #// Unload the texture
|
||||
rl.UnloadTexture(texture) # Unload the texture
|
||||
|
||||
rl.UnloadShader(lightSystem.shader)
|
||||
|
||||
rl.CloseWindow() #// Close window and OpenGL context
|
||||
rl.CloseWindow() # Close window and OpenGL context
|
||||
|
|
|
@ -37,10 +37,10 @@ model.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
|
|||
model2.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
|
||||
model3.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
|
||||
|
||||
light = Light(LIGHT_POINT, [ 0, 4, 0 ], Vector3Zero(), WHITE)
|
||||
lightSystem = LightSystem([ 0.2, 0.2, 0.2, 1.0 ], light)
|
||||
light = Light(LIGHT_POINT, [0, 4, 0], Vector3Zero(), WHITE)
|
||||
lightSystem = LightSystem([0.2, 0.2, 0.2, 1.0], light)
|
||||
|
||||
fog_color = rl.ffi.new('float[]', [0.2,0.2,1.0,1.0])
|
||||
fog_color = rl.ffi.new('float[]', [0.2, 0.2, 1.0, 1.0])
|
||||
fogC = rl.GetShaderLocation(lightSystem.shader, b'fogColor')
|
||||
rl.SetShaderValue(lightSystem.shader, fogC, fog_color, rl.SHADER_UNIFORM_VEC4);
|
||||
|
||||
|
@ -55,20 +55,19 @@ model2.materials[0].shader = lightSystem.shader
|
|||
model3.materials[0].shader = lightSystem.shader
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
a=0.0
|
||||
a = 0.0
|
||||
while not rl.WindowShouldClose():
|
||||
|
||||
a+=0.01
|
||||
camera.position.x = math.sin(a)*6
|
||||
camera.position.z = math.cos(a)*6
|
||||
|
||||
a += 0.01
|
||||
camera.position.x = math.sin(a) * 6
|
||||
camera.position.z = math.cos(a) * 6
|
||||
rl.UpdateCamera(camera)
|
||||
|
||||
lightSystem.update(camera.position)
|
||||
|
||||
|
||||
model.transform = rl.ffi.cast("Matrix *",MatrixMultiply(model.transform, MatrixRotateX(-0.025)))[0]
|
||||
model.transform = rl.ffi.cast("Matrix *",MatrixMultiply(model.transform, MatrixRotateZ(0.012)))[0]
|
||||
|
||||
model.transform = rl.ffi.cast("Matrix *", MatrixMultiply(model.transform, MatrixRotateX(-0.025)))[0]
|
||||
model.transform = rl.ffi.cast("Matrix *", MatrixMultiply(model.transform, MatrixRotateZ(0.012)))[0]
|
||||
|
||||
if rl.IsKeyDown(rl.KEY_UP):
|
||||
fogDensity = min(fogDensity + 0.001, 1)
|
||||
|
||||
|
@ -84,18 +83,16 @@ while not rl.WindowShouldClose():
|
|||
if rl.IsKeyDown(rl.KEY_SPACE):
|
||||
rl.ClearBackground(BLACK)
|
||||
|
||||
|
||||
rl.BeginMode3D(camera[0])
|
||||
rl.DrawModel(model, [0] * 3, 1, WHITE)
|
||||
rl.DrawModel(model2, [-2.6, 0, 0], 1, WHITE)
|
||||
rl.DrawModel(model3, [ 2.6, 0, 0], 1, WHITE)
|
||||
rl.DrawModel(model3, [2.6, 0, 0], 1, WHITE)
|
||||
|
||||
for i in range(-20, 20, 2):
|
||||
rl.DrawModel(model, [i, 0, 2], 1, WHITE)
|
||||
|
||||
|
||||
#Raylib removed this function
|
||||
#rl.DrawGizmo([1000, 1000, 1000])
|
||||
# Raylib removed this function
|
||||
# rl.DrawGizmo([1000, 1000, 1000])
|
||||
|
||||
rl.EndMode3D()
|
||||
|
||||
|
@ -110,4 +107,3 @@ rl.UnloadModel(model3)
|
|||
rl.UnloadTexture(texture)
|
||||
rl.UnloadShader(lightSystem.shader)
|
||||
rl.CloseWindow()
|
||||
|
||||
|
|
Reference in a new issue