Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

108 rader
4.6 KiB

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