erebus

annotate liberebus/src/texture.h @ 0:4abdce1361b9

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 Apr 2014 16:02:47 +0300
parents
children 474a0244f57d
rev   line source
nuclear@0 1 #ifndef TEXTURE_H_
nuclear@0 2 #define TEXTURE_H_
nuclear@0 3
nuclear@0 4 #include "image.h"
nuclear@0 5 #include "color.h"
nuclear@0 6
nuclear@0 7 class Texture {
nuclear@0 8 public:
nuclear@0 9 Image<float> img;
nuclear@0 10
nuclear@0 11 inline Color lookup(float u, float v) const;
nuclear@0 12
nuclear@0 13 bool load(const char *fname) { return img.load(fname); }
nuclear@0 14 };
nuclear@0 15
nuclear@0 16
nuclear@0 17 inline Color Texture::lookup(float u, float v) const
nuclear@0 18 {
nuclear@0 19 int xsz = img.get_width();
nuclear@0 20 int ysz = img.get_height();
nuclear@0 21
nuclear@0 22 int x = (float)u / (float)xsz;
nuclear@0 23 int y = (float)v / (float)ysz;
nuclear@0 24
nuclear@0 25 float *pix = img.get_pixels() + ((y % ysz) * xsz + (x % xsz)) * 4;
nuclear@0 26 return Color(pix[0], pix[1], pix[2]);
nuclear@0 27 }
nuclear@0 28
nuclear@0 29
nuclear@0 30 #endif // TEXTURE_H_