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
)
21 android_manifest_activity
"""android:screenOrientation="landscape""""
31 # All available models
32 var models: Array[Model] = [
33 new LeafModel(new Cube, new Material),
34 new LeafModel(new UVSphere(4.0, 32, 16), new Material),
35 new LeafModel(new UVSphere(4.0, 32, 16), new NormalsMaterial),
36 new Model("models
/Tree_01.obj
"),
37 new Model("models
/Oak_Fall_01.obj
"),
38 new Model("models
/Quandtum_BA-2_v
1_1.obj
"),
41 # Index of the current model in `models`
44 # Texture "Previous model
"
45 var ui_prev = new Texture("ui
/prev
.png
")
47 # Texture "Next model
"
48 var ui_next = new Texture("ui
/next
.png
")
55 var logo = new Texture("splash
.png
")
56 show_splash_screen logo
58 # Load all models passed as command line argument
59 for arg in args.to_a.reversed do
60 # Force an absolute path, this only works on desktop, but so does command args
63 var model = new Model(arg)
67 world_camera.near = 0.1
68 world_camera.far = 100.0
70 for model in models do
72 if model.errors.not_empty then print_error model.errors.join("\n
")
75 # Display the first model
76 model = models[model_index]
79 # Use 800 px in height as screen reference
80 ui_camera.reset_height 800.0
82 var prev_sprite = new Sprite(ui_prev,
83 ui_camera.bottom_left.offset(200, 40, 0))
84 prev_sprite.scale = 0.5
85 ui_sprites.add prev_sprite
87 var next_sprite = new Sprite(ui_next,
88 ui_camera.bottom_right.offset(-165, 40, 0))
89 next_sprite.scale = 0.5
90 ui_sprites.add next_sprite
93 # Set the currently displayed model
94 fun model=(model: Model)
96 if model isa ModelAsset then print "Model: {model.path}"
98 var actor = new Actor(model, new Point3d[Float](0.0, 0.0, 0.0))
101 actor.center.y -= model.center.y
104 var height = model.dimensions.x
105 height = height.max(model.dimensions.y)
106 height = height.max(model.dimensions.z)
107 world_camera.reset_height(height * 1.5)
113 # Cycle to the next or previous model, changing the index by `d`
114 fun cycle_model(d: Int)
116 model_index = (model_index + d + models.length) % models.length
117 model = models[model_index]
120 redef fun accept_event(event)
122 var display = display
123 if display == null then return super
125 if event isa QuitEvent then
127 else if event isa KeyEvent and event.is_down then
128 if event.is_arrow_right then
130 else if event.is_arrow_left then
133 else if event isa PointerEvent and not event.is_move and event.depressed then
134 if event.x.to_i > display.width / 2 then
142 private var clock = new Clock
148 var t = clock.total.to_f
153 # Move the light source
154 var dist_to_light = 20.0
156 light.position.x = dist_to_light * t.cos
157 light.position.y = 4.0
158 light.position.z = dist_to_light * t.sin