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.

102 lines
4.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply a shader to a 3d model
  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. * This example has been created using raylib 1.3 (www.raylib.com)
  13. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  14. *
  15. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #if defined(PLATFORM_DESKTOP)
  20. #define GLSL_VERSION 330
  21. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  22. #define GLSL_VERSION 100
  23. #endif
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  31. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
  32. // Define the camera to look into our 3d world
  33. Camera camera = { 0 };
  34. camera.position = (Vector3){ 4.0f, 4.0f, 4.0f };
  35. camera.target = (Vector3){ 0.0f, 1.0f, -1.0f };
  36. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  37. camera.fovy = 45.0f;
  38. camera.projection = CAMERA_PERSPECTIVE;
  39. Model model = LoadModel("resources/models/watermill.obj"); // Load OBJ model
  40. Texture2D texture = LoadTexture("resources/models/watermill_diffuse.png"); // Load model texture
  41. // Load shader for model
  42. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  43. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION));
  44. model.materials[0].shader = shader; // Set shader effect to 3d model
  45. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Bind texture to model
  46. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  47. SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. UpdateCamera(&camera); // Update camera
  56. //----------------------------------------------------------------------------------
  57. // Draw
  58. //----------------------------------------------------------------------------------
  59. BeginDrawing();
  60. ClearBackground(RAYWHITE);
  61. BeginMode3D(camera);
  62. DrawModel(model, position, 0.2f, WHITE); // Draw 3d model with texture
  63. DrawGrid(10, 1.0f); // Draw a grid
  64. EndMode3D();
  65. DrawText("(c) Watermill 3D model by Alberto Cano", screenWidth - 210, screenHeight - 20, 10, GRAY);
  66. DrawFPS(10, 10);
  67. EndDrawing();
  68. //----------------------------------------------------------------------------------
  69. }
  70. // De-Initialization
  71. //--------------------------------------------------------------------------------------
  72. UnloadShader(shader); // Unload shader
  73. UnloadTexture(texture); // Unload texture
  74. UnloadModel(model); // Unload model
  75. CloseWindow(); // Close window and OpenGL context
  76. //--------------------------------------------------------------------------------------
  77. return 0;
  78. }