Skip to content

Commit 38568dc

Browse files
author
ChrisDill
committed
Fixing shader examples.
- Updated shader examples to use Utils.cs for SetShaderValue and SetShaderValueV. - Added new functions in Utils.cs for SetShaderValue and SetShaderValueV. Current solution is not ideal but I managed to get them to run. I plan to review these functions later along with testing safer alternatives.
1 parent 19b3bae commit 38568dc

8 files changed

+94
-44
lines changed

Examples/Utils.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,44 @@ public unsafe static void SetMaterialShader(ref Model model, int materialIndex,
113113
Material *materials = (Material*)model.materials.ToPointer();
114114
materials[0].shader = shader;
115115
}
116+
117+
/*
118+
Overloads for Shader Values.
119+
Currently Raylib.SetShaderValue is overloaded with ref int and ref float for the value.
120+
Same sort of usage in raylib so I assume it is converted to void * although this might not be good idea to rely on.
121+
*/
122+
public unsafe static void SetShaderValue(Shader shader, int uniformLoc, ref int value, ShaderUniformDataType uniformType)
123+
{
124+
Raylib.SetShaderValue(shader, uniformLoc, ref value, uniformType);
125+
}
126+
127+
public unsafe static void SetShaderValue(Shader shader, int uniformLoc, ref float value, ShaderUniformDataType uniformType)
128+
{
129+
Raylib.SetShaderValue(shader, uniformLoc, ref value, uniformType);
130+
}
131+
132+
public static void SetShaderValue(Shader shader, int uniformLoc, int[] value, ShaderUniformDataType uniformType)
133+
{
134+
IntPtr data = Marshal.UnsafeAddrOfPinnedArrayElement(value, 0);
135+
Raylib.SetShaderValue(shader, uniformLoc, data, uniformType);
136+
}
137+
138+
public static void SetShaderValue(Shader shader, int uniformLoc, float[] value, ShaderUniformDataType uniformType)
139+
{
140+
IntPtr data = Marshal.UnsafeAddrOfPinnedArrayElement(value, 0);
141+
Raylib.SetShaderValue(shader, uniformLoc, data, uniformType);
142+
}
143+
144+
public static void SetShaderValueV(Shader shader, int uniformLoc, int[] value, ShaderUniformDataType uniformType, int count)
145+
{
146+
IntPtr data = Marshal.UnsafeAddrOfPinnedArrayElement(value, 0);
147+
Raylib.SetShaderValueV(shader, uniformLoc, data, uniformType, count);
148+
}
149+
150+
public static void SetShaderValueV(Shader shader, int uniformLoc, float[] value, ShaderUniformDataType uniformType, int count)
151+
{
152+
IntPtr data = Marshal.UnsafeAddrOfPinnedArrayElement(value, 0);
153+
Raylib.SetShaderValueV(shader, uniformLoc, data, uniformType, count);
154+
}
116155
}
117-
}
156+
}

Examples/shaders/shaders_basic_lighting.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
********************************************************************************************/
2727

2828
using System;
29-
using System.Runtime.InteropServices;
3029
using Raylib_cs;
3130
using static Raylib_cs.Raylib;
3231
using static Raylib_cs.Raymath;
@@ -36,8 +35,10 @@
3635
using static Raylib_cs.CameraType;
3736
using static Raylib_cs.KeyboardKey;
3837
using static Raylib_cs.ShaderLocationIndex;
39-
using static Raylib_cs.ShaderUniformDataType;
4038
using static Raylib_cs.MaterialMapType;
39+
using static Raylib_cs.Rlights;
40+
using static Raylib_cs.LightType;
41+
using static Raylib_cs.ShaderUniformDataType;
4142

