Jump to content
  • Advertisement
Sign in to follow this  
Arjan B

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

This topic is 965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by Arjan B

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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.

Edited by Nanoha

Share this post


Link to post
Share on other sites

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!

Edited by Arjan B

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!