raylib/ports/shaders/rlights
Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license.
Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event
will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you
wrote the original software. If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented
as being the original software.
3. This notice may not be removed or altered from any source distribution.
Code
rlights.odin ¶63 linesSource
1package rlights
2
3import rl "vendor:raylib"
4
5MAX_LIGHTS :: 4
6
7Light :: struct {
8 type: LightType,
9 enabled: b32,
10 position: [3]f32,
11 target: [3]f32,
12 color: rl.Color,
13 attenuation: f32,
14 enabledLoc: i32,
15 typeLoc: i32,
16 positionLoc: i32,
17 targetLoc: i32,
18 colorLoc: i32,
19 attenuationLoc: i32,
20}
21
22LightType :: enum i32 {
23 Directional,
24 Point,
25}
26
27lightsCount: i32
28
29CreateLight :: proc(type: LightType, position, target: [3]f32, color: rl.Color, shader: rl.Shader) -> (light: Light) {
30 if lightsCount < MAX_LIGHTS {
31 light.enabled = true
32 light.type = type
33 light.position = position
34 light.target = target
35 light.color = color
36
37 light.enabledLoc = i32(rl.GetShaderLocation(shader, rl.TextFormat("lights[%i].enabled", lightsCount)))
38 light.typeLoc = i32(rl.GetShaderLocation(shader, rl.TextFormat("lights[%i].type", lightsCount)))
39 light.positionLoc = i32(rl.GetShaderLocation(shader, rl.TextFormat("lights[%i].position", lightsCount)))
40 light.targetLoc = i32(rl.GetShaderLocation(shader, rl.TextFormat("lights[%i].target", lightsCount)))
41 light.colorLoc = i32(rl.GetShaderLocation(shader, rl.TextFormat("lights[%i].color", lightsCount)))
42
43 UpdateLightValues(shader, light)
44
45 lightsCount += 1
46 }
47
48 return
49}
50
51UpdateLightValues :: proc(shader: rl.Shader, light: Light) {
52 light := light
53
54 rl.SetShaderValue(shader, rl.ShaderLocationIndex(light.enabledLoc), &light.enabled, .INT)
55 rl.SetShaderValue(shader, rl.ShaderLocationIndex(light.typeLoc), &light.type, .INT)
56
57 rl.SetShaderValue(shader, rl.ShaderLocationIndex(light.positionLoc), &light.position, .VEC3)
58
59 rl.SetShaderValue(shader, rl.ShaderLocationIndex(light.targetLoc), &light.target, .VEC3)
60
61 color := [4]f32{ f32(light.color.r)/255, f32(light.color.g)/255, f32(light.color.b)/255, f32(light.color.a)/255 }
62 rl.SetShaderValue(shader, rl.ShaderLocationIndex(light.colorLoc), &color, .VEC4)
63}