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(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 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 [shaders] example - model shader"); // 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, 1.0f, -1.0f); camera.up = new Vector3(0.0f, 1.0f, 0.0f); camera.fovy = 45.0f; camera.type = CAMERA_PERSPECTIVE; Model model = LoadModel("resources/models/watermill.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/watermill_diffuse.png"); // Load model texture Shader shader = LoadShader("resources/shaders/glsl330/base.vs", "resources/shaders/glsl330/grayscale.fs"); // Load model shader Utils.SetMaterialShader(ref model, 0, ref shader); // Set shader effect to 3d model Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); // Bind texture to model Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); // Set model position SetCameraMode(camera, CAMERA_FREE); // 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 //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, position, 0.2f, WHITE); // Draw 3d model with texture DrawGrid(10, 1.0f); // Draw a grid EndMode3D(); DrawText("(c) Watermill 3D model by Alberto Cano", screenWidth - 210, screenHeight - 20, 10, GRAY); DrawText(string.Format("Camera3D position: ({0:0.00}, {0:0.00}, {0:0.00})", camera.position.X, camera.position.Y, camera.position.Z), 600, 20, 10, BLACK); DrawText(string.Format("Camera3D target: ({0:0.00}, {0:0.00}, {0:0.00})", camera.target.X, camera.target.Y, camera.target.Z), 600, 40, 10, GRAY); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(texture); // Unload texture UnloadModel(model); // Unload model 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 [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); }