Advertisement

Uploading a 1D texture in OpenGL - All (0, 0, 0, 1)

Started by November 25, 2015 04:01 PM
2 comments, last by Arjan B 9 years ago

Hi! I first create a lookup-table for a transfer function and then try to upload it as a 1D texture as follows:


for (unsigned i = 0; i < 1024; i++)
  tfm0[i] = qfeGetProbability(tf0, (float)i/1023.f);

glActiveTexture(GL_TEXTURE17);
if (glIsTexture(tfmTex0)
  glDeleteTextures(1, &tfmTex0);
glGenTextures(1, &tfmTex0);
glBindTexture(GL_TEXTURE_1D, tfmTex0);
glTexImage1D(GL_TEXTURE_1D, 0, GL_R16F, 1024, 0, GL_RED, GL_FLOAT, tfm0);

Right before the rendering call I make sure all the textures I need are bound to the right texture units:


glActiveTexture(GL_TEXTURE17);
glBindTexture(GL_TEXTURE_1D, tfmTex0); 

Then, I set my uniform variable for the 1D texture:


glUniform1f(glGetUniformLocation(shaderProgram, "tf0"), 17);

And this is how the 1D texture is defined in the fragment shader, where sampleNorm is a value between 0 and 1:


uniform sampler1D tf0;
vec4 tfValue = texture1D(tf0, sampleNorm);

Somehow, all of the tfValues end up being (0, 0, 0, 1), which I suspect is a default fallback value.

To be sure that I uploaded the values to the graphics card correctly, I also have this check right before the draw call:


float values[1024];
glActiveTexture(GL_TEXTURE17);
glGetTexImage(GL_TEXTURE_1D, 0, GL_RED, GL_FLOAT, values);

It retrieves the values in the texture I uploaded back to "normal" memory, and they show up to be exactly the values I expect them to be.

Does anyone have an idea of where things might be going wrong? What would cause the sampler in the fragment shader to return (0, 0, 0, 1), when it should be returning my values in the R-channel?

Thank you in advance,

Arjan

Did you check if any of the GL-calls is failing? Unfortunately this requires you to call "glGetError" after every call:


            GLenum err = glGetError();
            if(err != GL_NO_ERROR) // error

But it can be wrapped in a macro that you place around/after every call. If you are using modern OpenGL (I belive you need 4.0 at least), you can also register a debug message callback:


void APIENTRY openGlErrorCallback(GLenum source?, GLenum type?, GLuint id?, GLenum severity?, GLsizei length?, const GLchar* message?, void* userParam?); // implement this function as you want
glDebugMessageCallback(openGlErrorCallback, nullptr)

To get error messages automatically and in more detail.

See if this yields anything first.

Advertisement

I don't know if this is your problem but:


glUniform1f(glGetUniformLocation(shaderProgram, "tf0"), 17);
glUniform1i and glUniform1iv are the only two functions that may be used to load uniform variables defined as sampler types. Loading samplers with any other function will result in a GL_INVALID_OPERATION error

from https://www.opengl.org/sdk/docs/man/html/glUniform.xhtml

Might be worth changing and seeing if it helps. This caused me a great deal of time to track down when I had an issue setting my sampler once.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Wow, thanks a lot guys!

I ended up doing what Juliean said, which brought me right to the glUniform1f() function. And exactly as Nanoha stated, it generated a GL_INVALID_OPERATION error, which was fixed by simply replacing the 'f' with an 'i'. Wish I'd posted here sooner, I spent tons of hours sadly staring at my screen as well.

Thanks again!

This topic is closed to new replies.

Advertisement