public static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture drawing"); Image imBlank = GenImageColor(1024, 1024, BLANK); Texture2D texture = LoadTextureFromImage(imBlank); // Load blank texture to fill on shader UnloadImage(imBlank); // NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version Shader shader = LoadShader(null, string.Format("resources/shaders/glsl{0}/cubes_panning.fs", GLSL_VERSION)); float time = 0.0f; int timeLoc = GetShaderLocation(shader, "uTime"); Utils.SetShaderValue(shader, timeLoc, time, UNIFORM_FLOAT); SetTargetFPS(60); // Set our game to run at 60 frames-per-second // ------------------------------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- time = (float)GetTime(); Utils.SetShaderValue(shader, timeLoc, time, UNIFORM_FLOAT); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginShaderMode(shader); // Enable our custom shader for next shapes/textures drawings DrawTexture(texture, 0, 0, WHITE); // Drawing BLANK texture, all magic happens on shader EndShaderMode(); // Disable our custom shader, return to default shader DrawText("BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, MAROON); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public static void UpdateLightValues(Shader shader, Light light) { // Send to shader light enabled state and type Utils.SetShaderValue(shader, light.enabledLoc, light.enabled ? 1 : 0); Utils.SetShaderValue(shader, light.typeLoc, (int)light.type); // Send to shader light target position values float[] position = new[] { light.position.X, light.position.Y, light.position.Z }; Utils.SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3); // Send to shader light target position values float[] target = { light.target.X, light.target.Y, light.target.Z }; Utils.SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3); // Send to shader light color values float[] color = new[] { (float)light.color.r / (float)255, (float)light.color.g / (float)255, (float)light.color.b / (float)255, (float)light.color.a / (float)255 }; Utils.SetShaderValue(shader, light.colorLoc, color, UNIFORM_VEC4); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - fog"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D( new Vector3(2.0f, 2.0f, 6.0f), // position new Vector3(0.0f, 0.5f, 0.0f), // target new Vector3(0.0f, 1.0f, 0.0f), // up 45.0f, CAMERA_PERSPECTIVE); // fov, type // Load models and texture Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32)); Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32)); Texture2D texture = LoadTexture("resources/texel_checker.png"); // Assign texture to default model material Utils.SetMaterialTexture(ref modelA, 0, MAP_ALBEDO, ref texture); Utils.SetMaterialTexture(ref modelB, 0, MAP_ALBEDO, ref texture); Utils.SetMaterialTexture(ref modelC, 0, MAP_ALBEDO, ref texture); // Load shader and set up some uniforms Shader shader = LoadShader("resources/shaders/glsl330/base_lighting.vs", "resources/shaders/glsl330/fog.fs"); int * locs = (int *)shader.locs.ToPointer(); locs[(int)LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel"); locs[(int)LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); // Ambient light level int ambientLoc = GetShaderLocation(shader, "ambient"); Utils.SetShaderValue(shader, ambientLoc, new float[] { 0.2f, 0.2f, 0.2f, 1.0f }, UNIFORM_VEC4); float fogDensity = 0.15f; int fogDensityLoc = GetShaderLocation(shader, "fogDensity"); Utils.SetShaderValue(shader, fogDensityLoc, fogDensity, UNIFORM_FLOAT); // NOTE: All models share the same shader Utils.SetMaterialShader(ref modelA, 0, ref shader); Utils.SetMaterialShader(ref modelB, 0, ref shader); Utils.SetMaterialShader(ref modelC, 0, ref shader); // Using just 1 point lights CreateLight(0, LightType.LIGHT_POINT, new Vector3(0, 2, 6), Vector3.Zero, WHITE, shader); SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(ref camera); // Update camera if (IsKeyDown(KEY_UP)) { fogDensity += 0.001f; if (fogDensity > 1.0) { fogDensity = 1.0f; } } if (IsKeyDown(KEY_DOWN)) { fogDensity -= 0.001f; if (fogDensity < 0.0) { fogDensity = 0.0f; } } Utils.SetShaderValue(shader, fogDensityLoc, fogDensity, UNIFORM_FLOAT); // Rotate the torus modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025f)); modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012f)); // Update the light shader with the camera view position Utils.SetShaderValue(shader, locs[(int)LOC_VECTOR_VIEW], camera.position.X, ShaderUniformDataType.UNIFORM_VEC3); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(GRAY); BeginMode3D(camera); // Draw the three models DrawModel(modelA, Vector3.Zero, 1.0f, WHITE); DrawModel(modelB, new Vector3(-2.6f, 0, 0), 1.0f, WHITE); DrawModel(modelC, new Vector3(2.6f, 0, 0), 1.0f, WHITE); for (int i = -20; i < 20; i += 2) { DrawModel(modelA, new Vector3(i, 0, 2), 1.0f, WHITE); } EndMode3D(); DrawText(string.Format("Use KEY_UP/KEY_DOWN to change fog density [{0}]", fogDensity), 10, 10, 20, RAYWHITE); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(modelA); // Unload the model A UnloadModel(modelB); // Unload the model B UnloadModel(modelC); // Unload the model C UnloadTexture(texture); // Unload the texture UnloadShader(shader); // Unload shader CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(ConfigFlag.FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [models] example - pbr material"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(); camera.position = new Vector3(4.0f, 4.0f, 4.0f); camera.target = new Vector3(0.0f, 0.5f, 0.0f); camera.up = new Vector3(0.0f, 1.0f, 0.0f); camera.fovy = 45.0f; camera.type = CAMERA_PERSPECTIVE; // Load model and PBR material Model model = LoadModel("resources/pbr/trooper.obj"); // Unsafe pointers into model arrays. Material *materials = (Material *)model.materials.ToPointer(); Mesh * meshes = (Mesh *)model.meshes.ToPointer(); // Mesh tangents are generated... and uploaded to GPU // NOTE: New VBO for tangents is generated at default location and also binded to mesh VAO MeshTangents(ref meshes[0]); UnloadMaterial(materials[0]); // get rid of default material materials[0] = LoadMaterialPBR(new Color(255, 255, 255, 255), 1.0f, 1.0f); // Define lights attributes // NOTE: Shader is passed to every light on creation to define shader bindings internally CreateLight(LightType.LIGHT_POINT, new Vector3(LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), new Color(255, 0, 0, 255), materials[0].shader); CreateLight(LightType.LIGHT_POINT, new Vector3(0.0f, LIGHT_HEIGHT, LIGHT_DISTANCE), new Vector3(0.0f, 0.0f, 0.0f), new Color(0, 255, 0, 255), materials[0].shader); CreateLight(LightType.LIGHT_POINT, new Vector3(-LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), new Color(0, 0, 255, 255), materials[0].shader); CreateLight(LightType.LIGHT_DIRECTIONAL, new Vector3(0.0f, LIGHT_HEIGHT * 2.0f, -LIGHT_DISTANCE), new Vector3(0.0f, 0.0f, 0.0f), new Color(255, 0, 255, 255), materials[0].shader); SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(ref camera); // Update camera // Send to material PBR shader camera view position float[] cameraPos = { camera.position.X, camera.position.Y, camera.position.Z }; int * locs = (int *)materials[0].shader.locs.ToPointer(); Utils.SetShaderValue(materials[0].shader, (int)ShaderLocationIndex.LOC_VECTOR_VIEW, cameraPos, ShaderUniformDataType.UNIFORM_VEC3); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, Vector3Zero(), 1.0f, WHITE); DrawGrid(10, 1.0f); EndMode3D(); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- // Shaders and textures must be unloaded by user, // they could be in use by other models MaterialMap *maps = (MaterialMap *)materials[0].maps.ToPointer(); UnloadTexture(maps[(int)MaterialMapType.MAP_ALBEDO].texture); UnloadTexture(maps[(int)MaterialMapType.MAP_NORMAL].texture); UnloadTexture(maps[(int)MaterialMapType.MAP_METALNESS].texture); UnloadTexture(maps[(int)MaterialMapType.MAP_ROUGHNESS].texture); UnloadTexture(maps[(int)MaterialMapType.MAP_OCCLUSION].texture); UnloadTexture(maps[(int)MaterialMapType.MAP_IRRADIANCE].texture); UnloadTexture(maps[(int)MaterialMapType.MAP_PREFILTER].texture); UnloadTexture(maps[(int)MaterialMapType.MAP_BRDF].texture); UnloadShader(materials[0].shader); UnloadModel(model); // Unload skybox model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
// Load PBR material (Supports: ALBEDO, NORMAL, METALNESS, ROUGHNESS, AO, EMMISIVE, HEIGHT maps) // NOTE: PBR shader is loaded inside this function unsafe public static Material LoadMaterialPBR(Color albedo, float metalness, float roughness) { Material mat = Raylib.LoadMaterialDefault(); // NOTE: All maps textures are set to { 0 ) string PATH_PBR_VS = "resources/shaders/glsl330/pbr.vs"; string PATH_PBR_FS = "resources/shaders/glsl330/pbr.fs"; mat.shader = LoadShader(PATH_PBR_VS, PATH_PBR_FS); // Temporary unsafe pointers into material arrays. MaterialMap *maps = (MaterialMap *)mat.maps.ToPointer(); int * locs = (int *)mat.shader.locs.ToPointer(); // Get required locations points for PBR material // NOTE: Those location names must be available and used in the shader code locs[(int)ShaderLocationIndex.LOC_MAP_ALBEDO] = GetShaderLocation(mat.shader, "albedo.sampler"); locs[(int)ShaderLocationIndex.LOC_MAP_METALNESS] = GetShaderLocation(mat.shader, "metalness.sampler"); locs[(int)ShaderLocationIndex.LOC_MAP_NORMAL] = GetShaderLocation(mat.shader, "normals.sampler"); locs[(int)ShaderLocationIndex.LOC_MAP_ROUGHNESS] = GetShaderLocation(mat.shader, "roughness.sampler"); locs[(int)ShaderLocationIndex.LOC_MAP_OCCLUSION] = GetShaderLocation(mat.shader, "occlusion.sampler"); locs[(int)ShaderLocationIndex.LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap"); locs[(int)ShaderLocationIndex.LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap"); locs[(int)ShaderLocationIndex.LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT"); // Set view matrix location locs[(int)ShaderLocationIndex.LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel"); locs[(int)ShaderLocationIndex.LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos"); // Set PBR standard maps maps[(int)MaterialMapType.MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png"); maps[(int)MaterialMapType.MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png"); maps[(int)MaterialMapType.MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png"); maps[(int)MaterialMapType.MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png"); maps[(int)MaterialMapType.MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png"); // Set environment maps const string PATH_CUBEMAP_VS = "resources/shaders/glsl330/cubemap.vs"; // Path to equirectangular to cubemap vertex shader const string PATH_CUBEMAP_FS = "resources/shaders/glsl330/cubemap.fs"; // Path to equirectangular to cubemap fragment shader const string PATH_SKYBOX_VS = "resources/shaders/glsl330/skybox.vs"; // Path to skybox vertex shader const string PATH_IRRADIANCE_FS = "resources/shaders/glsl330/irradiance.fs"; // Path to irradiance (GI) calculation fragment shader const string PATH_PREFILTER_FS = "resources/shaders/glsl330/prefilter.fs"; // Path to reflection prefilter calculation fragment shader const string PATH_BRDF_VS = "resources/shaders/glsl330/brdf.vs"; // Path to bidirectional reflectance distribution function vertex shader const string PATH_BRDF_FS = "resources/shaders/glsl330/brdf.fs"; // Path to bidirectional reflectance distribution function fragment shader Shader shdrCubemap = LoadShader(PATH_CUBEMAP_VS, PATH_CUBEMAP_FS); Shader shdrIrradiance = LoadShader(PATH_SKYBOX_VS, PATH_IRRADIANCE_FS); Shader shdrPrefilter = LoadShader(PATH_SKYBOX_VS, PATH_PREFILTER_FS); Shader shdrBRDF = LoadShader(PATH_BRDF_VS, PATH_BRDF_FS); // Setup required shader locations Utils.SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), 0); Utils.SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), 0); Utils.SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), 0); Texture2D texHDR = LoadTexture("resources/dresden_square.hdr"); Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE); maps[(int)MaterialMapType.MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE); maps[(int)MaterialMapType.MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE); maps[(int)MaterialMapType.MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, BRDF_SIZE); UnloadTexture(cubemap); UnloadTexture(texHDR); // Unload already used shaders (to create specific textures) UnloadShader(shdrCubemap); UnloadShader(shdrIrradiance); UnloadShader(shdrPrefilter); UnloadShader(shdrBRDF); // Set textures filtering for better quality SetTextureFilter(maps[(int)MaterialMapType.MAP_ALBEDO].texture, FILTER_BILINEAR); SetTextureFilter(maps[(int)MaterialMapType.MAP_NORMAL].texture, FILTER_BILINEAR); SetTextureFilter(maps[(int)MaterialMapType.MAP_METALNESS].texture, FILTER_BILINEAR); SetTextureFilter(maps[(int)MaterialMapType.MAP_ROUGHNESS].texture, FILTER_BILINEAR); SetTextureFilter(maps[(int)MaterialMapType.MAP_OCCLUSION].texture, FILTER_BILINEAR); // Enable sample usage in shader for assigned textures Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), 1); Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), 1); Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), 1); Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), 1); Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), 1); int renderModeLoc = GetShaderLocation(mat.shader, "renderMode"); Utils.SetShaderValue(mat.shader, renderModeLoc, 0); // Set up material properties color maps[(int)MaterialMapType.MAP_ALBEDO].color = albedo; maps[(int)MaterialMapType.MAP_NORMAL].color = new Color(128, 128, 255, 255); maps[(int)MaterialMapType.MAP_METALNESS].value = metalness; maps[(int)MaterialMapType.MAP_ROUGHNESS].value = roughness; maps[(int)MaterialMapType.MAP_OCCLUSION].value = 1.0f; maps[(int)MaterialMapType.MAP_EMISSION].value = 0.5f; maps[(int)MaterialMapType.MAP_HEIGHT].value = 0.5f; return(mat); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib - simple shader mask"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(); camera.position = new Vector3(0.0f, 1.0f, 2.0f); camera.target = new Vector3(0.0f, 0.0f, 0.0f); camera.up = new Vector3(0.0f, 1.0f, 0.0f); camera.fovy = 45.0f; camera.type = CAMERA_PERSPECTIVE; SetCameraMode(camera, CAMERA_CUSTOM); // Define our three models to show the shader on Mesh torus = GenMeshTorus(.3f, 1, 16, 32); Model model1 = LoadModelFromMesh(torus); Mesh cube = GenMeshCube(.8f, .8f, .8f); Model model2 = LoadModelFromMesh(cube); // Generate model to be shaded just to see the gaps in the other two Mesh sphere = GenMeshSphere(1, 16, 16); Model model3 = LoadModelFromMesh(sphere); // Load the shader Shader shader = LoadShader("resources/shaders/glsl330/mask.vs", "resources/shaders/glsl330/mask.fs"); // Load and apply the diffuse texture (colour map) Texture2D texDiffuse = LoadTexture("resources/plasma.png"); Material * materials = (Material *)model1.materials.ToPointer(); MaterialMap *maps = (MaterialMap *)materials[0].maps.ToPointer(); maps[(int)MAP_ALBEDO].texture = texDiffuse; materials = (Material *)model2.materials.ToPointer(); maps = (MaterialMap *)materials[0].maps.ToPointer(); maps[(int)MAP_ALBEDO].texture = texDiffuse; // Using MAP_EMISSION as a spare slot to use for 2nd texture // NOTE: Don't use MAP_IRRADIANCE, MAP_PREFILTER or MAP_CUBEMAP // as they are bound as cube maps Texture2D texMask = LoadTexture("resources/mask.png"); materials = (Material *)model1.materials.ToPointer(); maps = (MaterialMap *)materials[0].maps.ToPointer(); maps[(int)MAP_EMISSION].texture = texMask; materials = (Material *)model2.materials.ToPointer(); maps = (MaterialMap *)materials[0].maps.ToPointer(); maps[(int)MAP_EMISSION].texture = texMask; int *locs = (int *)shader.locs.ToPointer(); locs[(int)LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask"); // Frame is incremented each frame to animate the shader int shaderFrame = GetShaderLocation(shader, "framesCounter"); // Apply the shader to the two models materials = (Material *)model1.materials.ToPointer(); materials[0].shader = shader; materials = (Material *)model2.materials.ToPointer(); materials[0].shader = shader; int framesCounter = 0; Vector3 rotation = new Vector3(0, 0, 0); // Model rotation angles SetTargetFPS(60); // Set to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- framesCounter++; rotation.X += 0.01f; rotation.Y += 0.005f; rotation.Z -= 0.0025f; // Send frames counter to shader for animation Utils.SetShaderValue(shader, shaderFrame, framesCounter, UNIFORM_INT); // Rotate one of the models model1.transform = MatrixRotateXYZ(rotation); UpdateCamera(ref camera); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(DARKBLUE); BeginMode3D(camera); DrawModel(model1, new Vector3(0.5f, 0, 0), 1, WHITE); DrawModelEx(model2, new Vector3(-.5f, 0, 0), new Vector3(1, 1, 0), 50, new Vector3(1, 1, 1), WHITE); DrawModel(model3, new Vector3(0, 0, -1.5f), 1, WHITE); DrawGrid(10, 1.0f); EndMode3D(); string frameText = $"Frame: {framesCounter}"; DrawRectangle(16, 698, MeasureText(frameText, 20) + 8, 42, BLUE); DrawText(frameText, 20, 700, 20, WHITE); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(model1); UnloadModel(model2); UnloadModel(model3); UnloadTexture(texDiffuse); // Unload default diffuse texture UnloadTexture(texMask); // Unload texture mask UnloadShader(shader); // Unload shader CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(); camera.position = new Vector3(2.0f, 2.0f, 6.0f); // Camera position camera.target = new Vector3(0.0f, 0.5f, 0.0f); // Camera looking at point camera.up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target) camera.fovy = 45.0f; // Camera field-of-view Y camera.type = CAMERA_PERSPECTIVE; // Camera mode type // Load models Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32)); Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32)); // Load models texture Texture2D texture = LoadTexture("resources/texel_checker.png"); // Assign texture to default model material Utils.SetMaterialTexture(ref modelA, 0, MAP_ALBEDO, ref texture); Utils.SetMaterialTexture(ref modelB, 0, MAP_ALBEDO, ref texture); Utils.SetMaterialTexture(ref modelC, 0, MAP_ALBEDO, ref texture); Shader shader = LoadShader("resources/shaders/glsl330/base_lighting.vs", "resources/shaders/glsl330/lighting.fs"); // Get some shader loactions int *locs = (int *)shader.locs.ToPointer(); locs[(int)LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel"); locs[(int)LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); // ambient light level int ambientLoc = GetShaderLocation(shader, "ambient"); Utils.SetShaderValue(shader, ambientLoc, new float[] { 0.2f, 0.2f, 0.2f, 1.0f }, ShaderUniformDataType.UNIFORM_VEC4); float angle = 6.282f; // All models use the same shader Utils.SetMaterialShader(ref modelA, 0, ref shader); Utils.SetMaterialShader(ref modelB, 0, ref shader); Utils.SetMaterialShader(ref modelC, 0, ref shader); // Using 4 point lights, white, red, green and blue Light[] lights = new Light[MAX_LIGHTS]; lights[0] = CreateLight(LightType.LIGHT_POINT, new Vector3(4, 2, 4), Vector3Zero(), WHITE, shader); lights[1] = CreateLight(LightType.LIGHT_POINT, new Vector3(4, 2, 4), Vector3Zero(), RED, shader); lights[2] = CreateLight(LightType.LIGHT_POINT, new Vector3(0, 4, 2), Vector3Zero(), GREEN, shader); lights[3] = CreateLight(LightType.LIGHT_POINT, new Vector3(0, 4, 2), Vector3Zero(), BLUE, shader); SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- if (IsKeyPressed(KEY_W)) { lights[0].enabled = !lights[0].enabled; } if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; } if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; } if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; } UpdateCamera(ref camera); // Update camera // Make the lights do differing orbits angle -= 0.02f; lights[0].position.X = (float)Math.Cos(angle) * 4.0f; lights[0].position.Z = (float)Math.Sin(angle) * 4.0f; lights[1].position.X = (float)Math.Cos(-angle * 0.6f) * 4.0f; lights[1].position.Z = (float)Math.Sin(-angle * 0.6f) * 4.0f; lights[2].position.Y = (float)Math.Cos(angle * 0.2f) * 4.0f; lights[2].position.Z = (float)Math.Sin(angle * 0.2f) * 4.0f; lights[3].position.Y = (float)Math.Cos(-angle * 0.35f) * 4.0f; lights[3].position.Z = (float)Math.Sin(-angle * 0.35f) * 4.0f; UpdateLightValues(shader, lights[0]); UpdateLightValues(shader, lights[1]); UpdateLightValues(shader, lights[2]); UpdateLightValues(shader, lights[3]); // Rotate the torus modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025f)); modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012f)); // Update the light shader with the camera view position float[] cameraPos = { camera.position.X, camera.position.Y, camera.position.Z }; Utils.SetShaderValue(shader, locs[(int)LOC_VECTOR_VIEW], cameraPos, ShaderUniformDataType.UNIFORM_VEC3); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); // Draw the three models DrawModel(modelA, Vector3Zero(), 1.0f, WHITE); DrawModel(modelB, new Vector3(-1.6f, 0, 0), 1.0f, WHITE); DrawModel(modelC, new Vector3(1.6f, 0, 0), 1.0f, WHITE); // Draw markers to show where the lights are if (lights[0].enabled) { DrawSphereEx(lights[0].position, 0.2f, 8, 8, WHITE); } if (lights[1].enabled) { DrawSphereEx(lights[1].position, 0.2f, 8, 8, RED); } if (lights[2].enabled) { DrawSphereEx(lights[2].position, 0.2f, 8, 8, GREEN); } if (lights[3].enabled) { DrawSphereEx(lights[3].position, 0.2f, 8, 8, BLUE); } DrawGrid(10, 1.0f); EndMode3D(); DrawFPS(10, 10); DrawText("Keys RGB & W toggle lights", 10, 30, 20, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(modelA); // Unload the modelA UnloadModel(modelB); // Unload the modelB UnloadModel(modelC); // Unload the modelC UnloadTexture(texture); // Unload the texture UnloadShader(shader); // Unload shader CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
// public const int GLSL_VERSION = 100; public static int Main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; SetConfigFlags(FLAG_WINDOW_RESIZABLE); InitWindow(screenWidth, screenHeight, "raylib [shaders] example - raymarching shapes"); Camera3D camera = new Camera3D(); camera.position = new Vector3(2.5f, 2.5f, 3.0f); // Camera position camera.target = new Vector3(0.0f, 0.0f, 0.7f); // Camera looking at point camera.up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target) camera.fovy = 65.0f; // Camera field-of-view Y SetCameraMode(camera, CAMERA_FREE); // Set camera mode // Load raymarching shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(null, string.Format("resources/shaders/glsl{0}/raymarching.fs", GLSL_VERSION)); // Get shader locations for required uniforms int viewEyeLoc = GetShaderLocation(shader, "viewEye"); int viewCenterLoc = GetShaderLocation(shader, "viewCenter"); int runTimeLoc = GetShaderLocation(shader, "runTime"); int resolutionLoc = GetShaderLocation(shader, "resolution"); float[] resolution = { (float)screenWidth, (float)screenHeight }; Utils.SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); float runTime = 0.0f; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Check if screen is resized //---------------------------------------------------------------------------------- if (IsWindowResized()) { screenWidth = GetScreenWidth(); screenHeight = GetScreenHeight(); resolution = new float[] { (float)screenWidth, (float)screenHeight }; Utils.SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); } // Update //---------------------------------------------------------------------------------- UpdateCamera(ref camera); // Update camera float[] cameraPos = { camera.position.X, camera.position.Y, camera.position.Z }; float[] cameraTarget = { camera.target.X, camera.target.Y, camera.target.Z }; float deltaTime = GetFrameTime(); runTime += deltaTime; // Set shader required uniform values Utils.SetShaderValue(shader, viewEyeLoc, cameraPos, UNIFORM_VEC3); Utils.SetShaderValue(shader, viewCenterLoc, cameraTarget, UNIFORM_VEC3); Utils.SetShaderValue(shader, runTimeLoc, runTime); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // We only draw a white full-screen rectangle, // frame is generated in shader using raymarching BeginShaderMode(shader); DrawRectangle(0, 0, screenWidth, screenHeight, WHITE); EndShaderMode(); DrawText("(c) Raymarching shader by Iñigo Quilez. MIT License.", screenWidth - 280, screenHeight - 20, 10, BLACK); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(4.0f, 1.0f, 4.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, CAMERA_PERSPECTIVE); // Load skybox model Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); Model skybox = LoadModelFromMesh(cube); // Load skybox shader and set required locations // NOTE: Some locations are automatically set at shader loading Shader shader = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs"); Utils.SetMaterialShader(ref skybox, 0, ref shader); Utils.SetShaderValue(shader, GetShaderLocation(shader, "environmentMap"), new int[] { (int)MAP_CUBEMAP }, UNIFORM_INT); // Load cubemap shader and setup required shader locations Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs"); Utils.SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), new int[] { 0 }, UNIFORM_INT); // Load HDR panorama (sphere) texture Texture2D texHDR = LoadTexture("resources/dresden_square.hdr"); // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture // NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, 512); Utils.SetMaterialTexture(ref skybox, 0, MAP_CUBEMAP, ref cubemap); UnloadTexture(texHDR); // Texture not required anymore, cubemap already generated UnloadShader(shdrCubemap); // Unload cubemap generation shader, not required anymore SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(ref camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(skybox, new Vector3(0, 0, 0), 1.0f, WHITE); DrawGrid(10, 1.0f); EndMode3D(); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(skybox); // Unload skybox model (and textures) CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia sets"); // Load julia set shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(null, string.Format("resources/shaders/glsl{0}/julia_set.fs", GLSL_VERSION)); // c constant to use in z^2 + c float[] c = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] }; // Offset and zoom to draw the julia set at. (centered on screen and default size) float[] offset = { -(float)screenWidth / 2, -(float)screenHeight / 2 }; float zoom = 1.0f; Vector2 offsetSpeed = new Vector2(0.0f, 0.0f); // Get variable (uniform) locations on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 int cLoc = GetShaderLocation(shader, "c"); int zoomLoc = GetShaderLocation(shader, "zoom"); int offsetLoc = GetShaderLocation(shader, "offset"); // Tell the shader what the screen dimensions, zoom, offset and c are float[] screenDims = { (float)screenWidth, (float)screenHeight }; Utils.SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, UNIFORM_VEC2); Utils.SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); Utils.SetShaderValue(shader, zoomLoc, zoomLoc); Utils.SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); int incrementSpeed = 0; // Multiplier of speed to change c value bool showControls = true; // Show controls bool pause = false; // Pause animation SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // Press [1 - 6] to reset c to a point of interest if (IsKeyPressed(KEY_ONE) || IsKeyPressed(KEY_TWO) || IsKeyPressed(KEY_THREE) || IsKeyPressed(KEY_FOUR) || IsKeyPressed(KEY_FIVE) || IsKeyPressed(KEY_SIX)) { if (IsKeyPressed(KEY_ONE)) { c[0] = POINTS_OF_INTEREST[0][0]; c[1] = POINTS_OF_INTEREST[0][1]; } else if (IsKeyPressed(KEY_TWO)) { c[0] = POINTS_OF_INTEREST[1][0]; c[1] = POINTS_OF_INTEREST[1][1]; } else if (IsKeyPressed(KEY_THREE)) { c[0] = POINTS_OF_INTEREST[2][0]; c[1] = POINTS_OF_INTEREST[2][1]; } else if (IsKeyPressed(KEY_FOUR)) { c[0] = POINTS_OF_INTEREST[3][0]; c[1] = POINTS_OF_INTEREST[3][1]; } else if (IsKeyPressed(KEY_FIVE)) { c[0] = POINTS_OF_INTEREST[4][0]; c[1] = POINTS_OF_INTEREST[4][1]; } else if (IsKeyPressed(KEY_SIX)) { c[0] = POINTS_OF_INTEREST[5][0]; c[1] = POINTS_OF_INTEREST[5][1]; } Utils.SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); } // Pause animation (c change) if (IsKeyPressed(KEY_SPACE)) { pause = !pause; } // Toggle whether or not to show controls if (IsKeyPressed(KEY_F1)) { showControls = !showControls; } if (!pause) { if (IsKeyPressed(KEY_RIGHT)) { incrementSpeed++; } else if (IsKeyPressed(KEY_LEFT)) { incrementSpeed--; } // TODO: The idea is to zoom and move around with mouse // Probably offset movement should be proportional to zoom level if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) || IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) { zoom += zoom * 0.003f; } if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { zoom -= zoom * 0.003f; } Vector2 mousePos = GetMousePosition(); offsetSpeed.X = mousePos.X - (float)screenWidth / 2; offsetSpeed.Y = mousePos.Y - (float)screenHeight / 2; // Slowly move camera to targetOffset offset[0] += GetFrameTime() * offsetSpeed.X * 0.8f; offset[1] += GetFrameTime() * offsetSpeed.Y * 0.8f; } else { offsetSpeed = new Vector2(0.0f, 0.0f); } Utils.SetShaderValue(shader, zoomLoc, zoom, UNIFORM_FLOAT); Utils.SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); // Increment c value with time float amount = GetFrameTime() * incrementSpeed * 0.0005f; c[0] += amount; c[1] += amount; Utils.SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(BLACK); // Clear the screen of the previous frame. // Using a render texture to draw Julia set BeginTextureMode(target); // Enable drawing to texture ClearBackground(BLACK); // Clear the render texture // Draw a rectangle in shader mode to be used as shader canvas // NOTE: Rectangle uses font white character texture coordinates, // so shader can not be applied here directly because input vertexTexCoord // do not represent full screen coordinates (space where want to apply shader) DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); EndTextureMode(); // Draw the saved texture and rendered julia set with shader // NOTE: We do not invert texture on Y, already considered inside shader BeginShaderMode(shader); DrawTexture(target.texture, 0, 0, WHITE); EndShaderMode(); if (showControls) { DrawText("Press Mouse buttons right/left to zoom in/out and move", 10, 15, 10, RAYWHITE); DrawText("Press KEY_F1 to toggle these controls", 10, 30, 10, RAYWHITE); DrawText("Press KEYS [1 - 6] to change point of interest", 10, 45, 10, RAYWHITE); DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, 60, 10, RAYWHITE); DrawText("Press KEY_SPACE to pause movement animation", 10, 75, 10, RAYWHITE); } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadRenderTexture(target); // Unload render texture CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(ConfigFlag.FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [models] example - pbr material"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(); camera.position = new Vector3(4.0f, 4.0f, 4.0f); camera.target = new Vector3(0.0f, 0.5f, 0.0f); camera.up = new Vector3(0.0f, 1.0f, 0.0f); camera.fovy = 45.0f; camera.type = CAMERA_PERSPECTIVE; // Load model and PBR material Model model = LoadModel("resources/pbr/trooper.obj"); // Unsafe pointers into model arrays. Material *materials = (Material *)model.materials.ToPointer(); Mesh * meshes = (Mesh *)model.meshes.ToPointer(); materials[0] = LoadMaterialPBR(new Color(255, 255, 255, 255), 1.0f, 1.0f); // Define lights attributes // NOTE: Shader is passed to every light on creation to define shader bindings internally CreateLight(0, LightType.LIGHT_POINT, new Vector3(LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), new Color(255, 0, 0, 255), materials[0].shader); CreateLight(1, LightType.LIGHT_POINT, new Vector3(0.0f, LIGHT_HEIGHT, LIGHT_DISTANCE), new Vector3(0.0f, 0.0f, 0.0f), new Color(0, 255, 0, 255), materials[0].shader); CreateLight(2, LightType.LIGHT_POINT, new Vector3(-LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), new Color(0, 0, 255, 255), materials[0].shader); CreateLight(3, LightType.LIGHT_DIRECTIONAL, new Vector3(0.0f, LIGHT_HEIGHT * 2.0f, -LIGHT_DISTANCE), new Vector3(0.0f, 0.0f, 0.0f), new Color(255, 0, 255, 255), materials[0].shader); SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(ref camera); // Update camera // Send to material PBR shader camera view position float[] cameraPos = { camera.position.X, camera.position.Y, camera.position.Z }; int * locs = (int *)materials[0].shader.locs.ToPointer(); Utils.SetShaderValue(materials[0].shader, (int)ShaderLocationIndex.LOC_VECTOR_VIEW, cameraPos, ShaderUniformDataType.UNIFORM_VEC3); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, Vector3.Zero, 1.0f, WHITE); DrawGrid(10, 1.0f); EndMode3D(); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadMaterial(materials[0]); // Unload material: shader and textures UnloadModel(model); // Unload model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public static int Main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [shaders] example - hot reloading"); string fragShaderFileName = "resources/shaders/glsl330/reload.fs"; long fragShaderFileModTime = GetFileModTime(fragShaderFileName); // Load raymarching shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(null, fragShaderFileName); // Get shader locations for required uniforms int resolutionLoc = GetShaderLocation(shader, "resolution"); int mouseLoc = GetShaderLocation(shader, "mouse"); int timeLoc = GetShaderLocation(shader, "time"); float[] resolution = new[] { (float)screenWidth, (float)screenHeight }; Utils.SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); float totalTime = 0.0f; bool shaderAutoReloading = false; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- totalTime += GetFrameTime(); Vector2 mouse = GetMousePosition(); float[] mousePos = new[] { mouse.X, mouse.Y }; // Set shader required uniform values SetShaderValue(shader, timeLoc, ref totalTime, UNIFORM_FLOAT); Utils.SetShaderValue(shader, mouseLoc, mousePos, UNIFORM_VEC2); // Hot shader reloading if (shaderAutoReloading || (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) { long currentFragShaderModTime = GetFileModTime(fragShaderFileName); // Check if shader file has been modified if (currentFragShaderModTime != fragShaderFileModTime) { // Try reloading updated shader Shader updatedShader = LoadShader(null, fragShaderFileName); if (updatedShader.id != GetShaderDefault().id) // It was correctly loaded { UnloadShader(shader); shader = updatedShader; // Get shader locations for required uniforms resolutionLoc = GetShaderLocation(shader, "resolution"); mouseLoc = GetShaderLocation(shader, "mouse"); timeLoc = GetShaderLocation(shader, "time"); // Reset required uniforms Utils.SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); } fragShaderFileModTime = currentFragShaderModTime; } } if (IsKeyPressed(KEY_A)) { shaderAutoReloading = !shaderAutoReloading; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // We only draw a white full-screen rectangle, frame is generated in shader BeginShaderMode(shader); DrawRectangle(0, 0, screenWidth, screenHeight, WHITE); EndShaderMode(); string info = $"PRESS [A] to TOGGLE SHADER AUTOLOADING: {(shaderAutoReloading ? "AUTO" : "MANUAL")}"; DrawText(info, 10, 10, 10, shaderAutoReloading ? RED : BLACK); if (!shaderAutoReloading) { DrawText("MOUSE CLICK to SHADER RE-LOADING", 10, 30, 10, BLACK); } // DrawText($"Shader last modification: ", 10, 430, 10, BLACK); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture waves"); // Load texture texture to apply shaders Texture2D texture = LoadTexture("resources/space.png"); // Load shader and setup location points and values Shader shader = LoadShader(null, string.Format("resources/shaders/glsl{0}/wave.fs", GLSL_VERSION)); int secondsLoc = GetShaderLocation(shader, "secondes"); int freqXLoc = GetShaderLocation(shader, "freqX"); int freqYLoc = GetShaderLocation(shader, "freqY"); int ampXLoc = GetShaderLocation(shader, "ampX"); int ampYLoc = GetShaderLocation(shader, "ampY"); int speedXLoc = GetShaderLocation(shader, "speedX"); int speedYLoc = GetShaderLocation(shader, "speedY"); // Shader uniform values that can be updated at any time float freqX = 25.0f; float freqY = 25.0f; float ampX = 5.0f; float ampY = 5.0f; float speedX = 8.0f; float speedY = 8.0f; float[] screenSize = { (float)GetScreenWidth(), (float)GetScreenHeight() }; Utils.SetShaderValue(shader, GetShaderLocation(shader, "size"), screenSize, UNIFORM_VEC2); Utils.SetShaderValue(shader, freqXLoc, freqX, UNIFORM_FLOAT); Utils.SetShaderValue(shader, freqYLoc, freqY, UNIFORM_FLOAT); Utils.SetShaderValue(shader, ampXLoc, ampX, UNIFORM_FLOAT); Utils.SetShaderValue(shader, ampYLoc, ampY, UNIFORM_FLOAT); Utils.SetShaderValue(shader, speedXLoc, speedX, UNIFORM_FLOAT); Utils.SetShaderValue(shader, speedYLoc, speedY, UNIFORM_FLOAT); float seconds = 0.0f; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //------------------------------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- seconds += GetFrameTime(); Utils.SetShaderValue(shader, secondsLoc, seconds, UNIFORM_FLOAT); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginShaderMode(shader); DrawTexture(texture, 0, 0, WHITE); DrawTexture(texture, texture.width, 0, WHITE); EndShaderMode(); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(texture); // Unload texture CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(4.0f, 1.0f, 4.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, CAMERA_PERSPECTIVE); // Load skybox model Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); Model skybox = LoadModelFromMesh(cube); // Load skybox shader and set required locations // NOTE: Some locations are automatically set at shader loading Shader shader = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs"); Utils.SetMaterialShader(ref skybox, 0, ref shader); Utils.SetShaderValue(shader, GetShaderLocation(shader, "environmentMap"), new int[] { (int)MAP_CUBEMAP }, UNIFORM_INT); Utils.SetShaderValue(shader, GetShaderLocation(shader, "vflipped"), new int[] { 1 }, UNIFORM_INT); // Load cubemap shader and setup required shader locations Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs"); Utils.SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), new int[] { 0 }, UNIFORM_INT); // Load HDR panorama (sphere) texture string panoFileName = "resources/dresden_square_2k.hdr"; Texture2D panorama = LoadTexture(panoFileName); // Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture // NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping Texture2D cubemap = GenTextureCubemap(shdrCubemap, panorama, 1024, PixelFormat.UNCOMPRESSED_R8G8B8A8); Utils.SetMaterialTexture(ref skybox, 0, MAP_CUBEMAP, ref cubemap); UnloadTexture(panorama); // Texture not required anymore, cubemap already generated SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(ref camera); // Update camera // Load new cubemap texture on drag&drop if (IsFileDropped()) { int count = 0; string[] droppedFiles = Utils.MarshalDroppedFiles(ref count); // Only support one file dropped if (count == 1) { if (IsFileExtension(droppedFiles[0], ".png;.jpg;.hdr;.bmp;.tga")) { // Unload current cubemap texture and load new one UnloadTexture(Utils.GetMaterialTexture(ref skybox, 0, MAP_CUBEMAP)); panorama = LoadTexture(droppedFiles[0]); panoFileName = droppedFiles[0]; // Generate cubemap from panorama texture cubemap = GenTextureCubemap(shdrCubemap, panorama, 1024, PixelFormat.UNCOMPRESSED_R8G8B8A8); Utils.SetMaterialTexture(ref skybox, 0, MAP_CUBEMAP, ref cubemap); UnloadTexture(panorama); } } // Clear internal buffers ClearDroppedFiles(); } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(skybox, new Vector3(0, 0, 0), 1.0f, WHITE); DrawGrid(10, 1.0f); EndMode3D(); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(skybox); // Unload skybox model (and textures) CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }