completed fog example (#18)

* complete fog example
as both fog and basic lighting need the light system it made sense
to seperate it out, there are a few functions from raymath
int rlmath.py

* added shaders custom uniform

* added shaders_texture_waves.py

* added shaders_texture_drawing.py

* bug fix - unwanted transparent effect!

Co-authored-by: codifies <nospam@antispam.com>
This commit is contained in:
chriscamacho 2020-09-19 09:07:39 +01:00 committed by GitHub
parent 6a3be55fe2
commit e49e2b4d65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 617 additions and 328 deletions

View file

@ -1,94 +0,0 @@
"""
Example converted to Python from:
http://bedroomcoders.co.uk/raylib-fog/
"""
from raylib.dynamic import raylib as rl, ffi
from raylib.colors import *
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT | rl.FLAG_WINDOW_RESIZABLE)
rl.InitWindow(1280, 768, b'Fog Test')
camera = ffi.new('struct Camera3D *', [
[2, 2, 6],
[0, 5, 0],
[0, 1, 0],
45,
rl.CAMERA_PERSPECTIVE
])
model = rl.LoadModelFromMesh(rl.GenMeshTorus(0.4, 1, 16, 32))
model2 = rl.LoadModelFromMesh(rl.GenMeshCube(1, 1, 1))
model3 = rl.LoadModelFromMesh(rl.GenMeshSphere(0.5, 32, 32))
texture = rl.LoadTexture(b'resources/test.png')
model.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
model2.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
model3.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
shader = rl.LoadShader(b'resources/shaders/fogLight.vs', b'resources/shaders/fogLight.fs')
shader.locs[rl.LOC_MATRIX_MODEL] = rl.GetShaderLocation(shader, b'matModel')
shader.locs[rl.LOC_VECTOR_VIEW] = rl.GetShaderLocation(shader, b'viewPos')
amb = rl.GetShaderLocation(shader, b'ambient')
rl.SetShaderValue(shader, amb, ffi.new('float[]', [0.2, 0.2, 0.2, 1.0]), rl.UNIFORM_VEC4)
fog_color = [0.2, 0.2, 1.0, 1.0]
fogC = rl.GetShaderLocation(shader, b'fogColor')
rl.SetShaderValue(shader, fogC, ffi.new('float[]', fog_color), rl.UNIFORM_VEC4)
fogD = rl.GetShaderLocation(shader, b'FogDensity')
fogDensity = 0.12
rl.SetShaderValue(shader, fogD, ffi.new('float[]', [fogDensity]), rl.UNIFORM_FLOAT)
model.materials[0].shader = shader
model2.materials[0].shader = shader
model3.materials[0].shader = shader
rl.SetCameraMode(camera[0], rl.CAMERA_ORBITAL)
rl.SetTargetFPS(60)
while not rl.WindowShouldClose():
rl.UpdateCamera(camera)
if rl.IsKeyDown(rl.KEY_UP):
fogDensity = min(fogDensity + 0.001, 1)
if rl.IsKeyDown(rl.KEY_DOWN):
fogDensity = max(fogDensity - 0.001, 0)
rl.SetShaderValue(shader, fogD, ffi.new('float[]', [fogDensity]), rl.UNIFORM_FLOAT)
rl.SetShaderValue(shader, shader.locs[rl.LOC_VECTOR_VIEW], ffi.new('float[]', [camera.position.x]), rl.UNIFORM_VEC3)
rl.BeginDrawing()
rl.ClearBackground([int(255 * i) for i in fog_color])
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)
for i in range(-20, 20, 2):
rl.DrawModel(model, [i, 0, 2], 1, WHITE)
rl.DrawGizmo([1000, 1000, 1000])
rl.EndMode3D()
rl.DrawFPS(10, 10)
rl.DrawText(f'Up/Down to change fog density: {fogDensity}'.encode('utf-8'), 10, 30, 20, WHITE)
rl.EndDrawing()
rl.CloseWindow()
rl.UnloadModel(model)
rl.UnloadModel(model2)
rl.UnloadModel(model3)
rl.UnloadTexture(texture)
rl.UnloadShader(shader)

View file

@ -0,0 +1,99 @@
from raylib.dynamic import raylib as rl, ffi
class LightSystem:
MAX_LIGHTS = 4 #// Max dynamic lights supported by shader
lightsCount = 0
lights = []
def __init__(self, ambient = [ 0.2, 0.2, 0.2, 1.0 ], *ls):
self.shader = rl.LoadShader(b"resources/shaders/fogLight.vs",
b"resources/shaders/fogLight.fs");
#// Get some shader loactions
self.shader.locs[rl.LOC_MATRIX_MODEL] = rl.GetShaderLocation(self.shader, b"matModel");
self.shader.locs[rl.LOC_VECTOR_VIEW] = rl.GetShaderLocation(self.shader, b"viewPos");
#// ambient light level
self.ambientLoc = rl.GetShaderLocation(self.shader, b"ambient");
v = ffi.new("struct Vector4 *", ambient)
rl.SetShaderValue(self.shader, self.ambientLoc, v, rl.UNIFORM_VEC4);
for light in ls:
self.add(light)
def add(self, light):
light.configure(len(self.lights), self.shader)
self.lights.append(light)
if len(self.lights) > self.MAX_LIGHTS:
raise Exception("Too many lights")
def update(self, cameraPos):
rl.SetShaderValue(self.shader, self.shader.locs[rl.LOC_VECTOR_VIEW], ffi.new("struct Vector3 *",cameraPos), rl.UNIFORM_VEC3)
for light in self.lights:
light.UpdateLightValues()
def draw(self):
for light in self.lights:
if light.enabled:
rl.DrawSphereEx(light.position[0], 0.2, 8, 8, light.color)
LIGHT_DIRECTIONAL=0
LIGHT_POINT=1
class Light:
def __init__(self, type, position, target, color):
self.enabled = True
self.type = type
self.position = ffi.new("struct Vector3 *",position)
self.target = target
self.color = color
def configure(self, id, shader):
self.shader = shader
#// TODO: Below code doesn't look good to me,
#// it assumes a specific shader naming and structure
#// Probably this implementation could be improved
self.enabledName = f"lights[{id}].enabled"
self.typeName = f"lights[{id}].type"
self.posName = f"lights[{id}].position"
self.targetName = f"lights[{id}].target"
self.colorName = f"lights[{id}].color"
self.enabledLoc = rl.GetShaderLocation(shader, self.enabledName.encode('utf-8'))
self.typeLoc = rl.GetShaderLocation(shader, self.typeName.encode('utf-8'))
self.posLoc = rl.GetShaderLocation(shader, self.posName.encode('utf-8'))
self.targetLoc = rl.GetShaderLocation(shader, self.targetName.encode('utf-8'))
self.colorLoc = rl.GetShaderLocation(shader, self.colorName.encode('utf-8'))
self.UpdateLightValues()
#// Send light properties to shader
#// NOTE: Light shader locations should be available
def UpdateLightValues(self):
#// Send to shader light enabled state and type
rl.SetShaderValue(self.shader, self.enabledLoc, ffi.new("int *",self.enabled), rl.UNIFORM_INT)
rl.SetShaderValue(self.shader, self.typeLoc, ffi.new("int *",self.type), rl.UNIFORM_INT)
#// Send to shader light position values
position = [ self.position.x, self.position.y, self.position.z]
rl.SetShaderValue(self.shader, self.posLoc, ffi.new("struct Vector3 *",position), rl.UNIFORM_VEC3)
#// Send to shader light target position values
target =[ self.target.x, self.target.y, self.target.z ]
rl.SetShaderValue(self.shader, self.targetLoc, ffi.new("struct Vector3 *",target), rl.UNIFORM_VEC3)
#// Send to shader light color values
color = [self.color[0]/255.0, self.color[1]/255.0, self.color[2]/255.0, self.color[3]/255.0]
rl.SetShaderValue(self.shader, self.colorLoc, ffi.new("struct Vector4 *",color), rl.UNIFORM_VEC4)

