Michał Borek

Tech blog

Scala – ray tracing – tekstury

Nie trzeba było długo czekać, a projekt raytracera w Scali doczekał się małej aktualizacji.

Dodane zostało teksturowanie.

Za mapowanie tekstury odpowiada następujący fragment kodu:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def getTexel(vp: Vector): Color = {
    hasTexture match {
        case false => color
        case true =>
            val vn = new Vector(0,-1,0)
            val ve = new Vector(-1,0,0)
            val phi = Math.acos(-vn.dot(vp))
            val v = phi / Math.Pi
            val theta = (Math.acos(vp.dot(ve) / Math.sin(phi))) / (2 * Math.Pi)
            val u = vn.crossProduct(ve).dot(vp) > 0 match {
                case true => theta
                case false => 1 - theta
            }
            val color = new java.awt.Color(
                textureFile.getRGB(((u * _textureScale * textureFile.getWidth + _tPosX) % (textureFile.getWidth - 1)) .toInt, ((v * _textureScale * textureFile.getHeight + _tPosY) % (textureFile.getHeight - 1)).toInt))
            new Color(color.getRed / 255.0, color.getGreen / 255.0, color.getBlue / 255.0)
        }
    }

Efekt załączony na poniższym rysunku:

Tekstury


Share