optix之紋理使用

 

一、在OpenGL中生成紋理textureui

  optix中的紋理直接使用OpenGL紋理的id,不能跨越OpenGL紋理,因此咱們先在OpenGL環境下生成一張紋理。this

  這個過程就很熟悉了:spa

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出現了錯誤:code

  

  緣由是我以前在上面紅色顯示的紋理格式處使用的 GL_RGB,這樣寫在OpenGL中毫無問題,可是引入Optix中出現了上圖錯誤,耽擱了很長時間。對象

 

二、Optix中生成TextureSampler 對象,並綁定到Program中blog

  生成TextureSampler 對象:ip

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中:get

void BindSampler(optix::Context& context, const char* target) {
        context[target]->setTextureSampler(this->textureSampler);
    }

  Host中的調用方式:string

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");

 

三、Program 中的紋理使用(紋理訪問)class

  輸入聲明:

//輸入紋理
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);

 

效果:(輸入一張圖,顯示出來)

相關文章
相關標籤/搜索