不改osg的.hdr插件也行,另一种替代方式是stbimage读取后,传递给osg,本质是一样的。
直接上代码了
#include <osg/TextureCubeMap>
#include <osg/TexGen>
#include <osg/TexEnvCombine>
#include <osgUtil/ReflectionMapGenerator>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/NodeVisitor>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>
#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);
}
#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<osg::Vec3Array> normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f));
osg::ref_ptr<osg::Vec2Array> 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<osg::Geometry> 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<osg::Texture2D> 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<osg::Geode> root = new osg::Geode;
root->addDrawable(quad.get());
root->getOrCreateStateSet()->setTextureAttributeAndModes(
0, texture.get());
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
return viewer.run();
}