Skip to content

Commit 95b31c4

Browse files
Pebazelectronstudio
authored andcommitted
Add Skybox Example (#5)
* added skybox example * use existing resource directory
1 parent 779fd39 commit 95b31c4

File tree

5 files changed

+131
-64
lines changed

5 files changed

+131
-64
lines changed

examples/models/models_skybox.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from raylib.dynamic import raylib as rl, ffi
2+
from raylib.colors import *
3+
4+
screenWidth = 1260
5+
screenHeight = 768
6+
7+
rl.InitWindow(screenWidth, screenHeight, b'Skymap Demo')
8+
9+
camera = ffi.new('struct Camera3D *', [[1, 1, 1], [4, 1, 4], [0, 1, 0], 70, 0])
10+
11+
cube = rl.GenMeshCube(100, 100, 100)
12+
skybox = rl.LoadModelFromMesh(cube)
13+
14+
skybox.materials[0].shader = rl.LoadShader(
15+
b'resources/shaders/skybox.vs',
16+
b'resources/shaders/skybox.fs'
17+
)
18+
19+
rl.SetShaderValue(
20+
skybox.materials[0].shader,
21+
rl.GetShaderLocation(skybox.materials[0].shader, b"environmentMap"),
22+
ffi.new('int[]', [rl.MAP_CUBEMAP]),
23+
rl.UNIFORM_INT
24+
)
25+
26+
shdrCubemap = rl.LoadShader(
27+
b'resources/shaders/cubemap.vs',
28+
b'resources/shaders/cubemap.fs'
29+
)
30+
31+
rl.SetShaderValue(
32+
shdrCubemap,
33+
rl.GetShaderLocation(shdrCubemap, b'equirectangularMap'),
34+
ffi.new('int[]', [0]),
35+
rl.UNIFORM_INT
36+
)
37+
38+
texHDR = rl.LoadTexture(b'resources/dresden_square.hdr')
39+
40+
skybox.materials[0].maps[rl.MAP_CUBEMAP].texture = rl.GenTextureCubemap(shdrCubemap, texHDR, 512);
41+
42+
rl.UnloadTexture(texHDR)
43+
rl.UnloadShader(shdrCubemap)
44+
45+
rl.SetCameraMode(camera[0], rl.CAMERA_FIRST_PERSON)
46+
47+
rl.SetTargetFPS(60)
48+
49+
while not rl.WindowShouldClose():
50+
rl.UpdateCamera(camera)
51+
rl.BeginDrawing()
52+
rl.ClearBackground(RAYWHITE)
53+
rl.BeginMode3D(camera[0])
54+
rl.DrawModel(skybox, [0, 0, 0], 1.0, WHITE)
55+
rl.DrawGrid(10, 1.0)
56+
for x in range(10):
57+
for y in range(10):
58+
rl.DrawCube([x * 2, 0, y * 2], 1, 1, 1, MAROON)
59+
rl.DrawCubeWires([x * 2, 0, y * 2], 1, 1, 1, RED)
60+
rl.EndMode3D()
61+
rl.DrawFPS(10, 10)
62+
rl.EndDrawing()
63+
64+
rl.CloseWindow()
65+
rl.UnloadShader(skybox.materials[0].shader)
66+
rl.UnloadTexture(skybox.materials[0].maps[rl.MAP_CUBEMAP].texture)
67+
rl.UnloadModel(skybox)

examples/models/resources/shaders/cubemap.fs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@
88

99
#version 330
1010

11-
# Input vertex attributes (from vertex shader)
12-
in vec3 fragPos
11+
// Input vertex attributes (from vertex shader)
12+
in vec3 fragPosition;
1313

14-
# Input uniform values
15-
uniform sampler2D equirectangularMap
14+
// Input uniform values
15+
uniform sampler2D equirectangularMap;
1616

17-
# Output fragment color
18-
out vec4 finalColor
17+
// Output fragment color
18+
out vec4 finalColor;
1919

2020
vec2 SampleSphericalMap(vec3 v)
21-
[
22-
vec2 uv = vec2(atan(v.z, v.x), asin(v.y))
23-
uv *= vec2(0.1591, 0.3183)
24-
uv += 0.5
25-
return uv
26-
]
21+
{
22+
vec2 uv = vec2(atan(v.z, v.x), asin(v.y));
23+
uv *= vec2(0.1591, 0.3183);
24+
uv += 0.5;
25+
return uv;
26+
}
2727

2828
void main()
29-
[
30-
# Normalize local position
31-
vec2 uv = SampleSphericalMap(normalize(fragPos))
29+
{
30+
// Normalize local position
31+
vec2 uv = SampleSphericalMap(normalize(fragPosition));
3232

33-
# Fetch color from texture map
34-
vec3 color = texture(equirectangularMap, uv).rgb
33+
// Fetch color from texture map
34+
vec3 color = texture(equirectangularMap, uv).rgb;
3535

36-
# Calculate final fragment color
37-
finalColor = vec4(color, 1.0)
38-
]
36+
// Calculate final fragment color
37+
finalColor = vec4(color, 1.0);
38+
}

examples/models/resources/shaders/cubemap.vs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88

99
#version 330
1010

11-
# Input vertex attributes
12-
in vec3 vertexPosition
11+
// Input vertex attributes
12+
in vec3 vertexPosition;
1313

14-
# Input uniform values
15-
uniform mat4 projection
16-
uniform mat4 view
14+
// Input uniform values
15+
uniform mat4 projection;
16+
uniform mat4 view;
1717

18-
# Output vertex attributes (to fragment shader)
19-
out vec3 fragPos
18+
// Output vertex attributes (to fragment shader)
19+
out vec3 fragPosition;
2020

2121
void main()
22-
[
23-
# Calculate fragment position based on model transformations
24-
fragPos = vertexPosition
22+
{
23+
// Calculate fragment position based on model transformations
24+
fragPosition = vertexPosition;
2525

26-
# Calculate final vertex position
27-
gl_Position = projection*view*vec4(vertexPosition, 1.0)
28-
]
26+
// Calculate final vertex position
27+
gl_Position = projection*view*vec4(vertexPosition, 1.0);
28+
}

examples/models/resources/shaders/skybox.fs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@
88

99
#version 330
1010

11-
# Input vertex attributes (from vertex shader)
12-
in vec3 fragPos
11+
// Input vertex attributes (from vertex shader)
12+
in vec3 fragPosition;
1313

14-
# Input uniform values
15-
uniform samplerCube environmentMap
14+
// Input uniform values
15+
uniform samplerCube environmentMap;
1616

17-
# Output fragment color
18-
out vec4 finalColor
17+
// Output fragment color
18+
out vec4 finalColor;
1919

2020
void main()
21-
[
22-
# Fetch color from texture map
23-
vec3 color = texture(environmentMap, fragPos).rgb
21+
{
22+
// Fetch color from texture map
23+
vec3 color = texture(environmentMap, fragPosition).rgb;
2424

25-
# Apply gamma correction
26-
color = color/(color + vec3(1.0))
27-
color = pow(color, vec3(1.0/2.2))
25+
// Apply gamma correction
26+
color = color/(color + vec3(1.0));
27+
color = pow(color, vec3(1.0/2.2));
2828

29-
# Calculate final fragment color
30-
finalColor = vec4(color, 1.0)
31-
]
29+
// Calculate final fragment color
30+
finalColor = vec4(color, 1.0);
31+
}

examples/models/resources/shaders/skybox.vs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@
88

99
#version 330
1010

11-
# Input vertex attributes
12-
in vec3 vertexPosition
11+
// Input vertex attributes
12+
in vec3 vertexPosition;
1313

14-
# Input uniform values
15-
uniform mat4 projection
16-
uniform mat4 view
14+
// Input uniform values
15+
uniform mat4 projection;
16+
uniform mat4 view;
1717

18-
# Output vertex attributes (to fragment shader)
19-
out vec3 fragPos
18+
// Output vertex attributes (to fragment shader)
19+
out vec3 fragPosition;
2020

2121
void main()
22-
[
23-
# Calculate fragment position based on model transformations
24-
fragPos = vertexPosition
22+
{
23+
// Calculate fragment position based on model transformations
24+
fragPosition = vertexPosition;
2525

26-
# Remove translation from the view matrix
27-
mat4 rotView = mat4(mat3(view))
28-
vec4 clipPos = projection*rotView*vec4(vertexPosition, 1.0)
26+
// Remove translation from the view matrix
27+
mat4 rotView = mat4(mat3(view));
28+
vec4 clipPos = projection*rotView*vec4(vertexPosition, 1.0);
2929

30-
# Calculate final vertex position
31-
gl_Position = clipPos.xyww
32-
]
30+
// Calculate final vertex position
31+
gl_Position = clipPos.xyzw;
32+
}

0 commit comments

Comments
 (0)