OpenGL 4.0 texture binding -


i'm trying bind multiple textures samplers in fragment shader. loading code seems work well. ati's codexl shows texture being loaded correctly.

however, when go bind textures model active textures 0 , 1 can not send value shader. when have shader uniform marked usampler2d , use uvec4 store color, should since texture provided unsigned bytes, white model. when change shader uniform sampler2d , use vec4 store color, gluniform1i call can no longer location of shader variable , nothing gets set active texture. results in diffuse texture being able used, can not normal texture. on bright side, diffuse texture being drawn on model way.

i'm not sure problem is. i've check several places online trying figure out, , have looked through redbook. know i'm missing something, or have state set wrong, can't seem find it. thank in advance can give me fix problem.

texture creation

int[] testwidth;  testwidth = new int[1]; testwidth[0] = 1000;  // first bind texture. bind();  // make sure textures enabled. // read ati cards need before mipmapping. glenable(gl_texture_2d);  // test make sure can create texture this. glteximage2d(gl_proxy_texture_2d, 0, format, width, height,              0, format, gl_unsigned_byte, null); glgettexlevelparameteriv(gl_proxy_texture_2d, 0, gl_texture_width,                          testwidth); if (testwidth[0] == 0) {    message("could not load texture onto graphics card."); } else {    // not sure part....but seems work.    glpixelstorei(gl_pack_alignment, 1);    glpixelstorei(gl_unpack_alignment, 1);     // load texture data.    glteximage2d(texture_type, 0, format, width, height,                 0, format, gl_unsigned_byte, (glvoid[]?)value);     // smaller mipmaps need linear mipmap coords.    // larger uses linear of main texture.    gltexparameterf(texture_type, gl_texture_min_filter,                    gl_linear_mipmap_linear);    gltexparameterf(texture_type, gl_texture_mag_filter,                    gl_linear);     // clamp texture edges.    gltexparameterf(texture_type, gl_texture_wrap_s,                    gl_clamp_to_edge);    gltexparameterf(texture_type, gl_texture_wrap_t,                    gl_clamp_to_edge);    gltexparameterf(texture_type, gl_texture_wrap_r,                    gl_clamp_to_edge);     // generate mipmaps. tex parameter there    // ati cards. again, it's read online.    gltexparameteri(gl_texture_2d, gl_generate_mipmap, gl_true);    glgeneratemipmap(texture_type); }  // unbind texture. unbind(); 

texture binding

if (currentshader != null) {    currentshader.set_uniform_matrix("model_matrix", ref model_matrix,                                     true);     if (material != null)    {       if (material.diffuse_texture != null)       {          glactivetexture(gl_texture0);          material.diffuse_texture.bind();          currentshader.set_uniform_texture("diffuse_texture",                                            constants.diffuse_texture);       if (material.normal_testure != null)       {          glactivetexture(gl_texture1);          material.normal_texture.bind();          currentshader.set_uniform_texture("normal_texture",                                            constants.normal_texture);       }    } }  // if there renderable render it. if (renderable != null) {    renderable.render(1.0); }  if (material != null) {    material.unbind(); } 

fragment shader

#version 400 core  /**  * smooth inward vertex color. smooth fragments  * in between vertices can value close  * positioned after being rasterized.  */ smooth in vec4 vertex_color;  /**  * smooth inward texture coordinates. smooth  * fragments in between vertices can  * value close positioned after being rasterized.  */ smooth in vec2 out_texture_coordinate;  /**  * color make fragment.  */ out vec4 frag_color;  /**  * models diffuse texture. mapped index 0.  */ uniform usampler2d diffuse_texture;  /**  * models normal texture. mapped index 1.  */ uniform usampler2d normal_texture;   /**  * starting function of shader.  */ void main(void) {    uvec4 diffusecolor;    uvec4 normalmodifier;     diffusecolor = texture(diffuse_texture, out_texture_coordinate);    normalmodifier = texture(normal_texture, out_texture_coordinate);     // discard fragments have alpha color less 0.05.    if (diffusecolor.a < 1.0)    {       // works part of depth testing remove fragments       // not useful.       discard;    }     frag_color = diffusecolor; } 

uniform setting

/**    * sets uniform value texture in shader.    *    * @param name name of uniform bind texture to.    * must have been registered.    *    * @param textureunit id texture unit bind uniform.    * not texture's id/reference, opengl texture unit    * reference bound to.    * set calling glactivetexture.    */   public void set_uniform_texture(string name, int textureunit)   {      // check make sure uniform given location already.      if (register_uniform(name) == true)      {         // set data uniform then.         gluniform1i(uniform_mapping.get(name), textureunit);      }      else      {         message("texture not set. %s", name);      }   }    /**    * register uniform passing data shader program.    *    * @return true if uniform found valid location;    * otherwise, false.    *    * @param name name parameter uniform location for.    * use name variable in shader.    */   public bool register_uniform(string name)   {      int location;       // make sure didn't location of uniform value.      if (uniform_mapping.has_key(name) == false)      {         location = constants.opengl_invalid_index;          // have no information uniform, try         // it's location.         location = glgetuniformlocation(reference, name);          // location 0 or higher if found uniform.         if (location != constants.opengl_invalid_index)         {            uniform_mapping.set(name, location);             return true;         }      }      else      {         // uniform found , can used.         return true;      }       debug("uniform %s not found!!!!!", name);      return false;   } 

setting internal format gl_rgb/a implies should using sampler2d , not usampler2d, though raw image data given unsigned bytes. edit given data gets converted internal format @ call glteximage2d (in case gl_rgba 8 bits per channel not has happen). however, graphics applications data needed higher accuracy, example when sampling texture non-"nearest" interpolation, why it's exposed floats.

to bind multiple textures...

glactivetexture(gl_texture0 + firsttextureindex); //firsttextureindex should unique amongst textures bound particular shader glbindtexture(gl_texture_2d, myfirsttexturehandle); gluniform1i(glgetuniformlocation(shaderprogramhandle, "firstsampler"), firsttextureindex); //note same index given in glactivetexture call. gluniform1i 

and repeat secondtextureindex, mysecondtexturehandle, "secondsampler" etc.

if glgetuniformlocation doesn't return location, double check use , affects shader output (or gets optimized out completely). check usual typos or missing "uniform" keyword etc.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -