0

我最终希望基于 3 个 1d 采样器为图像着色。我编写了这个快速程序来测试是否可以将纹理的红色通道映射到自身,即所谓的线性映射。

现在代码显示一个空白屏幕,尽管我在程序、着色器和一般执行中检查了所有错误。当我不执行映射时,纹理会按预期呈现。

我使用的是针对 Windows 的 GeForce Ti 550。

该方法的灵感主要来自:http ://www.arcsynthesis.org/gltut/Texturing/Tutorial%2014.html

制作纹理和采样器

glActiveTexture(GL_TEXTURE1);
glGenTextures(1,lut);
glBindTexture(GL_TEXTURE_1D,lut[0]);
unsigned char* linear = new unsigned char[256];//should be a 1->1 mapping
for (int i = 0 ;i < 256;i++)
{
    linear[i] = i;
}
glTexImage1D(GL_TEXTURE_1D, 0, GL_R8, 256, 0,GL_RED, GL_UNSIGNED_BYTE, linear);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
glBindTexture(GL_TEXTURE_1D, 0);
delete linear;
glGenSamplers(1,sampler);
glSamplerParameteri(sampler[0], GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(sampler[0], GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glSamplerParameteri(sampler[0], GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);   
glE();// no errors

制作程序,也没有错误

const char *vsrc1 =
    "attribute vec2 vecPosIn;\n"
    "attribute vec4 texPosIn;\n"
    "varying vec4 texPosOut;\n"
    "void main()\n"
    "{\n"
    "gl_Position = vec4(vecPosIn, 0.0, 1.0);\n"
    "texPosOut = texPosIn;\n"
    "}\n";
GLint vsH = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vsH,1,&vsrc1,NULL);
glCompileShader(vsH);
shaderCheck(vsH);
glE();
const char *fsrc1 =
    "uniform sampler2D tex0;\n"
    "uniform sampler1D tex1;\n"
    "varying vec4 texPosOut;\n"
    "void main(void)\n"
    "{\n"
    //"    gl_FragColor = texture2D(tex0, texPosOut.st);\n" //ignores texture and does what I expect
    "    float red = texture2D(tex0, texPosOut.st).r;\n"
    "    gl_FragColor = texture1D(tex1, red);\n"
    "}\n";
GLint fsH = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fsH,1,&fsrc1,NULL);
glCompileShader(fsH);
shaderCheck(fsH);
glE();
//Bind
programHandle = glCreateProgram();
glBindAttribLocation(programHandle,0,"vecPosIn");
glBindAttribLocation(programHandle,1,"texPosIn");
glBindAttribLocation(programHandle,2,"texPosOut");
glE();
//Link
glAttachShader( programHandle, vsH );
glAttachShader( programHandle, fsH );
glLinkProgram( programHandle );
programCheck(programHandle);
glE();
//Set render() invariants
glUseProgram( programHandle );
GLuint idk = glGetUniformLocation(programHandle, "tex1");
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_1D,lut[0]);
glUniform1i(idk,0);
glBindTexture(GL_TEXTURE_1D,0);
glUseProgram( 0 );
glE();
//Pack static data into a VAO
float vecData[] = {
    -1, 1,
    -1,-1,
    1, 1,
    -1,-1,
    1,-1,
    1, 1
};
float texData[] = { 
    0,1,
    0,0,
    1,1,
    0,0,
    1,0,
    1,1
};
vboHandles=(GLuint*)malloc(sizeof(int)*2);
glGenBuffers(2,vboHandles);
GLuint vecBuf=vboHandles[0];
GLuint texBuf=vboHandles[1];
glBindBuffer(GL_ARRAY_BUFFER, vecBuf);
glBufferData(GL_ARRAY_BUFFER, 8*2 * sizeof(GLfloat), vecData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, texBuf);
glBufferData(GL_ARRAY_BUFFER, 8*2 * sizeof(GLfloat), texData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glE();
glGenVertexArrays( 1, &vaoHandle );
glBindVertexArray(vaoHandle);
glEnableVertexAttribArray(0);  // Vertex position
glEnableVertexAttribArray(1);  // Vertex color
glBindBuffer(GL_ARRAY_BUFFER, vecBuf);
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL );
glBindBuffer(GL_ARRAY_BUFFER, texBuf);
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL ); //2 attribute per vertex
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
glE();

渲染函数

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glE();
//Bind
glUseProgram(programHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,textures[frame]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_1D,lut[0]);
GLuint idk = glGetUniformLocation(programHandle, "tex1");
glUniform1i(idk,0);
glBindSampler(0,sampler[0]);glE();
glE();
//Draw
glBindVertexArray(vaoHandle);
glDrawArrays(GL_TRIANGLES,0,8);
glBindVertexArray(0);
glE();
//Unbind
glActiveTexture(GL_TEXTURE1);
glBindSampler(0,0);
glBindTexture(GL_TEXTURE_1D,0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,0);
glUseProgram(0);
glE();
//DumpFrame
glFlush();
glFinish();
glE();
4

1 回答 1

1

你正在设置

glUniform1i(idk,0);

所以你告诉 GL 使用纹理单元 0 来采样“tex1”一维纹理,这只是无效的,因为你在那里绑定了一个二维纹理。

于 2013-07-15T15:01:01.900 回答