Skip to content

Commit

Permalink
raylib 3D camera demo
Browse files Browse the repository at this point in the history
  • Loading branch information
smaludzi committed May 26, 2024
1 parent d832d5d commit 332b481
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 13 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Never - Functional Programming Language
[![Build Status](https://travis-ci.org/never-lang/never.svg?branch=master)](https://travis-ci.org/never-lang/never)
[![Version](https://img.shields.io/github/release/never-lang/never.svg)](https://github.com/never-lang/never/releases)
[![Codecov](https://codecov.io/gh/never-lang/never/branch/master/graph/badge.svg)](https://codecov.io/gh/never-lang/never)

Expand Down
Binary file added docs/raylib_3d_camera.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 132 additions & 0 deletions docs/raylib_3d_camera.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
Another example which demos Never and raylib FFI.

Code mentioned below is adopted to from [raylib examples](https://www.raylib.com/examples.html).

![raylib 3d camera][raylib 3d camera]

```never
/**********************************************************************************************
* LICENSE: zlib/libpng
*
* raylib is 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) 2013-2020 Ramon Santamaria (@raysan5)
* Copyright (c) 2020 Slawomir Maludzinski (binding to Never language)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
/***
* A small hack in raylib library is needed
*
* Add the following function:
*
* Camera UpdateCameraCopy(Camera camera)
* {
* UpdateCamera(&camera);
* return camera;
* }
*
*/
use raylib
use raylib_camera
use raylib_models
use raylib_shapes
use raylib_text
let MAX_COLUMNS = 20;
func main() -> int
{
/* Initalization */
let screenWidth = 800;
let screenHeight = 450;
var camera = raylib.Camera3D;
var heights = {[ MAX_COLUMNS ]} : float;
var positions = {[ MAX_COLUMNS ]} : raylib.Vector3;
var colors = {[ MAX_COLUMNS ]} : raylib.Color;
var i = 0;
raylib.InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
/* Define the camera to look into our 3d world (position, target, up vector) */
camera = raylib.Camera3D(nil, nil, nil, 0.0, 0);
camera.position = raylib.Vector3(4.0, 2.0, 4.0);
camera.target = raylib.Vector3(0.0, 1.8, 0.0);
camera.up = raylib.Vector3(0.0, 1.0, 0.0);
camera.fovy = 60.0;
camera.type = raylib_camera.CameraType::CAMERA_PERSPECTIVE;
/* Generates some random columns */
for (i = 0; i < MAX_COLUMNS; i = i + 1)
{
heights[i] = raylib.GetRandomValue(1, 12);
positions[i] = raylib.Vector3( raylib.GetRandomValue(-15, 15), heights[i]/2, raylib.GetRandomValue(-15, 15) );
colors[i] = raylib.Color( chr(raylib.GetRandomValue(20, 255)), chr(raylib.GetRandomValue(10, 55)), chr(30), chr(255) )
};
/* Set a first person camera mode */
raylib_camera.SetCameraMode(camera, raylib_camera.CameraMode::CAMERA_FIRST_PERSON);
/* Set our game to run at 60 frames-per-second */
raylib.SetTargetFPS(200);
/* Main game loop */
while (!raylib.WindowShouldClose()) /* Detect window close button or ESC key */
{
/* Update camera */
camera = raylib_camera.UpdateCameraCopy(camera);
/* Draw */
raylib.BeginDrawing();
raylib.ClearBackground(raylib.RAYWHITE);
raylib.BeginMode3D(camera);
raylib_models.DrawPlane(raylib.Vector3(0.0, 0.0, 0.0), raylib.Vector2(32.0, 32.0), raylib.LIGHTGRAY); /* Draw ground */
raylib_models.DrawCube(raylib.Vector3(-16.0, 2.5, 0.0), 1.0, 5.0, 32.0, raylib.BLUE); /* Draw a blue wall */
raylib_models.DrawCube(raylib.Vector3(16.0, 2.5, 0.0), 1.0, 5.0, 32.0, raylib.LIME); /* Draw a green wall */
raylib_models.DrawCube(raylib.Vector3(0.0, 2.5, 16.0), 32.0, 5.0, 1.0, raylib.GOLD); /* Draw a yellow wall */
/* Draw some cubes around */
for (i = 0; i < MAX_COLUMNS; i = i + 1)
{
raylib_models.DrawCube(positions[i], 2.0, heights[i], 2.0, colors[i]);
raylib_models.DrawCubeWires(positions[i], 2.0, heights[i], 2.0, raylib.MAROON)
};
raylib.EndMode3D();
raylib_shapes.DrawRectangle( 10, 10, 220, 70, raylib.Fade(raylib.SKYBLUE, 0.5));
raylib_shapes.DrawRectangleLines( 10, 10, 220, 70, raylib.BLUE);
raylib_text.DrawText("First person camera default controls:", 20, 20, 10, raylib.BLACK);
raylib_text.DrawText("- Move with keys: W, A, S, D", 40, 40, 10, raylib.DARKGRAY);
raylib_text.DrawText("- Mouse move to look around", 40, 60, 10, raylib.DARKGRAY);
raylib.EndDrawing()
};
/* De-Initialization */
raylib.CloseWindow(); /* Close window and OpenGL context */
0
}
```

[raylib_cubes]: raylib_3d_camera.gif "raylib 3d camera"
72 changes: 60 additions & 12 deletions docs/raylib_cubes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@ Code mentioned below is adopted to from [raylib examples](https://www.raylib.com
![raylib cubes][raylib_cubes]

```never
/**********************************************************************************************
* LICENSE: zlib/libpng
*
* raylib is 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) 2013-2020 Ramon Santamaria (@raysan5)
* Copyright (c) 2020 Slawomir Maludzinski (binding to Never language)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
/*******************************************************************************************
*
* raylib [models] example - Waving cubes
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Codecat (@codecat) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Codecat (@codecat) and Ramon Santamaria (@raysan5)
* Copyright (c) 2020 Slawomir Maludzinski (Never port)
*
********************************************************************************************/
use raylib
use raylib_camera
use raylib_models
Expand All @@ -17,11 +55,24 @@ func main() -> int
let screenWidth = 800;
let screenHeight = 450;
let numBlocks = 15;
var blockScale = 0.0;
var time = 0.0d;
var cameraTime = 0.0d;
var scale = 0.0;
var scatter = 0.0;
var cubePos = raylib.Vector3;
var cubeColor = raylib.Color;
var camera = raylib.Camera3D;
var cubeSize = 0.0;
var i = 0;
var x = 0;
var y = 0;
var z = 0;
raylib.InitWindow(screenWidth, screenHeight, "raylib [models] example - waving cubes");
/* Initialize the camera */
let camera = raylib.Camera3D(nil, nil, nil, 0.0f, 0);
camera = raylib.Camera3D(nil, nil, nil, 0.0f, 0);
camera.position = raylib.Vector3(30.0f, 20.0f, 30.0f);
camera.target = raylib.Vector3(0.0f, 0.0f, 0.0f);
camera.up = raylib.Vector3(0.0f, 1.0f, 0.0f);
Expand All @@ -34,13 +85,13 @@ func main() -> int
while (!raylib.WindowShouldClose()) /* Detect window close button or ESC key */
{
/* Update */
let time = raylib.GetTime();
time = raylib.GetTime();
/* Calculate time scale for cube position and size */
let scale = (2.0f + sin(time)) * 0.7f;
scale = (2.0f + sin(time)) * 0.7f;
/* Move camera around the scene */
let cameraTime = time * 0.3;
cameraTime = time * 0.3;
camera.position.x = cos(cameraTime)*40.0f;
camera.position.z = sin(cameraTime)*40.0f;
Expand All @@ -53,33 +104,30 @@ func main() -> int
raylib_models.DrawGrid(10, 5.0f);
let x = 0;
for (x = 0; x < numBlocks; x = x + 1)
{
let y = 0;
for (y = 0; y < numBlocks; y = y + 1)
{
let z = 0;
for (z = 0; z < numBlocks; z = z + 1)
{
/* Scale of the blocks depends on x/y/z positions */
let blockScale = (x + y + z)/30.0f;
blockScale = (x + y + z)/30.0f;
/* Scatter makes the waving effect by adding blockScale over time */
let scatter = sin(blockScale*20.0f + (time*4.0f));
scatter = sin(blockScale*20.0f + (time*4.0f));
/* Calculate the cube position */
let cubePos = raylib.Vector3(
cubePos = raylib.Vector3(
(x - numBlocks/2)*(scale*3.0f) + scatter,
(y - numBlocks/2)*(scale*2.0f) + scatter,
(z - numBlocks/2)*(scale*3.0f) + scatter
);
/* Pick a color with a hue depending on cube position for the rainbow color effect */
let cubeColor = raylib.ColorFromHSV(raylib.Vector3(((x + y + z) * 18) % 360, 0.75f, 0.9f));
cubeColor = raylib.ColorFromHSV(raylib.Vector3(((x + y + z) * 18) % 360, 0.75f, 0.9f));
/* Calculate cube size */
let cubeSize = (2.4f - scale)*blockScale;
cubeSize = (2.4f - scale)*blockScale;
/* And finally, draw the cube! */
raylib_models.DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor)
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nav:
- FFI: ffi.md
- raylib FFI: raylib.md
- raylib cubes: raylib_cubes.md
- raylib 3d camera: raylib_3d_camera.md
- Rail Road Diagram: railroad.md
- Types: types.md
- Contact: contact.md

0 comments on commit 332b481

Please sign in to comment.