Esempio n. 1
0
func (c *Context) BindTexture(target int, texture *Texture) {
	if texture == nil {
		gl.BindTexture(uint32(target), 0)
		return
	}
	gl.BindTexture(uint32(target), texture.uint32)
}
Esempio n. 2
0
func (view *GameView) Update(t, dt float64) {
	if dt > 1 {
		dt = 0
	}
	window := view.director.window
	console := view.console
	if joystickReset(glfw.Joystick1) {
		view.director.ShowMenu()
	}
	if joystickReset(glfw.Joystick2) {
		view.director.ShowMenu()
	}
	if readKey(window, glfw.KeyEscape) {
		view.director.ShowMenu()
	}
	updateControllers(window, console)
	console.StepSeconds(dt)
	gl.BindTexture(gl.TEXTURE_2D, view.texture)
	setTexture(console.Buffer())
	drawBuffer(view.director.window)
	gl.BindTexture(gl.TEXTURE_2D, 0)
	if view.record {
		view.frames = append(view.frames, copyImage(console.Buffer()))
	}
}
Esempio n. 3
0
func createTexture() uint32 {
	var texture uint32
	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	gl.BindTexture(gl.TEXTURE_2D, 0)
	return texture
}
Esempio n. 4
0
func NewTexture() *Texture {
	texture := createTexture()
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexImage2D(
		gl.TEXTURE_2D, 0, gl.RGBA,
		textureSize, textureSize,
		0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
	gl.BindTexture(gl.TEXTURE_2D, 0)
	t := Texture{}
	t.texture = texture
	t.lookup = make(map[string]int)
	t.ch = make(chan string, 1024)
	return &t
}
Esempio n. 5
0
func LataaKuvat() {
	file, err := os.Open("tiles.png")
	if err != nil {
		panic("En saa avattua kuvatiedostoa: " + err.Error())
	}

	kuva, _, err := image.Decode(file)
	if err != nil {
		panic("Kuva on rikki: " + err.Error())
	}

	var texture uint32
	gl.GenTextures(1, &texture)

	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexImage2D(gl.TEXTURE_2D, 0, 4,
		int32(kuva.Bounds().Dx()), int32(kuva.Bounds().Dy()), 0,
		gl.RGBA, gl.UNSIGNED_BYTE,
		unsafe.Pointer(&kuva.(*image.RGBA).Pix[0]),
	)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	gl.Enable(gl.TEXTURE_2D)
}
Esempio n. 6
0
func newChipset(file string, tilesize int) chipset {
	imgFile, err := os.Open(file)
	if err != nil {
		log.Fatalf("texture %q not found on disk: %v\n", file, err)
	}
	img, _, err := image.Decode(imgFile)
	if err != nil {
		panic(err)
	}

	rgba := image.NewRGBA(img.Bounds())
	draw.Draw(rgba, rgba.Bounds(), img, image.ZP, draw.Src)

	var texture uint32
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0, gl.RGBA,
		int32(rgba.Rect.Size().X),
		int32(rgba.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rgba.Pix))

	return chipset{width: rgba.Bounds().Dx(), height: rgba.Bounds().Dy(), handle: texture, tilesize: tilesize}
}
Esempio n. 7
0
func (c *Context) bindTextureImpl(t Texture) error {
	_ = c.runOnContextThread(func() error {
		gl.BindTexture(gl.TEXTURE_2D, uint32(t))
		return nil
	})
	return nil
}
Esempio n. 8
0
func (ctx *DrawContext) drawHud(o *orrery.Orrery, frametime time.Duration) {
	txt, size, err := ctx.createHudTexture(o, frametime)
	if err != nil {
		log.Fatalf(`can't create texture from text surface: %s`, err)
	}
	defer gl.DeleteTextures(1, &txt)

	gl.MatrixMode(gl.PROJECTION)
	gl.PushMatrix()
	gl.LoadIdentity()
	gl.Ortho(0.0, float64(ctx.width), float64(ctx.height), 0.0, -1.0, 1.0)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Clear(gl.DEPTH_BUFFER_BIT)

	gl.BindTexture(gl.TEXTURE_2D, txt)
	gl.Enable(gl.TEXTURE_2D)
	defer gl.Disable(gl.TEXTURE_2D)

	gl.Color3f(1, 1, 1)
	gl.Begin(gl.QUADS)
	gl.TexCoord2f(0, 0)
	gl.Vertex2f(0.0, 0.0)
	gl.TexCoord2f(1, 0)
	gl.Vertex2f(float32(size[0]), 0.0)
	gl.TexCoord2f(1, 1)
	gl.Vertex2f(float32(size[0]), float32(size[1]))
	gl.TexCoord2f(0, 1)
	gl.Vertex2f(0.0, float32(size[1]))
	gl.End()

	gl.PopMatrix()
}
Esempio n. 9
0
func (img *glImage) DrawColoredAtXY(x, y int, color [4]float32) {
	// TODO have the state knwon globally somewhere so this does not need to be
	// called all the time
	gl.Enable(gl.TEXTURE_2D)
	gl.BindTexture(gl.TEXTURE_2D, img.id)

	gl.Begin(gl.QUADS)

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.left, img.top)
	gl.Vertex2i(int32(x), int32(y))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.right, img.top)
	gl.Vertex2i(int32(x+img.Width), int32(y))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.right, img.bottom)
	gl.Vertex2i(int32(x+img.Width), int32(y+img.Height))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.left, img.bottom)
	gl.Vertex2i(int32(x), int32(y+img.Height))

	gl.End()
}
Esempio n. 10
0
func loadTexture(fileName string) (uint32, error) {
	imgFile, err := os.Open("./res/textures/" + fileName)
	if err != nil {
		return 0, err
	}

	imgCfg, _, err := image.DecodeConfig(imgFile)
	if err != nil {
		return 0, err
	}
	_, err = imgFile.Seek(0, 0)
	if err != nil {
		return 0, err
	}

	w, h := int32(imgCfg.Width), int32(imgCfg.Height)

	img, _, err := image.Decode(imgFile)
	if err != nil {
		return 0, err
	}

	buffer := make([]byte, w*h*4)
	index := 0
	for y := 0; y < int(h); y++ {
		for x := 0; x < int(w); x++ {
			pixel := img.At(x, y).(color.NRGBA)
			buffer[index] = pixel.R
			buffer[index+1] = pixel.G
			buffer[index+2] = pixel.B
			buffer[index+3] = pixel.A

			index += 4
		}
	}

	var texture uint32

	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA8,
		w,
		h,
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(buffer))

	return texture, nil
}
Esempio n. 11
0
func (ctx *DrawContext) createHudTexture(o *orrery.Orrery, frametime time.Duration) (uint32, [2]int, error) {
	lines := []string{}

	if o.Paused {
		lines = append(lines, "PAUSED")
	}

	if ctx.verbose {
		lines = append(lines, []string{
			"WASD: Move, 1: Toggle wireframe, H: Toggle HUD verbosity, Q: Quit",
			"Mouse Wheel: Move fast, Mouse Btn #1: Spawn particle, V: Spawn 10 particles",
			"Space: Reset camera, P: Toggle pause",
		}...)
	}

	lines = append(lines, []string{
		fmt.Sprintf(` α: %0.2f θ: %0.2f`, ctx.cam.alpha, ctx.cam.theta),
		fmt.Sprintf(` x: %0.2f y: %0.2f z: %0.2f`, ctx.cam.Pos.X, ctx.cam.Pos.Y, ctx.cam.Pos.Z),
		fmt.Sprintf(` Last frame time: %s`, frametime),
	}...)

	if ctx.verbose {
		particles := o.Particles()
		lines = append(lines, fmt.Sprintf(`#P: %d`, len(particles)))
		for i, p := range particles {
			p.L.Lock()
			l := fmt.Sprintf(` π %d: %s`, i, p)
			p.L.Unlock()
			lines = append(lines, l)
		}
	}

	var txt uint32
	gl.GenTextures(1, &txt)

	gl.BindTexture(gl.TEXTURE_2D, txt)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_BASE_LEVEL, 0)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAX_LEVEL, 0)

	bg := color.RGBA{0, 0, 0, 0}
	fg := color.RGBA{0, 255, 255, 255}
	img, err := ctx.txt.RenderMultiline(lines, 12.5, bg, fg)
	if err != nil {
		return 0, [2]int{0, 0}, err
	}
	v := reflect.ValueOf(img.Pix)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA,
		int32(img.Bounds().Dx()), int32(img.Bounds().Dy()),
		0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(v.Index(0).UnsafeAddr()))

	return txt, [2]int{img.Bounds().Dx(), img.Bounds().Dy()}, nil
}
Esempio n. 12
0
func main() {
	runtime.LockOSThread()

	if err := glfw.Init(); err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	window, err := glfw.CreateWindow(800, 600, "fontstash example", nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()
	glfw.SwapInterval(1)
	gl.Init()

	data, err := ioutil.ReadFile(filepath.Join("..", "ClearSans-Regular.ttf"))
	if err != nil {
		panic(err)
	}

	gl.Enable(gl.TEXTURE_2D)

	tmpBitmap := make([]byte, 512*512)
	cdata, err, _, tmpBitmap := truetype.BakeFontBitmap(data, 0, 32, tmpBitmap, 512, 512, 32, 96)
	var ftex uint32
	gl.GenTextures(1, &ftex)
	gl.BindTexture(gl.TEXTURE_2D, ftex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, 512, 512, 0,
		gl.ALPHA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tmpBitmap[0]))

	gl.ClearColor(0.3, 0.3, 0.32, 1.)

	for !window.ShouldClose() {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.MatrixMode(gl.PROJECTION)
		gl.LoadIdentity()
		gl.Ortho(0, 800, 600, 0, 0, 1)
		gl.MatrixMode(gl.MODELVIEW)
		gl.LoadIdentity()
		gl.Disable(gl.DEPTH_TEST)
		gl.Color4ub(255, 255, 255, 255)
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

		my_print(100, 100, "The quick brown fox jumps over the fence", ftex, cdata)

		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Esempio n. 13
0
func (stash *Stash) FlushDraw() {
	i := 0
	texture := stash.ttTextures[i]
	tt := true
	for {
		if texture.nverts > 0 {
			gl.Enable(gl.TEXTURE_2D)
			gl.BindTexture(gl.TEXTURE_2D, texture.id)
			for k := 0; k < texture.nverts; k++ {
				gl.Begin(gl.QUADS)
				gl.Color4fv(&texture.color[0])
				gl.TexCoord2f(texture.verts[k*4+2], texture.verts[k*4+3])
				gl.Vertex2f(texture.verts[k*4+0], texture.verts[k*4+1])
				k++
				gl.Color4fv(&texture.color[0])
				gl.TexCoord2f(texture.verts[k*4+2], texture.verts[k*4+3])
				gl.Vertex2f(texture.verts[k*4+0], texture.verts[k*4+1])
				k++
				gl.Color4fv(&texture.color[0])
				gl.TexCoord2f(texture.verts[k*4+2], texture.verts[k*4+3])
				gl.Vertex2f(texture.verts[k*4+0], texture.verts[k*4+1])
				k++
				gl.Color4fv(&texture.color[0])
				gl.TexCoord2f(texture.verts[k*4+2], texture.verts[k*4+3])
				gl.Vertex2f(texture.verts[k*4+0], texture.verts[k*4+1])
				gl.End()
			}
			gl.Disable(gl.TEXTURE_2D)
			texture.nverts = 0
		}
		if tt {
			if i < len(stash.ttTextures)-1 {
				i++
				texture = stash.ttTextures[i]
			} else {
				i = 0
				if len(stash.bmTextures) > 0 {
					texture = stash.bmTextures[i]
					tt = false
				} else {
					break
				}
			}
		} else {
			if i < len(stash.bmTextures)-1 {
				i++
				texture = stash.bmTextures[i]
			} else {
				break
			}
		}
	}
}
Esempio n. 14
0
func NewTexture(img *image.RGBA, opts ...TextureOption) (*Texture, error) {
	if img.Stride != img.Rect.Size().X*4 {
		return nil, fmt.Errorf("unsupported stride in texture image")
	}
	opt := textureOption{
		filterMin: LINEAR,
		filterMag: LINEAR,
		wrap_s:    REPEAT,
		wrap_t:    REPEAT,
	}
	for _, o := range opts {
		o(&opt)
	}

	var id uint32
	gl.GenTextures(1, &id)
	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, id)
	defer gl.BindTexture(gl.TEXTURE_2D, 0)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(opt.filterMin))
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(opt.filterMag))
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, int32(opt.wrap_s))
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, int32(opt.wrap_t))
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(img.Rect.Size().X),
		int32(img.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(img.Pix))
	return &Texture{
		id:   id,
		size: img.Rect.Size(),
	}, nil
}
Esempio n. 15
0
func (s *Shader) updateUniforms(projectedMatrix Matrix4f, material *Material) {
	if s.uniforms == nil {
		return
	}

	if material.texture != nil {
		material.texture.bind()
	} else {
		gl.BindTexture(gl.TEXTURE_2D, 0)
	}

	s.setUniformM("transform", projectedMatrix)
	s.setUniform("color", material.color)
}
Esempio n. 16
0
func (atlas *FontAtlas) Draw(text string, b Bounds) {
	atlas.LoadGlyphs(text)

	gl.Enable(gl.BLEND)
	defer gl.Disable(gl.BLEND)
	gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)

	gl.Enable(gl.TEXTURE_2D)
	defer gl.Disable(gl.TEXTURE_2D)
	gl.BindTexture(gl.TEXTURE_2D, atlas.Texture)

	x := b.Min.X + atlas.drawPadding
	y := (b.Max.Y+b.Min.Y)/2 + (ceilPxf(atlas.maxBounds.Min.Y)+ceilPxf(atlas.maxBounds.Max.Y))/2

	p := rune(0)
	for _, r := range text {
		glyph := atlas.Rendered[r]

		dx := float32(glyph.Loc.Dx())
		dy := float32(glyph.Loc.Dy())

		px := x + ceilPxf(glyph.Bounds.Min.X) - glyphPadding
		py := y + ceilPxf(glyph.Bounds.Min.Y) - glyphPadding

		// this is not the ideal way of positioning the letters
		// will create positioning artifacts
		// but it the result is more
		px = float32(math.Trunc(float64(px)))
		py = float32(math.Trunc(float64(py)))

		gl.Begin(gl.QUADS)
		{
			gl.TexCoord2f(glyph.RelLoc.Min.X, glyph.RelLoc.Min.Y)
			gl.Vertex2f(px, py)
			gl.TexCoord2f(glyph.RelLoc.Max.X, glyph.RelLoc.Min.Y)
			gl.Vertex2f(px+dx, py)
			gl.TexCoord2f(glyph.RelLoc.Max.X, glyph.RelLoc.Max.Y)
			gl.Vertex2f(px+dx, py+dy)
			gl.TexCoord2f(glyph.RelLoc.Min.X, glyph.RelLoc.Max.Y)
			gl.Vertex2f(px, py+dy)
		}
		gl.End()

		k := atlas.Face.Kern(p, r)
		p = r
		x += ceilPxf(glyph.Advance + k)
	}
}
Esempio n. 17
0
func LoadTexture(path string) {
	//fmt.Printf("Trying to load texture %q: ", path)

	// Open the file
	file, err := os.Open(path)
	if err != nil {
		fmt.Println(os.Getwd())
		log.Fatal(err)
	}
	defer file.Close()

	// Decode the image
	img, _, err := image.Decode(file)
	if err != nil {
		log.Fatal(err)
	}

	bounds := img.Bounds()
	//fmt.Printf("Loaded %vx%v texture.\n", bounds.Dx(), bounds.Dy())

	var format int
	var pixPointer *uint8
	switch img := img.(type) {
	case *image.RGBA:
		format = gl.RGBA
		pixPointer = &img.Pix[0]
	case *image.NRGBA:
		format = gl.RGBA
		pixPointer = &img.Pix[0]
	case *image.Gray:
		format = gl.ALPHA
		pixPointer = &img.Pix[0]
	default:
		log.Fatalf("LoadTexture: Unsupported type %T.\n", img)
	}

	var texture uint32
	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.GENERATE_MIPMAP, gl.TRUE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_LOD_BIAS, -0.5)
	gl.TexImage2D(gl.TEXTURE_2D, 0, int32(format), int32(bounds.Dx()), int32(bounds.Dy()), 0, uint32(format), gl.UNSIGNED_BYTE, gl.Ptr(pixPointer))
	CheckGLError()
}
Esempio n. 18
0
func (v *Video) initGL() {
	if err := gl.Init(); err != nil {
		panic(err)
	}

	gl.Enable(gl.CULL_FACE)
	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)

	v.prog = createProgram(vertShaderSrcDef, fragShaderSrcDef)
	posAttrib := uint32(gl.GetAttribLocation(v.prog, gl.Str("vPosition"+"\x00")))
	texCoordAttr := uint32(gl.GetAttribLocation(v.prog, gl.Str("vTexCoord"+"\x00")))
	v.textureUni = gl.GetAttribLocation(v.prog, gl.Str("texture"+"\x00"))

	var texture uint32
	gl.GenTextures(1, &texture)
	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

	gl.UseProgram(v.prog)
	gl.EnableVertexAttribArray(posAttrib)
	gl.EnableVertexAttribArray(texCoordAttr)
	//posAttrib.EnableArray()
	//texCoordAttr.EnableArray()

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	verts := []float32{-1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0}
	gl.BufferData(gl.ARRAY_BUFFER, len(verts)*int(unsafe.Sizeof(verts[0])), gl.Ptr(verts), gl.STATIC_DRAW)

	var textCoorBuf uint32
	gl.GenBuffers(1, &textCoorBuf)
	gl.BindBuffer(gl.ARRAY_BUFFER, textCoorBuf)
	texVerts := []float32{0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}
	gl.BufferData(gl.ARRAY_BUFFER, len(texVerts)*int(unsafe.Sizeof(texVerts[0])), gl.Ptr(texVerts), gl.STATIC_DRAW)

	gl.VertexAttribPointer(posAttrib, 2, gl.FLOAT, false, 0, gl.PtrOffset(0))
	gl.VertexAttribPointer(texCoordAttr, 2, gl.FLOAT, false, 0, gl.PtrOffset(0))
	//posAttrib.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0))
	//texCoordAttr.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0))
}
Esempio n. 19
0
func (atlas *FontAtlas) draw(rendered *image.RGBA, b Bounds) {
	var texture uint32
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)

	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(rendered.Bounds().Dx()),
		int32(rendered.Bounds().Dy()),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rendered.Pix))

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

	gl.Begin(gl.QUADS)
	{
		gl.TexCoord2f(0, 0)
		gl.Vertex2f(b.Min.X, b.Min.Y)
		gl.TexCoord2f(1, 0)
		gl.Vertex2f(b.Max.X, b.Min.Y)
		gl.TexCoord2f(1, 1)
		gl.Vertex2f(b.Max.X, b.Max.Y)
		gl.TexCoord2f(0, 1)
		gl.Vertex2f(b.Min.X, b.Max.Y)
	}
	gl.End()

	gl.Disable(gl.BLEND)

	gl.DeleteTextures(1, &texture)
	gl.Disable(gl.TEXTURE_2D)
}
Esempio n. 20
0
func NewGLImageFromImage(img image.Image) (*glImage, error) {
	var rgba *image.RGBA
	if asRGBA, ok := img.(*image.RGBA); ok {
		rgba = asRGBA
	} else {
		rgba = image.NewRGBA(img.Bounds())
		if rgba.Stride != rgba.Rect.Size().X*4 {
			return nil, errors.New("unsupported stride")
		}
		draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
	}

	var tex uint32
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &tex)
	gl.BindTexture(gl.TEXTURE_2D, tex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(rgba.Bounds().Dx()),
		int32(rgba.Bounds().Dy()),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rgba.Pix),
	)

	return &glImage{
		tex,
		img.Bounds().Dx(),
		img.Bounds().Dy(),
		0.0,
		0.0,
		1.0,
		1.0,
	}, nil
}
Esempio n. 21
0
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) {
	var t uint32
	gl.GenTextures(1, &t)
	if t < 0 {
		return 0, errors.New("glGenTexture failed")
	}
	gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
	gl.BindTexture(gl.TEXTURE_2D, t)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(filter))
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(filter))

	var p interface{}
	if pixels != nil {
		p = pixels
	}
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))

	return Texture(t), nil
}
Esempio n. 22
0
func my_print(x, y float64, text string, ftex uint32, cdata []*truetype.BakedChar) {
	gl.Enable(gl.TEXTURE_2D)
	gl.BindTexture(gl.TEXTURE_2D, ftex)
	gl.Begin(gl.QUADS)
	for _, b := range text {
		if int(b) >= 32 && int(b) < 128 {
			var q *truetype.AlignedQuad
			x, q = truetype.GetBakedQuad(cdata, 512, 512, int(b)-32, x, y, true)
			gl.TexCoord2f(q.S0, q.T0)
			gl.Vertex2f(q.X0, q.Y0)
			gl.TexCoord2f(q.S1, q.T0)
			gl.Vertex2f(q.X1, q.Y0)
			gl.TexCoord2f(q.S1, q.T1)
			gl.Vertex2f(q.X1, q.Y1)
			gl.TexCoord2f(q.S0, q.T1)
			gl.Vertex2f(q.X0, q.Y1)
		}
	}
	gl.End()
}
Esempio n. 23
0
func Render() {
	gl.ClearColor(0, 0, 0, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.Color4f(1, 1, 1, 1)
	gl.BindTexture(gl.TEXTURE_2D, game.Texture)

	gl.PushMatrix()
	gl.Translatef(20-float32(game.PlayerX), 15-float32(game.PlayerY), 0)
	gl.Begin(gl.TRIANGLES)
	for i := 0; i < game.Width; i++ {
		for j := 0; j < game.Height; j++ {
			renderTile(game.Tiles[(j*game.Width)+i], float32(i), float32(j))
		}
	}
	// Player
	gl.Color4f(1, 0, 0, 1)
	renderTile(4, float32(game.PlayerX), float32(game.PlayerY))
	gl.End()
	gl.PopMatrix()
}
Esempio n. 24
0
func List(width, height int, list *draw.List) {
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	gl.Enable(gl.SCISSOR_TEST)
	defer gl.Disable(gl.SCISSOR_TEST)

	gl.EnableClientState(gl.VERTEX_ARRAY)
	defer gl.DisableClientState(gl.VERTEX_ARRAY)

	gl.EnableClientState(gl.COLOR_ARRAY)
	defer gl.DisableClientState(gl.COLOR_ARRAY)

	gl.EnableClientState(gl.TEXTURE_COORD_ARRAY)
	defer gl.DisableClientState(gl.TEXTURE_COORD_ARRAY)

	gl.VertexPointer(2, gl.FLOAT, vertexStride, unsafe.Pointer(&(list.Vertices[0].P)))
	gl.TexCoordPointer(2, gl.FLOAT, vertexStride, unsafe.Pointer(&(list.Vertices[0].UV)))
	gl.ColorPointer(4, gl.UNSIGNED_BYTE, vertexStride, unsafe.Pointer(&(list.Vertices[0].Color)))

	offset := 0
	for _, cmd := range list.Commands {
		if cmd.Count == 0 {
			continue
		}
		if cmd.Texture == 0 {
			gl.Disable(gl.TEXTURE_2D)
		} else {
			gl.Enable(gl.TEXTURE_2D)
			gl.BindTexture(gl.TEXTURE_2D, uint32(cmd.Texture))
		}

		x, y, w, h := cmd.Clip.AsInt32()
		gl.Scissor(x, int32(height)-y-h, w, h)
		gl.DrawElements(gl.TRIANGLES, int32(cmd.Count), indexType, gl.Ptr(list.Indicies[offset:]))
		offset += int(cmd.Count)
	}
}
Esempio n. 25
0
func (atlas *FontAtlas) upload() {
	if !atlas.Dirty {
		return
	}
	atlas.Dirty = false

	gl.Enable(gl.TEXTURE_2D)

	if atlas.Texture != 0 {
		gl.DeleteTextures(1, &atlas.Texture)
		atlas.Texture = 0
	}

	gl.GenTextures(1, &atlas.Texture)
	gl.BindTexture(gl.TEXTURE_2D, atlas.Texture)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)

	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(atlas.Image.Rect.Size().X),
		int32(atlas.Image.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(atlas.Image.Pix))

	if err := gl.GetError(); err != 0 {
		log.Println(err)
	}

	gl.Disable(gl.TEXTURE_2D)
}
Esempio n. 26
0
func newTexture(file string) uint32 {
	imgFile, err := os.Open(file)
	if err != nil {
		panic(err)
	}
	img, _, err := image.Decode(imgFile)
	if err != nil {
		panic(err)
	}

	rgba := image.NewRGBA(img.Bounds())
	if rgba.Stride != rgba.Rect.Size().X*4 {
		panic("unsupported stride")
	}
	draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)

	var texture uint32
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(rgba.Rect.Size().X),
		int32(rgba.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rgba.Pix))

	return texture
}
Esempio n. 27
0
func (o *openGLCanvas) Draw(tile image.Rectangle, where image.Rectangle, f tmx.FlipMode, tileset string) {
	if _, ok := o.sets[tileset]; !ok {
		o.sets[tileset] = newChipset(tileset, tile.Max.X-tile.Min.X)
	}

	c := o.sets[tileset]
	gl.BindTexture(gl.TEXTURE_2D, c.handle)

	// Texture coords
	fts := float32(c.tilesize)
	tileWidthPixels := fts / float32(c.width)
	tileHeightPixels := fts / float32(c.height)
	startX := (float32(tile.Min.X) / fts) * tileWidthPixels
	startY := (float32(tile.Min.Y) / fts) * tileHeightPixels
	endX := startX + tileWidthPixels
	endY := startY + tileHeightPixels

	// Draw coords
	drawX := float32(where.Min.X)
	drawY := float32(where.Min.Y)

	gl.Begin(gl.QUADS)
	{
		gl.TexCoord2f(startX, startY)
		gl.Vertex3f(drawX*o.scaleX, drawY*o.scaleY, 0)

		gl.TexCoord2f(startX, endY)
		gl.Vertex3f(drawX*o.scaleX, (drawY+fts)*o.scaleY, 0)

		gl.TexCoord2f(endX, endY)
		gl.Vertex3f((drawX+fts)*o.scaleX, (drawY+fts)*o.scaleY, 0)

		gl.TexCoord2f(endX, startY)
		gl.Vertex3f((drawX+fts)*o.scaleX, (drawY)*o.scaleY, 0)
	}
	gl.End()
}
Esempio n. 28
0
func New(cachew, cacheh int) *Stash {
	stash := &Stash{}

	// Create data for clearing the textures
	stash.emptyData = make([]byte, cachew*cacheh)

	// Create first texture for the cache
	stash.tw = cachew
	stash.th = cacheh
	stash.itw = 1 / float64(cachew)
	stash.ith = 1 / float64(cacheh)
	gl.Enable(gl.TEXTURE_2D)
	stash.ttTextures = make([]*Texture, 1)
	stash.ttTextures[0] = &Texture{}
	gl.GenTextures(1, &stash.ttTextures[0].id)
	gl.BindTexture(gl.TEXTURE_2D, stash.ttTextures[0].id)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, int32(cachew), int32(cacheh),
		0, gl.ALPHA, gl.UNSIGNED_BYTE, unsafe.Pointer(&stash.emptyData[0]))
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.Disable(gl.TEXTURE_2D)

	return stash
}
Esempio n. 29
0
// BindTexture binds a texture.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glBindTexture.xhtml
func BindTexture(target Enum, t Texture) {
	gl.BindTexture(uint32(target), t.Value)
}
Esempio n. 30
0
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translatef(0, 0, -3.0)
	gl.Rotatef(rotationX, 1, 0, 0)
	gl.Rotatef(rotationY, 0, 1, 0)

	rotationX += 0.5
	rotationY += 0.5

	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.Color4f(1, 1, 1, 1)

	gl.Begin(gl.QUADS)

	gl.Normal3f(0, 0, 1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, -1, 1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, -1, 1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, 1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, 1)

	gl.Normal3f(0, 0, -1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, -1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, 1, -1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, 1, -1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, -1)

	gl.Normal3f(0, 1, 0)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, -1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, 1, 1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, 1, 1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, -1)

	gl.Normal3f(0, -1, 0)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, -1, -1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, -1, -1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, 1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, 1)

	gl.Normal3f(1, 0, 0)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, -1, -1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, -1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, 1, 1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, 1)

	gl.Normal3f(-1, 0, 0)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, -1, -1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, 1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, 1, 1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, -1)

	gl.End()
}