7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ElixirAdvent Calendar 2024

Day 12

Elixirでraylibを使ってグラフィック描画してみる その4 〜 3DのBoxを描画 〜

Posted at

前回

命令を拡張して円を描きました

今回

3DのBoxを描きます

仕様

  • main

    • 画面を800x800
    • FPSは10に制限
    • main_loopでループする
  • main_loop

    • 3DのBoxの座標計算
    • 描画開始宣言
    • drawで描画処理
    • 描画終了宣言
    • main_loopループ
  • draw

    • カメラの位置
    • 3D開始
    • 3DのBox描画(座標はx,yを-10〜10まで動く 計算はmain_loop)
    • 11個のBoxを座標をランダムに表示(cubu関数)

ソース

lib/rayex_3d.ex
defmodule Rayex3d do
  alias Rayex.Structs, as: S
  use Rayex

  @color_white %S.Color{r: 245, g: 245, b: 245, a: 255}
  @color_gray %S.Color{r: 130, g: 130, b: 130, a: 255}

  def main() do
    init_window(800, 800, "window name")
    set_target_fps(10)
    main_loop(true, -10.0)
  end

  defp main_loop(true, c) do
    c = if c > 10, do: -10.0, else: c + 0.1

    begin_drawing()
    draw(c)
    end_drawing()
    main_loop(!window_should_close(), c)
  end

  defp main_loop(_, _), do: nil

  defp draw(c) do
    # clear_background(@color_white)

    camera = %S.Camera3D{
      position: %S.Vector3{x: 10.0, y: 10.0, z: 10.0},
      target: %S.Vector3{x: 0.0, y: 0.0, z: 0.0},
      up: %S.Vector3{x: 0.0, y: 1.0, z: 0.0},
      fovy: 45.0,
      projection: 0
    }

    update_camera(camera, 4)
    cube_position = %S.Vector3{x: c, y: c, z: 1.0}
    begin_mode_3d(camera)
    draw_cube(cube_position, 0.1, 0.1, 0.1, @color_gray)

    1..10
    |> Enum.each(fn _ ->
      cube()
    end)

    draw_grid(100, 1.0)
    end_mode_3d()
  end

  def cube() do
    x = Enum.random(-100..100) * 0.1
    y = Enum.random(-100..100) * 0.1
    z = Enum.random(-100..100) * 0.1
    cube_position = %S.Vector3{x: x, y: y, z: z}
    draw_cube(cube_position, 0.1, 0.1, 0.1, @color_gray)
  end
end

実行

$ mix run --eval "Rayex3d.main()"

image.png

defp draw(c) do の

clear_background(@color_white)を有効にすると1フレームごと画面がリセットします

image.png

7
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?