Ejemplo n.º 1
0
func (r *RenderTarget) applyBlendMode(mode BlendMode) {
	switch mode {
	// glBlendFuncSeparateEXT is used when available to avoid an incorrect alpha value when the target
	// is a RenderTexture -- in this case the alpha value must be written directly to the target buffer

	// Alpha blending
	default:
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	case BlendAlpha:
		/*if (GLEW_EXT_blend_func_separate) {
		    glBlendFuncSeparateEXT(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
		} else {*/
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
		//}

	// Additive blending
	case BlendAdd:
		/*if GLEW_EXT_blend_func_separate {
			gl.BlendFuncSeparateEXT(gl.SRC_ALPHA, gl.ONE, gl.ONE, gl.ONE)
		} else {*/
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE)
		//}

	// Multiplicative blending
	case BlendMultiply:
		gl.BlendFunc(gl.DST_COLOR, gl.ZERO)

	// No blending
	case BlendNone:
		gl.BlendFunc(gl.ONE, gl.ZERO)
	}

	r.lastBlendMode = mode
}
Ejemplo n.º 2
0
func (sg *OpenGLGraphics) Rect(x, y, w, h int, style chart.Style) {
	// log.Panicf("Unimplemented: %s", whoami())
	x, y, w, h = chart.SanitizeRect(x, y, w, h, style.LineWidth)
	defer glh.OpenGLSentinel()()

	//

	glh.With(glh.Attrib{gl.ENABLE_BIT | gl.COLOR_BUFFER_BIT}, func() {
		glh.ColorC(style.FillColor)

		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

		gl.Begin(gl.QUADS)
		glh.Squarei(x, y, w, h)
		gl.End()
	})

	if style.LineWidth != 0 {
		gl.LineWidth(float32(style.LineWidth))
		//log.Print("Linewidth: ", float32(style.LineWidth))

		glh.With(glh.Attrib{gl.ENABLE_BIT | gl.COLOR_BUFFER_BIT}, func() {
			glh.ColorC(style.LineColor)

			gl.Enable(gl.BLEND)
			gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

			gl.Begin(gl.LINE_LOOP)
			glh.Squarei(x, y, w, h)
			gl.End()
		})
	}

	// linecol := style.LineColor
	// if linecol != "" {
	// 	s = fmt.Sprintf("stroke:%s; ", linecol)
	// } else {
	// 	linecol = "#808080"
	// }
	// s += fmt.Sprintf("stroke-width: %d; ", style.LineWidth)
	// s += fmt.Sprintf("opacity: %.2f; ", 1-style.Alpha)
	// if style.FillColor != "" {
	// 	s += fmt.Sprintf("fill: %s; fill-opacity: %.2f", style.FillColor, 1-style.Alpha)
	// } else {
	// 	s += "fill-opacity: 0"
	// }
	// sg.svg.Rect(x, y, w, h, s)
	// GenericRect(sg, x, y, w, h, style) // TODO
}
Ejemplo n.º 3
0
func main() {

	if !glfw.Init() {
		log.Fatal("glfw failed to initialize")
	}
	defer glfw.Terminate()

	window, err := glfw.CreateWindow(640, 480, "Deformable", nil, nil)
	if err != nil {
		log.Fatal(err.Error())
	}

	window.MakeContextCurrent()
	glfw.SwapInterval(1)
	window.SetMouseButtonCallback(handleMouseButton)
	window.SetKeyCallback(handleKeyDown)
	window.SetInputMode(glfw.Cursor, glfw.CursorHidden)

	gl.Init()
	initGL()

	i := 16
	m = GenerateMap(1600/i, 1200/i, i)
	for running && !window.ShouldClose() {

		x, y := window.GetCursorPosition()

		if drawing != 0 {
			m.Add(int(x)+int(camera[0]), int(y)+int(camera[1]), drawing, brushSizes[currentBrushSize])
		}

		gl.Clear(gl.COLOR_BUFFER_BIT)
		gl.LoadIdentity()

		gl.PushMatrix()
		gl.PushAttrib(gl.CURRENT_BIT | gl.ENABLE_BIT | gl.LIGHTING_BIT | gl.POLYGON_BIT | gl.LINE_BIT)
		gl.Translatef(-camera[0], -camera[1], 0)
		m.Draw()
		gl.PopAttrib()
		gl.PopMatrix()

		gl.PushAttrib(gl.COLOR_BUFFER_BIT)
		gl.LineWidth(2)
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.ONE_MINUS_DST_COLOR, gl.ZERO)
		// gl.Enable(gl.LINE_SMOOTH)
		// gl.Hint(gl.LINE_SMOOTH_HINT, gl.NICEST)

		gl.Translatef(float32(x), float32(y), 0)

		gl.EnableClientState(gl.VERTEX_ARRAY)
		gl.VertexPointer(2, gl.DOUBLE, 0, cursorVerts)
		gl.DrawArrays(gl.LINE_LOOP, 0, 24)
		gl.PopAttrib()

		window.SwapBuffers()
		glfw.PollEvents()
	}

}
Ejemplo n.º 4
0
func (m *Map) Draw() {

	// gl.Enable(gl.PRIMITIVE_RESTART)
	// gl.PrimitiveRestartIndex(PRIMITIVE_RESTART)
	gl.EnableClientState(gl.VERTEX_ARRAY)
	gl.Translatef(float32(m.gridSize/2), float32(m.gridSize/2), 0)

	if m.renderSmooth {
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
		gl.Enable(gl.POLYGON_SMOOTH)
		gl.Hint(gl.POLYGON_SMOOTH_HINT, gl.NICEST)
	}

	if m.renderMode == 1 {
		gl.LineWidth(1)
		gl.VertexPointer(2, gl.FLOAT, 0, m.gridLines)
		gl.Color3f(0.2, 0.2, 0.2)
		gl.DrawArrays(gl.LINES, 0, len(m.gridLines)/2)
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
	}

	for _, vl := range m.vl {
		if len(vl.vertices) > 0 {
			gl.VertexPointer(2, gl.FLOAT, 0, vl.vertices)
			gl.Color3f(vl.colors[0], vl.colors[1], vl.colors[2])
			gl.DrawElements(gl.TRIANGLES, len(vl.indices), gl.UNSIGNED_INT, vl.indices)
		}
	}

}
Ejemplo n.º 5
0
Archivo: 09.go Proyecto: nzlov/gogl
func initGL() (err error) {

	goph, err := os.Open(texturefiles[0])
	if err != nil {
		panic(err)
	}
	defer goph.Close()

	texture, err = createTexture(goph)

	gl.ShadeModel(gl.SMOOTH)
	gl.ClearColor(0, 0, 0, 0.5)
	gl.ClearDepth(1)
	gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE) // 设置混色函数取得半透明效果
	gl.Enable(gl.BLEND)
	gl.Enable(gl.TEXTURE_2D)

	rand.Seed(time.Now().Unix())
	for loop = 0; loop < num; loop++ {
		star[loop].angle = 0.0
		star[loop].dist = float32(loop) / num * 5
		//fmt.Println(loop, star[loop].dist)
		star[loop].r = uint8(rand.Int() % 256)
		star[loop].g = uint8(rand.Int() % 256)
		star[loop].b = uint8(rand.Int() % 256)
	}

	return
}
Ejemplo n.º 6
0
func (pen *Pen) lineTo(x, y int) {
	gl.Enable(gl.BLEND)
	gl.Enable(gl.POINT_SMOOTH)
	gl.Enable(gl.LINE_SMOOTH)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Color4f(0.0, 0.0, 0.0, 0.1)
	gl.Begin(gl.LINES)

	var p [2]int
	for i := range pen.points {
		p = pen.points[i]
		if p[0] == 0 && p[1] == 0 {
			continue
		}

		if distanceTo(x, y, p[0], p[1]) < 10.0 {
			gl.Vertex2i(x, y)
			gl.Vertex2i(p[0], p[1])
		}
	}

	gl.End()

	pen.n = (pen.n + 1) % len(pen.points)
	pen.points[pen.n][0] = x
	pen.points[pen.n][1] = y
	pen.moveTo(x, y)
}
Ejemplo n.º 7
0
// Opens a new window.
func (c *Controller) Open(win *Window) (err error) {
	c.Win = win
	mode := glfw.Windowed
	if win.Fullscreen {
		mode = glfw.Fullscreen
	}
	if win.Resize == false {
		glfw.OpenWindowHint(glfw.WindowNoResize, 1)
	}
	if err = glfw.OpenWindow(win.Width, win.Height, 0, 0, 0, 0, 0, 0, mode); err != nil {
		return
	}
	gl.Init()
	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	v1, v2, v3 := glfw.GLVersion()
	log.Printf("OpenGL version: %v %v %v\n", v1, v2, v3)
	fb_supported := glfw.ExtensionSupported("GL_EXT_framebuffer_object")
	log.Printf("Framebuffer supported: %v\n", fb_supported)
	c.SetClearColor(0, 0, 0, 0)
	if win.VSync == true {
		glfw.SetSwapInterval(1) // Limit to refresh
	}
	glfw.SetWindowTitle(win.Title)
	glfw.SetWindowSizeCallback(func(w, h int) {
		log.Printf("Resizing window to %v, %v\n", w, h)
		c.resize()
	})
	err = c.resize()
	return
}
Ejemplo n.º 8
0
func (sg *OpenGLGraphics) Line(x0, y0, x1, y1 int, style chart.Style) {
	//log.Panicf("Unimplemented: %s", whoami())
	defer glh.OpenGLSentinel()()

	gl.LineWidth(float32(style.LineWidth))

	// TODO: line stipple?
	//sc := chart.Color2RGBA(style.LineColor, uint8(style.Alpha*255))
	//log.Printf("color: %s %d %d %d %d", style.FillColor, sc.R, sc.G, sc.B, sc.A)

	glh.ColorC(style.LineColor)
	//sc := style.LineColor
	//gl.Color4ub(sc.R, sc.G, sc.B, sc.A)
	// TODO: Check this works

	glh.With(glh.Attrib{gl.ENABLE_BIT | gl.COLOR_BUFFER_BIT}, func() {
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

		glh.With(glh.Primitive{gl.LINES}, func() {
			gl.Vertex2i(x0, y0)
			gl.Vertex2i(x1, y1)
		})
	})
}
Ejemplo n.º 9
0
Archivo: gl.go Proyecto: jteeuwen/dcpu
// initGL initializes GLFW and OpenGL.
func initGL(c *cpu.CPU, width, height int) error {
	err := glfw.Init()
	if err != nil {
		return err
	}

	glfw.OpenWindowHint(glfw.WindowNoResize, 1)

	err = glfw.OpenWindow(width, height, 8, 8, 8, 8, 8, 0, glfw.Windowed)
	if err != nil {
		glfw.Terminate()
		return err
	}

	glfw.SetWindowTitle("DCPU")
	glfw.SetSwapInterval(1)
	glfw.SetWindowSizeCallback(onResize)

	gl.Init()
	err = glh.CheckGLError()
	if err != nil {
		return err
	}

	gl.Disable(gl.LIGHTING)
	gl.Disable(gl.DEPTH_TEST)
	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0, 0, 0, 1)
	return err
}
Ejemplo n.º 10
0
func (s *System) draw() {
	s.cmds <- func() {
		gl.Enable(gl.DEPTH_TEST)
		gl.Enable(gl.BLEND)
		gl.DepthFunc(gl.LEQUAL)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
		//s.shader.Use()
		//s.shader.AssignUniforms(uniforms)
	}
	for _, _ = range s.meshes.layouts {
		s.submit(renderState{})
	}
	for i := range s.buckets {
		list := &s.buckets[i]
		list.sort()
		s.cmds <- list.execute
	}
	return
	/*
		s.cmds <- func() {
			for _, geom := range geoms {
				// gl state
				// texture units
				// uniforms
				//shader.AssignUniforms()
				shader.SetGeometry(&geom)
				shader.Draw()
			}
		}
	*/
}
Ejemplo n.º 11
0
func initGL() (err error) {
	if err = loadTextures(); err != nil {
		return
	}

	gl.ShadeModel(gl.SMOOTH)
	gl.ClearColor(0, 0, 0, 0)
	gl.ClearDepth(1)
	gl.DepthFunc(gl.LEQUAL)
	gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.DEPTH_TEST)

	//alpha通道的值为 0.0意味着物体材质是完全透明的。1.0 则意味着完全不透明
	//以全亮度绘制此物体,并对其进行50%的alpha混合(半透明)。
	//当混合选项打开时,此物体将会产生50%的透明效果
	gl.Color4f(1, 1, 1, 0.5)           //全亮度, 50% Alpha 混合
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE) //基于源象素alpha通道值的半透明混合函数

	gl.Lightfv(gl.LIGHT1, gl.AMBIENT, ambient)
	gl.Lightfv(gl.LIGHT1, gl.AMBIENT, diffuse)
	gl.Lightfv(gl.LIGHT1, gl.POSITION, lightpos)
	gl.Enable(gl.LIGHT1)
	return
}
Ejemplo n.º 12
0
// initGL initializes GLFW and OpenGL.
func initGL() error {
	err := glfw.Init()
	if err != nil {
		return err
	}

	err = glfw.OpenWindow(AtlasSize, AtlasSize, 8, 8, 8, 8, 0, 0, glfw.Windowed)
	if err != nil {
		glfw.Terminate()
		return err
	}

	glfw.SetWindowTitle("Texture atlas example")
	glfw.SetSwapInterval(1)
	glfw.SetWindowSizeCallback(onResize)
	glfw.SetKeyCallback(onKey)

	gl.Init()
	if err = glh.CheckGLError(); err != nil {
		return err
	}

	gl.Disable(gl.DEPTH_TEST)
	gl.Disable(gl.LIGHTING)
	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0.2, 0.2, 0.23, 1.0)
	return nil
}
Ejemplo n.º 13
0
// OpenGL draw function
func draw() {
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.Enable(gl.BLEND)
	gl.Enable(gl.POINT_SMOOTH)
	gl.Enable(gl.LINE_SMOOTH)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.LoadIdentity()

	gl.Begin(gl.LINES)
	gl.Color3f(.2, .2, .2)
	for i := range staticLines {
		x := staticLines[i].GetAsSegment().A.X
		y := staticLines[i].GetAsSegment().A.Y
		gl.Vertex3f(float32(x), float32(y), 0)
		x = staticLines[i].GetAsSegment().B.X
		y = staticLines[i].GetAsSegment().B.Y
		gl.Vertex3f(float32(x), float32(y), 0)
	}
	gl.End()

	gl.Color4f(.3, .3, 1, .8)
	// draw balls
	for _, ball := range balls {
		gl.PushMatrix()
		pos := ball.Body.Position()
		rot := ball.Body.Angle() * chipmunk.DegreeConst
		gl.Translatef(float32(pos.X), float32(pos.Y), 0.0)
		gl.Rotatef(float32(rot), 0, 0, 1)
		drawCircle(float64(ballRadius), 60)
		gl.PopMatrix()
	}
}
Ejemplo n.º 14
0
// Binds the framebuffer for drawing.
func (fb *Framebuffer) Bind() {
	fb.Buffer.Bind()
	gl.Viewport(0, 0, fb.Width, fb.Height)
	gl.Enable(gl.TEXTURE_2D)
	gl.Disable(gl.DEPTH_TEST)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.BLEND)
}
Ejemplo n.º 15
0
func main() {
	_ = fmt.Sprint()

	if !glfw.Init() {
		panic("Can't init glfw!")
	}
	defer glfw.Terminate()

	// antialiasing
	//glfw.WindowHint(glfw.Samples, 4)

	window, err = glfw.CreateWindow(WindowWidth, WindowHeight, "Mozaik", nil, nil)
	if err != nil {
		panic(err)
	}
	defer window.Destroy()

	// Ensure thread context
	window.MakeContextCurrent()
	//glfw.SwapInterval(1)

	window.SetKeyCallback(keyCb)
	window.SetMouseButtonCallback(mouseCb)

	gl.Init()
	gl.ClearColor(0.9, 0.85, 0.46, 0.0)
	// useless in 2D
	gl.Disable(gl.DEPTH_TEST)
	// antialiasing
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.LINE_SMOOTH)

	for i := int32(32); i < 72; i++ {
		font := loadFonts(i)
		defer font.Release()
		fonts = append(fonts, font)
	}

	// Compute window radius
	windowRadius = math.Sqrt(math.Pow(WindowHeight, 2) + math.Pow(WindowWidth, 2))

	// Use window coordinates
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, WindowWidth, WindowHeight, 0, 0, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()

	g = NewGame()

	g.Start()
	go eventLoop(g)
	go renderLoop(g)
	Main()
	g.Stop()
}
Ejemplo n.º 16
0
func handleText(camera *PerspectiveCamera, text *Text) {
	// Material
	material := text.Material()
	program := material.Program()
	if program == nil {
		program = createTextProgram(text)
		material.SetProgram(program)
	}
	program.Use()
	defer program.Unuse()

	if c, ok := material.(Colored); ok {
		if c.Color() != nil {
			program.uniforms["diffuse"].apply(c.Color())
		}
	}

	if t, ok := material.(Textured); ok {
		texture := t.Texture()
		if texture != nil {
			gl.ActiveTexture(gl.TEXTURE0)
			texture.Bind()
			defer texture.Unbind()
			program.uniforms["texture"].apply(texture)
		}
	}

	for _, attribute := range program.attributes {
		attribute.enable()
		defer attribute.disable()
		attribute.bindBuffer()
		defer attribute.unbindBuffer()
		attribute.pointer()
		attribute.bindBuffer()
	}

	vertexAttrib := gl.AttribLocation(0)
	vertexAttrib.EnableArray()
	defer vertexAttrib.DisableArray()
	text.vertexBuffer.Bind(gl.ARRAY_BUFFER)
	defer text.vertexBuffer.Unbind(gl.ARRAY_BUFFER)
	vertexAttrib.AttribPointer(2, gl.FLOAT, false, 0, nil)

	if t, ok := material.(Wireframed); ok {
		if t.Wireframe() {
			gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
		} else {
			gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
		}
	}

	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	gl.DrawArrays(gl.TRIANGLES, 0, len(text.Geometry().Vertices))
}
Ejemplo n.º 17
0
func (w *Window) renderInit() {
	//bounds := w.body.Bounds()
	gl.Disable(gl.MULTISAMPLE)
	gl.Enable(gl.DEPTH_TEST)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
	//gl.Viewport(0, 0, bounds.Max.X, bounds.Max.Y)
	gl.ClearDepth(1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
}
Ejemplo n.º 18
0
func drawHex(x, y, kind int, alpha float32) {
	if kind == 6 {
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE)
		starTex.Bind(gl.TEXTURE_2D)
		gl.Begin(gl.QUADS)
		gl.TexCoord2f(0, 0)
		gl.Vertex2i(x, y)
		gl.TexCoord2f(0, 1)
		gl.Vertex2i(x, y+HEX_HEIGHT)
		gl.TexCoord2f(1, 1)
		gl.Vertex2i(x+HEX_WIDTH, y+HEX_HEIGHT)
		gl.TexCoord2f(1, 0)
		gl.Vertex2i(x+HEX_WIDTH, y)
		gl.End()
	} else {
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
		var r, g, b float32
		switch kind {
		case 0:
			r = 1
		case 1:
			g = 1
		case 2:
			b = 1
		case 3:
			r = 1
			g = 1
		case 4:
			r = 1
			b = 1
		case 5:
			g = 1 - 222/255
			b = 1
		}
		hexTex.Bind(gl.TEXTURE_2D)
		gl.Begin(gl.QUADS)
		if alpha < 1 {
			gl.Color4f(r, g, b, alpha)
		} else {
			gl.Color3f(r, g, b)
		}
		gl.TexCoord2f(0, 0)
		gl.Vertex2i(x, y)
		gl.TexCoord2f(0, 1)
		gl.Vertex2i(x, y+HEX_HEIGHT)
		gl.TexCoord2f(1, 1)
		gl.Vertex2i(x+HEX_WIDTH, y+HEX_HEIGHT)
		gl.TexCoord2f(1, 0)
		gl.Vertex2i(x+HEX_WIDTH, y)
		gl.End()
	}
}
Ejemplo n.º 19
0
func setupGL(w, h int) {
	gl.Viewport(0, 0, w, h)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(w), float64(h), 0, 0, 1)

	gl.ShadeModel(gl.SMOOTH)
	gl.ClearColor(1, 1, 1, 0)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Disable(gl.DEPTH_TEST)
	gl.Hint(gl.LINE_SMOOTH_HINT|gl.LINE_SMOOTH_HINT, gl.NICEST)
}
Ejemplo n.º 20
0
func (p *Path) glDraw(canv *Canvas) {
	for e := p.Segs.Front(); e != nil; e = e.Next() {
		e.Value.(PathSegment).Draw(canv)
	}

	if GetPt(p.endPoints.Back()).Mathgl().ApproxEqual(
		GetPt(p.endPoints.Front()).Mathgl()) {
		p.endPoints.Remove(p.endPoints.Back())
	}

	if p.endPoints.Len() < 3 {
		return
	}

	gGl.Activate(gGl.TriangleDrawer)

	gl.BlendFunc(gl.ONE_MINUS_DST_ALPHA, gl.ZERO)
	defer gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	pa := make(p2t.PointArray, 0, p.endPoints.Len())
	for e := p.endPoints.Front(); e != nil; e = e.Next() {
		p := GetPt(e)
		pa = append(pa, &p2t.Point{X: float64(p.X), Y: float64(p.Y)})
	}
	p2t.Init(pa)
	triArr := p2t.Triangulate()
	vertices := make([]float32, 6, 6)
	for _, tri := range triArr {
		for i, triPt := range tri.Point {
			pt := canv.toGLPoint(Pt(triPt.X, triPt.Y))
			vertices[i*2] = pt.X
			vertices[i*2+1] = pt.Y
		}
		gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, vertices, gl.STATIC_DRAW)
		gl.DrawArrays(gl.TRIANGLES, 0, 3)
	}
}
Ejemplo n.º 21
0
func initGL() (err error) {
	gl.Init()
	gl.ShadeModel(gl.SMOOTH)
	gl.ClearColor(0, 0, 0, 0)
	gl.ClearDepth(1)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
	gl.DepthFunc(gl.NEVER)
	gl.Enable(gl.BLEND)
	gl.DepthMask(true)

	//loadShader()

	return
}
Ejemplo n.º 22
0
func OpenGLInit() *OpenGL {
	g := &OpenGL{
		QuadraticDrawer: newQuadraticDrawer(),
		TriangleDrawer:  newTriangleDrawer(),
		FillDrawer:      newFillDrawer(),
	}
	g.QuadraticDrawConfig = &QuadraticDrawConfig{glProgramInfo{g.QuadraticDrawer.program}, false}
	g.QuadraticDrawer.AddConfig(g.QuadraticDrawConfig)
	g.GlColorConfig = &GlColorConfig{glProgramInfo{g.QuadraticDrawer.program}, color.RGBA{0, 0, 0, 1}}
	g.configs = append(g.configs, g.GlColorConfig)
	g.GlColorConfig.SetColor(Black)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.BLEND)
	gl.Enable(gl.STENCIL_TEST)
	return g
}
Ejemplo n.º 23
0
func (h *Hex) Render(alpha float32, drawFromCenter bool) {
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	if h.Kind == HexFlower {
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE)
		starTex.Bind(gl.TEXTURE_2D)
	} else {
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)
		hexTex.Bind(gl.TEXTURE_2D)
		gl.GetError()
		var r, g, b uint8
		r = uint8(colors.Colors[h.Kind-1][0])
		g = uint8(colors.Colors[h.Kind-1][1])
		b = uint8(colors.Colors[h.Kind-1][2])
		if alpha < 1 {
			gl.Color4ub(r, g, b, uint8(alpha*255))
		} else {
			gl.Color3ub(r, g, b)
		}
	}
	gl.Begin(gl.QUADS)
	gl.TexCoord2f(0, 0)
	if drawFromCenter {
		gl.Vertex2i(HEX_WIDTH/2, HEX_HEIGHT/2)
	} else {
		gl.Vertex2i(HEX_WIDTH, HEX_HEIGHT)
	}
	gl.TexCoord2f(0, 1)
	if drawFromCenter {
		gl.Vertex2i(HEX_WIDTH/2, -HEX_HEIGHT/2)
	} else {
		gl.Vertex2i(HEX_WIDTH, 0)
	}
	gl.TexCoord2f(1, 1)
	if drawFromCenter {
		gl.Vertex2i(-HEX_WIDTH/2, -HEX_HEIGHT/2)
	} else {
		gl.Vertex2i(0, 0)
	}
	gl.TexCoord2f(1, 0)
	if drawFromCenter {
		gl.Vertex2i(-HEX_WIDTH/2, HEX_HEIGHT/2)
	} else {
		gl.Vertex2i(0, HEX_HEIGHT)
	}
	gl.End()
}
Ejemplo n.º 24
0
func TexturedQuad(t *glh.Texture, x, y, w, h int) {
	glh.With(t, func() {
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
		gl.Color4f(255.0, 255.0, 255.0, 1.0)
		gl.Begin(gl.TRIANGLE_FAN)

		gl.TexCoord2f(0, 0)
		gl.Vertex2i(x, y)
		gl.TexCoord2f(1, 0)
		gl.Vertex2i(x+w, y)
		gl.TexCoord2f(1, 1)
		gl.Vertex2i(x+w, y+h)
		gl.TexCoord2f(0, 1)
		gl.Vertex2i(x, y+h)

		gl.End()
	})
}
Ejemplo n.º 25
0
func initGL() (err error) {
	if err = loadTextures(); err != nil {
		return
	}

	gl.ShadeModel(gl.SMOOTH)
	gl.ClearColor(0, 0, 0, 0)
	gl.ClearDepth(1)
	gl.DepthFunc(gl.LEQUAL)
	gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.DEPTH_TEST)
	gl.Color4f(1, 1, 1, 0.5)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE)

	gl.Lightfv(gl.LIGHT1, gl.AMBIENT, ambient)
	gl.Lightfv(gl.LIGHT1, gl.AMBIENT, diffuse)
	gl.Lightfv(gl.LIGHT1, gl.POSITION, lightpos)
	gl.Enable(gl.LIGHT1)
	return
}
Ejemplo n.º 26
0
Archivo: 10.go Proyecto: nzlov/gogl
func initGL() (err error) {

	goph, err := os.Open(texturefiles)
	if err != nil {
		panic(err)
	}
	defer goph.Close()

	texture, err = createTexture(goph)

	gl.Enable(gl.TEXTURE_2D)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE) // 设置混色函数取得半透明效果
	gl.ClearColor(0, 0, 0, 0.0)
	gl.ClearDepth(1)
	gl.DepthFunc(gl.LESS)
	gl.Enable(gl.DEPTH_TEST)
	gl.ShadeModel(gl.SMOOTH)
	gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)

	SetupWorld()

	return
}
Ejemplo n.º 27
0
Archivo: graf.go Proyecto: pwaller/mema
func Init() {
	//gl.Enable(gl.DEPTH_TEST)

	var tmpmaxa []float32 = []float32{0}

	gl.GetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, tmpmaxa)
	MaxAnisotropy = tmpmaxa[0]
	log.Print("MaxAnisotropy: ", MaxAnisotropy)

	// Anti-aliasing
	gl.Enable(gl.LINE_SMOOTH)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	//gl.BlendFunc(gl.SRC_ALPHA, gl.DST_ALPHA)
	//gl.BlendFunc(gl.SRC_ALPHA_SATURATE, gl.ONE)
	gl.Hint(gl.LINE_SMOOTH_HINT, gl.NICEST)
	gl.Hint(gl.GENERATE_MIPMAP_HINT, gl.NICEST)

	extensions := gl.GetString(gl.EXTENSIONS)
	if !strings.Contains(extensions, "GL_ARB_framebuffer_object") {
		log.Panic("No FBO support :-(")
	}
}
Ejemplo n.º 28
0
Archivo: text.go Proyecto: pwaller/glh
// Draw `text` at `x`, `y`.
func (text *Text) Draw(x, y int) {
	var w, h = text.W, text.H

	if text.Flipped {
		h = -h
	}
	With(Attrib{gl.ENABLE_BIT | gl.COLOR_BUFFER_BIT}, func() {
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.DST_ALPHA)
		//gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
		With(text, func() {
			gl.TexEnvi(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)
			DrawQuadi(x, y, w, h)
		})
	})

	if text.DebugRect {
		gl.LineWidth(1)
		With(Primitive{gl.LINE_LOOP}, func() {
			sp := 0
			Squarei(x-sp, y+sp, w+sp*2, h-sp*2)
		})
	}
}
Ejemplo n.º 29
0
func drawBorderAtXY(x, y float32, reverse int) {
	if x <= 80 || y <= 80 {
		return
	}
	gl.PushMatrix()
	gl.Translatef(x, y, 0)
	if reverse == 1 {
		gl.Rotatef(60, 0, 0, 1)
	}
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE)
	borderTex.Bind(gl.TEXTURE_2D)
	gl.Begin(gl.QUADS)
	gl.TexCoord2f(0, 0)
	gl.Vertex2i(-38, -38)
	gl.TexCoord2f(0, 1)
	gl.Vertex2i(-38, 38)
	gl.TexCoord2f(1, 1)
	gl.Vertex2i(38, 38)
	gl.TexCoord2f(1, 0)
	gl.Vertex2i(38, -38)
	gl.End()
	gl.PopMatrix()
}
Ejemplo n.º 30
0
func (sg *OpenGLGraphics) Scatter(points []chart.EPoint, plotstyle chart.PlotStyle, style chart.Style) {
	//log.Panicf("Unimplemented: %s", whoami())
	//chart.GenericScatter(sg, points, plotstyle, style)

	// TODO: Implement error bars/symbols

	buffer := glh.NewMeshBuffer(glh.RenderArrays,
		glh.NewPositionAttr(2, gl.DOUBLE, gl.STATIC_DRAW),
		glh.NewColorAttr(3, gl.UNSIGNED_INT, gl.STATIC_DRAW))

	// var vertices glh.ColorVertices

	positions := make([]float64, len(points)*2)
	colors := make([]uint32, len(points)*3)

	for _, p := range points {
		r, g, b, _ := style.LineColor.RGBA()

		colors = append(colors, r, g, b)
		positions = append(positions, p.X, p.Y)

		// vertices.Add(glh.ColorVertex{
		// 	glh.MkRGBA(style.LineColor),
		// 	glh.Vertex{float32(p.X), float32(p.Y)}})
	}

	buffer.Add()

	if style.LineWidth != 0 {
		gl.LineWidth(float32(style.LineWidth))
	} else {
		gl.LineWidth(1)
	}

	glh.With(glh.Attrib{gl.ENABLE_BIT | gl.COLOR_BUFFER_BIT}, func() {
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

		buffer.Render(gl.LINE_STRIP)
	})

	/***********************************************
	// First pass: Error bars
	ebs := style
	ebs.LineColor, ebs.LineWidth, ebs.LineStyle = ebs.FillColor, 1, chart.SolidLine
	if ebs.LineColor == "" {
		ebs.LineColor = "#404040"
	}
	if ebs.LineWidth == 0 {
		ebs.LineWidth = 1
	}
	for _, p := range points {
		xl, yl, xh, yh := p.BoundingBox()
		// fmt.Printf("Draw %d: %f %f-%f\n", i, p.DeltaX, xl,xh)
		if !math.IsNaN(p.DeltaX) {
			sg.Line(int(xl), int(p.Y), int(xh), int(p.Y), ebs)
		}
		if !math.IsNaN(p.DeltaY) {
			sg.Line(int(p.X), int(yl), int(p.X), int(yh), ebs)
		}
	}

	// Second pass: Line
	if (plotstyle&chart.PlotStyleLines) != 0 && len(points) > 0 {
		path := fmt.Sprintf("M %d,%d", int(points[0].X), int(points[0].Y))
		for i := 1; i < len(points); i++ {
			path += fmt.Sprintf("L %d,%d", int(points[i].X), int(points[i].Y))
		}
		st := linestyle(style)
		sg.svg.Path(path, st)
	}

	// Third pass: symbols
	if (plotstyle&chart.PlotStylePoints) != 0 && len(points) != 0 {
		for _, p := range points {
			sg.Symbol(int(p.X), int(p.Y), style)
		}
	}

	****************************************************/
}