简体   繁体   English

将2D图像映射到3D纹理

[英]Mapping a 2D image to a 3D texture

Question: I am trying to map a single 2D image to a 3D texture. 问题:我正在尝试将单个2D图像映射到3D纹理。 When I visualize the output, OpenGL renders a white texture floating in black background. 当我可视化输出时,OpenGL渲染漂浮在黑色背景中的白色纹理。 Why is this happening? 为什么会这样呢? Is it possible to map a 2D image to a 3D texture? 是否可以将2D图像映射到3D纹理?

My goal is to place multiple 2D images along the z-dimension to resemble a volume. 我的目标是沿z维放置多个2D图像,使其类似于一个体积。 An example of such an approach is in this video: http://cvlab.epfl.ch/research/medical/em/synapses 该视频中提供了这种方法的示例: http : //cvlab.epfl.ch/research/medical/em/synapses

Approaches attempted: I took 2D images and mapped them to 2D textures. 尝试的方法:我拍摄了2D图像并将其映射到2D纹理。 I have also used 2D texture arrays. 我还使用了2D纹理数组。 Both of these approaches provide a good result. 这两种方法均提供了良好的结果。 However, when I tried to paint the surface of a 3D texture with the same image, I get a white texture floating around in a black space. 但是,当我尝试用相同的图像绘制3D纹理的表面时,我得到了在黑色空间中漂浮的白色纹理。

Code I have followed the texture mapping tutorial that is available on NeHe's website. 代码我遵循了NeHe网站上提供的纹理映射教程。 http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/ http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/

Except changes to these three functions, everything else in the program (present in the solution file in the website) is the same. 除了对这三个功能的更改以外,程序中的其他所有内容(位于网站的解决方案文件中)都是相同的。

... // header file declarations

#include <glext.h>

PFNGLTEXIMAGE3DPROC glTexImage3D;
GLuint  texture;    


int LoadGLTextures()    // Load Bitmaps And Convert To Textures
{
            /* load an image file directly as a new OpenGL texture */

            texture = SOIL_load_OGL_texture("Data/NeHe.bmp",SOIL_LOAD_AUTO, 
            SOIL_CREATE_NEW_ID,S OIL_FLAG_INVERT_Y);
            // "Data/NeHe.bmp"

            if(texture == 0) {return false;}

            glTexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
            if (glTexImage3D == NULL) 
            {
            printf("Error in line %d: Couldn't load glTexImage3D function. Aborting.\n", __LINE__);
            return -1;
            }

    glBindTexture(GL_TEXTURE_3D, texture);
    glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    //glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 4096, 4096, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    return true;    
}


int InitGL(GLvoid)      
{
            if (!LoadGLTextures())  
            { return FALSE; }

    glEnable(GL_TEXTURE_3D);
    // glEnable(GL_TEXTURE_2D); 
    glShadeModel(GL_SMOOTH);    //
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);   // Black Background
    glClearDepth(1.0f);         // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);    
    glDepthFunc(GL_LEQUAL);     
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective calculations
    return TRUE;    // Initialization Went OK
}


int DrawGLScene(GLvoid)   
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();   
    glTranslatef(0.0f,0.0f,-5.0f);
    glRotatef(xrot,1.0f,0.0f,0.0f);
    glRotatef(yrot,0.0f,1.0f,0.0f);
    glRotatef(zrot,0.0f,0.0f,1.0f);

    // glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBindTexture(GL_TEXTURE_3D, texture);
    glBegin(GL_QUADS);

    glTexCoord3f(0.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  0.0f);
    glTexCoord3f(1.0f, 0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  0.0f);
    glTexCoord3f(1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  0.0f);
    glTexCoord3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  0.0f);

    glEnd();

    xrot+=0.3f;
    yrot+=0.2f;
    zrot+=0.4f;
    return TRUE;                                        
}

...

It is better to use 3D textures instead of 2D ones as you will be able to apply rotations correctly using glRotate(). 最好使用3D纹理而不是2D纹理,因为您可以使用glRotate()正确应用旋转。 You will also need to do blending and/or alpha-testing to see different layers of your 2D image. 您还需要进行混合和/或Alpha测试,以查看2D图像的不同层。

To create a 3D texture, you can create an array of dimension [width x height x number_of_slices] and then store the raw data slice by slice 要创建3D纹理,您可以创建一个尺寸为[宽x高x number_of_slices]的数组,然后按切片存储原始数据

Look at this tutorial: http://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering?msg=4729498#xx4729498xx 查看本教程: http : //www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering? msg= 4729498#xx4729498xx

It's very good and it worked for me to do the same thing 很好,对我来说做同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM