osg真helloworld

本文档详细介绍了如何下载OpenSceneGraph 3.6.5版本,并在Visual Studio环境中进行配置。接着展示了一个简单的HelloWorld程序,包括引入必要的库,设置纹理并加载资源文件,最后成功运行显示3D场景。
摘要由CSDN通过智能技术生成


一、3.6.5 下载

http://www.openscenegraph.com/index.php/download-section/stable-releases


二、vs 工程 配置

bin
include
lib
在这里插入图片描述
include、lib添加至工程 VC++目录下的 包含目录、库目录
bin目录可直接拷贝至 vs工程 Debug 目录下

三、HelloWorld

1.引入库

代码如下(WIN32控制台程序 或 C/C++预处理器定义添加WIN32):

#include <string>

#include <osgViewer/Viewer>
#include <osg/Texture2D>
#include <osgDB/ReadFile>
#include <osg/Geode>
#include <osg/ShapeDrawable>

// bin 目录或可添加至 环境变量
int main()
{
	/*std::string cowData = R"(E:\OsgProjects\OpenSceneGraph-3.6.5-VC2022-64-Debug\OpenSceneGraph-Data-3.4.0\OpenSceneGraph-Data\cow.osg)";
	osgViewer::Viewer viewer;
	viewer.setSceneData(osgDB::readNodeFile(cowData));
	return viewer.run();*/

	std::string world_png = R"(E:\OsgProjects\OpenSceneGraph-3.6.5-VC2022-64-Debug\png\world.png)";

	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	viewer->setUpViewInWindow(50, 50, 800, 600);
	
	// 叶节点
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	
	// Texture
	osg::ref_ptr<osg::Texture> texture = new osg::Texture2D(osgDB::readRefImageFile(world_png));
	geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(), 1.0f)));
	auto stateSet = geode->getOrCreateStateSet();
	stateSet->setTextureAttributeAndModes(0, texture);
	viewer->setSceneData(geode);
	return viewer->run();
}

2.运行

在这里插入图片描述

参考:https://blog.csdn.net/forcsdn_tang/article/details/122273695

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值