ソースを参照

3d Camera: Added support for field-of-view Y

raysan5 9 年 前
コミット
d8bd8634ab

+ 2 - 1
examples/core_3d_camera_first_person.c

@@ -23,7 +23,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
     
     // Define the camera to look into our 3d world (position, target, up vector)
-    Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 60.0f };
 
     // Generates some random columns
     float heights[MAX_COLUMNS];
@@ -40,6 +40,7 @@ int main()
     Vector3 playerPosition = { 4.0f, 2.0f, 4.0f };       // Define player position
     
     SetCameraMode(CAMERA_FIRST_PERSON);         // Set a first person camera mode
+    SetCameraFovy(camera.fovy);                 // Set internal camera field-of-view Y
 
     SetTargetFPS(60);                           // Set our game to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------

+ 2 - 0
examples/core_3d_camera_free.c

@@ -25,12 +25,14 @@ int main()
     camera.position = (Vector3){ 0.0f, 10.0f, 10.0f };  // Camera position
     camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };      // Camera looking at point
     camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };          // Camera up vector (rotation towards target)
+    camera.fovy = 45.0f;                                // Camera field-of-view Y
     
     Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
     
     SetCameraMode(CAMERA_FREE);         // Set a free camera mode
     SetCameraPosition(camera.position); // Set internal camera position to match our camera position
     SetCameraTarget(camera.target);     // Set internal camera target to match our camera target
+    SetCameraFovy(camera.fovy);         // Set internal camera field-of-view Y
 
     SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------

+ 1 - 0
examples/core_3d_mode.c

@@ -25,6 +25,7 @@ int main()
     camera.position = (Vector3){ 0.0f, 10.0f, 10.0f };  // Camera position
     camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };      // Camera looking at point
     camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };          // Camera up vector (rotation towards target)
+    camera.fovy = 45.0f;                                // Camera field-of-view Y
 
     Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
 

+ 2 - 0
examples/core_3d_picking.c

@@ -25,6 +25,7 @@ int main()
     camera.position = (Vector3){ 0.0f, 10.0f, 10.0f };  // Camera position
     camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };      // Camera looking at point
     camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };          // Camera up vector (rotation towards target)
+    camera.fovy = 45.0f;                                // Camera field-of-view Y
 
     Vector3 cubePosition = { 0.0f, 1.0f, 0.0f };
     Vector3 cubeSize = { 2.0f, 2.0f, 2.0f };
@@ -35,6 +36,7 @@ int main()
     
     SetCameraMode(CAMERA_FREE);         // Set a free camera mode
     SetCameraPosition(camera.position); // Set internal camera position to match our camera position
+    SetCameraFovy(camera.fovy);         // Set internal camera field-of-view Y
 
     SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------

+ 2 - 1
examples/core_world_screen.c

@@ -21,7 +21,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 
     Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
     
@@ -30,6 +30,7 @@ int main()
     SetCameraMode(CAMERA_FREE);         // Set a free camera mode
     SetCameraPosition(camera.position); // Set internal camera position to match our camera position
     SetCameraTarget(camera.target);     // Set internal camera target to match our camera target
+    SetCameraFovy(camera.fovy);         // Set internal camera field-of-view Y
 
     SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------

+ 2 - 1
examples/models_billboard.c

@@ -21,7 +21,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [models] example - drawing billboards");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 5.0f, 4.0f, 5.0f }, { 0.0f, 2.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 5.0f, 4.0f, 5.0f }, { 0.0f, 2.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 
     Texture2D bill = LoadTexture("resources/billboard.png");     // Our texture billboard
     Vector3 billPosition = { 0.0f, 2.0f, 0.0f };                 // Position where draw billboard
@@ -29,6 +29,7 @@ int main()
     SetCameraMode(CAMERA_ORBITAL);      // Set an orbital camera mode
     SetCameraPosition(camera.position); // Set internal camera position to match our camera position
     SetCameraTarget(camera.target);     // Set internal camera target to match our camera target
