2
0

shaders_model_shader.c 4.4 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. int main()
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. int screenWidth = 800;
  29. 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.type = 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, FormatText("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[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. DrawText(FormatText("Camera position: (%.2f, %.2f, %.2f)", camera.position.x, camera.position.y, camera.position.z), 600, 20, 10, BLACK);
  67. DrawText(FormatText("Camera target: (%.2f, %.2f, %.2f)", camera.target.x, camera.target.y, camera.target.z), 600, 40, 10, GRAY);
  68. DrawFPS(10, 10);
  69. EndDrawing();
  70. //----------------------------------------------------------------------------------
  71. }
  72. // De-Initialization
  73. //--------------------------------------------------------------------------------------
  74. UnloadShader(shader); // Unload shader
  75. UnloadTexture(texture); // Unload texture
  76. UnloadModel(model); // Unload model
  77. CloseWindow(); // Close window and OpenGL context
  78. //--------------------------------------------------------------------------------------
  79. return 0;
  80. }