OSG学习:纹理映射(二)——一维/二维/简单立方图纹理映射

ゞ 浴缸里的玫瑰 2022-05-21 00:46 1085阅读 0赞

以下内容来自:

1、《OpenSceneGraph三维渲染引擎编程指南》肖鹏 刘更代 徐明亮 清华大学出版社

2、《OpenSceneGraph三维渲染引擎设计与实践》王锐 钱学雷 清华大学出版社

3、自己的总结

下载完整工程OSG_10_Texture1D2DCubMpa

创建C++项目后,首先需要配置OSG环境,具体步骤看OSG学习:WIN10系统下OSG+VS2017编译及运行第六步:新建OSG项目测试。

直接来看代码:

  1. // stdafx.h
  2. #include <osg/Geode>
  3. #include <osg/Texture1D> //一维纹理映射
  4. #include <osg/Texture2D> //二维纹理映射
  5. #include <osg/TextureCubeMap> //立方体纹理映射
  6. #include <osg/TexGen> //指定用于自动生成纹理坐标的函数,可以设置纹理的计算方式是以物体坐标空间还是相机坐标空间来进行不同的计算
  7. #include <osg/ShapeDrawable> //预定义几何体类,派生自osg::Drawable类。OSG中使用该类来将OSG内嵌的预定义几何体与osg::Drawable关联以渲染这些几何体
  8. #include <osg/MatrixTransform>
  9. #include <osgViewer/Viewer>
  10. #include <osgDB/ReadFile>
  11. #include <osgDB/WriteFile>
  12. #include <osgUtil/Optimizer>
  13. //.cpp
  14. /*
  15. *设置一维纹理的数据 函数定义的一种方法
  16. *一维纹理只有一个方向有像素变化
  17. *因此此处手动为其指定内容:
  18. *新建一个Image对象并向其中传入颜色数据
  19. *然后设置一维纹理的边界截取方式为重复映射
  20. *则色带被反复绘制在物体上
  21. *
  22. *函数参数为节点的渲染状态,无返回值
  23. */
  24. void createTexture1D(osg::StateSet &ss)
  25. {
  26. //实例化一个osg::Image类,用于封装存储纹理图像数据的图像类
  27. osg::ref_ptr<osg::Image> image = new osg::Image();
  28. /*virtual void osg::Image::setImage(int s, int t, int r,
  29. GLint internalTextureformat,
  30. GLenum pixelFormat,
  31. GLenum type,
  32. unsigned char *data,
  33. AllocationMode mode,
  34. int packing = 1)
  35. */
  36. image->setImage(256, 1, 1,
  37. GL_RGBA, GL_RGBA,
  38. GL_UNSIGNED_BYTE,
  39. new unsigned char[4 * 256],
  40. osg::Image::USE_NEW_DELETE);
  41. unsigned char *ptr = image->data();
  42. for (unsigned int i = 0; i < 256; ++i)
  43. {
  44. *ptr++ = i;
  45. *ptr++ = i;
  46. *ptr++ = 255;
  47. *ptr++ = 255;
  48. }
  49. osg::ref_ptr<osg::Texture1D> texture = new osg::Texture1D();
  50. texture->setImage(image.get());
  51. /*设置纹理的坐标/包装模式
  52. enum WrapParmeter
  53. {
  54. WRAP_S, //x轴
  55. WRAP_T, //y轴
  56. WRAP_T //z轴
  57. };
  58. enum WrapMode
  59. {
  60. CLAMP, //截取
  61. CLAMP_TO_EDGE, //边框始终被忽略
  62. CLAMP_TO_BORDER, //它使用的纹理取自图像的边框,没有边框就使用常量边框的颜色
  63. REPEAT, //纹理的重复映射
  64. MIRROR //纹理镜像的重复映射
  65. };
  66. */
  67. texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
  68. /*设置纹理的过滤方法/过滤处理
  69. enum FilterParameter
  70. {
  71. MIN_FILTER, //缩小
  72. MAG_FILTER //放大
  73. };
  74. enum FilterMode
  75. {
  76. LINEAR, //以周围4个像素的平均值作为纹理
  77. LINEAR_MIPMAP_LINEAR, //使用线性均和计算两个纹理的值
  78. LINEAR_MIPMAP_NEAREST, //线性地改写临近的纹理单元值
  79. NEAREST, //取比较接近的像素作为纹理
  80. NEAREST_MIPMAP_LINEAR, //在两个纹理中选择最临近的纹理,并取它们之间的线性均和值
  81. NEAREST_MIPMAP_NEAREST //选择最临近的纹理单元值
  82. };
  83. */
  84. texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
  85. ss.setTextureAttributeAndModes(0, texture.get());
  86. }
  87. /*
  88. *设置一维纹理的数据 函数定义的另一种方法
  89. *
  90. *函数返回值为渲染状态
  91. */
  92. osg::ref_ptr<osg::StateSet> createTexture1D()
  93. {
  94. //创建贴图对象,实际上是一个高度为1的二维图像
  95. osg::ref_ptr<osg::Image> image = new osg::Image;
  96. //为image分配一个空间
  97. image->allocateImage(1024, 1, 1, GL_RGBA, GL_FLOAT);
  98. //设置纹理图像数据格式RGBA
  99. image->setInternalTextureFormat(GL_RGBA);
  100. //为image填充数据
  101. osg::Vec4 *dataPtr = (osg::Vec4 *)image->data();
  102. for (int i = 0; i < 1024; ++i)
  103. {
  104. *dataPtr++ = osg::Vec4(1.0f, 0.5f, 0.8f, 0.5f);
  105. }
  106. //创建一维纹理
  107. osg::ref_ptr<osg::Texture1D> texture = new osg::Texture1D();
  108. //设置环绕模式
  109. texture->setWrap(osg::Texture1D::WRAP_S, osg::Texture1D::MIRROR);
  110. //设置滤波
  111. texture->setFilter(osg::Texture1D::MIN_FILTER, osg::Texture1D::LINEAR);
  112. //设置贴图
  113. texture->setImage(image.get());
  114. //设置自动纹理坐标,并制定相关的平面
  115. osg::ref_ptr<osg::TexGen> texgen = new osg::TexGen;
  116. /*设置纹理坐标的自动生成模式
  117. enum Mode
  118. {
  119. OBJECT_LINEAR, //物体线性,纹理贴图与移动物体保持固定
  120. EYE_LINEAR, //产生移动物体的动态轮廓线
  121. SPHERE_MAP, //球体贴图
  122. NORMAL_MAP, //法线贴图,用于立方图纹理
  123. REFLECTION_MAP //反射贴图
  124. };
  125. */
  126. texgen->setMode(osg::TexGen::OBJECT_LINEAR);
  127. /*纹理坐标
  128. enum Coord
  129. {
  130. S, //x
  131. T, //y
  132. R, //z
  133. Q //w
  134. };
  135. */
  136. texgen->setPlane(osg::TexGen::S, osg::Plane(0.0f, 0.0f, 1.0f, -10000));
  137. osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
  138. //启用一维纹理
  139. stateset->setTextureAttribute(0, texture.get(), osg::StateAttribute::OVERRIDE);
  140. stateset->setTextureMode(0, GL_TEXTURE_1D, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
  141. //启用纹理生成器
  142. stateset->setTextureAttribute(0, texgen.get(), osg::StateAttribute::OVERRIDE);
  143. stateset->setTextureMode(0, GL_TEXTURE_GEN_S, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
  144. return stateset.get();
  145. }
  146. /*
  147. *设置二维纹理的数据
  148. *直接使用已有的图像文件即可
  149. *还设置了纹理的边界颜色
  150. *
  151. *函数返回值为纹理
  152. */
  153. osg::ref_ptr<osg::Texture2D> createTexture2D()
  154. {
  155. osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D();
  156. texture->setImage(osgDB::readImageFile("Images/clockface.jpg"));
  157. texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
  158. texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
  159. texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
  160. texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
  161. texture->setBorderColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));
  162. return texture.get();
  163. }
  164. /*
  165. *设置立方体纹理的数据
  166. *立方图纹理需要6个面的图像数据
  167. *常用于环境贴图的实现
  168. *额外的渲染属性TexGen
  169. *用于自动为立方图纹理的作用对象生成纹理坐标
  170. *
  171. *函数返回值为渲染状态
  172. */
  173. osg::ref_ptr<osg::StateSet> createTextureCubeMap()
  174. {
  175. osg::ref_ptr<osg::TextureCubeMap> texture = new osg::TextureCubeMap();
  176. texture->setImage(osg::TextureCubeMap::POSITIVE_X, osgDB::readImageFile("Cubemap_snow/posx.jpg"));
  177. texture->setImage(osg::TextureCubeMap::NEGATIVE_X, osgDB::readImageFile("Cubemap_snow/negx.jpg"));
  178. texture->setImage(osg::TextureCubeMap::POSITIVE_Y, osgDB::readImageFile("Cubemap_snow/posy.jpg"));
  179. texture->setImage(osg::TextureCubeMap::NEGATIVE_Y, osgDB::readImageFile("Cubemap_snow/negy.jpg"));
  180. texture->setImage(osg::TextureCubeMap::POSITIVE_Z, osgDB::readImageFile("Cubemap_snow/posz.jpg"));
  181. texture->setImage(osg::TextureCubeMap::NEGATIVE_Z, osgDB::readImageFile("Cubemap_snow/negz.jpg"));
  182. texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
  183. texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
  184. texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
  185. texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
  186. texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
  187. osg::ref_ptr<osg::StateSet> sta = createTexture1D();
  188. sta->setTextureAttributeAndModes(0, texture.get());
  189. sta->setTextureAttributeAndModes(0, new osg::TexGen);
  190. return sta.get();
  191. }
  192. int main()
  193. {
  194. //第1个一维纹理图像
  195. osg::ref_ptr<osg::Geode> texl1D = new osg::Geode();
  196. //使用createTexturedQuadGeometry()快速构建一个四边形,并设置其纹理坐标范围为(0, 0)到(3,1),以实现水平方向的重复条带
  197. //createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float l, float b, float r, float t);用于创建具有纹理坐标的四边形几何图形
  198. //参数1是从左下角起始的点,是这个起始点的世界坐标,这个起点按照参数2和参数3来画矩形,纹理坐标范围为(l, b)到(r, t)
  199. texl1D->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(-3.0, 0.0, -0.5), osg::Vec3(1.0, 0.0, 0.0), osg::Vec3(0.0, 0.0, 1.0), 0.0, 0.0, 3.0, 1.0));
  200. //函数参数为当前节点的渲染状态,无返回值,则函数内进行渲染
  201. createTexture1D(*(texl1D->getOrCreateStateSet()));
  202. //第2个一维纹理图像
  203. osg::ref_ptr<osg::Node> texl1D2 = osgDB::readNodeFile("cessna.osg");
  204. //函数返回值为渲染状态,需要设置节点的渲染状态
  205. osg::ref_ptr<osg::StateSet> sta = createTexture1D();
  206. texl1D2->setStateSet(sta.get());
  207. //使用矩阵转换节点缩小模型,一般使用矩阵转换节点可以缩放模型(缩放到原来的多少倍)、更改模型的位置(向某个方向移动多少)、绕轴旋转
  208. //不能分成三个写,这样最后一个会把前面的覆盖,由于这是矩阵乘法,因此顺序不同结果也不同
  209. //setMatrix(osg::Matrix::scale(osg::Vec3(double sx, double sy, double sz)) * osg::Matrix::translate(osg::Vec3(double tx, double ty, double tz)) * osg::Matrix::rotate(double angle, const Vec3d &axis));
  210. osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform();
  211. mt->setMatrix(osg::Matrix::scale(0.05, 0.05, 0.05));
  212. mt->addChild(texl1D2);
  213. //第1个二维纹理图像
  214. osg::ref_ptr<osg::Geode> texl2D = new osg::Geode();
  215. //设置四边形的纹理坐标范围为(-0.1, -0.1)到(1.1, 1.1),因此会额外绘制出纹理的边界
  216. texl2D->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(2, 0.0, -0.5), osg::Vec3(1.0, 0.0, 0.0), osg::Vec3(0.0, 0.0, 1.0), -0.1, -0.1, 1.1, 1.1));
  217. //函数返回值为纹理,它为渲染状态的属性,因此需要先获取或设置节点的渲染状态,然后设置渲染状态的属性和模式
  218. osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D();
  219. tex = createTexture2D();
  220. osg::ref_ptr<osg::StateSet> sta2 = new osg::StateSet();
  221. sta2->setTextureAttributeAndModes(0, createTexture2D());
  222. texl2D->setStateSet(sta2.get());
  223. //上面5句可以合并成下面1句
  224. //texl2D->getOrCreateStateSet()->setTextureAttributeAndModes(0, createTexture2D());
  225. //第1个立方体纹理图像
  226. osg::ref_ptr<osg::Geode> texlCube = new osg::Geode();
  227. texlCube->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(5.0, 0.0, 0.0), 0.7)));
  228. //函数返回值为渲染状态,需要设置节点的渲染状态
  229. osg::ref_ptr<osg::StateSet> sta1 = new osg::StateSet();
  230. texlCube->setStateSet(createTextureCubeMap());
  231. osg::ref_ptr<osg::Group> root = new osg::Group();
  232. root->addChild(texl1D.get());
  233. root->addChild(mt.get());
  234. root->addChild(texl2D.get());
  235. root->addChild(texlCube.get());
  236. //优化场景数据
  237. osgUtil::Optimizer optimizer;
  238. optimizer.optimize(root.get());
  239. osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
  240. viewer->setSceneData(root.get());
  241. viewer->realize();
  242. return viewer->run();
  243. }

显示结果:

70

发表评论

表情:
评论列表 (有 0 条评论,1085人围观)

还没有评论,来说两句吧...

相关阅读