Skip to content

Commit 218d9a1

Browse files
test, review and fix the shader examples. #47
1 parent ff84bdf commit 218d9a1

File tree

7 files changed

+86
-147
lines changed

7 files changed

+86
-147
lines changed

examples/shaders/light_system.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from raylib.dynamic import raylib as rl, ffi
1+
import raylib as rl
22

33

44
class LightSystem:
@@ -11,13 +11,13 @@ def __init__(self, ambient = [ 0.2, 0.2, 0.2, 1.0 ], *ls):
1111
b"resources/shaders/fogLight.fs");
1212

1313
#// Get some shader loactions
14-
self.shader.locs[rl.LOC_MATRIX_MODEL] = rl.GetShaderLocation(self.shader, b"matModel");
15-
self.shader.locs[rl.LOC_VECTOR_VIEW] = rl.GetShaderLocation(self.shader, b"viewPos");
14+
self.shader.locs[rl.SHADER_LOC_MATRIX_MODEL] = rl.GetShaderLocation(self.shader, b"matModel");
15+
self.shader.locs[rl.SHADER_LOC_VECTOR_VIEW] = rl.GetShaderLocation(self.shader, b"viewPos");
1616

1717
#// ambient light level
1818
self.ambientLoc = rl.GetShaderLocation(self.shader, b"ambient");
19-
v = ffi.new("struct Vector4 *", ambient)
20-
rl.SetShaderValue(self.shader, self.ambientLoc, v, rl.UNIFORM_VEC4);
19+
v = rl.ffi.new("struct Vector4 *", ambient)
20+
rl.SetShaderValue(self.shader, self.ambientLoc, v, rl.SHADER_UNIFORM_VEC4);
2121

2222
for light in ls:
2323
self.add(light)
@@ -29,7 +29,7 @@ def add(self, light):
2929
raise Exception("Too many lights")
3030

3131
def update(self, cameraPos):
32-
rl.SetShaderValue(self.shader, self.shader.locs[rl.LOC_VECTOR_VIEW], ffi.new("struct Vector3 *",cameraPos), rl.UNIFORM_VEC3)
32+
rl.SetShaderValue(self.shader, self.shader.locs[rl.SHADER_LOC_VECTOR_VIEW], rl.ffi.new("struct Vector3 *",cameraPos), rl.SHADER_UNIFORM_VEC3)
3333
for light in self.lights:
3434
light.UpdateLightValues()
3535

@@ -49,7 +49,7 @@ class Light:
4949
def __init__(self, type, position, target, color):
5050
self.enabled = True
5151
self.type = type
52-
self.position = ffi.new("struct Vector3 *",position)
52+
self.position = rl.ffi.new("struct Vector3 *",position)
5353
self.target = target
5454
self.color = color
5555

@@ -80,20 +80,20 @@ def configure(self, id, shader):
8080
#// NOTE: Light shader locations should be available
8181
def UpdateLightValues(self):
8282
#// Send to shader light enabled state and type
83-
rl.SetShaderValue(self.shader, self.enabledLoc, ffi.new("int *",self.enabled), rl.UNIFORM_INT)
84-
rl.SetShaderValue(self.shader, self.typeLoc, ffi.new("int *",self.type), rl.UNIFORM_INT)
83+
rl.SetShaderValue(self.shader, self.enabledLoc, rl.ffi.new("int *",self.enabled), rl.SHADER_UNIFORM_INT)
84+
rl.SetShaderValue(self.shader, self.typeLoc, rl.ffi.new("int *",self.type), rl.SHADER_UNIFORM_INT)
8585

8686
#// Send to shader light position values
8787
position = [ self.position.x, self.position.y, self.position.z]
88-
rl.SetShaderValue(self.shader, self.posLoc, ffi.new("struct Vector3 *",position), rl.UNIFORM_VEC3)
88+
rl.SetShaderValue(self.shader, self.posLoc, rl.ffi.new("struct Vector3 *",position), rl.SHADER_UNIFORM_VEC3)
8989

9090
#// Send to shader light target position values
9191
target =[ self.target.x, self.target.y, self.target.z ]
92-
rl.SetShaderValue(self.shader, self.targetLoc, ffi.new("struct Vector3 *",target), rl.UNIFORM_VEC3)
92+
rl.SetShaderValue(self.shader, self.targetLoc, rl.ffi.new("struct Vector3 *",target), rl.SHADER_UNIFORM_VEC3)
9393