View file

@ -78,6 +78,7 @@ void main()
// gamma
finalColor = pow(finalColor, vec4(1.0/2.2));
float dist = length(viewPos - fragPosition) ;
float fogFactor = 1.0 / exp( (dist * FogDensity) * (dist * FogDensity));
@ -88,4 +89,5 @@ void main()
fogFactor = clamp( fogFactor, 0.0, 1.0 );
finalColor = mix(fogColor, finalColor, fogFactor);
}

View file

@ -0,0 +1,93 @@
# just a few functions from raymath
from raylib.dynamic import raylib as rl, ffi
import math
PI = 3.14159265358979323846
DEG2RAD = (PI/180.0)
RAD2DEG = (180.0/PI)
def Clamp(value: float, minv: float, maxv: float):
#res = value < minv ? minv : value
res = minv if value < minv else value
#return res > maxv ? maxv : res
return maxv if res > maxv else res
def Lerp(start: float, end: float, amount: float):
return start + amount*(end - start)
def Vector2Zero():
return ffi.new("struct Vector2 *",[ 0, 0])
def Vector3Zero():
return ffi.new("struct Vector3 *",[ 0, 0, 0])
def MatrixRotateX(angle):
result = MatrixIdentity();
cosres = math.cos(angle);
sinres = math.sin(angle);
result.m5 = cosres;
result.m6 = -sinres;
result.m9 = sinres;
result.m10 = cosres;
return result;
def MatrixRotateY(angle):
result = MatrixIdentity()
cosres = math.cos(angle);
sinres = math.sin(angle);
result.m0 = cosres;
result.m2 = sinres;
result.m8 = -sinres;
result.m10 = cosres;
return result;
def MatrixIdentity():
result = ffi.new("struct Matrix *",[ 1.0, 0.0, 0.0, 0.0,0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ])
return result
def MatrixRotateZ(angle):
result = MatrixIdentity();
cosres = math.cos(angle);
sinres = math.sin(angle);
result.m0 = cosres;
result.m1 = -sinres;
result.m4 = sinres;
result.m5 = cosres;
return result
def MatrixMultiply(left, right):
result = ffi.new("struct Matrix *")
result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
return result

285
examples/shaders/shaders_basic_lighting.py Normal file → Executable file
View file

