Esempio n. 1
0
// main creates a new scene with a camera and 3 vectors, renders the scene,
// draws the result to an image and encodes the image as PNG into a file.
// Optionally specify the filename.
func main() {
	var filename = flag.String("file", "triangle.png", "Filename to store image")
	flag.Parse()
	cam := render.NewDefCam()
	v1 := geom.NewVec4(-1, -1, -3)
	v2 := geom.NewVec4(0, 1, -5)
	v3 := geom.NewVec4(1, -1, -3)
	t := cam.PerspTransf(500, 500)
	w1 := t.Transf(v1)
	w1.Norm()
	w2 := t.Transf(v2)
	w2.Norm()
	w3 := t.Transf(v3)
	w3.Norm()
	x1 := tmath.Round(w1[0])
	y1 := tmath.Round(w1[1])
	x2 := tmath.Round(w2[0])
	y2 := tmath.Round(w2[1])
	x3 := tmath.Round(w3[0])
	y3 := tmath.Round(w3[1])
	img := image.NewImage(500, 500)
	col := color.RGBA{255, 255, 0, 255}
	img.DrawDot(x1, y1, col)
	img.DrawDot(x2, y2, col)
	img.DrawDot(x3, y3, col)
	img.DrawLine(x1, y1, x2, y2, col)
	img.DrawLine(x2, y2, x3, y3, col)
	img.DrawLine(x3, y3, x1, y1, col)
	file, err := os.Create(*filename)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	img.WritePng(file)
}
Esempio n. 2
0
// main creates a new scene with a camera and a triangle, renders the scene,
// draws the result to a window and displays it. The middle point of the
// triangle continuously changes position relative to the current time.
func main() {
	win, err := window.NewWindow(1024, 768, "Three Render 2", true)
	if err != nil {
		panic(err)
	}
	defer window.Terminate()
	cam := render.NewDefCam()
	p := geom.NewTri4(-1, 0, -3, 0, 1, -3, 1, 0, -3)
	for close := false; !close; close = win.ShouldClose() {
		now := time.Now()
		m := &p[1][1]
		*m = math.Sin(float64(now.UnixNano()) / 1e9)
		c := &cam.At[0]
		*c = math.Cos(float64(now.UnixNano()) / 1e9)
		cam.Ar = float64(win.Width()) / float64(win.Height())
		t := cam.PerspTransf(win.Width(), win.Height())
		q := p.Transf(t)
		win.Clear()
		q.Draw(win)
		win.Update()
	}
}
Esempio n. 3
0
// main creates a new scene with a camera and a triangle, renders the scene,
// draws the result to a window and displays it. The middle point of the
// triangle continuously changes position relative to the current time.
func main() {
	win, err := window.NewWindow(1024, 768, "Three Move", true)
	if err != nil {
		panic(err)
	}
	defer window.Terminate()
	cam := render.NewDefCam()
	p := geom.NewTri4(-1, 0, -3, 0, 1, -3, 1, 0, -3)
	then := time.Now()
	now := time.Now()
	for close := false; !close; close = win.ShouldClose() || win.KeyDown(window.KeyQ) {
		then = now
		now = time.Now()
		dt := now.Sub(then)
		d := mgeom.Vec3{0, 0, 0}
		if win.KeyDown(window.KeyW) {
			d.Add(&cam.At)
		}
		if win.KeyDown(window.KeyS) {
			d.Sub(&cam.At)
		}
		if win.KeyDown(window.KeyA) {
			d.Add(mgeom.Cross(&cam.At, &cam.Up))
		}
		if win.KeyDown(window.KeyD) {
			d.Add(mgeom.Cross(&cam.Up, &cam.At))
		}
		d.Norm()
		d.Scale(dt.Seconds())
		cam.Eye.Add(&d)
		cam.Ar = float64(win.Width()) / float64(win.Height())
		t := cam.PerspTransf(win.Width(), win.Height())
		q := p.Transf(t)
		win.Clear()
		q.Draw(win)
		win.Update()
	}
}