+    SetCameraFovy(camera.fovy);         // Set internal camera field-of-view Y
 
     SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------

+ 1 - 1
examples/models_box_collisions.c

@@ -21,7 +21,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
     
     Vector3 playerPosition = { 0.0f, 1.0f, 2.0f };
     Vector3 playerSize = { 1.0f, 2.0f, 1.0f };

+ 2 - 1
examples/models_cubicmap.c

@@ -21,7 +21,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [models] example - cubesmap loading and drawing");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 16.0f, 14.0f, 16.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 16.0f, 14.0f, 16.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 
     Image image = LoadImage("resources/cubicmap.png");      // Load cubicmap image (RAM)
     Texture2D cubicmap = LoadTextureFromImage(image);       // Convert image to texture to display (VRAM)
@@ -37,6 +37,7 @@ int main()
     
     SetCameraMode(CAMERA_ORBITAL);      // Set an orbital camera mode
     SetCameraPosition(camera.position); // Set internal camera position to match our custom camera position
+    SetCameraFovy(camera.fovy);         // Set internal camera field-of-view Y
 
     SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------

+ 1 - 1
examples/models_geometric_shapes.c

@@ -21,7 +21,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [models] example - geometric shapes");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 
     SetTargetFPS(60);   // Set our game to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------

+ 1 - 1
examples/models_heightmap.c

@@ -21,7 +21,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing");
 
     // Define our custom camera to look into our 3d world
-    Camera camera = {{ 18.0f, 16.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 18.0f, 16.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 
     Image image = LoadImage("resources/heightmap.png");         // Load heightmap image (RAM)
     Texture2D texture = LoadTextureFromImage(image);            // Convert image to texture (VRAM)

+ 1 - 1
examples/models_obj_loading.c

@@ -21,7 +21,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [models] example - obj model loading");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 
     Model dwarf = LoadModel("resources/model/dwarf.obj");                   // Load OBJ model
     Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png");   // Load model texture

+ 1 - 1
examples/shaders_basic_lighting.c

@@ -36,7 +36,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
     
     // Camera initialization
-    Camera camera = {{ 8.0f, 8.0f, 8.0f }, { 0.0f, 3.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 8.0f, 8.0f, 8.0f }, { 0.0f, 3.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
     
     // Model initialization
     Vector3 position = { 0.0f, 0.0f, 0.0f };

+ 1 - 1
examples/shaders_custom_uniform.c

@@ -30,7 +30,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 
     Model dwarf = LoadModel("resources/model/dwarf.obj");                   // Load OBJ model
     Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png");   // Load model texture

+ 1 - 1
examples/shaders_model_shader.c

@@ -30,7 +30,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 
     Model dwarf = LoadModel("resources/model/dwarf.obj");                   // Load OBJ model
     Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png");   // Load model texture

+ 1 - 1
examples/shaders_postprocessing.c

@@ -30,7 +30,7 @@ int main()
     InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader");
 
     // Define the camera to look into our 3d world
-    Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+    Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
     
     Model dwarf = LoadModel("resources/model/dwarf.obj");                   // Load OBJ model
     Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png");   // Load model texture

+ 7 - 1
src/camera.c

@@ -84,7 +84,7 @@ typedef enum { MOVE_FRONT = 0, MOVE_LEFT, MOVE_BACK, MOVE_RIGHT, MOVE_UP, MOVE_D
 //----------------------------------------------------------------------------------
 // Global Variables Definition
 //----------------------------------------------------------------------------------
-static Camera internalCamera = {{ 2.0f, 0.0f, 2.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
+static Camera internalCamera = {{ 2.0f, 0.0f, 2.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
 static Vector2 cameraAngle = { 0.0f, 0.0f };
 static float cameraTargetDistance = 5.0f;
 static Vector2 cameraMousePosition = { 0.0f, 0.0f };
@@ -212,6 +212,12 @@ void SetCameraTarget(Vector3 target)
     cameraTargetDistance = sqrt(dx*dx + dy*dy + dz*dz);
 }
 
+// Set internal camera fovy
+void SetCameraFovy(float fovy)
+{
+    internalCamera.fovy = fovy;
+}
+
 // Set camera pan key to combine with mouse movement (free camera)
 void SetCameraPanControl(int panKey)
 {

+ 1 - 0
src/camera.h

@@ -81,6 +81,7 @@ void UpdateCameraPlayer(Camera *camera, Vector3 *position); // Update camera and
 
 void SetCameraPosition(Vector3 position);                   // Set internal camera position
 void SetCameraTarget(Vector3 target);                       // Set internal camera target
+void SetCameraFovy(float fovy);                             // Set internal camera field-of-view-y
 
 void SetCameraPanControl(int panKey);                       // Set camera pan key to combine with mouse movement (free camera)
 void SetCameraAltControl(int altKey);                       // Set camera alt key to combine with mouse movement (free camera)

+ 10 - 10
src/core.c

@@ -609,10 +609,10 @@ void Begin3dMode(Camera camera)
 
     rlPushMatrix();                     // Save previous matrix, which contains the settings for the 2d ortho projection
     rlLoadIdentity();                   // Reset current matrix (PROJECTION)
-
+    
     // Setup perspective projection
     float aspect = (float)screenWidth/(float)screenHeight;
-    double top = 0.01*tan(45.0*PI/360.0);
+    double top = 0.01*tan(camera.fovy*PI/360.0);
     double right = top*aspect;
 
     // NOTE: zNear and zFar values are important when computing depth buffer values
@@ -883,7 +883,7 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera)
     TraceLog(DEBUG, "Device coordinates: (%f, %f, %f)", deviceCoords.x, deviceCoords.y, deviceCoords.z);
     
     // Calculate projection matrix (from perspective instead of frustum)
-    Matrix matProj = MatrixPerspective(45.0, ((double)GetScreenWidth()/(double)GetScreenHeight()), 0.01, 1000.0);
+    Matrix matProj = MatrixPerspective(camera.fovy, ((double)GetScreenWidth()/(double)GetScreenHeight()), 0.01, 1000.0);
     
     // Calculate view matrix from camera look at
     Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
@@ -936,7 +936,7 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera)
 Vector2 WorldToScreen(Vector3 position, Camera camera)
 {    
     // Calculate projection matrix (from perspective instead of frustum
-    Matrix matProj = MatrixPerspective(45.0f, (float)((float)GetScreenWidth() / (float)GetScreenHeight()), 0.01f, 1000.0f);
+    Matrix matProj = MatrixPerspective(camera.fovy, (double)GetScreenWidth()/(double)GetScreenHeight(), 0.01, 1000.0);
     
     // Calculate view matrix from camera look at (and transpose it)
     Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
@@ -1752,8 +1752,8 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int
     // Normalize gestureEvent.position[0] for screenWidth and screenHeight
     gestureEvent.position[0].x /= (float)GetScreenWidth(); 
     gestureEvent.position[0].y /= (float)GetScreenHeight();
-    
-    // Gesture data is sent to gestures system for processing
+	
+	// Gesture data is sent to gestures system for processing
     ProcessGestureEvent(gestureEvent);
 #endif
 }
@@ -1890,7 +1890,7 @@ static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
                     
                     // TODO: GPU assets reload in case of lost focus (lost context)
                     // NOTE: This problem has been solved just unbinding and rebinding context from display
-                    /*
+					/*
                     if (assetsReloadRequired)
                     {
                         for (int i = 0; i < assetsCount; i++)
@@ -2471,9 +2471,9 @@ static void *GamepadThread(void *arg)
     const int joystickAxisY = 1;
 
     // Read gamepad event
-    struct js_event gamepadEvent;
+	struct js_event gamepadEvent;
     
-    while (1) 
+	while (1) 
     {
         if (read(gamepadStream, &gamepadEvent, sizeof(struct js_event)) == (int)sizeof(struct js_event))
         {
@@ -2507,7 +2507,7 @@ static void *GamepadThread(void *arg)
                 */
             }
         }
-    }
+	}
     
     return NULL;
 }

+ 9 - 19
src/models.c

@@ -558,19 +558,9 @@ Model LoadModel(const char *fileName)
         // NOTE: model properties (transform, texture, shader) are initialized inside rlglLoadModel()
         model = rlglLoadModel(mesh);     // Upload vertex data to GPU
 
-        // Now that vertex data is uploaded to GPU VRAM, we can free arrays from CPU RAM
-        // NOTE 1: We don't need CPU vertex data on OpenGL 3.3 or ES2... for static meshes...
-        // NOTE 2: ...but we could keep CPU vertex data in case we need to update the mesh
-        
-        /*
-        if (rlGetVersion() != OPENGL_11)
-        {
-            free(mesh.vertices);
-            free(mesh.texcoords);
-            free(mesh.normals);
-            free(mesh.colors);
-        }
-        */
+        // NOTE: Now that vertex data is uploaded to GPU VRAM, we can free arrays from CPU RAM
+        // We don't need CPU vertex data on OpenGL 3.3 or ES2... for static meshes...
+        // ...but we could keep CPU vertex data in case we need to update the mesh
     }
 
     return model;
@@ -1082,16 +1072,16 @@ void UnloadModel(Model model)
     free(model.mesh.vertices);
     free(model.mesh.texcoords);
     free(model.mesh.normals);
-    if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2);
-    if (model.mesh.tangents != NULL) free(model.mesh.tangents);
-    if (model.mesh.colors != NULL) free(model.mesh.colors);
+    free(model.mesh.colors);
+    //if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2); // Not used
+    //if (model.mesh.tangents != NULL) free(model.mesh.tangents); // Not used
     
     rlDeleteBuffers(model.mesh.vboId[0]);   // vertex
     rlDeleteBuffers(model.mesh.vboId[1]);   // texcoords
     rlDeleteBuffers(model.mesh.vboId[2]);   // normals
-    rlDeleteBuffers(model.mesh.vboId[3]);   // texcoords2
-    rlDeleteBuffers(model.mesh.vboId[4]);   // tangents
-    rlDeleteBuffers(model.mesh.vboId[5]);   // colors
+    //rlDeleteBuffers(model.mesh.vboId[3]);   // texcoords2 (NOT USED)
+    //rlDeleteBuffers(model.mesh.vboId[4]);   // tangents (NOT USED)
+    //rlDeleteBuffers(model.mesh.vboId[5]);   // colors (NOT USED)
 
     rlDeleteVertexArrays(model.mesh.vaoId);
     

+ 5 - 4
src/raylib.h

@@ -309,10 +309,10 @@ typedef struct SpriteFont {
 
 // Camera type, defines a camera position/orientation in 3d space
 typedef struct Camera {
-    Vector3 position;
-    Vector3 target;
-    Vector3 up;
-    //float fovy;             // Field-Of-View apperture in Y (degrees)
+    Vector3 position;       // Camera position
+    Vector3 target;         // Camera target it looks-at
+    Vector3 up;             // Camera up vector (rotation over its axis)
+    float fovy;             // Field-Of-View apperture in Y (degrees)
 } Camera;
 
 // Bounding box type
@@ -630,6 +630,7 @@ void UpdateCameraPlayer(Camera *camera, Vector3 *position); // Update camera and
 
 void SetCameraPosition(Vector3 position);                   // Set internal camera position
 void SetCameraTarget(Vector3 target);                       // Set internal camera target
+void SetCameraFovy(float fovy);                             // Set internal camera field-of-view-y
 
 void SetCameraPanControl(int panKey);                       // Set camera pan key to combine with mouse movement (free camera)
 void SetCameraAltControl(int altKey);                       // Set camera alt key to combine with mouse movement (free camera)