You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Model shader
  4. *
  5. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  6. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
  7. *
  8. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
  9. * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
  10. * raylib comes with shaders ready for both versions, check raylib/shaders install folder
  11. *
  12. * Example originally created with raylib 1.3, last time updated with raylib 3.7
  13. *
  14. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  15. * BSD-like license that allows static linking with closed source software
  16. *
  17. * Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
  18. *
  19. ********************************************************************************************/
  20. #include "raylib.h"
  21. #if defined(PLATFORM_DESKTOP)
  22. #define GLSL_VERSION 330
  23. #else // PLATFORM_ANDROID, PLATFORM_WEB
  24. #define GLSL_VERSION 100
  25. #endif
  26. //------------------------------------------------------------------------------------
  27. // Program main entry point
  28. //------------------------------------------------------------------------------------
  29. int main(void)
  30. {
  31. // Initialization
  32. //--------------------------------------------------------------------------------------
  33. const int screenWidth = 800;
  34. const int screenHeight = 450;
  35. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  36. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
  37. // Define the camera to look into our 3d world
  38. Camera camera = { 0 };
  39. camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; // Camera position
  40. camera.target = (Vector3){ 0.0f, 1.0f, -1.0f }; // Camera looking at point
  41. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  42. camera.fovy = 45.0f; // Camera field-of-view Y
  43. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  44. Model model = LoadModel("resources/models/watermill.obj"); // Load OBJ model
  45. Texture2D texture = LoadTexture("resources/models/watermill_diffuse.png"); // Load model texture
  46. // Load shader for model
  47. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  48. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION));
  49. model.materials[0].shader = shader; // Set shader effect to 3d model
  50. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Bind texture to model
  51. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  52. DisableCursor(); // Limit cursor to relative movement inside the window
  53. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  54. //--------------------------------------------------------------------------------------
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. // Update
  59. //----------------------------------------------------------------------------------
  60. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. BeginMode3D(camera);
  67. DrawModel(model, position, 0.2f, WHITE); // Draw 3d model with texture
  68. DrawGrid(10, 1.0f); // Draw a grid
  69. EndMode3D();
  70. DrawText("(c) Watermill 3D model by Alberto Cano", screenWidth - 210, screenHeight - 20, 10, GRAY);
  71. DrawFPS(10, 10);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. UnloadShader(shader); // Unload shader
  78. UnloadTexture(texture); // Unload texture
  79. UnloadModel(model); // Unload model
  80. CloseWindow(); // Close window and OpenGL context
  81. //--------------------------------------------------------------------------------------
  82. return 0;
  83. }