[C++] Texture Mapping Cilindro
Buonasera, devo effettuare il texture mapping del cilindro, ho applicato correttamente le formule per calcolare i punti u, v, a partire dal punto d'intersezione del cilindro, però non ottengo un texture mapping corretto, l'immagine risulta completamente distorta.Il codice per calcolare l'intersezione con il cilindro e i punti u,v è il seguente:
Questo è il codice relativo alla texture:
Non riesco a capire dove sia il problema.
[xdom="Raptorista"]Ho messo il codice nell'ambiente appropriato.[/xdom]
bool cylinder::hit(const ray& ray, float t_min, float t_max, hit_record& rec) const { float temp; float ox = ray.o.x; float oy = ray.o.y; float oz = ray.o.z; float dx = ray.d.x; float dy = ray.d.y; float dz = ray.d.z; float a = dx * dx + dz * dz; float b = 2.0f * (ox * dx + oz * dz); float c = ox * ox + oz * oz - radius * radius; float disc = b * b - 4.0f * a * c; if (disc < 0.0) return(false); else { float e = sqrt(disc); float denom = 2.0f * a; temp = (-b - e) / denom; // smaller root if (temp < t_max && temp > t_min) { float yhit = oy + temp * dy; if (yhit > y0 && yhit < y1) { rec.t = temp; rec.normal = normalize(vector3D((ox + temp * dx) * inv_radius, 0.0f, (oz + temp * dz) * inv_radius)); rec.p = ray.point_at_parameter(rec.t); float phi = (atan(rec.p.x / rec.p.z)); rec.u = phi / (2 * 3.14); rec.v = (rec.p.y + 1) / 2; //rec.m = mat; // test for hitting from inside if (dot(-ray.d, rec.normal) < 0.0f) rec.normal = -rec.normal; return (true); } } temp = (-b + e) / denom; // larger root if (temp < t_max && temp > t_min) { float yhit = oy + temp * dy; if (yhit > y0 && yhit < y1) { rec.t = temp; rec.normal = normalize(vector3D((ox + temp * dx) * inv_radius, 0.0f, (oz + temp * dz) * inv_radius)); rec.p = ray.point_at_parameter(rec.t); float phi = (atan(rec.p.x / rec.p.z)); rec.u = phi / (2 * 3.14); rec.v = (rec.p.y + 1) / 2; //rec.m = mat; // test for hitting inside surface if (dot(-ray.d, rec.normal) < 0.0f) rec.normal = -rec.normal; return (true); } } } return (false); }
Questo è il codice relativo alla texture:
color image_texture::value(float u, float v, const point3D& p) const { int i = (1.0f-u)*float(imageWidth); int j = (v)*imageHeight - 0.001f; if (i < 0) i = 0; if (j < 0) j = 0; if (i > imageWidth - 1) i = imageWidth - 1; if (j > imageHeight - 1) j = imageHeight - 1; Uint32 value = getpixel(surface, i, j); float red = float((value >> 16) & 0xff) / 255.0f; float green = float((value >> 8) & 0xff) / 255.0f; float blue = float(value & 0xff) / 255.0f; return color(blue, green, red); }
Non riesco a capire dove sia il problema.
[xdom="Raptorista"]Ho messo il codice nell'ambiente appropriato.[/xdom]
Risposte
Sarebbe utile vedere una immagine del risultato in modo da capire meglio che cosa intendi con "immagine distorta" e cercare l'errore in modo più mirato. Non vedo inoltre il codice che calcola le coordinate (u, v) ed è la parte del codice che più facilmente è sbagliata se la texture appare distorta.