4243
namespace Examples
4344
{
@@ -86,7 +87,7 @@ public unsafe static int Main()
8687

8788
// ambient light level
8889
int ambientLoc = GetShaderLocation(shader, "ambient");
89-
SetShaderValue(shader, ambientLoc, new float[] { 0.2f, 0.2f, 0.2f, 1.0f }, UNIFORM_VEC4);
90+
Utils.SetShaderValue(shader, ambientLoc, new float[] { 0.2f, 0.2f, 0.2f, 1.0f }, UNIFORM_VEC4);
9091

9192
float angle = 6.282f;
9293

@@ -141,8 +142,7 @@ public unsafe static int Main()
141142

142143
// Update the light shader with the camera view position
143144
float[] cameraPos = { camera.position.x, camera.position.y, camera.position.z };
144-
IntPtr value = Marshal.UnsafeAddrOfPinnedArrayElement(cameraPos, 0);
145-
// SetShaderValue(shader, shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3);
145+
Utils.SetShaderValue(shader, locs[(int)LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3);
146146
//----------------------------------------------------------------------------------
147147

148148
// Draw

Examples/shaders/shaders_fog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public unsafe static int Main()
8181

8282
float fogDensity = 0.15f;
8383
int fogDensityLoc = GetShaderLocation(shader, "fogDensity");
84-
SetShaderValue(shader, fogDensityLoc, ref fogDensity, UNIFORM_FLOAT);
84+
Utils.SetShaderValue(shader, fogDensityLoc, ref fogDensity, UNIFORM_FLOAT);
8585

8686
// NOTE: All models share the same shader
8787
Utils.SetMaterialShader(ref modelA, 0, ref shader);
@@ -115,14 +115,14 @@ public unsafe static int Main()
115115
if (fogDensity < 0.0) fogDensity = 0.0f;
116116
}
117117

118-
SetShaderValue(shader, fogDensityLoc, ref fogDensity, UNIFORM_FLOAT);
118+
Utils.SetShaderValue(shader, fogDensityLoc, ref fogDensity, UNIFORM_FLOAT);
119119

120120
// Rotate the torus
121121
modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025f));
122122
modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012f));
123123

124124
// Update the light shader with the camera view position
125-
SetShaderValue(shader, locs[(int)LOC_VECTOR_VIEW], ref camera.position.x, UNIFORM_VEC3);
125+
Utils.SetShaderValue(shader, locs[(int)LOC_VECTOR_VIEW], ref camera.position.x, UNIFORM_VEC3);
126126
//----------------------------------------------------------------------------------
127127

128128
// Draw

Examples/shaders/shaders_julia_set.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using static Raylib_cs.Color;
2222
using static Raylib_cs.KeyboardKey;
2323
using static Raylib_cs.MouseButton;
24-
using static Raylib_cs.ShaderLocationIndex;
2524
using static Raylib_cs.ShaderUniformDataType;
2625

2726
namespace Examples
@@ -54,7 +53,7 @@ public static int Main()
5453
Shader shader = LoadShader(null, string.Format("resources/shaders/glsl{0}/julia_set.fs", GLSL_VERSION));
5554

5655
// c constant to use in z^2 + c
57-
float[] c = new float[2] { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] };
56+
float[] c = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] };
5857

5958
// Offset and zoom to draw the julia set at. (centered on screen and default size)
6059
float[] offset = { -(float)screenWidth / 2, -(float)screenHeight / 2 };
@@ -70,11 +69,11 @@ public static int Main()
7069

7170
// Tell the shader what the screen dimensions, zoom, offset and c are
7271
float[] screenDims = { (float)screenWidth, (float)screenHeight };
73-
SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), ref screenDims, UNIFORM_VEC2);
72+
Utils.SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, UNIFORM_VEC2);
7473

75-
SetShaderValue(shader, cLoc, ref c, UNIFORM_VEC2);
76-
SetShaderValue(shader, zoomLoc, ref zoomLoc, UNIFORM_FLOAT);
77-
SetShaderValue(shader, offsetLoc, ref offset, UNIFORM_VEC2);
74+
Utils.SetShaderValue(shader, cLoc, c, UNIFORM_VEC2);
75+
Utils.SetShaderValue(shader, zoomLoc, ref zoomLoc, UNIFORM_FLOAT);
76+
Utils.SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2);
7877

7978
// Create a RenderTexture2D to be used for render to texture
8079
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
@@ -130,11 +129,20 @@ public static int Main()
130129
c[0] = POINTS_OF_INTEREST[5][0];
131130
c[1] = POINTS_OF_INTEREST[5][1];
132131
}
133-
SetShaderValue(shader, cLoc, ref c, UNIFORM_VEC2);
132+
Utils.SetShaderValue(shader, cLoc, c, UNIFORM_VEC2);
134133
}
135134

136-
if (IsKeyPressed(KEY_SPACE)) pause = !pause; // Pause animation (c change)
137-
if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls
135+
// Pause animation (c change)
136+
if (IsKeyPressed(KEY_SPACE))
137+
{
138+
pause = !pause;
139+
}
140+
141+
// Toggle whether or not to show controls
142+
if (IsKeyPressed(KEY_F1))
143+
{
144+
showControls = !showControls;
145+
}
138146

