Guest User

texture mapping

a guest
Jun 17th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. //Texture coloring
  2.  
  3. vec2_t tex1, tex2, tex3;
  4. vec2_copy_dest(&tex1, &rt->shape->vertices[0]->texCoord);
  5. vec2_copy_dest(&tex2, &rt->shape->vertices[1]->texCoord);
  6. vec2_copy_dest(&tex3, &rt->shape->vertices[2]->texCoord);
  7.  
  8. vec2_mul(&tex1, 1.f/rt->pRaster[0]->coords.z);
  9. vec2_mul(&tex2, 1.f/rt->pRaster[1]->coords.z);
  10. vec2_mul(&tex3, 1.f/rt->pRaster[2]->coords.z);
  11.  
  12. //rt->bc is a vec3_t with barycentric coordinates saved
  13.  
  14. float texx = rt->bc.coords.x*tex1.coords.x +
  15. rt->bc.coords.y*tex2.coords.x +
  16. rt->bc.coords.z*tex3.coords.x;
  17. float texy = rt->bc.coords.x*tex1.coords.y +
  18. rt->bc.coords.y*tex2.coords.y +
  19. rt->bc.coords.z*tex3.coords.y;
  20.  
  21. //this cuts texture in perspective...may be right should tested
  22. texx = (texx / rt->z)/rt->z;
  23. texy = (texy / rt->z)/rt->z;
  24. //can cause number over 1.08f seems a magic number otherwise we must iterate twice for interpolation
  25.  
  26. texx = interpolate_lin(texx, 0.f, 0.f, 1.08f, 512.f);
  27. texy = interpolate_lin(texy, 0.f, 0.f, 1.08f, 512.f);
  28.  
  29. int texIndex = floor(texy) * rt->renderer->imgWidth + floor(texx);
  30. crgb_crgb_copy(&curCol1, &rt->renderer->texture[texIndex]);
Advertisement
Add Comment
Please, Sign In to add comment