1/*******************************************************************************************
2*
3* raylib [shaders] example - Simple shader mask
4*
5* This example has been created using raylib 2.5 (www.raylib.com)
6* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7*
8* Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
9*
10* Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
11*
12********************************************************************************************
13*
14* After a model is loaded it has a default material, this material can be
15* modified in place rather than creating one from scratch...
16* While all of the maps have particular names, they can be used for any purpose
17* except for three maps that are applied as cubic maps (see below)
18*
19********************************************************************************************/
20
21#include "raylib.h"
22#include "raymath.h"
23
24#if defined(PLATFORM_DESKTOP)
25 #define GLSL_VERSION 330
26#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
27 #define GLSL_VERSION 100
28#endif
29
30int main(void)
31{
32 // Initialization
33 //--------------------------------------------------------------------------------------
34 const int screenWidth = 800;
35 const int screenHeight = 450;
36
37 InitWindow(screenWidth, screenHeight, "raylib - simple shader mask");
38
39 // Define the camera to look into our 3d world
40 Camera camera = { 0 };
41 camera.position = (Vector3){ 0.0f, 1.0f, 2.0f };
42 camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
43 camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
44 camera.fovy = 45.0f;
45 camera.type = CAMERA_PERSPECTIVE;
46
47 // Define our three models to show the shader on
48 Mesh torus = GenMeshTorus(.3, 1, 16, 32);
49 Model model1 = LoadModelFromMesh(torus);
50
51 Mesh cube = GenMeshCube(.8,.8,.8);
52 Model model2 = LoadModelFromMesh(cube);
53
54 // Generate model to be shaded just to see the gaps in the other two
55 Mesh sphere = GenMeshSphere(1, 16, 16);
56 Model model3 = LoadModelFromMesh(sphere);
57
58 // Load the shader
59 Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/mask.fs", GLSL_VERSION));
60
61 // Load and apply the diffuse texture (colour map)
62 Texture texDiffuse = LoadTexture("resources/plasma.png");
63 model1.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
64 model2.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
65
66 // Using MAP_EMISSION as a spare slot to use for 2nd texture
67 // NOTE: Don't use MAP_IRRADIANCE, MAP_PREFILTER or MAP_CUBEMAP
68 // as they are bound as cube maps
69 Texture texMask = LoadTexture("resources/mask.png");
70 model1.materials[0].maps[MAP_EMISSION].texture = texMask;
71 model2.materials[0].maps[MAP_EMISSION].texture = texMask;
72 shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask");
73
74 // Frame is incremented each frame to animate the shader
75 int shaderFrame = GetShaderLocation(shader, "frame");
76
77 // Apply the shader to the two models
78 model1.materials[0].shader = shader;
79 model2.materials[0].shader = shader;
80
81 int framesCounter = 0;
82 Vector3 rotation = { 0 }; // Model rotation angles
83
84 SetTargetFPS(60); // Set to run at 60 frames-per-second
85 //--------------------------------------------------------------------------------------
86
87 // Main game loop
88 while (!WindowShouldClose()) // Detect window close button or ESC key
89 {
90 // Update
91 //----------------------------------------------------------------------------------
92 framesCounter++;
93 rotation.x += 0.01f;
94 rotation.y += 0.005f;
95 rotation.z -= 0.0025f;
96
97 // Send frames counter to shader for animation
98 SetShaderValue(shader, shaderFrame, &framesCounter, UNIFORM_INT);
99
100 // Rotate one of the models
101 model1.transform = MatrixRotateXYZ(rotation);
102
103 UpdateCamera(&camera);
104 //----------------------------------------------------------------------------------
105
106 // Draw
107 //----------------------------------------------------------------------------------
108 BeginDrawing();
109
110 ClearBackground(DARKBLUE);
111
112 BeginMode3D(camera);
113
114 DrawModel(model1, (Vector3){0.5,0,0}, 1, WHITE);
115 DrawModelEx(model2, (Vector3){-.5,0,0}, (Vector3){1,1,0}, 50, (Vector3){1,1,1}, WHITE);
116 DrawModel(model3,(Vector3){0,0,-1.5}, 1, WHITE);
117 DrawGrid(10, 1.0f); // Draw a grid
118
119 EndMode3D();
120
121 DrawRectangle(16, 698, MeasureText(FormatText("Frame: %i", framesCounter), 20) + 8, 42, BLUE);
122 DrawText(FormatText("Frame: %i", framesCounter), 20, 700, 20, WHITE);
123
124 DrawFPS(10, 10);
125
126 EndDrawing();
127 //----------------------------------------------------------------------------------
128 }
129
130 // De-Initialization
131 //--------------------------------------------------------------------------------------
132 UnloadModel(model1);
133 UnloadModel(model2);
134 UnloadModel(model3);
135
136 UnloadTexture(texDiffuse); // Unload default diffuse texture
137 UnloadTexture(texMask); // Unload texture mask
138
139 UnloadShader(shader); // Unload shader
140
141 CloseWindow(); // Close window and OpenGL context
142 //--------------------------------------------------------------------------------------
143
144 return 0;
145}
146