shaders_model_shader.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  34. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
  35. // Define the camera to look into our 3d world
  36. Camera camera = { 0 };
  37. camera.position = (Vector3){ 4.0f, 4.0f, 4.0f };
  38. camera.target = (Vector3){ 0.0f, 1.0f, -1.0f };
  39. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  40. camera.fovy = 45.0f;
  41. camera.projection = CAMERA_PERSPECTIVE;
  42. Model model = LoadModel("resources/models/watermill.obj"); // Load OBJ model
  43. Texture2D texture = LoadTexture("resources/models/watermill_diffuse.png"); // Load model texture
  44. // Load shader for model
  45. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  46. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION));
  47. model.materials[0].shader = shader; // Set shader effect to 3d model
  48. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Bind texture to model
  49. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  50. SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. UpdateCamera(&camera); // Update camera
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. BeginMode3D(camera);
  65. DrawModel(model, position, 0.2f, WHITE); // Draw 3d model with texture
  66. DrawGrid(10, 1.0f); // Draw a grid
  67. EndMode3D();
  68. DrawText("(c) Watermill 3D model by Alberto Cano", screenWidth - 210, screenHeight - 20, 10, GRAY);
  69. DrawFPS(10, 10);
  70. EndDrawing();
  71. //----------------------------------------------------------------------------------
  72. }
  73. // De-Initialization
  74. //--------------------------------------------------------------------------------------
  75. UnloadShader(shader); // Unload shader
  76. UnloadTexture(texture); // Unload texture
  77. UnloadModel(model); // Unload model
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }