• 六,使用stbimage库加载.hdr


    不改osg的.hdr插件也行,另一种替代方式是stbimage读取后,传递给osg,本质是一样的。
    直接上代码了

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #define STB_IMAGE_IMPLEMENTATION
    #include “stb_image.h”

    int main()
    {
    #if 1
    stbi_set_flip_vertically_on_load(true);
    int width, height, nrComponents;
    std::string strHDRName = “D:/tutorial/LearnOpenGL-master/LearnOpenGL-master/resources/textures/hdr/newport_loft.hdr”;
    float * data = stbi_loadf(strHDRName.c_str(), &width, &height, &nrComponents, 0);
    osg::ref_ptrosg::Image image = new osg::Image;
    unsigned int hdrTexture;
    if (data)
    {
    image->allocateImage(width, height,1, GL_RGB, GL_UNSIGNED_BYTE);
    unsigned char* ptr = (unsigned char*) image->data();
    int nbElements = width * height * 3;

    	float mul = 1.0f;
    	for (int i = 0; i < nbElements; i++) 
    	{
    		float element = *data++;
    		element *= mul;
    
    		if (element < 0)
    		{
    			element = 0;
    		}
    		else if (element > 1)
    		{
    			element = 1;
    		}
    		int intElement = (int)(element * 255.0f);
    		*ptr++ = intElement;
    	}
    	//unsigned char* ptr = image->data();
    	//glGenTextures(1, &hdrTexture);
    	//glBindTexture(GL_TEXTURE_2D, hdrTexture);
    	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16, width, height, 0, GL_RGB, GL_FLOAT, data);
    	//
    	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    	//stbi_image_free(data);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    #else
    osg::ref_ptrosg::Image image = osgDB::readImageFile(strHDRName);
    #endif
    int imageWidth = image->s();
    int imageHeight = image->t();
    int imageDepth = image->r();
    osg::ref_ptrosg::Vec3Array vertices = new osg::Vec3Array;

    vertices->push_back(osg::Vec3(-imageWidth, 0.0f, -imageHeight));
    vertices->push_back(osg::Vec3(imageWidth, 0.0f, -imageHeight));
    vertices->push_back(osg::Vec3(imageWidth, 0.0f, imageHeight));
    vertices->push_back(osg::Vec3(-imageWidth, 0.0f, imageHeight));
    osg::ref_ptr normals = new osg::Vec3Array;
    normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f));
    osg::ref_ptr texcoords = new osg::Vec2Array;
    texcoords->push_back(osg::Vec2(0.0f, 0.0f));
    texcoords->push_back(osg::Vec2(1.0f, 0.0f));
    texcoords->push_back(osg::Vec2(1.0f, 1.0f));
    texcoords->push_back(osg::Vec2(0.0f, 1.0f));
    osg::ref_ptr quad = new osg::Geometry;
    quad->setVertexArray(vertices.get());
    quad->setNormalArray(normals.get());
    quad->setNormalBinding(osg::Geometry::BIND_OVERALL);
    quad->setTexCoordArray(0, texcoords.get());
    quad->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
    
    osg::ref_ptr texture = new osg::Texture2D;
    texture->setImage(image.get());
    texture->setTextureSize(imageWidth, imageHeight);
    texture->setInternalFormat(GL_RGB16);
    //texture->setInternalFormat(GL_RGB16);
    //texture->setInternalFormat(GL_RGBA16F_ARB);
    // texture->setSourceFormat(GL_RGB16);
    // texture->setSourceType(GL_FLOAT);
    texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
    texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
    texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
    texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
    //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, data);
    //
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    osg::ref_ptr root = new osg::Geode;
    root->addDrawable(quad.get());
    root->getOrCreateStateSet()->setTextureAttributeAndModes(
    	0, texture.get());
    
    osgViewer::Viewer viewer;
    viewer.setSceneData(root.get());
    return viewer.run();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    }
    在这里插入图片描述

  • 相关阅读:
    MySQL基础学习笔记
    仙人掌之歌——投石问路(1)
    第七章:敏捷开发工具方法-part2-CI/CD工具介绍
    企业媒体信息发布系统,一键发布省时省力
    在 Android 中创建静态应用程序快捷方式
    基于单片机技术的自动停车器的设计
    matlab线性代数常用函数
    重生奇迹mu格斗怎么加点
    跨域请求方案整理实践
    滑动窗口 解题思路
  • 原文地址:https://blog.csdn.net/directx3d_beginner/article/details/133306607