11.scene manager

本文介绍了如何使用Ogre游戏引擎手动选择场景管理器并加载地图,以及如何创建模型,包括平面、实体、光照和手动对象,并利用StaticGeometry提高渲染效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进入新的一章,建立新一个空项目,依然基于ExampleApplication,createscene()为空。

1.手选scene manager和加载地图

	virtual void chooseSceneManager(void)
	{
		ResourceGroupManager::getSingleton().addResourceLocation("../../media/packs/chiropteraDM.pk3",
			"Zip", ResourceGroupManager::getSingleton().getWorldResourceGroupName(), true);
		ResourceGroupManager::getSingleton().initialiseResourceGroup(ResourceGroupManager::getSingleton().getWorldResourceGroupName());
		mSceneMgr = mRoot ->createSceneManager("BspSceneManager");
		mSceneMgr ->setWorldGeometry("maps/chiropteradm.bsp");
	}

2.手动创建模型

	void createScene()
	{
		Ogre::Plane plane(Vector3::UNIT_Y, -10);
		Ogre::MeshManager::getSingleton().createPlane("plane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
			plane, 1500, 1500, 200, 200, true, 1, 5, 5, Vector3::UNIT_Z);
		Ogre::Entity* ent = mSceneMgr ->createEntity("GrassPlane", "plane");
		mSceneMgr ->getRootSceneNode() ->createChildSceneNode() ->attachObject(ent);
		ent ->setMaterialName("Examples/GrassFloor");

		Ogre::Light* light = mSceneMgr ->createLight("Light1");
		light ->setType(Ogre::Light::LT_DIRECTIONAL);
		light ->setDirection(Ogre::Vector3(1, -1, 0));

		Ogre::ManualObject* manual = mSceneMgr ->createManualObject("grass");
		manual ->begin("Examples/GrassBlades", RenderOperation::OT_TRIANGLE_LIST);

		manual ->position(5.0, 0.0, 0.0);
		manual ->textureCoord(1, 1);
		manual ->position(-5.0, 10.0, 0.0);
		manual ->textureCoord(0, 0);
		manual ->position(-5.0, 0.0, 0.0);
		manual ->textureCoord(0, 1);
		manual ->position(5.0, 10.0, 0.0);
		manual ->textureCoord(1, 0);

		manual ->position(2.5, 0.0, 4.3);
		manual ->textureCoord(1, 1);
		manual ->position(-2.5, 10.0, -4.3);
		manual ->textureCoord(0, 0);
		manual ->position(-2.0, 0.0, -4.3);
		manual ->textureCoord(0, 1);
		manual ->position(2.5, 10.0, 4.3);
		manual ->textureCoord(1, 0);

		manual ->position(2.5, 0.0, -4.3);
		manual ->textureCoord(1, 1);
		manual ->position(-2.5, 10.0, 4.3);
		manual ->textureCoord(0, 0);
		manual ->position(-2.0, 0.0, 4.3);
		manual ->textureCoord(0, 1);
		manual ->position(2.5, 10.0, -4.3);
		manual ->textureCoord(1, 0);

		manual ->index(0);
		manual ->index(1);
		manual ->index(2);

		manual ->index(0);
		manual ->index(3);
		manual ->index(1);

		manual ->index(4);
		manual ->index(5);
		manual ->index(6);

		manual ->index(4);
		manual ->index(7);
		manual ->index(5);

		manual ->index(8);
		manual ->index(9);
		manual ->index(10);

		manual ->index(8);
		manual ->index(11);
		manual ->index(9);

		manual ->end();
		manual ->convertToMesh("BladesOfGrass");

		Ogre::StaticGeometry* field = mSceneMgr ->createStaticGeometry("FieldOfGrass");

		for(int i = 0; i < 50; i++)
		{
			for(int j = 0; j < 50; j++)
			{
				Ogre::Entity* ent = mSceneMgr ->createEntity("BladesOfGrass");
				field ->addEntity(ent, Ogre::Vector3(i * 5, -10, j * 5));
			}
		}
		field ->build();
	}
};
这次主要用到了staticGeometry,一般来说,这样可以大大加快渲染速度。
根据Main类和Controller类代码,修改ManagerLoginController类代码(只修改这个类,不增添额外java类) Main类:package opera; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("/view/start.fxml")); primaryStage.setTitle("图书管理系统"); primaryStage.setScene(new Scene(root, 800, 600)); primaryStage.setResizable(false); primaryStage.show(); } public static void main(String[] args) { launch(args); } } Controller类:package opera; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.stage.Stage; import java.io.IOException; import javafx.event.ActionEvent; // 添加必要的导入 public class Controller { protected Stage getCurrentStage(Parent root) { return (Stage) root.getScene().getWindow(); } protected void showErrorAlert(String message) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("错误"); alert.setHeaderText(null); alert.setContentText(message); alert.showAndWait(); } protected void showInfoAlert(String message) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("提示"); alert.setHeaderText(null); alert.setContentText(message); alert.showAndWait(); } protected void switchScene(String fxmlPath) throws IOException { Parent root = FXMLLoader.load(getClass().getResource(fxmlPath)); Stage stage = getCurrentStage(root); stage.setScene(new Scene(root)); stage.show(); } // 添加缺失的事件处理方法 public void handleUserLogin(ActionEvent event) { showInfoAlert("用户登录功能"); // 这里添加实际登录逻辑 } public void handleManagerLogin(ActionEvent event) { showInfoAlert("管理员登录功能"); // 这里添加实际登录逻辑 } public void handleExit(ActionEvent event) { Stage stage = (Stage) ((javafx.scene.control.Button) event.getSource()).getScene().getWindow(); stage.close(); } } ManagerLoginController类:package opera; import javafx.fxml.FXML; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; public class ManagerLoginController extends Controller { @FXML private TextField managerIdField; @FXML private PasswordField managerPasswordField; @FXML private void handleManagerLogin() { try { String managerId = managerIdField.getText(); String password = managerPasswordField.getText(); if (managerId.isEmpty() || password.isEmpty()) { showErrorAlert("管理员ID和密码不能为空"); return; } if (AuthService.validateManager(managerId, password)) { switchScene("/view/manager_dashboard.fxml"); } else { showErrorAlert("管理员认证失败"); } } catch (Exception e) { showErrorAlert("系统错误:" + e.getMessage()); } } }
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值