2
0

shaders_model_shader.c 4.2 KB

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