mirror of
https://github.com/raysan5/raylib.git
synced 2026-03-10 02:55:41 +00:00
Update shaders_cel_shading.c
This commit is contained in:
@@ -9,20 +9,20 @@
|
||||
*
|
||||
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
|
||||
*
|
||||
* Example contributed by Gleb A (@ggrizzly)
|
||||
* Example contributed by Gleb A (@ggrizzly) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2015-2026 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2026 Gleb A (@ggrizzly)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include "rlgl.h"
|
||||
#include <stddef.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <math.h> // Required for: sinf(), cosf()
|
||||
|
||||
#define RLIGHTS_IMPLEMENTATION
|
||||
#include "rlights.h"
|
||||
@@ -33,40 +33,6 @@
|
||||
#define GLSL_VERSION 100
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Model table: path, optional diffuse texture path (NULL = embedded), draw scale
|
||||
//------------------------------------------------------------------------------------
|
||||
typedef struct {
|
||||
const char *modelPath;
|
||||
const char *texturePath; // NULL for GLB files with embedded textures
|
||||
float scale;
|
||||
float outlineThickness;
|
||||
} ModelInfo;
|
||||
|
||||
static const ModelInfo MODEL = { "resources/models/old_car_new.glb", NULL, 0.75f, 0.005f };
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Load model and its diffuse texture (if any). Does NOT assign a shader.
|
||||
//------------------------------------------------------------------------------------
|
||||
static Model celLoadModel()
|
||||
{
|
||||
Model model = LoadModel(MODEL.modelPath);
|
||||
|
||||
if (MODEL.texturePath != NULL)
|
||||
{
|
||||
Texture2D tex = LoadTexture(MODEL.texturePath);
|
||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = tex;
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
static void ApplyShaderToModel(Model model, Shader shader)
|
||||
{
|
||||
model.materials[0].shader = shader;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
@@ -81,16 +47,24 @@ int main(void)
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - cel shading");
|
||||
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 9.0f, 6.0f, 9.0f };
|
||||
camera.target = (Vector3){ 0.0f, 1.0f, 0.0f };
|
||||
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
|
||||
camera.fovy = 45.0f;
|
||||
camera.position = (Vector3){ 9.0f, 6.0f, 9.0f };
|
||||
camera.target = (Vector3){ 0.0f, 1.0f, 0.0f };
|
||||
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
|
||||
camera.fovy = 45.0f;
|
||||
camera.projection = CAMERA_PERSPECTIVE;
|
||||
|
||||
// Load model
|
||||
Model model = LoadModel("resources/models/old_car_new.glb");
|
||||
|
||||
// Load cel shader
|
||||
Shader celShader = LoadShader(TextFormat("resources/shaders/glsl%i/cel.vs", GLSL_VERSION),
|
||||
TextFormat("resources/shaders/glsl%i/cel.fs", GLSL_VERSION));
|
||||
Shader celShader = LoadShader(
|
||||
TextFormat("resources/shaders/glsl%i/cel.vs", GLSL_VERSION),
|
||||
TextFormat("resources/shaders/glsl%i/cel.fs", GLSL_VERSION));
|
||||
celShader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(celShader, "viewPos");
|
||||
|
||||
// Apply cel shader to model, keep copy of default shader
|
||||
Shader defaultShader = model.materials[0].shader;
|
||||
model.materials[0].shader = celShader;
|
||||
|
||||
// numBands: controls toon quantization steps (2 = hard binary, 20 = near-smooth)
|
||||
float numBands = 10.0f;
|
||||
@@ -108,14 +82,9 @@ int main(void)
|
||||
Light lights[MAX_LIGHTS] = { 0 };
|
||||
lights[0] = CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 50.0f }, Vector3Zero(), WHITE, celShader);
|
||||
|
||||
|
||||
bool celEnabled = true;
|
||||
bool celEnabled = true;
|
||||
bool outlineEnabled = true;
|
||||
|
||||
Model model = celLoadModel();
|
||||
Shader defaultShader = model.materials[0].shader;
|
||||
ApplyShaderToModel(model, celShader);
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
@@ -133,7 +102,8 @@ int main(void)
|
||||
if (IsKeyPressed(KEY_Z))
|
||||
{
|
||||
celEnabled = !celEnabled;
|
||||
ApplyShaderToModel(model, celEnabled ? celShader : defaultShader);
|
||||
if (celEnabled) model.materials[0].shader = celShader; // Apply cel shader to model
|
||||
else model.materials[0].shader = defaultShader; // Apply default shader to model
|
||||
}
|
||||
|
||||
// [C] Toggle outline on/off
|
||||
@@ -146,11 +116,7 @@ int main(void)
|
||||
|
||||
// Spin light opposite to CAMERA_ORBITAL (0.5 rad/s), angled 45 degrees off vertical
|
||||
float t = (float)GetTime();
|
||||
lights[0].position = (Vector3){
|
||||
sinf(-t * 0.3f) * 5.0f,
|
||||
5.0f,
|
||||
cosf(-t * 0.3f) * 5.0f
|
||||
};
|
||||
lights[0].position = (Vector3){ sinf(-t*0.3f)*5.0f, 5.0f, cosf(-t*0.3f)*5.0f };
|
||||
|
||||
for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(celShader, lights[i]);
|
||||
//----------------------------------------------------------------------------------
|
||||
@@ -166,25 +132,31 @@ int main(void)
|
||||
if (outlineEnabled)
|
||||
{
|
||||
// Outline pass: cull front faces, draw extruded back faces as silhouette
|
||||
float thickness = MODEL.outlineThickness;
|
||||
float thickness = 0.005f;
|
||||
SetShaderValue(outlineShader, outlineThicknessLoc, &thickness, SHADER_UNIFORM_FLOAT);
|
||||
|
||||
rlSetCullFace(RL_CULL_FACE_FRONT);
|
||||
ApplyShaderToModel(model, outlineShader);
|
||||
DrawModel(model, Vector3Zero(), MODEL.scale, WHITE);
|
||||
ApplyShaderToModel(model, celEnabled ? celShader : defaultShader);
|
||||
|
||||
model.materials[0].shader = outlineShader;
|
||||
|
||||
DrawModel(model, Vector3Zero(), 0.75f, WHITE);
|
||||
|
||||
if (celEnabled) model.materials[0].shader = celShader; // Apply cel shader to model
|
||||
else model.materials[0].shader = defaultShader; // Apply default shader to model
|
||||
|
||||
rlSetCullFace(RL_CULL_FACE_BACK);
|
||||
}
|
||||
|
||||
DrawModel(model, Vector3Zero(), MODEL.scale, WHITE);
|
||||
DrawModel(model, Vector3Zero(), 0.75f, WHITE);
|
||||
DrawSphereEx(lights[0].position, 0.2f, 50, 50, YELLOW); // Light position indicator
|
||||
DrawGrid(10, 10.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
DrawText(TextFormat("Cel: %s [Z]", celEnabled ? "ON" : "OFF"), 10, 65, 20, celEnabled ? DARKGREEN : DARKGRAY);
|
||||
DrawText(TextFormat("Outline: %s [C]", outlineEnabled ? "ON" : "OFF"), 10, 90, 20, outlineEnabled ? DARKGREEN : DARKGRAY);
|
||||
DrawText(TextFormat("Bands: %.0f [Q/E]", numBands), 10, 115, 20, DARKGRAY);
|
||||
DrawText(TextFormat("Cel: %s [Z]", celEnabled? "ON" : "OFF"), 10, 65, 20, celEnabled? DARKGREEN : DARKGRAY);
|
||||
DrawText(TextFormat("Outline: %s [C]", outlineEnabled? "ON" : "OFF"), 10, 90, 20, outlineEnabled? DARKGREEN : DARKGRAY);
|
||||
DrawText(TextFormat("Bands: %.0f [Q/E]", numBands), 10, 115, 20, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
@@ -195,6 +167,7 @@ int main(void)
|
||||
UnloadModel(model);
|
||||
UnloadShader(celShader);
|
||||
UnloadShader(outlineShader);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user