9494
#// Send to shader light color values
9595
color = [self.color[0]/255.0, self.color[1]/255.0, self.color[2]/255.0, self.color[3]/255.0]
96-
rl.SetShaderValue(self.shader, self.colorLoc, ffi.new("struct Vector4 *",color), rl.UNIFORM_VEC4)
96+
rl.SetShaderValue(self.shader, self.colorLoc, rl.ffi.new("struct Vector4 *",color), rl.SHADER_UNIFORM_VEC4)
9797

9898

9999

examples/shaders/rlmath.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
# just a few functions from raymath
2-
#from raylib.dynamic import raylib as rl, ffi
3-
from raylib import rl, ffi
2+
3+
import raylib as rl
44
import math
55

6-
#<<<<<<< HEAD
7-
#<<<<<<< HEAD
8-
#<<<<<<< HEAD
9-
#=======
10-
#>>>>>>> 2e2e575 (added shaders custom uniform)
11-
#=======
12-
#>>>>>>> 1775ffc4b093c881ee44a8027b4143add066d738
6+
137
PI = 3.14159265358979323846
148
DEG2RAD = (PI/180.0)
159
RAD2DEG = (180.0/PI)
@@ -27,16 +21,9 @@ def Lerp(start: float, end: float, amount: float):
2721
def Vector2Zero():
2822
return ffi.new("struct Vector2 *",[ 0, 0])
2923

30-
#<<<<<<< HEAD
31-
#<<<<<<< HEAD
32-
#=======
33-
#>>>>>>> ffe4403 (complete fog example)
34-
#=======
35-
#>>>>>>> 2e2e575 (added shaders custom uniform)
36-
#=======
37-
#>>>>>>> 1775ffc4b093c881ee44a8027b4143add066d738
24+
3825
def Vector3Zero():
39-
return ffi.new("struct Vector3 *",[ 0, 0, 0])
26+
return rl.ffi.new("struct Vector3 *",[ 0, 0, 0])
4027

4128
def MatrixRotateX(angle):
4229
result = MatrixIdentity();
@@ -68,7 +55,7 @@ def MatrixRotateY(angle):
6855

6956

7057
def MatrixIdentity():
71-
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 ])
58+
result = rl.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 ])
7259
return result
7360

7461

@@ -88,7 +75,7 @@ def MatrixRotateZ(angle):
8875

8976

9077
def MatrixMultiply(left, right):
91-
result = ffi.new("struct Matrix *")
78+
result = rl.ffi.new("struct Matrix *")
9279
result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
9380
result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
9481
result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;

examples/shaders/shaders_basic_lighting.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,10 @@
2727
# *
2828
# ********************************************************************************************/
2929

30-
#<<<<<<< HEAD
31-
#<<<<<<< HEAD
32-
#<<<<<<< HEAD
33-
from raylib import rl, ffi
34-
#=======
35-
#from raylib.dynamic import raylib as rl, ffi
36-
#>>>>>>> ffe4403 (complete fog example)
37-
#=======
38-
#from raylib.static import rl, ffi
39-
#>>>>>>> 10b63b9 (added shaders_texture_waves.py)
40-
#=======
41-
#from raylib.static import rl, ffi
42-
#>>>>>>> 1775ffc4b093c881ee44a8027b4143add066d738
30+
31+
import raylib as rl
32+
33+
4334
from raylib.colors import *
4435
from dataclasses import dataclass
4536
from enum import Enum
@@ -60,7 +51,7 @@
6051
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT| rl.FLAG_WINDOW_RESIZABLE); # Enable Multi Sampling Anti Aliasing 4x (if available)
6152
rl.InitWindow(screenWidth, screenHeight, b"raylib [shaders] example - basic lighting")
6253

