盖莫游戏引擎2.1.1的地形小例
代码如下:
1 #include <GEngine/Gaimo.hpp>
2
3 using namespace std;
4 using namespace core;
5 void RotateView(float angle, float x, float y, float z);
6 //! 处理键盘响应
7 void KeyCheck(RefPtr<Input> input);
8 RefPtr<Camera> camera;
9 RefPtr<Text> font;
10 char textbuf[20]="";
11 float yrot=0.0f;
12 float dv=0.0f;
13 int Main()
14 {
15 //! 初始化引擎设备并得到设备指针
16 RefPtr<Device> device = InitDevice("地形测试");
17 //! 得到引擎场景指针
18 RefPtr<SceneManager> scenemanager = device->GetSceneManager();
19 //! 得到引擎资源指针
20 RefPtr<ResourceManager> resourcemanager = device->GetResourceManager();
21 //! 获取图形管理器指针
22 RefPtr<ImageManager> imagemanager = resourcemanager->GetImageManager();
23 //! 得到视频驱动器指针
24 RefPtr<VideoDriver> videodriver = device->GetVideoDriver();
25 font= resourcemanager->GetTextManager()->CreateText("heiti","黑体",16);
26
27 //! 得到地形
28 RefPtr<Terrain> terrain = scenemanager->GetTerrain();
29 //! 载入高程图
30 terrain->LoadHeightMap("..\\terrain\\terrain.raw",1024,16);
31 //! 获取地形图形指针
32 RefPtr<Image> mainimage = imagemanager->CreateObject("mainimage", "..\\terrain\\terrain.bmp");
33 RefPtr<Image> detailimage = imagemanager->CreateObject("detailimage","..\\terrain\\detail.bmp");
34
35 //! 获取纹理管理器
36 RefPtr<TextureManager> texturemanager = resourcemanager->GetTextureManager();
37 //! 获取纹理指针
38 RefPtr<Texture> maintexture = texturemanager->CreateTexture("maintex",mainimage);
39 RefPtr<Texture> detailtexture = texturemanager->CreateTexture("detailtex",detailimage);
40 terrain->LoadTexture(maintexture,detailtexture);
41
42 //! 获取摄像机指针
43 camera = scenemanager->GetCameraManager()->CreateCamera("camera",Vector3f(300,10,300),
44 Vector3f(10,10,0));
45 camera->SetViewPort(Recti(0,0,640,480));
46 camera->SetPerspective(45.0f,640.0f/480.0f,0.1f,600.0f);
47
48 device->GetVideoDriver()->EnableCulling();
49
50 //! 获取雾指针
51 RefPtr<Fog> fog = resourcemanager->GetFog("..\\script//fog.lua","fog");
52 fog->Render();
53
54 Vector3f camera_pos,camera_view;
55 BEGIN_LOOP(device)
56 videodriver->SetClearBuffer(ENGINE_CLEAR_COLOR | ENGINE_CLEAR_DEPTH);
57 camera->Render();
58 terrain->Render();
59
60 KeyCheck(device->GetInput());
61
62 camera_pos = camera->GetPosition();
63
64 sprintf(textbuf,"camerapos:(%f,%f,%f)",camera_pos.x,camera_pos.y,camera_pos.z);
65 font->Render(10,15,textbuf);
66 camera_view = camera->GetView();
67 sprintf(textbuf,"cameraview:(%f,%f,%f)",camera_view.x,camera_view.y,camera_view.z);
68 font->Render(10,35,textbuf);
69 RotateView(yrot,0.0f,1.0f,0.0f);
70 camera->MoveCamera(dv);
71 sprintf(textbuf,"nowheight:%f",terrain->GetAveHeight(camera_pos.x,camera_pos.z));
72 font->Render(10,55,textbuf);
73 camera_pos = camera->GetPosition();
74 camera->SetPosition(Vector3f(camera_pos.x,terrain->GetAveHeight(camera_pos.x,camera_pos.z)+30,camera_pos.z));
75 //! 保持视线水平
76 camera_view = camera->GetView();
77 camera->SetView(Vector3f(camera_view.x,terrain->GetAveHeight(camera_pos.x,camera_pos.z)+30,camera_view.z));
78 dv = 0;
79 END_LOOP(device)
80 return 0;
81 }
代码看似很多
其实很多代码都是简单的取指针操作
下面是贴图