user1782677
user1782677

Reputation: 2007

using glTexture in a shader

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.

Upvotes: 0

Views: 403

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 473212

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.

Upvotes: 1

datenwolf
datenwolf

Reputation: 162164

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);

Upvotes: 0

Related Questions