This commit is contained in:
Laurent Le Goff 2011-04-21 21:36:00 +02:00
parent a7681160d7
commit b476b6531f
3 changed files with 107 additions and 108 deletions

View file

@ -1,11 +1,11 @@
// Ported from GLUT's samples. Original copyright below applies. // Ported from GLUT's samples. Original copyright below applies.
/* Copyright (c) Mark J. Kilgard, 1996. */ /* Copyright (c) Mark J. Kilgard, 1996. */
/* This program is freely distributable without licensing fees /* This program is freely distributable without licensing fees
and is provided without guarantee or warrantee expressed or and is provided without guarantee or warrantee expressed or
implied. This program is -not- in the public domain. */ implied. This program is -not- in the public domain. */
/* This program is a response to a question posed by Gil Colgate /* This program is a response to a question posed by Gil Colgate
<gcolgate@sirius.com> about how lengthy a program is required using <gcolgate@sirius.com> about how lengthy a program is required using
OpenGL compared to using Direct3D immediate mode to "draw a OpenGL compared to using Direct3D immediate mode to "draw a
@ -13,111 +13,111 @@
want it to be blue at the top vertex, red at the left vertex, and 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 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 program is; Gil has used Direct3D and his guess is "about 3000
lines of code". */ lines of code". */
package main package main
import ( import (
"os" "os"
"math" "math"
"io/ioutil" "io/ioutil"
"strings" "strings"
"gl" "gl"
"glut" "glut"
"image" "image"
"draw2d.googlecode.com/hg/draw2d" "draw2d.googlecode.com/hg/draw2d"
"draw2d.googlecode.com/hg/draw2dgl" "draw2d.googlecode.com/hg/draw2dgl"
"draw2d.googlecode.com/hg/postscript" "draw2d.googlecode.com/hg/postscript"
"log" "log"
"time" "time"
) )
var postscriptContent string var postscriptContent string
func TestDrawCubicCurve(gc draw2d.GraphicContext) { func TestDrawCubicCurve(gc draw2d.GraphicContext) {
// draw a cubic curve // draw a cubic curve
x, y := 25.6, 128.0 x, y := 25.6, 128.0
x1, y1 := 102.4, 230.4 x1, y1 := 102.4, 230.4
x2, y2 := 153.6, 25.6 x2, y2 := 153.6, 25.6
x3, y3 := 230.4, 128.0 x3, y3 := 230.4, 128.0
gc.SetStrokeColor(image.NRGBAColor{0, 0, 0, 0xff}) gc.SetStrokeColor(image.NRGBAColor{0, 0, 0, 0xff})
gc.SetLineWidth(10) gc.SetLineWidth(10)
gc.MoveTo(x, y) gc.MoveTo(x, y)
gc.CubicCurveTo(x1, y1, x2, y2, x3, y3) gc.CubicCurveTo(x1, y1, x2, y2, x3, y3)
gc.Stroke() gc.Stroke()
gc.SetStrokeColor(image.NRGBAColor{0xFF, 0x33, 0x33, 0x99}) gc.SetStrokeColor(image.NRGBAColor{0xFF, 0x33, 0x33, 0x99})
gc.SetLineWidth(6) gc.SetLineWidth(6)
// draw segment of curve // draw segment of curve
gc.MoveTo(x, y) gc.MoveTo(x, y)
gc.LineTo(x1, y1) gc.LineTo(x1, y1)
gc.MoveTo(x2, y2) gc.MoveTo(x2, y2)
gc.LineTo(x3, y3) gc.LineTo(x3, y3)
gc.Stroke() gc.Stroke()
} }
var ( var (
width, height int width, height int
rotate int rotate int
) )
func reshape(w, h int) { func reshape(w, h int) {
/* Because Gil specified "screen coordinates" (presumably with an /* Because Gil specified "screen coordinates" (presumably with an
upper-left origin), this short bit of code sets up the coordinate upper-left origin), this short bit of code sets up the coordinate
system to correspond to actual window coodrinates. This code system to correspond to actual window coodrinates. This code
wouldn't be required if you chose a (more typical in 3D) abstract wouldn't be required if you chose a (more typical in 3D) abstract
coordinate system. */ coordinate system. */
gl.ClearColor(1, 1, 1, 1) gl.ClearColor(1, 1, 1, 1)
//fmt.Println(gl.GetString(gl.EXTENSIONS)) //fmt.Println(gl.GetString(gl.EXTENSIONS))
gl.Viewport(0, 0, w, h) /* Establish viewing area to cover entire window. */ gl.Viewport(0, 0, w, h) /* Establish viewing area to cover entire window. */
gl.MatrixMode(gl.PROJECTION) /* Start modifying the projection matrix. */ gl.MatrixMode(gl.PROJECTION) /* Start modifying the projection matrix. */
gl.LoadIdentity() /* Reset project matrix. */ gl.LoadIdentity() /* Reset project matrix. */
gl.Ortho(0, float64(w), 0, float64(h), -1, 1) /* Map abstract coords directly to window coords. */ 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.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.Translatef(0, float32(-h), 0) /* Shift origin up to upper-left corner. */
gl.Enable(gl.BLEND) gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
width, height = w, h width, height = w, h
} }
func display() { func display() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.LineWidth(1) gl.LineWidth(1)
gc := draw2dgl.NewGraphicContext(width, height) gc := draw2dgl.NewGraphicContext(width, height)
gc.Translate(380, 400) gc.Translate(380, 400)
gc.Scale(1, -1) gc.Scale(1, -1)
rotate = (rotate + 10) % 360 rotate = (rotate + 10) % 360
gc.Rotate(float64(rotate) * math.Pi / 180) gc.Rotate(float64(rotate) * math.Pi / 180)
gc.Translate(-380, -400) gc.Translate(-380, -400)
interpreter := postscript.NewInterpreter(gc) interpreter := postscript.NewInterpreter(gc)
reader := strings.NewReader(postscriptContent) reader := strings.NewReader(postscriptContent)
lastTime := time.Nanoseconds() lastTime := time.Nanoseconds()
interpreter.Execute(reader) interpreter.Execute(reader)
dt := time.Nanoseconds() - lastTime dt := time.Nanoseconds() - lastTime
log.Printf("Redraw in : %f ms\n", float64(dt)*1e-6) log.Printf("Redraw in : %f ms\n", float64(dt)*1e-6)
gl.Flush() /* Single buffered, so needs a flush. */ gl.Flush() /* Single buffered, so needs a flush. */
glut.PostRedisplay() glut.PostRedisplay()
} }
func main() { func main() {
src, err := os.OpenFile("../resource/postscript/tiger.ps", 0, 0) src, err := os.OpenFile("../resource/postscript/tiger.ps", 0, 0)
if err != nil { if err != nil {
log.Println("can't find postscript file.") log.Println("can't find postscript file.")
return return
} }
defer src.Close() defer src.Close()
bytes, err := ioutil.ReadAll(src) bytes, err := ioutil.ReadAll(src)
postscriptContent = string(bytes) postscriptContent = string(bytes)
glut.Init() glut.Init()
glut.InitWindowSize(800, 800) glut.InitWindowSize(800, 800)
glut.CreateWindow("single triangle") glut.CreateWindow("single triangle")
glut.DisplayFunc(display) glut.DisplayFunc(display)
glut.ReshapeFunc(reshape) glut.ReshapeFunc(reshape)
glut.MainLoop() glut.MainLoop()
} }

View file

@ -118,7 +118,7 @@ func WndProc(hwnd, msg uint32, wparam, lparam int32) uintptr {
hdc := wingui.BeginPaint(hwnd, &ps) hdc := wingui.BeginPaint(hwnd, &ps)
gc := draw2d.NewGraphicContext(backBuffer) gc := draw2d.NewGraphicContext(backBuffer)
gc.SetFillColor(image.RGBAColor{0xFF, 0xFF, 0xFF, 0xFF}) gc.SetFillColor(image.RGBAColor{0xFF, 0xFF, 0xFF, 0xFF})
// gc.Clear() // gc.Clear()
gc.Save() gc.Save()
//gc.Translate(0, -380) //gc.Translate(0, -380)
interpreter := postscript.NewInterpreter(gc) interpreter := postscript.NewInterpreter(gc)

View file

@ -155,4 +155,3 @@ func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) {
gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor) gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor)
gc.Current.Path = new(draw2d.PathStorage) gc.Current.Path = new(draw2d.PathStorage)
} }