OpenGL Texture loading, but not applied (?) -


i've got little problem texturing.

i load mesh via assimp, works fine, model looks way should.. it's black..

so, @ first load shader , start load mesh, including vertices, faces , uv's now. keep @ minimum, show texture relevant code.

    texid = glgetuniformlocation(shaderprogram, "mytexturesampler");     loadscene(modelpath); 

loadscene import whole modelscene through modelpath.

so, first part don't think problem, be, maybe missed something, while debugging seemed fine. go texture using freeimage library.

bool loadgltextures(const aiscene* scene) {      /* unload image first */     if (pimage)      {         freeimage_unload(pimage);         pimage = 0;     }      if (scene->hastextures())      {         std::cerr << "support meshes embedded textures not (yet)             implemented" << std::endl;         return false;     }      /* gettexture filenames , numb of textures */     (unsigned int m=0; m < scene->mnummaterials; m++)     {         int texindex = 0;         aireturn texfound = ai_success;          aistring path;  // filename          while (texfound == ai_success)         {             texfound = scene->mmaterials[m]->gettexture(aitexturetype_diffuse,                 texindex, &path);         textureidmap[path.data] = null;         //fill map textures, pointers still null yet         texindex++;         }     }      int numtextures = textureidmap.size();      /* create , fill array gl texture ids */     textureids = new gluint[numtextures];     glgentextures(numtextures, textureids); /* texture name generation */      /* iterator */     std::map<std::string, gluint*>::iterator itr = textureidmap.begin();      (int i=0; < numtextures; i++)     {         std::string filename = (*itr).first;  // filename         (*itr).second = &textureids[i]; // save texture id filename in map         itr++; // next texture          std::string fileloc = basepath + filename;  /* loading of image */          // check file signature , deduce format.           free_image_format fif = freeimage_getfiletype(fileloc.c_str(), 0);            // if still unknown, try guess file format file extension.           if(fif == fif_unknown)           {               fif = freeimage_getfiffromfilename(fileloc.c_str());           }            // if still unkown, return failure.           if(fif == fif_unknown)           {               return false;           }            // check plugin has reading capabilities , load file.           if(freeimage_fifsupportsreading(fif))           {               pimage = freeimage_load(fif, fileloc.c_str());           }           if (pimage != 0) /* if no error occured: */         {             glbindtexture(gl_texture_2d, textureids[i]);              unsigned int width = pimage ? freeimage_getwidth(pimage) : 0;             unsigned int height = pimage ? freeimage_getheight(pimage) : 0;             const byte *bits = pimage ? freeimage_getbits(pimage) : 0;              gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_repeat);             gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat);             gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear);             gltexparameteri(gl_texture_2d, gl_texture_min_filter,                 gl_linear_mipmap_linear);              // initialise texture image data.               glteximage2d(gl_texture_2d, 0, gl_rgb, width, height, 0, gl_rgb,                  gl_unsigned_byte, bits);             }             else             {                 /* error occured */                 std::cerr << "couldn't load image: " + fileloc << std::endl;             }         }      //return success;     return true; } 

after got texture, go mesh(es) part relevant, uv part , binding, lets skip material loading - there no light yet, kept @ minimum.

void initmaterial(const aimaterial* mtl) {      int texindex = 0;     aistring texpath; //contains filename of texture      if(ai_success == mtl->gettexture(aitexturetype_diffuse, texindex, &texpath))     {         //bind texture         gluint texid = *textureidmap[texpath.data];         glbindtexture(gl_texture_2d, texid);         glactivetexture(gl_texture0);     } } 

so, following part go vertices , normals, don't matter me @ moment, vertices work, while texture doesn't...

gluseprogram(shaderprogram);  gluniform1i(texid, 0);  glenablevertexattribarray(1); glbindbuffer(gl_array_buffer, tbo); glvertexattribpointer(1, 2, gl_float, gl_false, 0, (void*)0);  gldisablevertexattribarray(1); 

and completion, shaders, kept basic now, test texturing.

vertexshader

layout(location = 1) in vec2 vertexuv;  // output data ; interpolated each fragment. out vec2 uv;  void main(){  ....      // uv of vertex. no special space one.     uv = vertexuv; } 

fragmentshader

// interpolated values vertex shaders in vec2 uv;  // ouput data out vec3 color;  // values stay constant whole mesh. uniform sampler2d mytexturesampler;  void main(){      // output color = color of texture @ specified uv     color = texture2d( mytexturesampler, uv ).rgb; } 

so, that's it. said, no error, loads fine , displayed instead of texture blackness.

you specify gl_linear_mipmap_linear minifiaction filter, texture not mipmap complete, since specify level 0 (i assume image bigger 1x1 here). there several options:

  1. use filter gl_linear. useful quick test see if starts working then.
  2. supply whole set of mipmap leves. each level has 1/2 of width , 1/2 of height of previous one, until 1x1 reached.
  3. let gl generate mipmap levels via glgeneratemipmap().

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -