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.

92 lines
4.0 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. 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 = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  29. Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  30. Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture
  31. Shader shader = LoadShader("resources/shaders/glsl330/base.vs",
  32. "resources/shaders/glsl330/grayscale.fs"); // Load model shader
  33. dwarf.material.shader = shader; // Set shader effect to 3d model
  34. dwarf.material.texDiffuse = texture; // Bind texture to model
  35. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  36. SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode
  37. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  38. //--------------------------------------------------------------------------------------
  39. // Main game loop
  40. while (!WindowShouldClose()) // Detect window close button or ESC key
  41. {
  42. // Update
  43. //----------------------------------------------------------------------------------
  44. UpdateCamera(&camera); // Update camera
  45. //----------------------------------------------------------------------------------
  46. // Draw
  47. //----------------------------------------------------------------------------------
  48. BeginDrawing();
  49. ClearBackground(RAYWHITE);
  50. Begin3dMode(camera);
  51. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  52. DrawGrid(10, 1.0f); // Draw a grid
  53. End3dMode();
  54. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  55. DrawText(FormatText("Camera position: (%.2f, %.2f, %.2f)", camera.position.x, camera.position.y, camera.position.z), 600, 20, 10, BLACK);
  56. DrawText(FormatText("Camera target: (%.2f, %.2f, %.2f)", camera.target.x, camera.target.y, camera.target.z), 600, 40, 10, GRAY);
  57. DrawFPS(10, 10);
  58. EndDrawing();
  59. //----------------------------------------------------------------------------------
  60. }
  61. // De-Initialization
  62. //--------------------------------------------------------------------------------------
  63. UnloadShader(shader); // Unload shader
  64. UnloadTexture(texture); // Unload texture
  65. UnloadModel(dwarf); // Unload model
  66. CloseWindow(); // Close window and OpenGL context
  67. //--------------------------------------------------------------------------------------
  68. return 0;
  69. }