-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem.go
78 lines (66 loc) · 1.7 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
. "github.com/fogleman/fauxgl"
"github.com/nfnt/resize"
"os"
"path/filepath"
"time"
)
// ... (constants and variable declarations)
const (
scale = 4
width = 512
height = 512
near = 1
far = 10
storageDir = "/var/www/cdn/uploads"
cdnDirectory = "/var/www/cdn/thumbnails"
)
var (
eye = V(-2, 4, 8)
center = V(0, 0, 0)
up = V(0, 1, 0)
light = V(.1, 1, .6).Normalize()
ambient = HexColor("#828282")
fovy = float64(15)
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: program_name hash")
return
}
hash := os.Args[1]
objFilePath := filepath.Join(storageDir, hash+".obj")
pngFilePath := filepath.Join(storageDir, hash+".png")
thumbnailFilePath := filepath.Join(cdnDirectory, hash+".png")
itemMesh, err := LoadOBJ(objFilePath)
if err != nil {
fmt.Println("Error loading OBJ file:", err)
return
}
itemTexture, err := LoadTexture(pngFilePath)
if err != nil {
fmt.Println("Error loading PNG texture:", err)
return
}
mesh := NewEmptyMesh()
start := time.Now()
mesh.Add(itemMesh)
mesh.BiUnitCube()
context := NewContext(width*scale, height*scale)
shader := NewPhongShader(LookAt(eye, center, up).Perspective(fovy, float64(width)/float64(height), near, far), light, eye)
shader.SpecularPower = 0
shader.AmbientColor = ambient
context.Shader = shader
shader.Texture = itemTexture
context.DrawMesh(itemMesh)
thumbnailImage := resize.Resize(width, height, context.Image(), resize.Bilinear)
err = SavePNG(thumbnailFilePath, thumbnailImage)
if err != nil {
fmt.Println("Error saving thumbnail:", err)
return
}
fmt.Println("Thumbnail created:", thumbnailFilePath)
fmt.Println(time.Since(start))
}