1、在OpenGL中生成纹理texture
optix中的纹理直接使用OpenGL纹理的id,不能跨越OpenGL纹理,所以我们先在OpenGL环境下生成一张纹理。
这个过程就很熟悉了:
void WKS::Texture::GenTextureFromFile(const char* name, std::string directory) {
std::string fileName = directory + '/' + std::string(name);
int width, height, channel;
unsigned char\* image = SOIL\_load\_image(fileName.c\_str(), &width, &height, &channel, SOIL\_LOAD\_RGBA);
//Assign texture to ID;
glGenTextures(, &textureID);
glBindTexture(GL\_TEXTURE\_2D, textureID);
//Parameters
glTexParameteri(GL\_TEXTURE\_2D, GL\_TEXTURE\_MIN\_FILTER, GL\_LINEAR);
glTexParameteri(GL\_TEXTURE\_2D, GL\_TEXTURE\_MAG\_FILTER, GL\_LINEAR);
glTexParameteri(GL\_TEXTURE\_2D, GL\_TEXTURE\_WRAP\_S, GL\_CLAMP\_TO\_EDGE);
glTexParameteri(GL\_TEXTURE\_2D, GL\_TEXTURE\_WRAP\_T, GL\_CLAMP\_TO\_EDGE);
glTexImage2D(GL\_TEXTURE\_2D, , **GL\_RGBA32F**, width, height, , GL\_RGBA, GL\_UNSIGNED\_BYTE, image);
glGenerateMipmap(GL\_TEXTURE\_2D);
glBindTexture(GL\_TEXTURE\_2D, );
SOIL\_free\_image\_data(image);
}
这个本来很简单,但是我在这儿遇到了一个错误,将此生成的纹理放入Optix出现了错误:
原因是我之前在上面红色显示的纹理格式处使用的 GL_RGB,这样写在OpenGL中毫无问题,但是引入Optix中出现了上图错误,耽搁了很长时间。
2、Optix中生成TextureSampler 对象,并绑定到Program中
生成TextureSampler 对象:
OptixTexture(optix::Context& context, GLuint texid) {
textureSampler = context->createTextureSamplerFromGLImage(texid, RT_TARGET_GL_TEXTURE_2D);
textureSampler->setWrapMode(, RT_WRAP_CLAMP_TO_EDGE);
textureSampler->setWrapMode(, RT_WRAP_CLAMP_TO_EDGE);
textureSampler->setIndexingMode(RT_TEXTURE_INDEX_ARRAY_INDEX);
textureSampler->setReadMode(RT_TEXTURE_READ_ELEMENT_TYPE);
textureSampler->setMaxAnisotropy(1.0f);
textureSampler->setFilteringModes(RT_FILTER_NEAREST, RT_FILTER_NEAREST, RT_FILTER_NONE);
}
绑定到Program中:
void BindSampler(optix::Context& context, const char* target) {
context[target]->setTextureSampler(this->textureSampler);
}
Host中的调用方式:
this->tex_wall = new WKS::Texture("back.jpg", "source/texture/skybox");
this->optixTexture = new OptixTexture(context, tex_wall->GetTextureID());
this->optixTexture->BindSampler(context, "tex_wall");
3、Program 中的纹理使用(纹理访问)
输入声明:
//输入纹理
rtTextureSampler
访问:
float4 color = tex2D(tex_wall, launch_index.x*1.0f/screen.x, 1.0f-launch_index.y*1.0f/screen.y);
效果:(输入一张图,显示出来)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章