0

I'm using openGL and what I want to do is render my scene to a texture and then store that texture so that I can pass it into my fragment shader to be used to render something else.

I created a texture using glGenTexture() and attached it to a frame buffer and then rendered the frame buffer with glBindFrameBuffer(). I then set the framebuffer back to 0 to render back to the screen but now I'm stuck.

In my fragment shader I have a uniform sampler 2D 'myTexture' that I want to use to store the texture. How do I go about doing this?

For .jpg/png images that I found online I just used the following code:

glUseProgram(Shader->GetProgramID());
GLint ID = glGetUniformLocation(
              Shader->GetProgramID(), "Map");
glUniform1i(ID, 0);
glActiveTexture(GL_TEXTURE0);
MapImage->Bind();
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

However this code doesn't work for the glTexture I created. Specifically I can't call myTexture->Bind() in the same way.

genpfault
  • 51,148
  • 11
  • 85
  • 139
user1782677
  • 1,963
  • 5
  • 26
  • 48

2 Answers2

1

If you truly have "a uniform sampler 2D 'myTexture'" in your shader, why are you getting the uniform location for "Map"? You should be getting the uniform location for "myTexture"; that's the sampler's name, after all.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • I was just using that as an example of how I accessed a *different* shader. But when I tried to apply it to 'myTexture' ti ddint' work. Mostly because the texture I used for map came from a jpg while the one I used for 'myTexture' came from using glGenTexture() – user1782677 Feb 05 '13 at 00:06
  • @user1782677: *All* OpenGL textures come from `glGenTextures`, whether you use it directly or indirectly via a library. – Nicol Bolas Feb 05 '13 at 00:07
0

So I created a texture using glGenTexture() and attached it to a frame buffer

So in other words you did this:

glActiveTexture(GL_TEXTURE0);
GLuint myTexture;
glGenTextures(1, &myTexture);
glBindTexture(GL_TEXTURE_2D, myTexture);

// very important and often missed by newbies:
// A framebuffer attachment must be initialized but not bound
// when using the framebuffer, so it must be unbound after
// initializing
glTexImage2D(…, NULL);
glBindTexture(GL_TEXTURE_2D, 0);

glBindFramebuffer(…);
glFramebufferTexture2D(…, myTexture, …);

Okay, so myTexture is the name handle for the texture. What do we have to do? Unbinding the FBO, so that we can use the attached texture as a image source, so:

glBindFrameuffer(…, 0);

glActiveTexture(GL_TEXTURE0 + n);
glBindTexture(GL_TEXTURE_2D, myTexture);

glUseProgram(…);
glUniformi(texture_sampler_location, n);
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thank you! I will try this and see if it works. But Where are you getting your 'n' variable from? – user1782677 Feb 05 '13 at 00:13
  • @user1782677: I put that `n` variable there to make clear the connection between the value passed to a sampler uniform and the texture unit. A texture bound in the texture unit GL_TEXTURE0 is accessed by a sampler uniform of value 0 (and not GL_TEXTURE0). `n` can be any numeric value less than the maximum number of texture units supported by the used OpenGL implementation. – datenwolf Feb 05 '13 at 01:51