@ -1,3 +1,4 @@
#!/usr/bin/env python3
# /*******************************************************************************************
# *
# * raylib [shaders] example - basic lighting
@ -23,259 +24,82 @@
# *
# * Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
# *
# *
# ********************************************************************************************/
from raylib.static import *
from raylib.static import rl, ffi
from raylib.colors import *
from dataclasses import dataclass
from enum import Enum
from typing import Any
import math
# a few functions ported from raymath
from rlmath import *
def MatrixRotateX(angle):
result = MatrixIdentity();
cosres = math.cos(angle);
sinres = math.sin(angle);
result.m5 = cosres;
result.m6 = -sinres;
result.m9 = sinres;
result.m10 = cosres;
return result;
def MatrixRotateY(angle):
result = MatrixIdentity()
cosres = math.cos(angle);
sinres = math.sin(angle);
result.m0 = cosres;
result.m2 = sinres;
result.m8 = -sinres;
result.m10 = cosres;
return result;
def MatrixIdentity():
result = ffi.new("struct Matrix *",[ 1.0, 0.0, 0.0, 0.0,0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ])
return result
def MatrixRotateZ(angle):
result = MatrixIdentity();
cosres = math.cos(angle);
sinres = math.sin(angle);
result.m0 = cosres;
result.m1 = -sinres;
result.m4 = sinres;
result.m5 = cosres;
return result
def MatrixMultiply(left, right):
result = ffi.new("struct Matrix *")
result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
return result
#//----------------------------------------------------------------------------------
#// Types and Structures Definition
#//----------------------------------------------------------------------------------
class LightSystem:
MAX_LIGHTS = 4 #// Max dynamic lights supported by shader
lightsCount = 0
lights = []
def __init__(self, ambient = [ 0.2, 0.2, 0.2, 1.0 ], *ls):
self.shader = LoadShader(b"resources/shaders/glsl330/basic_lighting.vs",
b"resources/shaders/glsl330/basic_lighting.fs");
#// Get some shader loactions
self.shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(self.shader, b"matModel");
self.shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(self.shader, b"viewPos");
#// ambient light level
self.ambientLoc = GetShaderLocation(self.shader, b"ambient");
v = ffi.new("struct Vector4 *", ambient)
SetShaderValue(self.shader, self.ambientLoc, v, UNIFORM_VEC4);
for light in ls:
self.add(light)
def add(self, light):
light.configure(len(self.lights), self.shader)
self.lights.append(light)
if len(self.lights) > self.MAX_LIGHTS:
raise Exception("Too many lights")
def update(self, cameraPos):
SetShaderValue(self.shader, self.shader.locs[LOC_VECTOR_VIEW], ffi.new("struct Vector3 *",cameraPos), UNIFORM_VEC3)
for light in self.lights:
light.UpdateLightValues()
def draw(self):
for light in self.lights:
if light.enabled:
DrawSphereEx(light.position[0], 0.2, 8, 8, light.color)
LIGHT_DIRECTIONAL=0
LIGHT_POINT=1
class Light:
def __init__(self, type, position, target, color):
self.enabled = True
self.type = type
self.position = ffi.new("struct Vector3 *",position)
self.target = target
self.color = color
def configure(self, id, shader):
self.shader = shader
#// TODO: Below code doesn't look good to me,
# // it assumes a specific shader naming and structure
# // Probably this implementation could be improved
self.enabledName = f"lights[{id}].enabled"
self.typeName = f"lights[{id}].type"
self.posName = f"lights[{id}].position"
self.targetName = f"lights[{id}].target"
self.colorName = f"lights[{id}].color"
self.enabledLoc = GetShaderLocation(shader, self.enabledName.encode('utf-8'))
self.typeLoc = GetShaderLocation(shader, self.typeName.encode('utf-8'))
self.posLoc = GetShaderLocation(shader, self.posName.encode('utf-8'))
self.targetLoc = GetShaderLocation(shader, self.targetName.encode('utf-8'))
self.colorLoc = GetShaderLocation(shader, self.colorName.encode('utf-8'))
self.UpdateLightValues()
#// Send light properties to shader
#// NOTE: Light shader locations should be available
def UpdateLightValues(self):
#// Send to shader light enabled state and type
SetShaderValue(self.shader, self.enabledLoc, ffi.new("int *",self.enabled), UNIFORM_INT)
SetShaderValue(self.shader, self.typeLoc, ffi.new("int *",self.type), UNIFORM_INT)
#// Send to shader light position values
position = [ self.position.x, self.position.y, self.position.z]
SetShaderValue(self.shader, self.posLoc, ffi.new("struct Vector3 *",position), UNIFORM_VEC3)
#// Send to shader light target position values
target =[ self.target.x, self.target.y, self.target.z ]
SetShaderValue(self.shader, self.targetLoc, ffi.new("struct Vector3 *",target), UNIFORM_VEC3)
#// Send to shader light color values
color = [self.color[0]/255.0, self.color[1]/255.0, self.color[2]/255.0, self.color[3]/255.0]
SetShaderValue(self.shader, self.colorLoc, ffi.new("struct Vector4 *",color), UNIFORM_VEC4)
def Vector3Zero():
return ffi.new("struct Vector3 *",[ 0, 0, 0])
# lighting system
from light_system import *
#// Initialization
#//--------------------------------------------------------------------------------------
screenWidth = 800;
screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT); # Enable Multi Sampling Anti Aliasing 4x (if available)
InitWindow(screenWidth, screenHeight, b"raylib [shaders] example - basic lighting")
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")
#// Define the camera to look into our 3d world
cameraPtr = ffi.new("struct Camera3D *")
camera = cameraPtr[0]
camera.position = [ 2.0, 2.0, 6.0 ] # // Camera position
camera.target = [ 0.0, 0.5, 0.0]# // Camera looking at point
camera.up = [ 0.0, 1.0, 0.0]# // Camera up vector (rotation towards target)
camera.fovy = 45.0 # // Camera field-of-view Y
camera.type = CAMERA_PERSPECTIVE # // Camera mode type
camera = ffi.new('struct Camera3D *', [
[2, 2, 6],
[0, .5, 0],
[0, 1, 0],
45,
rl.CAMERA_PERSPECTIVE
])
#// Load models
modelA = LoadModelFromMesh(GenMeshTorus(0.4, 1.0, 16, 32))
modelB = LoadModelFromMesh(GenMeshCube(1.0, 1.0, 1.0))
modelC = LoadModelFromMesh(GenMeshSphere(0.5, 32, 32))
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
texture = LoadTexture(b"resources/texel_checker.png")
texture = rl.LoadTexture(b"resources/texel_checker.png")
#// Assign texture to default model material
modelA.materials[0].maps[MAP_DIFFUSE].texture = texture
modelB.materials[0].maps[MAP_DIFFUSE].texture = texture
modelC.materials[0].maps[MAP_DIFFUSE].texture = texture
modelA.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
modelB.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
modelC.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
angle = 6.282;
#// Using 4 point lights, white, red, green and blue
#lights[0] = Light(LIGHT_POINT, ffi.new("struct Vector3 *",[ 400, 400, 400 ]), Vector3Zero(), WHITE, shader)
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)
fogD = rl.GetShaderLocation(lightSystem.shader, b'FogDensity')
fogDensity = 0.0
#// All models use the same shader
#// 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
SetCameraMode(camera, CAMERA_ORBITAL) #// Set an orbital camera mode
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 WindowShouldClose(): #// Detect window close button or ESC key
while not rl.WindowShouldClose(): #// Detect window close button or ESC key
#// Update
#//----------------------------------------------------------------------------------
if IsKeyPressed(KEY_W): lights0.enabled = not lights0.enabled
if IsKeyPressed(KEY_R): lights1.enabled = not lights1.enabled
if IsKeyPressed(KEY_G): lights2.enabled = not lights2.enabled
if IsKeyPressed(KEY_B): lights3.enabled = not lights3.enabled
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
UpdateCamera(cameraPtr); #// Update camera
rl.UpdateCamera(camera) #// Update camera
#// Make the lights do differing orbits
angle -= 0.02
@ -292,54 +116,49 @@ while not WindowShouldClose(): #// Detect window close button or ESC
lightSystem.update(camera.position)
#// Rotate the torus
modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025))[0]
modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012))[0]
#//----------------------------------------------------------------------------------
#// Draw
#//----------------------------------------------------------------------------------
BeginDrawing()
rl.BeginDrawing()
ClearBackground(RAYWHITE)
rl.ClearBackground(RAYWHITE)
BeginMode3D(camera)
rl.BeginMode3D(camera[0])
#// Draw the three models
DrawModel(modelA, [0,0,0], 1.0, WHITE)
DrawModel(modelB, [-1.6,0,0], 1.0, WHITE)
DrawModel(modelC, [ 1.6,0,0], 1.0, WHITE)
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
lightSystem.draw()
DrawGrid(10, 1.0)
rl.DrawGrid(10, 1.0)
EndMode3D()
rl.EndMode3D()
DrawFPS(10, 10)
rl.DrawFPS(10, 10)
DrawText(b"Keys RGB & W toggle lights", 10, 30, 20, DARKGRAY)
rl.DrawText(b"Keys RGB & W toggle lights", 10, 30, 20, DARKGRAY)
EndDrawing()
rl.EndDrawing()
#//----------------------------------------------------------------------------------
#// De-Initialization
#//--------------------------------------------------------------------------------------
UnloadModel(modelA) # // Unload the modelA
UnloadModel(modelB) # // Unload the modelB
UnloadModel(modelC) # // Unload the modelC
rl.UnloadModel(modelA) # // Unload the modelA
rl.UnloadModel(modelB) # // Unload the modelB
rl.UnloadModel(modelC) # // Unload the modelC
UnloadTexture(texture) #// Unload the texture
UnloadShader(shader) #// Unload shader
rl.UnloadTexture(texture) #// Unload the texture
CloseWindow(); #// Close window and OpenGL context
rl.UnloadShader(lightSystem.shader)
rl.CloseWindow() #// Close window and OpenGL context