63-
camera = ffi.new('struct Camera3D *', [
54+
camera = rl.ffi.new('struct Camera3D *', [
6455
[2, 2, 6],
6556
[0, .5, 0],
6657
[0, 1, 0],
@@ -77,9 +68,9 @@
7768
texture = rl.LoadTexture(b"resources/texel_checker.png")
7869

7970
#// Assign texture to default model material
80-
modelA.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
81-
modelB.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
82-
modelC.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
71+
modelA.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
72+
modelB.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
73+
modelC.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
8374

8475
angle = 6.282;
8576

@@ -137,8 +128,8 @@
137128
#// Rotate the torus
138129
# modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025)[0])[0]
139130
# modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012)[0])[0]
140-
modelA.transform = ffi.cast('Matrix *', MatrixMultiply(modelA.transform, MatrixRotateX(-0.025)))[0]
141-
modelA.transform = ffi.cast('Matrix *', MatrixMultiply(modelA.transform, MatrixRotateZ(0.012)))[0]
131+
modelA.transform = rl.ffi.cast('Matrix *', MatrixMultiply(modelA.transform, MatrixRotateX(-0.025)))[0]
132+
modelA.transform = rl.ffi.cast('Matrix *', MatrixMultiply(modelA.transform, MatrixRotateZ(0.012)))[0]
142133

143134
if (rl.IsKeyPressed(rl.KEY_F)):
144135
rl.ToggleFullscreen()

examples/shaders/shaders_custom_uniform.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
#!/usr/bin/env python3
2-
#<<<<<<< HEAD
3-
#<<<<<<< HEAD
4-
5-
#=======
61
# /*******************************************************************************************
72
# *
8-
# * raylib [shaders] example - basic lighting
3+
# * raylib [shaders] example - custom uniform
94
# *
105
# * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
116
# * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
@@ -30,12 +25,9 @@
3025
# *
3126
# *
3227
# ********************************************************************************************/
33-
#>>>>>>> 2e2e575 (added shaders custom uniform)
34-
#=======#
35-
#
36-
#>>>>>>> 1775ffc4b093c881ee44a8027b4143add066d738
3728

38-
from raylib.dynamic import raylib as rl, ffi
29+
30+
import raylib as rl
3931
from raylib.colors import *
4032
import math
4133

@@ -49,9 +41,9 @@
4941
screenHeight = 450;
5042

5143
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT| rl.FLAG_WINDOW_RESIZABLE); # Enable Multi Sampling Anti Aliasing 4x (if available)
52-
rl.InitWindow(screenWidth, screenHeight, b"raylib [shaders] example - basic lighting")
44+
rl.InitWindow(screenWidth, screenHeight, b"raylib [shaders] example - custom uniform")
5345

54-
camera = ffi.new('struct Camera3D *', [
46+
camera = rl.ffi.new('struct Camera3D *', [
5547
[2, 12, 6],
5648
[0, .5, 0],
5749
[0, 1, 0],
@@ -63,15 +55,15 @@
6355
texture = rl.LoadTexture(b"resources/models/barracks_diffuse.png") # // Load model texture (diffuse map)
6456

6557
#// Assign texture to default model material
66-
model.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
58+
model.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
6759
#// NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
6860
shader = rl.LoadShader(b"", b"resources/shaders/glsl330/swirl.fs")
6961
swirlCenterLoc = rl.GetShaderLocation(shader, b"center")
7062
angle = 6.282;
7163

7264

7365

74-
swirl = ffi.new("struct Vector2 *", [0,0])
66+
swirl = rl.ffi.new("struct Vector2 *", [0,0])
7567

7668
target = rl.LoadRenderTexture(screenWidth, screenHeight)
7769

@@ -90,7 +82,7 @@
9082

9183
swirl.x = rl.GetMouseX()
9284
swirl.y = screenHeight - rl.GetMouseY()
93-
rl.SetShaderValue(shader, swirlCenterLoc, swirl, rl.UNIFORM_VEC2);
85+
rl.SetShaderValue(shader, swirlCenterLoc, swirl, rl.SHADER_UNIFORM_VEC2);
9486
#//----------------------------------------------------------------------------------
9587

9688
#// Draw

examples/shaders/shaders_fog.py

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
"""
32
43
Example converted to Python from:
@@ -8,20 +7,9 @@
87
98
109
"""
11-
#
12-
#<<<<<<< HEAD
13-
#<<<<<<< HEAD
14-
#<<<<<<< HEAD
15-
from raylib import rl, ffi
16-
#=======
17-
#from raylib.dynamic import raylib as rl, ffi
18-
#>>>>>>> ffe4403 (complete fog example)
19-
#=======
20-
#from raylib.static import rl, ffi
21-
#>>>>>>> 10b63b9 (added shaders_texture_waves.py)
22-
#=======
23-
#from raylib.static import rl, ffi
24-
#>>>>>>> 1775ffc4b093c881ee44a8027b4143add066d738
10+
11+
import raylib as rl
12+
2513
from raylib.colors import *
2614
import math
2715

@@ -31,7 +19,7 @@
3119
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT | rl.FLAG_WINDOW_RESIZABLE)
3220
rl.InitWindow(1280, 768, b'Fog Test')
3321

34-
camera = ffi.new('struct Camera3D *', [
22+
camera = rl.ffi.new('struct Camera3D *', [
3523
[6, 2, 6],
3624
[0, .5, 0],
3725
[0, 1, 0],
@@ -45,26 +33,20 @@
4533

4634
texture = rl.LoadTexture(b'resources/test.png')
4735

48-
model.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
49-
model2.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
50-
model3.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
36+
model.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
37+
model2.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
38+
model3.materials[0].maps[rl.MATERIAL_MAP_ALBEDO].texture = texture
5139

5240
light = Light(LIGHT_POINT, [ 0, 4, 0 ], Vector3Zero(), WHITE)
5341
lightSystem = LightSystem([ 0.2, 0.2, 0.2, 1.0 ], light)
54-
#
55-
#<<<<<<< HEAD
56-
#<<<<<<< HEAD
57-
fog_color = ffi.new('float[]', [0.2,0.2,1.0,1.0])
42+
43+
fog_color = rl.ffi.new('float[]', [0.2,0.2,1.0,1.0])
5844
fogC = rl.GetShaderLocation(lightSystem.shader, b'fogColor')
59-
rl.SetShaderValue(lightSystem.shader, fogC, fog_color, rl.UNIFORM_VEC4);
60-
#=======
61-
#fog_color = [0.2, 0.2, 1.0, 1.0]
62-
#>>>>>>> ffe4403 (complete fog example)
63-
#=======
64-
#fog_color = ffi.new('float[]', [0.2,0.2,1.0,1.0])
45+
rl.SetShaderValue(lightSystem.shader, fogC, fog_color, rl.SHADER_UNIFORM_VEC4);
46+
6547
fogC = rl.GetShaderLocation(lightSystem.shader, b'fogColor')
66-
rl.SetShaderValue(lightSystem.shader, fogC, fog_color, rl.UNIFORM_VEC4);
67-
#>>>>>>> 1775ffc4b093c881ee44a8027b4143add066d738
48+
rl.SetShaderValue(lightSystem.shader, fogC, fog_color, rl.SHADER_UNIFORM_VEC4);
49+
6850
fogD = rl.GetShaderLocation(lightSystem.shader, b'FogDensity')
6951
fogDensity = 0.12
7052

@@ -84,30 +66,24 @@
8466
lightSystem.update(camera.position)
8567

8668

87-
model.transform = ffi.cast("Matrix *",MatrixMultiply(model.transform, MatrixRotateX(-0.025)))[0]
88-
model.transform = ffi.cast("Matrix *",MatrixMultiply(model.transform, MatrixRotateZ(0.012)))[0]
69+
model.transform = rl.ffi.cast("Matrix *",MatrixMultiply(model.transform, MatrixRotateX(-0.025)))[0]
70+
model.transform = rl.ffi.cast("Matrix *",MatrixMultiply(model.transform, MatrixRotateZ(0.012)))[0]
8971

9072
if rl.IsKeyDown(rl.KEY_UP):
9173
fogDensity = min(fogDensity + 0.001, 1)
9274

9375
if rl.IsKeyDown(rl.KEY_DOWN):
9476
fogDensity = max(fogDensity - 0.001, 0)
9577

96-
rl.SetShaderValue(lightSystem.shader, fogD, ffi.new('float[]', [fogDensity]), rl.UNIFORM_FLOAT)
78+
rl.SetShaderValue(lightSystem.shader, fogD, rl.ffi.new('float[]', [fogDensity]), rl.SHADER_UNIFORM_FLOAT)
9779

9880
rl.BeginDrawing()
9981

10082
rl.ClearBackground([int(255 * i) for i in fog_color])
101-
#<<<<<<< HEAD
102-
#<<<<<<< HEAD
103-
# if rl.IsKeyDown(rl.KEY_SPACE):
104-
# rl.ClearBackground(BLACK)
105-
#=======
106-
#>>>>>>> ffe4403 (complete fog example)
107-
#=======
83+
10884
if rl.IsKeyDown(rl.KEY_SPACE):
10985
rl.ClearBackground(BLACK)
110-
#>>>>>>> 1775ffc4b093c881ee44a8027b4143add066d738
86+
11187

11288
rl.BeginMode3D(camera[0])
11389
rl.DrawModel(model, [0] * 3, 1, WHITE)
@@ -118,7 +94,8 @@
11894
rl.DrawModel(model, [i, 0, 2], 1, WHITE)
11995

12096

121-
rl.DrawGizmo([1000, 1000, 1000])
97+
#Raylib removed this function
98+
#rl.DrawGizmo([1000, 1000, 1000])
12299

123100
rl.EndMode3D()
124101

0 commit comments

Comments
 (0)