shaders_model_shader.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply a shader to a 3d model (adapted for HTML5 platform)
  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_WEB)
  20. #include <emscripten/emscripten.h>
  21. #endif
  22. //----------------------------------------------------------------------------------
  23. // Global Variables Definition
  24. //----------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  27. // Define the camera to look into our 3d world
  28. Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  29. Model dwarf; // OBJ model
  30. Texture2D texture; // Model texture
  31. Shader shader; // Postpro shader
  32. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  33. //----------------------------------------------------------------------------------
  34. // Module Functions Declaration
  35. //----------------------------------------------------------------------------------
  36. void UpdateDrawFrame(void); // Update and Draw one frame
  37. //----------------------------------------------------------------------------------
  38. // Main Enry Point
  39. //----------------------------------------------------------------------------------
  40. int main()
  41. {
  42. // Initialization
  43. //--------------------------------------------------------------------------------------
  44. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  45. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
  46. dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  47. texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture
  48. shader = LoadShader("resources/shaders/glsl100/base.vs",
  49. "resources/shaders/glsl100/grayscale.fs"); // Load model shader
  50. dwarf.material.shader = shader; // Set shader effect to 3d model
  51. dwarf.material.texDiffuse = texture; // Bind texture to model
  52. // Setup orbital camera
  53. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  54. #if defined(PLATFORM_WEB)
  55. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  56. #else
  57. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  58. //--------------------------------------------------------------------------------------
  59. // Main game loop
  60. while (!WindowShouldClose()) // Detect window close button or ESC key
  61. {
  62. UpdateDrawFrame();
  63. }
  64. #endif
  65. // De-Initialization
  66. //--------------------------------------------------------------------------------------
  67. UnloadShader(shader); // Unload shader
  68. UnloadTexture(texture); // Unload texture
  69. UnloadModel(dwarf); // Unload model
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }
  74. //----------------------------------------------------------------------------------
  75. // Module Functions Definition
  76. //----------------------------------------------------------------------------------
  77. void UpdateDrawFrame(void)
  78. {
  79. // Update
  80. //----------------------------------------------------------------------------------
  81. UpdateCamera(&camera); // Update internal camera and our camera
  82. //----------------------------------------------------------------------------------
  83. // Draw
  84. //----------------------------------------------------------------------------------
  85. BeginDrawing();
  86. ClearBackground(RAYWHITE);
  87. Begin3dMode(camera);
  88. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  89. DrawGrid(10, 1.0f); // Draw a grid
  90. End3dMode();
  91. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  92. DrawFPS(10, 10);
  93. EndDrawing();
  94. //----------------------------------------------------------------------------------
  95. }