View file

@ -0,0 +1,99 @@
#!/usr/bin/env python3
from raylib.dynamic import raylib as rl, ffi
from raylib.colors import *
import math
# a few functions ported from raymath
from rlmath import *
#// Initialization
#//--------------------------------------------------------------------------------------
screenWidth = 800;
screenHeight = 450;
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 = ffi.new('struct Camera3D *', [
[2, 12, 6],
[0, .5, 0],
[0, 1, 0],
45,
rl.CAMERA_PERSPECTIVE
])
model = rl.LoadModel(b"resources/models/barracks.obj") # // Load OBJ model
texture = rl.LoadTexture(b"resources/models/barracks_diffuse.png") # // Load model texture (diffuse map)
#// Assign texture to default model material
model.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
#// NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
shader = rl.LoadShader(b"", b"resources/shaders/glsl330/swirl.fs")
swirlCenterLoc = rl.GetShaderLocation(shader, b"center")
angle = 6.282;
swirl = ffi.new("struct Vector2 *", [0,0])
target = rl.LoadRenderTexture(screenWidth, screenHeight)
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
#//----------------------------------------------------------------------------------
angle -= 0.002
camera.position.x = math.sin(angle) * 30.0
camera.position.z = math.cos(angle) * 30.0
rl.UpdateCamera(camera) #// Update camera
swirl.x = rl.GetMouseX()
swirl.y = screenHeight - rl.GetMouseY()
rl.SetShaderValue(shader, swirlCenterLoc, swirl, rl.UNIFORM_VEC2);
#//----------------------------------------------------------------------------------
#// Draw
#//----------------------------------------------------------------------------------
rl.BeginDrawing()
rl.BeginTextureMode(target)
rl.ClearBackground(RAYWHITE)
rl.BeginMode3D(camera[0])
#// Draw the three models
rl.DrawModel(model, [0,0,0], 1.0, WHITE)
rl.DrawGrid(10, 1.0)
rl.EndTextureMode()
rl.EndMode3D()
rl.BeginShaderMode(shader)
#// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
rl.DrawTextureRec(target.texture, [ 0, 0, target.texture.width,-target.texture.height], [0.0], WHITE)
rl.EndShaderMode()
#// Draw some 2d text over drawn texture
rl.DrawText(b"(c) Barracks 3D model by Alberto Cano", screenWidth - 220, screenHeight - 20, 10, GRAY);
rl.DrawFPS(10, 10)
rl.EndDrawing()
#//----------------------------------------------------------------------------------
#// De-Initialization
#//--------------------------------------------------------------------------------------
rl.UnloadModel(model) # // Unload the model
rl.UnloadTexture(texture) #// Unload the texture
rl.CloseWindow() #// Close window and OpenGL context

106
examples/shaders/shaders_fog.py Executable file
View file

@ -0,0 +1,106 @@
#!/usr/bin/env python3
"""
Example converted to Python from:
http://bedroomcoders.co.uk/raylib-fog/
port to python completed by codifies - dont know who started it
"""
from raylib.static import rl, ffi
from raylib.colors import *
import math
from rlmath import *
from light_system import *
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT | rl.FLAG_WINDOW_RESIZABLE)
rl.InitWindow(1280, 768, b'Fog Test')
camera = ffi.new('struct Camera3D *', [
[6, 2, 6],
[0, .5, 0],
[0, 1, 0],
45,
rl.CAMERA_PERSPECTIVE
])
model = rl.LoadModelFromMesh(rl.GenMeshTorus(0.4, 1, 16, 32))
model2 = rl.LoadModelFromMesh(rl.GenMeshCube(1, 1, 1))
model3 = rl.LoadModelFromMesh(rl.GenMeshSphere(0.5, 32, 32))
texture = rl.LoadTexture(b'resources/test.png')
model.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
model2.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
model3.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
light = Light(LIGHT_POINT, [ 0, 4, 0 ], Vector3Zero(), WHITE)
lightSystem = LightSystem([ 0.2, 0.2, 0.2, 1.0 ], light)
fog_color = 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.UNIFORM_VEC4);
fogD = rl.GetShaderLocation(lightSystem.shader, b'FogDensity')
fogDensity = 0.12
model.materials[0].shader = lightSystem.shader
model2.materials[0].shader = lightSystem.shader
model3.materials[0].shader = lightSystem.shader
rl.SetTargetFPS(60)
a=0.0
while not rl.WindowShouldClose():
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 = MatrixMultiply(model.transform, MatrixRotateX(-0.025))[0]
model.transform = MatrixMultiply(model.transform, MatrixRotateZ(0.012))[0]
if rl.IsKeyDown(rl.KEY_UP):
fogDensity = min(fogDensity + 0.001, 1)
if rl.IsKeyDown(rl.KEY_DOWN):
fogDensity = max(fogDensity - 0.001, 0)
rl.SetShaderValue(lightSystem.shader, fogD, ffi.new('float[]', [fogDensity]), rl.UNIFORM_FLOAT)
rl.BeginDrawing()
rl.ClearBackground([int(255 * i) for i in fog_color])
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)
for i in range(-20, 20, 2):
rl.DrawModel(model, [i, 0, 2], 1, WHITE)
rl.DrawGizmo([1000, 1000, 1000])
rl.EndMode3D()
rl.DrawFPS(10, 10)
rl.DrawText(f'Up/Down to change fog density: {fogDensity}'.encode('utf-8'), 10, 30, 20, WHITE)
rl.EndDrawing()
rl.UnloadModel(model)
rl.UnloadModel(model2)
rl.UnloadModel(model3)
rl.UnloadTexture(texture)
rl.UnloadShader(lightSystem.shader)
rl.CloseWindow()

