zoukankan      html  css  js  c++  java
  • optix之纹理使用

    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(1, &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, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
        glGenerateMipmap(GL_TEXTURE_2D);
    
        glBindTexture(GL_TEXTURE_2D, 0);
        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(0, RT_WRAP_CLAMP_TO_EDGE);
            textureSampler->setWrapMode(1, 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, 2> tex_wall;

      访问:

    float4 color = tex2D(tex_wall, launch_index.x*1.0f/screen.x, 1.0f-launch_index.y*1.0f/screen.y);

    效果:(输入一张图,显示出来)

  • 相关阅读:
    operamasks—omMessageBox的使用
    operamasks-omGrid的使用
    SQL Server转sqlite数据库
    operamasks—omMessageTip的使用
    operamasks—omBorderLayout布局
    选择器(E:hover/E:active/E:focus的使用)
    CSS3(各UI元素状态伪类选择器受浏览器的支持情况)
    选择器的使用(nth-of-type和nth-last-of-type选择器)
    选择器的使用(nth-child和nth-last-child选择器)
    选择器的使用(first-child和last-child选择器)
  • 原文地址:https://www.cnblogs.com/chen9510/p/11968394.html
Copyright © 2011-2022 走看看