1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shaders] example - Raymarching shapes generation |
4 | * |
5 | * NOTE: This example requires raylib OpenGL 3.3 for shaders support and only #version 330 |
6 | * is currently supported. OpenGL ES 2.0 platforms are not supported at the moment. |
7 | * |
8 | * This example has been created using raylib 2.0 (www.raylib.com) |
9 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
10 | * |
11 | * Copyright (c) 2018 Ramon Santamaria (@raysan5) |
12 | * |
13 | ********************************************************************************************/ |
14 | |
15 | #include "raylib.h" |
16 | |
17 | #if defined(PLATFORM_DESKTOP) |
18 | #define GLSL_VERSION 330 |
19 | #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB -> Not supported at this moment |
20 | #define GLSL_VERSION 100 |
21 | #endif |
22 | |
23 | int main(void) |
24 | { |
25 | // Initialization |
26 | //-------------------------------------------------------------------------------------- |
27 | int screenWidth = 800; |
28 | int screenHeight = 450; |
29 | |
30 | SetConfigFlags(FLAG_WINDOW_RESIZABLE); |
31 | InitWindow(screenWidth, screenHeight, "raylib [shaders] example - raymarching shapes" ); |
32 | |
33 | Camera camera = { 0 }; |
34 | camera.position = (Vector3){ 2.5f, 2.5f, 3.0f }; // Camera position |
35 | camera.target = (Vector3){ 0.0f, 0.0f, 0.7f }; // Camera looking at point |
36 | camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) |
37 | camera.fovy = 65.0f; // Camera field-of-view Y |
38 | |
39 | SetCameraMode(camera, CAMERA_FREE); // Set camera mode |
40 | |
41 | // Load raymarching shader |
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/raymarching.fs" , GLSL_VERSION)); |
44 | |
45 | // Get shader locations for required uniforms |
46 | int viewEyeLoc = GetShaderLocation(shader, "viewEye" ); |
47 | int viewCenterLoc = GetShaderLocation(shader, "viewCenter" ); |
48 | int runTimeLoc = GetShaderLocation(shader, "runTime" ); |
49 | int resolutionLoc = GetShaderLocation(shader, "resolution" ); |
50 | |
51 | float resolution[2] = { (float)screenWidth, (float)screenHeight }; |
52 | SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); |
53 | |
54 | float runTime = 0.0f; |
55 | |
56 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
57 | //-------------------------------------------------------------------------------------- |
58 | |
59 | // Main game loop |
60 | while (!WindowShouldClose()) // Detect window close button or ESC key |
61 | { |
62 | // Check if screen is resized |
63 | //---------------------------------------------------------------------------------- |
64 | if(IsWindowResized()) |
65 | { |
66 | screenWidth = GetScreenWidth(); |
67 | screenHeight = GetScreenHeight(); |
68 | float resolution[2] = { (float)screenWidth, (float)screenHeight }; |
69 | SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); |
70 | } |
71 | |
72 | // Update |
73 | //---------------------------------------------------------------------------------- |
74 | UpdateCamera(&camera); // Update camera |
75 | |
76 | float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; |
77 | float cameraTarget[3] = { camera.target.x, camera.target.y, camera.target.z }; |
78 | |
79 | float deltaTime = GetFrameTime(); |
80 | runTime += deltaTime; |
81 | |
82 | // Set shader required uniform values |
83 | SetShaderValue(shader, viewEyeLoc, cameraPos, UNIFORM_VEC3); |
84 | SetShaderValue(shader, viewCenterLoc, cameraTarget, UNIFORM_VEC3); |
85 | SetShaderValue(shader, runTimeLoc, &runTime, UNIFORM_FLOAT); |
86 | //---------------------------------------------------------------------------------- |
87 | |
88 | // Draw |
89 | //---------------------------------------------------------------------------------- |
90 | BeginDrawing(); |
91 | |
92 | ClearBackground(RAYWHITE); |
93 | |
94 | // We only draw a white full-screen rectangle, |
95 | // frame is generated in shader using raymarching |
96 | BeginShaderMode(shader); |
97 | DrawRectangle(0, 0, screenWidth, screenHeight, WHITE); |
98 | EndShaderMode(); |
99 | |
100 | DrawText("(c) Raymarching shader by IƱigo Quilez. MIT License." , screenWidth - 280, screenHeight - 20, 10, BLACK); |
101 | |
102 | EndDrawing(); |
103 | //---------------------------------------------------------------------------------- |
104 | } |
105 | |
106 | // De-Initialization |
107 | //-------------------------------------------------------------------------------------- |
108 | UnloadShader(shader); // Unload shader |
109 | |
110 | CloseWindow(); // Close window and OpenGL context |
111 | //-------------------------------------------------------------------------------------- |
112 | |
113 | return 0; |
114 | } |
115 | |