View file

@ -0,0 +1,75 @@
#!/usr/bin/env python3
from raylib.dynamic import raylib as rl, ffi
from raylib.colors import *
# a few functions ported from raymath
from rlmath import *
#// Initialization
#//--------------------------------------------------------------------------------------
screenWidth = 800;
screenHeight = 450;
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 = ffi.new('struct Camera3D *', [
[2, 12, 6],
[0, .5, 0],
[0, 1, 0],
45,
rl.CAMERA_PERSPECTIVE
])
imBlank = rl.GenImageColor(1024, 1024, BLANK)
texture = rl.LoadTextureFromImage(imBlank) #// Load blank texture to fill on shader
rl.UnloadImage(imBlank);
#// NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
shader = rl.LoadShader(b"", b"resources/shaders/glsl330/cubes_panning.fs");
time = ffi.new("float *", 0.0)
timeLoc = rl.GetShaderLocation(shader, b"uTime");
rl.SetShaderValue(shader, timeLoc, time, rl.UNIFORM_FLOAT);
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
#//----------------------------------------------------------------------------------
time[0] = rl.GetTime();
rl.SetShaderValue(shader, timeLoc, time, rl.UNIFORM_FLOAT);
#//----------------------------------------------------------------------------------
#// Draw
#//----------------------------------------------------------------------------------
rl.BeginDrawing()
rl.ClearBackground(RAYWHITE)
rl.BeginShaderMode(shader) #// Enable our custom shader for next shapes/textures drawings
rl.DrawTexture(texture, 0, 0, WHITE) #// Drawing BLANK texture, all magic happens on shader
rl.EndShaderMode() #// Disable our custom shader, return to default shader
rl.DrawText(b"BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, MAROON);
rl.EndDrawing()
#//----------------------------------------------------------------------------------
#// De-Initialization
#//--------------------------------------------------------------------------------------
rl.UnloadTexture(texture) #// Unload the texture
rl.CloseWindow() #// Close window and OpenGL context

View file

@ -0,0 +1,90 @@
#!/usr/bin/env python3
from raylib.static import rl, ffi
from raylib.colors import *
import math
# a few functions ported from raymath
from rlmath import *
from ctypes import byref
#// Initialization
#//--------------------------------------------------------------------------------------
screenWidth = 800;
screenHeight = 450;
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]")
camera = ffi.new('struct Camera3D *', [
[2, 12, 6],
[0, .5, 0],
[0, 1, 0],
45,
rl.CAMERA_PERSPECTIVE
])
texture = rl.LoadTexture(b"resources/space.png")
shader = rl.LoadShader(b"", b"resources/shaders/glsl330/wave.fs")
secondsLoc = rl.GetShaderLocation(shader, b"secondes")
freqXLoc = rl.GetShaderLocation(shader, b"freqX")
freqYLoc = rl.GetShaderLocation(shader, b"freqY")
ampXLoc = rl.GetShaderLocation(shader, b"ampX")
ampYLoc = rl.GetShaderLocation(shader, b"ampY")
speedXLoc = rl.GetShaderLocation(shader, b"speedX")
speedYLoc = rl.GetShaderLocation(shader, b"speedY")
freqX = ffi.new("float *", 25.0)
freqY = ffi.new("float *", 25.0)
ampX = ffi.new("float *", 5.0)
ampY = ffi.new("float *", 5.0)
speedX = ffi.new("float *", 8.0)
speedY = ffi.new("float *", 8.0)
screenSize = ffi.new("struct Vector2 *",[ rl.GetScreenWidth(), rl.GetScreenHeight() ])
rl.SetShaderValue(shader, rl.GetShaderLocation(shader, b"size"), screenSize, rl.UNIFORM_VEC2)
rl.SetShaderValue(shader, freqXLoc, freqX, rl.UNIFORM_FLOAT)
rl.SetShaderValue(shader, freqYLoc, freqY, rl.UNIFORM_FLOAT)
rl.SetShaderValue(shader, ampXLoc, ampX, rl.UNIFORM_FLOAT)
rl.SetShaderValue(shader, ampYLoc, ampY, rl.UNIFORM_FLOAT)
rl.SetShaderValue(shader, speedXLoc, speedX, rl.UNIFORM_FLOAT)
rl.SetShaderValue(shader, speedYLoc, speedY, rl.UNIFORM_FLOAT)
seconds = ffi.new("float *", 0.0)
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
#//----------------------------------------------------------------------------------
seconds[0] += rl.GetFrameTime()
rl.SetShaderValue(shader, secondsLoc, seconds, rl.UNIFORM_FLOAT)
#//----------------------------------------------------------------------------------
#// Draw
#//----------------------------------------------------------------------------------
rl.BeginDrawing()
rl.ClearBackground(RAYWHITE)
rl.BeginShaderMode(shader);
rl.DrawTexture(texture, 0, 0, WHITE);
rl.DrawTexture(texture, texture.width, 0, WHITE);
rl.EndShaderMode();
rl.EndDrawing()
#//----------------------------------------------------------------------------------
#// De-Initialization
#//--------------------------------------------------------------------------------------
rl.CloseWindow() #// Close window and OpenGL context