/
redgpu
/
raygpu
Обзор
Документация
Войти
/
redgpu
/
raygpu
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/split_screen_camera3d.c
249 строк
11 KB
manuel
Get rid of zeroinit and split public / internal api
08 окт 2025, 15:57
08 окт 2025, 15:57
93c9bdf
Код
Авторство
О чём код?
/******************************************************************************************* * * raylib [core] example - 3d cmaera split screen * * Example originally created with raylib 3.7, last time updated with raylib 4.0 * * Example contributed by Jeffery Myers (@JeffM2501) 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) 2021-2024 Jeffery Myers (@JeffM2501) * ********************************************************************************************/ #include <raygpu.h> //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ void DrawCube(Vector3 position, float width, float height, float length, Color color) { float x = position.x; float y = position.y; float z = position.z; //rlRotatef(45, 0, 1, 0); //rlScalef(1.0f, 1.0f, 1.0f); // NOTE: Vertices are directly scaled on definition UseNoTexture(); rlBegin(RL_TRIANGLES); rlColor4ub(color.r, color.g, color.b, color.a); // Front face rlNormal3f(0.0f, 0.0f, 1.0f); rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Right rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right // Back face rlNormal3f(0.0f, 0.0f, -1.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Left rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left // Top face rlNormal3f(0.0f, 1.0f, 0.0f); rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left rlVertex3f(x - width/2, y + height/2, z + length/2); // Bottom Left rlVertex3f(x + width/2, y + height/2, z + length/2); // Bottom Right rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left rlVertex3f(x + width/2, y + height/2, z + length/2); // Bottom Right // Bottom face rlNormal3f(0.0f, -1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Top Left rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left rlVertex3f(x + width/2, y - height/2, z - length/2); // Top Right rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right rlVertex3f(x - width/2, y - height/2, z - length/2); // Top Left // Right face rlNormal3f(1.0f, 0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Left rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Left rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Left // Left face rlNormal3f(-1.0f, 0.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Right rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Right rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Right rlEnd(); } typedef Camera3D Camera; int main(void) { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera split screen"); // Setup player 1 camera and screen Camera cameraPlayer1 = { 0 }; cameraPlayer1.fovy = 50.0f; cameraPlayer1.up.y = 1.0f; cameraPlayer1.target.y = 1.0f; cameraPlayer1.position.z = -10.0f; cameraPlayer1.position.y = 1.0f; RenderTexture screenPlayer1 = LoadRenderTexture(screenWidth / 2, screenHeight); // Setup player two camera and screen Camera cameraPlayer2 = { 0 }; cameraPlayer2.fovy = 50.0f; cameraPlayer2.up.y = 1.0f; cameraPlayer2.target.y = 3.0f; cameraPlayer2.position.x = -3.0f; cameraPlayer2.position.y = 3.0f; RenderTexture screenPlayer2 = LoadRenderTexture(screenWidth / 2, screenHeight); // Build a flipped rectangle the size of the split view to use for drawing later Rectangle splitScreenRect = { 0.0f, 0.0f, (float)screenPlayer1.texture.width, (float)screenPlayer1.texture.height }; // Grid data int count = 5; float spacing = 4; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // If anyone moves this frame, how far will they move based on the time since the last frame // this moves thigns at 10 world units per second, regardless of the actual FPS BeginDrawing(); ClearBackground(SKYBLUE); float offsetThisFrame = 10.0f * GetFrameTime(); // Move Player1 forward and backwards (no turning) if (IsKeyDown(KEY_W)) { cameraPlayer1.position.z += offsetThisFrame; cameraPlayer1.target.z += offsetThisFrame; } else if (IsKeyDown(KEY_S)) { cameraPlayer1.position.z -= offsetThisFrame; cameraPlayer1.target.z -= offsetThisFrame; } // Move Player2 forward and backwards (no turning) if (IsKeyDown(KEY_UP)) { cameraPlayer2.position.x += offsetThisFrame; cameraPlayer2.target.x += offsetThisFrame; } else if (IsKeyDown(KEY_DOWN)) { cameraPlayer2.position.x -= offsetThisFrame; cameraPlayer2.target.x -= offsetThisFrame; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- // Draw Player1 view to the render texture BeginTextureMode(screenPlayer1); ClearBackground(SKYBLUE); BeginMode3D(cameraPlayer1); // Draw scene: grid of cube trees on a plane to make a "world" //DrawPlane((Vector3){ 0, 0, 0 }, (Vector2){ 50, 50 }, BEIGE); // Simple world plane for (float x = -count*spacing; x <= count*spacing; x += spacing) { for (float z = -count*spacing; z <= count*spacing; z += spacing) { DrawCube((Vector3) { x, 1.5f, z }, 1, 1, 1, LIME); DrawCube((Vector3) { x, 0.5f, z }, 0.25f, 1, 0.25f, BROWN); } } // Draw a cube at each player's position //DrawCube(cameraPlayer1.position, 1, 1, 1, RED); DrawCube(cameraPlayer2.position, 1, 1, 1, BLUE); EndMode3D(); DrawRectangle(0, 0, GetScreenWidth()/2, 40, Fade(RAYWHITE, 0.8f)); DrawText("PLAYER1: W/S to move", 10, 10, 15, MAROON); EndTextureMode(); // Draw Player2 view to the render texture BeginTextureMode(screenPlayer2); ClearBackground(SKYBLUE); BeginMode3D(cameraPlayer2); // Draw scene: grid of cube trees on a plane to make a "world" //DrawPlane((Vector3){ 0, 0, 0 }, (Vector2){ 50, 50 }, BEIGE); // Simple world plane for (float x = -count*spacing; x <= count*spacing; x += spacing) { for (float z = -count*spacing; z <= count*spacing; z += spacing) { DrawCube((Vector3) { x, 1.5f, z }, 1, 1, 1, LIME); DrawCube((Vector3) { x, 0.5f, z }, 0.25f, 1, 0.25f, BROWN); } } // Draw a cube at each player's position DrawCube(cameraPlayer1.position, 1, 1, 1, RED); //DrawCube(cameraPlayer2.position, 1, 1, 1, BLUE); EndMode3D(); DrawText("PLAYER2: UP/DOWN to move", 10, 10, 15, DARKBLUE); DrawRectangle(0, 0, GetScreenWidth()/2, 40, Fade(RAYWHITE, 0.8f)); EndTextureMode(); // Draw both views render textures to the screen side by side ClearBackground(BLACK); DrawTextureRec(screenPlayer1.texture, splitScreenRect, (Vector2){ 0, 0 }, WHITE); DrawTextureRec(screenPlayer2.texture, splitScreenRect, (Vector2){ screenWidth/2.0f, 0 }, WHITE); DrawRectangle(GetScreenWidth()/2 - 2, 0, 4, GetScreenHeight(), LIGHTGRAY); EndDrawing(); } // De-Initialization //-------------------------------------------------------------------------------------- //UnloadRenderTexture(screenPlayer1); // Unload render texture //UnloadRenderTexture(screenPlayer2); // Unload render texture SetWindowShouldClose(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }