ZTK's picture

Conceptual question about GL.ActiveTexture

Hello everyone,

I am learning openTK and this site has been super helpful! I've gotten a lot out of reading these threads, so I want to say thank you to the helpful people that post replies.

I've been stuck on something for the past week or so now, so I'd like to post for help. I believe I'm probably just fundamentally misunderstanding something, so this may be an easy question to answer :)

Let me provide context of what I'm trying to do first. I'm trying to simply draw a square on the screen whose contents are a texture. Then, on each frame, I want to apply John Conway's Game of Life rules to that texture using a fragment shader. The end program should be showing a square on the screen whose contents are moving according to the rules of the game.

So at a high level, my understand of what I need to do is:

* Create two texture objects
* Create a framebuffer object
* Create a fragment shader that reads from texture1, applies the per-pixel rules, and writes to texture 2
* In the main program, use GL.BindTexture to alternate between texture1 and texture2 on each frame

WIth that said, here is my question. I've found that in order to pass multiple textures as uniforms to a fragment shader, I have to start with GL.ActiveTexture(TextureUnit.Texture1), and not GL.ActiveTexture(TextureUnit.Texture0). So if I want to pass four textures to a fragment shader, I will make four calls to GL.ActiveTexture that will look like


Also, when I've looked at the tutorials that come with OpenTK, specifically the "Framebuffer Objects" tutorial, I've noticed that everything is done without a call to GL.ActiveTexture. In other words, staying with Texture0. If I call GL.ActiveTexture(TextureUnit.Texture1) in that tutorial code, then the triangles don't get drawn.

So this gets to the heart of my confusion. It seems that in order for anything to render to a framebuffer object, the active texture needs to be set to Texture0. But in order to be able to pass multiple textures to fragment shader, the active textures need to be 1...N.

I feel like I must just fundamentally not getting a concept here. I've asked several people about this in person and haven't been able to make progress. Does my question make sense? Is there something I've fundamentally not getting?

I'd very appreciate any help or clarifications as to the source of my confusion. I haven't posted code here as I feel this is already a long post, but I can give a succinct code example (all based on the examples) of what I mean, if that would help to be more concrete. I'll give one tiny snippet to show what I mean. Below is a screenshot of the "Framebuffer Objects" tutorial unmodified. And then there is a screenshot of what it looks like if I make the active texture Texture1.

Thank you very much!

Inline Images


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tksuoran's picture

With shaders you need to use GL.Uniform1() together with GL.ActiveTexture(). For every texture uniform you need to choose unique texture unit and do: GL.Uniform1(slot, textureIndex); GL.ActiveTexture(TextureUnit.Texture0 + textureIndex); and bind your texture. And textureIndex can and should start from 0.

ZTK's picture

Thank you for the response. I must be confused because for me I found that textureIndex cannot start with zero if passing to a shader. I have written a blog entry to show this http://www.opentk.com/node/2559. Is there something I am missing?

tksuoran's picture

It needs fixing, I will reply to your entry.