/* Copyright (C) 2011, Stephane Bisaro, aka iri License : you can do what you want with this source code This code is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */ /* SO3Engine API First example to learn how to : - create and destroy a 3d scene - add basics objects : shell, camera, ... - add an 3d object and its resources - apply a rotation (or others) on an object - performs a rendering A 3d scene contains all your objects and resources. A shell is a point (~ dummy) without father but you define a position in the 3d scene. A shell is a grand-father. There is at least one shell to link 3d objects and create a tree. You need a camera to "see" (so, to display on the screen) the 3d scene This camera (you can add several cameras) should be put in a position and an orientation. For that, you need to create a shell : you link the camera to this last shell. You set position and orientation to the shell and the camera will follow it. You need too a default light, usually an ambiant. Independently of the scene, you should create a viewport : this is like a window for displaying what the camera see. Next, you could load all resources (materials, shaders, ...) used by unloaded 3d objects. Otherwise, you should link each resource to each 3d object. Then, you load 3d objects. If theirs resources has been already loaded, links are automatic. Finally, you can define all needed callbacks like pre- post- rendering. Don't forget to destroy groups, buffer, viewport and scene before close the VM */ /* Note : i post this code originally on the forum : http://www.scolring.org/forum/viewtopic.php?id=314 */ var pathRsc = "tutorials/so3engine/1/rsc/";; typeof win = ObjWin;; /* 3D variables */ var scnUnit = 1.0;; /* 1 meter, SO3Engine takes the meter as the unit */ typeof scn = SO3_SCENE;; /* 3d scen */ typeof camera = SO3_OBJECT;; /* default camera */ typeof shellCamera = SO3_OBJECT;; /* shell for the camera */ typeof shell = SO3_OBJECT;; /* grand-father */ typeof cylinder = SO3_OBJECT;; /* our poor object ! */ typeof viewport = SO3_VIEWPORT;; /* to display what the camera see */ /* Buffer */ typeof buffer = SO3_BUFFER;; /* Destroy the application */ fun end ()= SO3ViewportDestroy viewport; SO3GroupDelete scn "GrpEx1resources"; SO3SceneDelete scn; SO3DestroyBuffer buffer; _closemachine;; fun win_end (obj, user_data)= end;; /* Pre rendering Perform a rotation ... */ fun cbPreRender (obj, user_data, elapsed_time) = SO3ObjectRotate cylinder 0.1 [1.0 1.0 1.0] SO3_LOCAL_TS; 0;; /* Post rendering Here, nothing */ fun cbPostRender (obj, user_data, elapsed_time) = 0;; /* Defines the 3d callbacks */ fun def3dCallbacks ()= /* Sets the callback for our pre render event The prototype of thecallback is : fun [SO3_SCENE u0 I] u1 the supplemental parameter is the elapsed time (in ms) since the last render */ SO3CbScenePreRender scn @cbPreRender 0; /* Sets the callback for our post render event The prototype of thecallback is : fun [SO3_SCENE u0 I] u1 the supplemental parameter is the elapsed time (in ms) since the last render */ SO3CbScenePostRender scn @cbPostRender 0; 0;; fun load3dResources () = /* create a group (see Ogre doc) to our 3d resources) Several groups can be created */ SO3GroupCreate scn "GrpEx1resources"; /* Load any 3d resources in our scn. Some flags are availables : SO3_RESOURCE_MESH SO3_RESOURCE_MATERIAL SO3_RESOURCE_TEXTURE SO3_RESOURCE_SKELETON SO3_RESOURCE_GPUPROGRAM SO3_RESOURCE_HIGHLEVELGPUPROGRAM SO3_RESOURCE_PARTICLES SO3_RESOURCE_PARTICLE_SYSTEM */ SO3SceneLoadResource scn "GrpEx1resources" _checkpack strcat pathRsc "scene.material" SO3_RESOURCE_MATERIAL; SO3SceneLoadResource scn "GrpEx1resources" _checkpack strcat pathRsc "cylinder.mesh" SO3_RESOURCE_MESH; /* Createan SO3 object from the mesh entity */ set cylinder = SO3SceneLoadEntity scn "GrpEx1resources" "Cylinder" _checkpack strcat pathRsc "cylinder.mesh"; /* Link our object to the shell */ SO3ObjectLink cylinder shell; SO3ObjectSetPosition cylinder [0.0 0.0 0.0]; SO3ObjectSetScale cylinder [2.0 2.0 15.0]; 0;; fun init3d ()= /* create our new scene */ set scn = SO3SceneCreate _channel "example 1"; /* Create and define our camera */ set camera = SO3CameraCreate scn "camera_0"; set shellCamera = SO3SceneNodeCreate scn "shell_camera"; SO3ObjectRotatePitch shellCamera (-.0.5) SO3_LOCAL_TS; SO3ObjectLink camera shellCamera; /* first : the child, second, the father */ SO3ObjectSetPosition camera [0.0 0.0 (110.0 *. scnUnit)]; /* Set an ambient light of our scene */ SO3SceneSetAmbientLight scn 0x000000; /* Create and define a viewport */ set viewport = SO3ViewportCreate _channel buffer camera 0.0 0.0 1.0 1.0 0; SO3ViewportSetBackgroundColor viewport make_rgba 200 200 200 0; /* create a shell (3d objects) */ set shell = SO3SceneNodeCreate scn "shell"; SO3ObjectSetPosition shell [0.0 0.0 0.0]; /* center of the world */ load3dResources; def3dCallbacks; 0;; fun initBuffer ()= let _GETwindowPositionSize win -> [_ _ w h] in set buffer = SO3BufferCreate _channel win 0 0 w h; 0;; fun main ()= _showconsole; set win = _CRwindow _channel nil 50 50 500 400 WN_NORMAL "SO3Engine : example 1"; _CBwinDestroy win @win_end 0; initBuffer; init3d; 0;;