如何用bsf创建第一个3D场景:从零开始的完整教程
如何用bsf创建第一个3D场景:从零开始的完整教程
【免费下载链接】B3DFrameworkModern C++ library for the development of real-time graphical applications项目地址: https://gitcode.com/gh_mirrors/bs/B3DFramework
bsf(B3DFramework)是一个现代化的C++库,专为实时图形应用开发设计。本教程将带你从零开始,使用bsf创建第一个3D场景,涵盖环境搭建、场景初始化、模型加载、光照设置等核心步骤,让你快速掌握这个强大框架的基础用法。
1. 准备工作:搭建开发环境
1.1 获取bsf源代码
首先需要克隆bsf仓库到本地:
git clone https://gitcode.com/gh_mirrors/bs/bsf1.2 编译项目
bsf使用CMake作为构建系统,按照官方文档的编译指南进行编译:
- 详细编译步骤可参考 Documentation/GitHub/compiling.md
- 依赖项信息请查阅 Documentation/GitHub/dependencies.md
2. 初始化应用程序
2.1 入口函数设置
创建一个新的C++文件,包含bsf的入口头文件并定义主函数:
#include "BsEntry.h" using namespace bs; int bs_main(int argc, char* argv[]) { // 应用程序代码将在这里编写 return 0; }2.2 启动应用框架
使用Application::startUp方法初始化bsf框架,设置窗口大小和标题:
Application::startUp( VideoMode(1280, 720), // 窗口分辨率 "bsf第一个3D场景", // 窗口标题 false); // false表示窗口模式3. 创建场景和相机
3.1 场景对象基础
bsf采用实体-组件模型,所有场景元素都是场景对象(SceneObject),通过添加组件实现功能。创建场景对象的基本语法:
HSceneObject so = SceneObject::create("对象名称");3.2 添加相机组件
相机是渲染场景的"眼睛",需要创建相机组件并设置为主相机:
// 创建相机场景对象 HSceneObject cameraSO = SceneObject::create("MainCamera"); // 添加相机组件 HCamera camera = cameraSO->addComponent<CCamera>(); // 设置为主相机 camera->setMain(true); // 定位相机 cameraSO->setPosition(Vector3(40.0f, 30.0f, 230.0f)); // 让相机看向原点 cameraSO->lookAt(Vector3(0, 0, 0));相机组件定义在 Source/Foundation/bsfCore/Components/BsCCamera.h
4. 添加3D模型
4.1 创建可渲染组件
要在场景中显示3D模型,需要使用CRenderable组件:
// 创建模型场景对象 HSceneObject modelSO = SceneObject::create("3DModel"); // 添加可渲染组件 HRenderable renderable = modelSO->addComponent<CRenderable>();4.2 加载网格资源
使用导入器加载FBX模型文件:
// 导入网格资源 HMesh mesh = gImporter().import<Mesh>("models/your_model.fbx"); // 为可渲染组件设置网格 renderable->setMesh(mesh);资源管理详细说明见 Documentation/Manuals/docs/00_User_Manuals/03_Resources/00_resourceBasicsAndImport.md
4.3 创建材质
材质控制模型的外观,使用bsf内置的PBR着色器创建材质:
// 获取内置标准着色器 HShader shader = BuiltinResources::instance().getBuiltinShader(BuiltinShader::Standard); // 创建材质 HMaterial material = Material::create(shader); // 为材质设置纹理 HTexture albedoTex = gImporter().import<Texture>("textures/your_texture.png"); material->setTexture("gAlbedoTex", albedoTex); // 将材质应用到可渲染组件 renderable->setMaterial(material);材质系统详解可参考 Documentation/Manuals/docs/00_User_Manuals/04_Rendering/03_simpleMaterial.md
5. 设置光照
5.1 添加方向光
方向光模拟太阳光,照亮整个场景:
// 创建方向光场景对象 HSceneObject dirLightSO = SceneObject::create("DirectionalLight"); // 添加灯光组件 HLight dirLight = dirLightSO->addComponent<CLight>(); // 设置为方向光 dirLight->setType(LightType::Directional); // 设置光颜色(白色) dirLight->setColor(Color::White); // 设置强度 dirLight->setIntensity(1.0f); // 旋转灯光朝向 dirLightSO->setRotation(Quaternion(Degree(-45), Degree(30), Degree(0)));5.2 添加点光源
点光源从一点向四周发射光线,适合模拟灯泡等光源:
HSceneObject pointLightSO = SceneObject::create("PointLight"); HLight pointLight = pointLightSO->addComponent<CLight>(); pointLight->setType(LightType::Radial); pointLight->setColor(Color(1.0f, 0.8f, 0.6f)); // 暖黄色 pointLight->setIntensity(1000.0f); pointLight->setAttenuationRadius(10.0f); // 光照范围 pointLightSO->setPosition(Vector3(0, 5, 0)); // 放置在模型上方灯光系统完整指南见 Documentation/Manuals/docs/00_User_Manuals/04_Rendering/05_lights.md
6. 运行主循环
完成场景设置后,启动应用程序主循环:
// 运行主循环 Application::instance().runMainLoop(); // 关闭应用程序 Application::shutDown();7. 完整代码示例
#include "BsEntry.h" #include "Scene/BsSceneObject.h" #include "Components/BsCCamera.h" #include "Components/BsCRenderable.h" #include "Components/BsCLight.h" #include "Resources/BsBuiltinResources.h" #include "Importer/BsImporter.h" using namespace bs; int bs_main(int argc, char* argv[]) { // 启动应用程序 Application::startUp(VideoMode(1280, 720), "bsf第一个3D场景", false); // 创建相机 HSceneObject cameraSO = SceneObject::create("MainCamera"); HCamera camera = cameraSO->addComponent<CCamera>(); camera->setMain(true); cameraSO->setPosition(Vector3(40.0f, 30.0f, 230.0f)); cameraSO->lookAt(Vector3(0, 0, 0)); // 创建3D模型 HSceneObject modelSO = SceneObject::create("3DModel"); HRenderable renderable = modelSO->addComponent<CRenderable>(); // 加载网格和材质 HMesh mesh = gImporter().import<Mesh>("models/your_model.fbx"); HShader shader = BuiltinResources::instance().getBuiltinShader(BuiltinShader::Standard); HMaterial material = Material::create(shader); HTexture albedoTex = gImporter().import<Texture>("textures/your_texture.png"); material->setTexture("gAlbedoTex", albedoTex); renderable->setMesh(mesh); renderable->setMaterial(material); modelSO->setPosition(Vector3(0, 0, 0)); // 创建方向光 HSceneObject dirLightSO = SceneObject::create("DirectionalLight"); HLight dirLight = dirLightSO->addComponent<CLight>(); dirLight->setType(LightType::Directional); dirLight->setColor(Color::White); dirLight->setIntensity(1.0f); dirLightSO->setRotation(Quaternion(Degree(-45), Degree(30), Degree(0))); // 运行主循环 Application::instance().runMainLoop(); Application::shutDown(); return 0; }8. 扩展学习资源
- 官方文档:Documentation/Manuals/docs/index.md
- 场景组件系统:Documentation/Manuals/docs/00_User_Manuals/02_scenesAndComponents.md
- 渲染技术:Documentation/Manuals/docs/00_User_Manuals/12_Advanced_Rendering/00_intro.md
- 物理系统:Documentation/Manuals/docs/00_User_Manuals/09_Physics/00_intro.md
通过本教程,你已经掌握了使用bsf创建基本3D场景的核心步骤。接下来可以尝试添加更多模型、调整材质参数或实现简单的交互逻辑,进一步探索这个强大框架的 capabilities! 🚀
【免费下载链接】B3DFrameworkModern C++ library for the development of real-time graphical applications项目地址: https://gitcode.com/gh_mirrors/bs/B3DFramework
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
