GLGS:如何将采样器“连接”到纹理?

时间:2013-01-23 21:54:33

标签: opengl glsl geometry-shader

我正在尝试从几何着色器中的3D纹理中读取:

#version 150

layout(points) in; // origo of cell
layout(points, max_vertices = 1) out;

uniform sampler3D text;

void main (void) 
{   
    for(int i = 0; i < gl_in.length(); ++i)
    {
        // texture coordinates:
        float u, v, w;

        // set u, v, and w somehow
        ...

        float value = texture(text, vec3(u, v, w)).r;

        bool show;

        // set show based on value somehow:
        ...

        if(show) {      
            gl_Position = gl_in[i].gl_Position;
            EmitVertex();
            EndPrimitive();  
        }  
    }
}

这就是我在初始化GL代码中设置texure的方法:

int nx = 101;
int ny = 101;
int nz = 101;
float *data = new float[nx*ny*nz];
// set data[] somehow:
...
glEnable(GL_TEXTURE_3D);
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,      // level-of-detail number. 0 is the base image level.
             GL_RED, // internal format
         nx, ny, nz,
                  0,      // border
             GL_RED, // pixel format 
           GL_FLOAT,  // data type of the pixel data 
               data);

但是如何将几何着色器内的采样器“text”与纹理相关联?

到目前为止,我还没有告诉OpenGL有一个名为“text”的采样器,它应该采样纹理。

编辑:我尝试了以下内容:

GLint textLoc = glGetUniformLocation(program, "text");
glUniform1i(textLoc, 0); // sends 0 to "text" in shader
// why do I not just hardcode 0 inside the geometry shader instead ? 
glBindTexture(GL_TEXTURE_3D , texture); 
glActiveTexture(GL_TEXTURE0); // same as 0 ?
GLuint sampler_state = 0;
glGenSamplers(1, &sampler_state);
glBindSampler(0, sampler_state); // what does this do?

这里有什么问题?

0 个答案:

没有答案