View Full Version : Texture access from Fragment Shader by id

10-20-2015, 08:49 AM
Hi, i'm trying to render my scene also with OpenGL.
Already succeed to render to FBO (offscreen) etc.
But i need one more thing.
To get some insight to what i'm doing, lets say, i need to render N number of spheres.
I don't want to render each separately - because as i read in some tutorial:
"The first idea to draw many particles would be to use the previous tutorial’s code, and call glDrawArrays once for each particle. This is a very bad idea, because this means that all your shiny GTX’ 512+ multiprocessors will all be dedicated to draw ONE quad (obviously, only one will be used, so that’s 99% efficiency loss)."
Since every particle use the same mesh (meshed sphere) i'd like to grab them into one buffer and render with "Instanced" draw.
So far - no problem occured. The problem is, that all sphere may have different material, and.. texture..
I know how to make those sphere have different materials, but i have no idea how to do something like this in Fragment Shader:

#version 330 core

in vec4 position;
in vec2 coords;
in int material_id
in int texture_id;

uniform vec4 diffuse[10];

out vec4 color;

void main() {
color = diffuse[material_id]*texture( [how to ?] texture_id, coords);
In other words, i just want to use any created texture from a shader without binding it like:

uniform sampler2D diff_tex;

and somewhere in host code:

int d_tex_loc = glGetUniformLocation(program,"diff_tex");

and before rendering:

glBindTexture(GL_TEXTURE_2D, my_texture);

For now, the only way i see is to bind all textures [or at least those which i'll use during Instanced draw]
But the question is: can i access texture loaded on GPU without binding it?
This would be much more easier for me.. i could then just send texture id and access it.

Alfonse Reinheart
10-20-2015, 11:03 AM
i just want to use any created texture from a shader without binding it

Not unless your graphics card supports ARB_bindless_texture (https://www.opengl.org/registry/specs/ARB/bindless_texture.txt). You can simulate this by using an array texture (https://www.opengl.org/wiki/Array_Texture), with the array layers being for different objects. You simply pass the array layer to the texture.

10-20-2015, 12:29 PM
I think i'm gonna love you! Not sure yet, but probably problem is solved. Thank U :)