2015-04-19 15:14:42 +00:00
|
|
|
// +build ignore
|
|
|
|
|
2015-04-16 09:51:13 +00:00
|
|
|
// Ported from GLUT's samples. Original copyright below applies.
|
2011-04-27 08:06:14 +00:00
|
|
|
|
|
|
|
/* Copyright (c) Mark J. Kilgard, 1996. */
|
|
|
|
|
2015-04-16 09:51:13 +00:00
|
|
|
/* This program is freely distributable without licensing fees
|
|
|
|
and is provided without guarantee or warrantee expressed or
|
2011-04-27 08:06:14 +00:00
|
|
|
implied. This program is -not- in the public domain. */
|
|
|
|
|
2015-04-16 09:51:13 +00:00
|
|
|
/* This program is a response to a question posed by Gil Colgate
|
|
|
|
<gcolgate@sirius.com> about how lengthy a program is required using
|
|
|
|
OpenGL compared to using Direct3D immediate mode to "draw a
|
|
|
|
triangle at screen coordinates 0,0, to 200,200 to 20,200, and I
|
|
|
|
want it to be blue at the top vertex, red at the left vertex, and
|
|
|
|
green at the right vertex". I'm not sure how long the Direct3D
|
|
|
|
program is; Gil has used Direct3D and his guess is "about 3000
|
2011-04-27 08:06:14 +00:00
|
|
|
lines of code". */
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-01-13 09:14:12 +00:00
|
|
|
"io/ioutil"
|
2011-04-27 08:06:14 +00:00
|
|
|
"log"
|
2012-01-13 09:14:12 +00:00
|
|
|
"math"
|
|
|
|
"os"
|
2015-04-16 09:56:55 +00:00
|
|
|
"runtime"
|
2012-01-13 09:14:12 +00:00
|
|
|
"strings"
|
2011-04-27 08:06:14 +00:00
|
|
|
"time"
|
2015-04-16 09:51:13 +00:00
|
|
|
|
2015-04-16 09:56:55 +00:00
|
|
|
"github.com/go-gl/gl/v2.1/gl"
|
|
|
|
"github.com/go-gl/glfw/v3.1/glfw"
|
2015-04-16 09:51:13 +00:00
|
|
|
"github.com/llgcode/draw2d/draw2dgl"
|
2015-04-17 10:11:33 +00:00
|
|
|
"github.com/llgcode/ps"
|
2011-04-27 08:06:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var postscriptContent string
|
|
|
|
|
|
|
|
var (
|
|
|
|
width, height int
|
|
|
|
rotate int
|
2015-04-16 09:56:55 +00:00
|
|
|
window *glfw.Window
|
2011-04-27 08:06:14 +00:00
|
|
|
)
|
|
|
|
|
2015-04-16 09:56:55 +00:00
|
|
|
func reshape(window *glfw.Window, w, h int) {
|
2011-04-27 08:06:14 +00:00
|
|
|
gl.ClearColor(1, 1, 1, 1)
|
2015-04-16 09:51:13 +00:00
|
|
|
//fmt.Println(gl.GetString(gl.EXTENSIONS))
|
2015-04-16 09:56:55 +00:00
|
|
|
gl.Viewport(0, 0, int32(w), int32(h)) /* Establish viewing area to cover entire window. */
|
2011-04-27 08:06:14 +00:00
|
|
|
gl.MatrixMode(gl.PROJECTION) /* Start modifying the projection matrix. */
|
|
|
|
gl.LoadIdentity() /* Reset project matrix. */
|
|
|
|
gl.Ortho(0, float64(w), 0, float64(h), -1, 1) /* Map abstract coords directly to window coords. */
|
|
|
|
gl.Scalef(1, -1, 1) /* Invert Y axis so increasing Y goes down. */
|
|
|
|
gl.Translatef(0, float32(-h), 0) /* Shift origin up to upper-left corner. */
|
|
|
|
gl.Enable(gl.BLEND)
|
|
|
|
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
|
2011-05-18 21:23:31 +00:00
|
|
|
gl.Disable(gl.DEPTH_TEST)
|
2011-04-27 08:06:14 +00:00
|
|
|
width, height = w, h
|
|
|
|
}
|
|
|
|
|
|
|
|
func display() {
|
2015-04-17 13:35:50 +00:00
|
|
|
|
2011-04-27 08:06:14 +00:00
|
|
|
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
2015-04-17 13:35:50 +00:00
|
|
|
|
|
|
|
lastTime := time.Now()
|
2011-04-27 08:06:14 +00:00
|
|
|
gl.LineWidth(1)
|
|
|
|
gc := draw2dgl.NewGraphicContext(width, height)
|
|
|
|
|
|
|
|
gc.Translate(380, 400)
|
|
|
|
gc.Scale(1, -1)
|
|
|
|
rotate = (rotate + 1) % 360
|
|
|
|
gc.Rotate(float64(rotate) * math.Pi / 180)
|
|
|
|
gc.Translate(-380, -400)
|
2015-04-17 13:35:50 +00:00
|
|
|
|
2015-04-17 10:11:33 +00:00
|
|
|
interpreter := ps.NewInterpreter(gc)
|
2011-04-27 08:06:14 +00:00
|
|
|
reader := strings.NewReader(postscriptContent)
|
2015-04-17 13:35:50 +00:00
|
|
|
|
2011-04-27 08:06:14 +00:00
|
|
|
interpreter.Execute(reader)
|
2012-01-13 09:14:12 +00:00
|
|
|
dt := time.Now().Sub(lastTime)
|
2011-04-27 08:06:14 +00:00
|
|
|
log.Printf("Redraw in : %f ms\n", float64(dt)*1e-6)
|
|
|
|
gl.Flush() /* Single buffered, so needs a flush. */
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-04-17 10:11:33 +00:00
|
|
|
src, err := os.OpenFile("../../ps/samples/tiger.ps", 0, 0)
|
2011-04-27 08:06:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("can't find postscript file.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
bytes, err := ioutil.ReadAll(src)
|
|
|
|
postscriptContent = string(bytes)
|
2015-04-16 09:56:55 +00:00
|
|
|
err = glfw.Init()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer glfw.Terminate()
|
|
|
|
|
|
|
|
window, err = glfw.CreateWindow(800, 800, "Show Tiger in OpenGL", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
window.MakeContextCurrent()
|
|
|
|
window.SetSizeCallback(reshape)
|
|
|
|
window.SetKeyCallback(onKey)
|
|
|
|
|
|
|
|
glfw.SwapInterval(1)
|
|
|
|
|
|
|
|
err = gl.Init()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-04-17 13:35:50 +00:00
|
|
|
reshape(window, 800, 800)
|
2015-04-16 09:56:55 +00:00
|
|
|
for !window.ShouldClose() {
|
|
|
|
display()
|
2015-04-17 14:54:29 +00:00
|
|
|
window.SwapBuffers()
|
2015-04-16 09:56:55 +00:00
|
|
|
glfw.PollEvents()
|
|
|
|
// time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
|
|
|
|
switch {
|
|
|
|
case key == glfw.KeyEscape && action == glfw.Press,
|
|
|
|
key == glfw.KeyQ && action == glfw.Press:
|
|
|
|
w.SetShouldClose(true)
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 08:06:14 +00:00
|
|
|
|
2015-04-16 09:56:55 +00:00
|
|
|
func init() {
|
|
|
|
runtime.LockOSThread()
|
2011-04-27 08:06:14 +00:00
|
|
|
}
|