139147
if (!pause)
140148
{
@@ -157,17 +165,20 @@ public static int Main()
157165
offset[0] += GetFrameTime() * offsetSpeed.x * 0.8f;
158166
offset[1] += GetFrameTime() * offsetSpeed.y * 0.8f;
159167
}
160-
else offsetSpeed = new Vector2(0.0f, 0.0f);
168+
else
169+
{
170+
offsetSpeed = new Vector2(0.0f, 0.0f);
171+
}
161172

162-
SetShaderValue(shader, zoomLoc, ref zoom, UNIFORM_FLOAT);
163-
SetShaderValue(shader, offsetLoc, ref offset, UNIFORM_VEC2);
173+
Utils.SetShaderValue(shader, zoomLoc, ref zoom, UNIFORM_FLOAT);
174+
Utils.SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2);
164175

165176
// Increment c value with time
166177
float amount = GetFrameTime() * incrementSpeed * 0.0005f;
167178
c[0] += amount;
168179
c[1] += amount;
169180

170-
SetShaderValue(shader, cLoc, ref c, UNIFORM_VEC2);
181+
Utils.SetShaderValue(shader, cLoc, c, UNIFORM_VEC2);
171182
}
172183
//----------------------------------------------------------------------------------
173184

@@ -179,7 +190,7 @@ public static int Main()
179190

180191
// Using a render texture to draw Julia set
181192
BeginTextureMode(target); // Enable drawing to texture
182-
ClearBackground(BLACK); // Clear the render texture
193+
ClearBackground(BLACK); // Clear the render texture
183194

184195
// Draw a rectangle in shader mode to be used as shader canvas
185196
// NOTE: Rectangle uses font white character texture coordinates,

Examples/shaders/shaders_palette_switch.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class shaders_palette_switch
3535
const int VALUES_PER_COLOR = 3;
3636

3737
static int[][] palettes = new int[][] {
38-
{ // 3-BIT RGB
38+
new int[] { // 3-BIT RGB
3939
0, 0, 0,
4040
255, 0, 0,
4141
0, 255, 0,
@@ -45,7 +45,7 @@ public class shaders_palette_switch
4545
255, 255, 0,
4646
255, 255, 255,
4747
},
48-
{ // AMMO-8 (GameBoy-like)
48+
new int[] { // AMMO-8 (GameBoy-like)
4949
4, 12, 6,
5050
17, 35, 24,
5151
30, 58, 41,
@@ -55,7 +55,7 @@ public class shaders_palette_switch
5555
190, 220, 127,
5656
238, 255, 204,
5757
},
58-
{ // RKBV (2-strip film)
58+
new int[] { // RKBV (2-strip film)
5959
21, 25, 26,
6060
138, 76, 88,
6161
217, 98, 117,
@@ -110,7 +110,7 @@ public static int Main()
110110

111111
// Send new value to the shader to be used on drawing.
112112
// NOTE: We are sending RGB triplets w/o the alpha channel
113-
SetShaderValueV(shader, paletteLoc, ref palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE);
113+
Utils.SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE);
114114
//----------------------------------------------------------------------------------
115115

116116
// Draw

Examples/shaders/shaders_raymarching.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static int Main()
6060
int resolutionLoc = GetShaderLocation(shader, "resolution");
6161

6262
float[] resolution = { (float)screenWidth, (float)screenHeight };
63-
SetShaderValue(shader, resolutionLoc, ref resolution, UNIFORM_VEC2);
63+
Utils.SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2);
6464

6565
float runTime = 0.0f;
6666

@@ -77,7 +77,7 @@ public static int Main()
7777
screenWidth = GetScreenWidth();
7878
screenHeight = GetScreenHeight();
7979
resolution = new float[] { (float)screenWidth, (float)screenHeight };
80-
SetShaderValue(shader, resolutionLoc, ref resolution, UNIFORM_VEC2);
80+
Utils.SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2);
8181
}
8282

8383
// Update
@@ -91,9 +91,9 @@ public static int Main()
9191
runTime += deltaTime;
9292

9393
// Set shader required uniform values
94-
SetShaderValue(shader, viewEyeLoc, ref cameraPos, UNIFORM_VEC3);
95-
SetShaderValue(shader, viewCenterLoc, ref cameraTarget, UNIFORM_VEC3);
96-
SetShaderValue(shader, runTimeLoc, ref runTime, UNIFORM_FLOAT);
94+
Utils.SetShaderValue(shader, viewEyeLoc, cameraPos, UNIFORM_VEC3);
95+
Utils.SetShaderValue(shader, viewCenterLoc, cameraTarget, UNIFORM_VEC3);
96+
Utils.SetShaderValue(shader, runTimeLoc, ref runTime, UNIFORM_FLOAT);
9797
//----------------------------------------------------------------------------------
9898

9999
// Draw

Examples/shaders/shaders_texture_drawing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static int Main()
4242

4343
float time = 0.0f;
4444
int timeLoc = GetShaderLocation(shader, "uTime");
45-
SetShaderValue(shader, timeLoc, ref time, UNIFORM_FLOAT);
45+
Utils.SetShaderValue(shader, timeLoc, ref time, UNIFORM_FLOAT);
4646

4747
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
4848
// -------------------------------------------------------------------------------------------------------------
@@ -53,7 +53,7 @@ public static int Main()
5353
// Update
5454
//----------------------------------------------------------------------------------
5555
time = (float)GetTime();
56-
SetShaderValue(shader, timeLoc, ref time, UNIFORM_FLOAT);
56+
Utils.SetShaderValue(shader, timeLoc, ref time, UNIFORM_FLOAT);
5757
//----------------------------------------------------------------------------------
5858

5959
// Draw

Examples/shaders/shaders_texture_waves.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ public unsafe static int Main()
6161
float speedY = 8.0f;
6262

6363
float[] screenSize = { (float)GetScreenWidth(), (float)GetScreenHeight() };
64-
SetShaderValue(shader, GetShaderLocation(shader, "size"), ref screenSize, UNIFORM_VEC2);
65-
SetShaderValue(shader, freqXLoc, ref freqX, UNIFORM_FLOAT);
66-
SetShaderValue(shader, freqYLoc, ref freqY, UNIFORM_FLOAT);
67-
SetShaderValue(shader, ampXLoc, ref ampX, UNIFORM_FLOAT);
68-
SetShaderValue(shader, ampYLoc, ref ampY, UNIFORM_FLOAT);
69-
SetShaderValue(shader, speedXLoc, ref speedX, UNIFORM_FLOAT);
70-
SetShaderValue(shader, speedYLoc, ref speedY, UNIFORM_FLOAT);
64+
Utils.SetShaderValue(shader, GetShaderLocation(shader, "size"), screenSize, UNIFORM_VEC2);
65+
Utils.SetShaderValue(shader, freqXLoc, ref freqX, UNIFORM_FLOAT);
66+
Utils.SetShaderValue(shader, freqYLoc, ref freqY, UNIFORM_FLOAT);
67+
Utils.SetShaderValue(shader, ampXLoc, ref ampX, UNIFORM_FLOAT);
68+
Utils.SetShaderValue(shader, ampYLoc, ref ampY, UNIFORM_FLOAT);
69+
Utils.SetShaderValue(shader, speedXLoc, ref speedX, UNIFORM_FLOAT);
70+
Utils.SetShaderValue(shader, speedYLoc, ref speedY, UNIFORM_FLOAT);
7171

7272
float seconds = 0.0f;
7373

7474
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
75-
// -------------------------------------------------------------------------------------------------------------
75+
//-------------------------------------------------------------------------------------------------------------
7676

7777
// Main game loop
7878
while (!WindowShouldClose()) // Detect window close button or ESC key
@@ -81,7 +81,7 @@ public unsafe static int Main()
8181
//----------------------------------------------------------------------------------
8282
seconds += GetFrameTime();
8383

84-
SetShaderValue(shader, secondsLoc, ref seconds, UNIFORM_FLOAT);
84+
Utils.SetShaderValue(shader, secondsLoc, ref seconds, UNIFORM_FLOAT);
8585
//----------------------------------------------------------------------------------
8686

8787
// Draw
@@ -106,7 +106,7 @@ public unsafe static int Main()
106106
UnloadShader(shader); // Unload shader
107107
UnloadTexture(texture); // Unload texture
108108

109-
CloseWindow(); // Close window and OpenGL context
109+
CloseWindow(); // Close window and OpenGL context
110110
//--------------------------------------------------------------------------------------
111111

112112
return 0;

0 commit comments

Comments
 (0)