Skip to content

Commit 4ce5b08

Browse files
committed
added skybox example
1 parent 779fd39 commit 4ce5b08

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
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'res/shader/skybox.vs.glsl',
16+
b'res/shader/skybox.fs.glsl'
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'res/shader/cubemap.vs.glsl',
28+
b'res/shader/cubemap.fs.glsl'
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'res/img/skymap.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/res/img/skymap.hdr

1.45 MB
Binary file not shown.

examples/res/shader/cubemap.fs.glsl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*******************************************************************************************
2+
*
3+
* rPBR [shader] - Equirectangular to cubemap fragment shader
4+
*
5+
* Copyright (c) 2017 Victor Fisac
6+
*
7+
**********************************************************************************************/
8+
9+
#version 330
10+
11+
// Input vertex attributes (from vertex shader)
12+
in vec3 fragPosition;
13+
14+
// Input uniform values
15+
uniform sampler2D equirectangularMap;
16+
17+
// Output fragment color
18+
out vec4 finalColor;
19+
20+
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+
}
27+
28+
void main()
29+
{
30+
// Normalize local position
31+
vec2 uv = SampleSphericalMap(normalize(fragPosition));
32+
33+
// Fetch color from texture map
34+
vec3 color = texture(equirectangularMap, uv).rgb;
35+
36+
// Calculate final fragment color
37+
finalColor = vec4(color, 1.0);
38+
}

examples/res/shader/cubemap.vs.glsl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*******************************************************************************************
2+
*
3+
* rPBR [shader] - Equirectangular to cubemap vertex shader
4+
*
5+
* Copyright (c) 2017 Victor Fisac
6+
*
7+
**********************************************************************************************/
8+
9+
#version 330
10+
11+
// Input vertex attributes
12+
in vec3 vertexPosition;
13+
14+
// Input uniform values
15+
uniform mat4 projection;
16+
uniform mat4 view;
17+
18+
// Output vertex attributes (to fragment shader)
19+
out vec3 fragPosition;
20+
21+
void main()
22+
{
23+
// Calculate fragment position based on model transformations
24+
fragPosition = vertexPosition;
25+
26+
// Calculate final vertex position
27+
gl_Position = projection*view*vec4(vertexPosition, 1.0);
28+
}

examples/res/shader/skybox.fs.glsl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*******************************************************************************************
2+
*
3+
* rPBR [shader] - Background skybox fragment shader
4+
*
5+
* Copyright (c) 2017 Victor Fisac
6+
*
7+
**********************************************************************************************/
8+
9+
#version 330
10+
11+
// Input vertex attributes (from vertex shader)
12+
in vec3 fragPosition;
13+
14+
// Input uniform values
15+
uniform samplerCube environmentMap;
16+
17+
// Output fragment color
18+
out vec4 finalColor;
19+
20+
void main()
21+
{
22+
// Fetch color from texture map
23+
vec3 color = texture(environmentMap, fragPosition).rgb;
24+
25+
// Apply gamma correction
26+
color = color/(color + vec3(1.0));
27+
color = pow(color, vec3(1.0/2.2));
28+
29+
// Calculate final fragment color
30+
finalColor = vec4(color, 1.0);
31+
}

examples/res/shader/skybox.vs.glsl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************************************
2+
*
3+
* rPBR [shader] - Background skybox vertex shader
4+
*
5+
* Copyright (c) 2017 Victor Fisac
6+
*
7+
**********************************************************************************************/
8+
9+
#version 330
10+
11+
// Input vertex attributes
12+
in vec3 vertexPosition;
13+
14+
// Input uniform values
15+
uniform mat4 projection;
16+
uniform mat4 view;
17+
18+
// Output vertex attributes (to fragment shader)
19+
out vec3 fragPosition;
20+
21+
void main()
22+
{
23+
// Calculate fragment position based on model transformations
24+
fragPosition = vertexPosition;
25+
26+
// Remove translation from the view matrix
27+
mat4 rotView = mat4(mat3(view));
28+
vec4 clipPos = projection*rotView*vec4(vertexPosition, 1.0);
29+
30+
// Calculate final vertex position
31+
gl_Position = clipPos.xyzw;
32+
}

0 commit comments

Comments
 (0)