1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Portable game to destroy asteroids
16 module model_viewer
is
17 app_name
"Model Viewer"
18 app_namespace
"org.nitlanguage.model_viewer"
19 app_version
(1, 0, git_revision
)
23 import gamnit
::landscape
29 # All available models
30 var models
: Array[Model] = [
31 new LeafModel(new Cube, new Material),
32 new LeafModel(new UVSphere(4.0, 32, 16), new Material),
33 new LeafModel(new UVSphere(4.0, 32, 16), new NormalsMaterial),
34 new Model("models/Tree_01.obj"),
35 new Model("models/Oak_Fall_01.obj"),
36 new Model("models/Quandtum_BA-2_v1_1.obj"),
39 # Index of the current model in `models`
42 # Texture "Previous model"
43 var ui_prev
= new Texture("ui/prev.png")
45 # Texture "Next model"
46 var ui_next
= new Texture("ui/next.png")
53 var logo
= new Texture("splash.png")
54 show_splash_screen logo
56 # Load all models passed as command line argument
57 for arg
in args
.to_a
.reversed
do
58 # Force an absolute path, this only works on desktop, but so does command args
61 var model
= new Model(arg
)
65 world_camera
.near
= 0.1
66 world_camera
.far
= 100.0
68 for model
in models
do
70 if model
.errors
.not_empty
then print_error model
.errors
.join
("\n")
73 # Display the first model
74 model
= models
[model_index
]
77 # Use 800 px in height as screen reference
78 ui_camera
.reset_height
800.0
80 var prev_sprite
= new Sprite(ui_prev
,
81 ui_camera
.bottom_left
.offset
(200, 40, 0))
82 prev_sprite
.scale
= 0.5
83 ui_sprites
.add prev_sprite
85 var next_sprite
= new Sprite(ui_next
,
86 ui_camera
.bottom_right
.offset
(-165, 40, 0))
87 next_sprite
.scale
= 0.5
88 ui_sprites
.add next_sprite
91 # Set the currently displayed model
92 fun model
=(model
: Model)
94 if model
isa ModelAsset then print
"Model: {model.path}"
96 var actor
= new Actor(model
, new Point3d[Float](0.0, 0.0, 0.0))
99 actor
.center
.y
-= model
.center
.y
102 var height
= model
.dimensions
.x
103 height
= height
.max
(model
.dimensions
.y
)
104 height
= height
.max
(model
.dimensions
.z
)
105 world_camera
.reset_height
(height
* 1.5)
111 # Cycle to the next or previous model, changing the index by `d`
112 fun cycle_model
(d
: Int)
114 model_index
= (model_index
+ d
+ models
.length
) % models
.length
115 model
= models
[model_index
]
118 redef fun accept_event
(event
)
120 var display
= display
121 if display
== null then return super
123 if event
isa QuitEvent then
125 else if event
isa KeyEvent and event
.is_down
then
126 if event
.is_arrow_right
then
128 else if event
.is_arrow_left
then
131 else if event
isa PointerEvent and not event
.is_move
and event
.depressed
then
132 if event
.x
.to_i
> display
.width
/ 2 then
140 private var clock
= new Clock
146 var t
= clock
.total
.to_f
151 # Move the light source
152 var dist_to_light
= 20.0
154 light
.position
.x
= dist_to_light
* t
.cos
155 light
.position
.y
= 4.0
156 light
.position
.z
= dist_to_light
* t
.sin