MCPcopy Create free account
hub / github.com/RobLoach/raylib-cpp / main

Function main

examples/shaders/shaders_basic_lighting.cpp:39–145  ·  view source on GitHub ↗

------------------------------------------------------------------------------------ Program main entry point ------------------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

37// Program main entry point
38//------------------------------------------------------------------------------------
39int main(void)
40{
41 // Initialization
42 //--------------------------------------------------------------------------------------
43 const int screenWidth = 800;
44 const int screenHeight = 450;
45
46 SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
47 InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
48
49 // Define the camera to look into our 3d world
50 raylib::Camera camera;
51 camera.position = Vector3{ 2.0f, 4.0f, 6.0f }; // Camera position
52 camera.target = Vector3{ 0.0f, 0.5f, 0.0f }; // Camera looking at point
53 camera.up = Vector3{ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
54 camera.fovy = 45.0f; // Camera field-of-view Y
55 camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
56
57 // Load basic lighting shader
58 raylib::Shader shader (TextFormat("resources/shaders/glsl%i/lighting.vs", GLSL_VERSION),
59 TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
60 // Get some required shader locations
61 shader.locs[SHADER_LOC_VECTOR_VIEW] = shader.GetLocation("viewPos");
62 // NOTE: "matModel" location name is automatically assigned on shader loading,
63 // no need to get the location again if using that uniform name
64 //shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
65
66 // Ambient light level (some basic lighting)
67 int ambientLoc = shader.GetLocation("ambient");
68 std::array<float, 4> ambientValues = {0.1f, 0.1f, 0.1f, 1.0f};
69 shader.SetValue(ambientLoc, ambientValues.data(), SHADER_UNIFORM_VEC4);
70
71 // Create lights
72 std::array<Light, MAX_LIGHTS> lights = {
73 CreateLight(LIGHT_POINT, Vector3 {-2, 1, -2}, Vector3Zero(), YELLOW, shader),
74 CreateLight(LIGHT_POINT, Vector3 {2, 1, 2}, Vector3Zero(), RED, shader),
75 CreateLight(LIGHT_POINT, Vector3 {-2, 1, 2}, Vector3Zero(), GREEN, shader),
76 CreateLight(LIGHT_POINT, Vector3 {2, 1, -2}, Vector3Zero(), BLUE, shader),
77 };
78
79 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
80 //--------------------------------------------------------------------------------------
81
82 // Main game loop
83 while (!WindowShouldClose()) // Detect window close button or ESC key
84 {
85 // Update
86 //----------------------------------------------------------------------------------
87 camera.Update(CAMERA_ORBITAL);
88
89 // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
90 std::array<float, 3> cameraPos = { camera.position.x, camera.position.y, camera.position.z };
91 SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos.data(), SHADER_UNIFORM_VEC3);
92
93 // Check key inputs to enable/disable lights
94 if (IsKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; }
95 if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
96 if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; }

Callers

nothing calls this directly

Calls 10

InitWindowFunction · 0.85
IsKeyPressedFunction · 0.85
UpdateLightValuesFunction · 0.85
DrawPlaneFunction · 0.85
DrawCubeFunction · 0.85
DrawSphereWiresFunction · 0.85
GetLocationMethod · 0.80
CreateLightFunction · 0.70
DrawTextFunction · 0.50
UpdateMethod · 0.45

Tested by

no test coverage detected