public static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [text] example - font filters"); string msg = "Loaded Font"; // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) // TTF Font loading with custom generation parameters Font font = LoadFontEx("resources/KAISG.ttf", 96, null, 0); // Generate mipmap levels to use trilinear filtering // NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR GenTextureMipmaps(ref font.texture); float fontSize = font.baseSize; Vector2 fontPosition = new Vector2(40, screenHeight / 2 - 80); Vector2 textSize = new Vector2(0.0f, 0.0f); // Setup texture scaling filter SetTextureFilter(font.texture, FILTER_POINT); TextureFilterMode currentFontFilter = FILTER_POINT; 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 //---------------------------------------------------------------------------------- fontSize += GetMouseWheelMove() * 4.0f; // Choose font texture filter method if (IsKeyPressed(KEY_ONE)) { SetTextureFilter(font.texture, FILTER_POINT); currentFontFilter = FILTER_POINT; } else if (IsKeyPressed(KEY_TWO)) { SetTextureFilter(font.texture, FILTER_BILINEAR); currentFontFilter = FILTER_BILINEAR; } else if (IsKeyPressed(KEY_THREE)) { // NOTE: Trilinear filter won't be noticed on 2D drawing SetTextureFilter(font.texture, FILTER_TRILINEAR); currentFontFilter = FILTER_TRILINEAR; } textSize = MeasureTextEx(font, msg, fontSize, 0); if (IsKeyDown(KEY_LEFT)) { fontPosition.X -= 10; } else if (IsKeyDown(KEY_RIGHT)) { fontPosition.X += 10; } // Load a dropped TTF file dynamically (at current fontSize) if (IsFileDropped()) { int count = 0; string[] droppedFiles = Utils.MarshalDroppedFiles(ref count); // NOTE: We only support first ttf file dropped if (IsFileExtension(droppedFiles[0], ".ttf")) { UnloadFont(font); font = LoadFontEx(droppedFiles[0], (int)fontSize, null, 0); } ClearDroppedFiles(); } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY); DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY); DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY); DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY); DrawTextEx(font, msg, fontPosition, fontSize, 0, BLACK); DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY); DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY); if (currentFontFilter == FILTER_POINT) { DrawText("POINT", 570, 400, 20, BLACK); } else if (currentFontFilter == FILTER_POINT) { DrawText("BILINEAR", 570, 400, 20, BLACK); } else if (currentFontFilter == FILTER_TRILINEAR) { DrawText("TRILINEAR", 570, 400, 20, BLACK); } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadFont(font); // Font unloading 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 - models loading"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(); camera.position = new Vector3(50.0f, 50.0f, 50.0f); // Camera position camera.target = new Vector3(0.0f, 10.0f, 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 Model model = LoadModel("resources/models/castle.obj"); // Load model Texture2D texture = LoadTexture("resources/models/castle_diffuse.png"); // Load model texture // Set map diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); // Set model position Mesh * meshes = (Mesh *)model.meshes.ToPointer(); BoundingBox bounds = MeshBoundingBox(meshes[0]); // Set model bounds // NOTE: bounds are calculated from the original size of the model, // if model is scaled on drawing, bounds must be also scaled SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode bool selected = false; // Selected object flag 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); // Load new models/textures on dragref if (IsFileDropped()) { int count = 0; string[] droppedFiles = Utils.MarshalDroppedFiles(ref count); if (count == 1) // Only support one file dropped { if (IsFileExtension(droppedFiles[0], ".obj") || IsFileExtension(droppedFiles[0], ".gltf") || IsFileExtension(droppedFiles[0], ".iqm")) // Model file formats supported { UnloadModel(model); // Unload previous model model = LoadModel(droppedFiles[0]); // Load new model // Set current map diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); meshes = (Mesh *)model.meshes.ToPointer(); bounds = MeshBoundingBox(meshes[0]); // TODO: Move camera position from target enough distance to visualize model properly } else if (IsFileExtension(droppedFiles[0], ".png")) // Texture file formats supported { // Unload current model texture and load new one UnloadTexture(texture); texture = LoadTexture(droppedFiles[0]); Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); } } ClearDroppedFiles(); // Clear internal buffers } // Select model on mouse click if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { // Check collision between ray and box if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) { selected = !selected; } else { selected = false; } } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, position, 1.0f, WHITE); // Draw 3d model with texture DrawGrid(20, 10.0f); // Draw a grid if (selected) { DrawBoundingBox(bounds, GREEN); // Draw selection box } EndMode3D(); DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY); if (selected) { DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN); } DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(texture); // Unload texture UnloadModel(model); // Unload model 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 [core] example - drop files"); int count = 0; string[] droppedFiles = {}; SetTargetFPS(60); //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- if (IsFileDropped()) { droppedFiles = Utils.MarshalDroppedFiles(ref count); } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); if (count == 0) { DrawText("Drop your files to this window!", 100, 40, 20, DARKGRAY); } else { DrawText("Dropped files:", 100, 40, 20, DARKGRAY); for (int i = 0; i < count; i++) { if (i % 2 == 0) { DrawRectangle(0, 85 + 40 * i, screenWidth, 40, Fade(LIGHTGRAY, 0.5f)); } else { DrawRectangle(0, 85 + 40 * i, screenWidth, 40, Fade(LIGHTGRAY, 0.3f)); } DrawText(droppedFiles[i], 120, 100 + 40 * i, 10, GRAY); } DrawText("Drop new files...", 100, 110 + 40 * count, 20, DARKGRAY); } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- ClearDroppedFiles(); // Clear internal buffers 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); }