raylib/ports/models/models_orthographic_projection

Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license. Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
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 screenshot of models_orthographic_projection

Code

models_orthographic_projection.odin ¶
107 linesSource

1/*******************************************************************************************
2*
3*   raylib [models] example - orthographic projection
4*
5*   Example complexity rating: [★☆☆☆] 1/4
6*
7*   Example originally created with raylib 2.0, last time updated with raylib 3.7
8*
9*   Example contributed by Max Danielsson (@autious) and reviewed by Ramon Santamaria (@raysan5)
10*
11*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12*   BSD-like license that allows static linking with closed source software
13*
14*   Copyright (c) 2018-2025 Max Danielsson (@autious) and Ramon Santamaria (@raysan5)
15*
16********************************************************************************************/
17
18package raylib_examples
19
20import rl "vendor:raylib"
21
22FOVY_PERSPECTIVE :: 45
23WIDTH_ORTHOGRAPHIC :: 10
24
25//------------------------------------------------------------------------------------
26// Program main entry point
27//------------------------------------------------------------------------------------
28main :: proc() {
29	// Initialization
30	//--------------------------------------------------------------------------------------
31	SCREEN_WIDTH :: 800
32	SCREEN_HEIGHT :: 450
33
34	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [models] example - orthographic projection")
35
36	// Define the camera to look into our 3d world
37	camera := rl.Camera {
38		position = {0, 10, 10},
39		//target = {0, 0, 0},
40		up = {0, 1, 0},
41		fovy = FOVY_PERSPECTIVE,
42		projection = .PERSPECTIVE,
43	}
44
45	rl.SetTargetFPS(60)               // Set our game to run at 60 frames-per-second
46	//--------------------------------------------------------------------------------------
47
48	// Main game loop
49	for !rl.WindowShouldClose() {    // Detect window close button or ESC key
50		// Update
51		//----------------------------------------------------------------------------------
52		if rl.IsKeyPressed(.SPACE) {
53			if camera.projection == .PERSPECTIVE {
54				camera.fovy = WIDTH_ORTHOGRAPHIC
55				camera.projection = .ORTHOGRAPHIC
56			} else {
57				camera.fovy = FOVY_PERSPECTIVE
58				camera.projection = .PERSPECTIVE
59			}
60		}
61		//----------------------------------------------------------------------------------
62
63		// Draw
64		//----------------------------------------------------------------------------------
65		rl.BeginDrawing()
66
67			rl.ClearBackground(rl.RAYWHITE)
68
69			rl.BeginMode3D(camera)
70
71				rl.DrawCube({-4, 0, 2}, 2, 5, 2, rl.RED)
72				rl.DrawCubeWires({-4, 0, 2}, 2, 5, 2, rl.GOLD)
73				rl.DrawCubeWires({-4, 0, -2}, 3, 6, 2, rl.MAROON)
74
75				rl.DrawSphere({-1, 0, -2}, 1, rl.GREEN)
76				rl.DrawSphereWires({1, 0, 2}, 2, 16, 16, rl.LIME)
77
78				rl.DrawCylinder({4, 0, -2}, 1, 2, 3, 4, rl.SKYBLUE)
79				rl.DrawCylinderWires({4, 0, -2}, 1, 2, 3, 4, rl.DARKBLUE)
80				rl.DrawCylinderWires({4, -1, 2}, 1, 1, 2, 6, rl.BROWN)
81
82				rl.DrawCylinder({1, 0, -4}, 0, 1.5, 3, 8, rl.GOLD)
83				rl.DrawCylinderWires({1, 0, -4}, 0, 1.5, 3, 8, rl.PINK)
84
85				rl.DrawGrid(10, 1)        // Draw a grid
86
87			rl.EndMode3D()
88
89			rl.DrawText("Press Spacebar to switch camera type", 10, rl.GetScreenHeight() - 30, 20, rl.DARKGRAY)
90
91			if camera.projection == .ORTHOGRAPHIC {
92				rl.DrawText("ORTHOGRAPHIC", 10, 40, 20, rl.BLACK)
93			} else if camera.projection == .PERSPECTIVE {
94				rl.DrawText("PERSPECTIVE", 10, 40, 20, rl.BLACK)
95			}
96
97			rl.DrawFPS(10, 10)
98
99		rl.EndDrawing()
100		//----------------------------------------------------------------------------------
101	}
102
103	// De-Initialization
104	//--------------------------------------------------------------------------------------
105	rl.CloseWindow()        // Close window and OpenGL context
106	//--------------------------------------------------------------------------------------
107}

Declarations Used 33