Пример #1
0
// Initialize texture storage. _REQUIRED_ before using it as a framebuffer target.
func (t *Texture) Init() {
	With(t, func() {
		// generate base level storage
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, t.W, t.H, 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
		// generate required number of mipmaps given texture dimensions
		gl.GenerateMipmap(gl.TEXTURE_2D)
	})
}
func (self *OpenGLRenderer) LoadTexture(texture *render.Texture) gl.Texture {
	glTexture := gl.GenTexture()
	glTexture.Bind(gl.TEXTURE_2D)
	defer glTexture.Unbind(gl.TEXTURE_2D)

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

	self.glTexImage2D(gl.TEXTURE_2D, texture)

	gl.GenerateMipmap(gl.TEXTURE_2D)

	return glTexture
}
Пример #3
0
func MakeTextureFromTGA(fname string) gl.Texture {
	tex := gl.GenTexture()

	tex.Bind(gl.TEXTURE_2D)
	glfw.LoadTexture2D(fname, 0)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
	gl.GenerateMipmap(gl.TEXTURE_2D)

	//	glh.OpenGLSentinel() // check for errors

	return tex
}
Пример #4
0
// bind the given atlas to the current GL context
//
// the current implementation is very stupid, since it will
// upload the texture every single call.
//
// later, improve this to upload only if there is a real need for it
func (a *Atlas) bind() error {
	// discard any possible error
	if err := checkGlError(); err != nil {
		return err
	}
	if gl.Object(a.gltex).IsTexture() {
		a.gltex = gl.GenTexture()
	}
	a.gltex.Bind(gl.TEXTURE_2D)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, a.data.Bounds().Dx(), a.data.Bounds().Dy(), 0, gl.RGBA, gl.UNSIGNED_BYTE, a.data.Pix)
	if err := checkGlError(gl.OUT_OF_MEMORY, gl.INVALID_OPERATION); err != nil {
		return err
	}

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
	gl.GenerateMipmap(gl.TEXTURE_2D)
	panicGlError()
	return nil
}
Пример #5
0
func LoadSkybox() {
	gl.ActiveTexture(gl.TEXTURE0)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	t := gl.GenTexture()
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	t.Bind(gl.TEXTURE_CUBE_MAP)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	const filename = "Skybox_tut13_384x256.png"
	img, err := LoadImage(filename)
	if err != nil {
		panic(err)
	}

	size := img.Bounds().Size()
	w := size.X
	h := size.Y
	if w/3 != h/2 {
		panic("incorrect aspect ratio")
	}
	s := h / 2 // Size of one face of the cube.

	targets := [...]gl.GLenum{
		gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
		gl.TEXTURE_CUBE_MAP_POSITIVE_X,
		gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
		gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
		gl.TEXTURE_CUBE_MAP_NEGATIVE_Z,
		gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
	}
	rs := []image.Rectangle{
		image.Rect(0*s, 0*s, 1*s, 1*s), // 0 side.
		image.Rect(1*s, 0*s, 2*s, 1*s), // 1 side.
		image.Rect(2*s, 0*s, 3*s, 1*s), // 2 side.
		image.Rect(0*s, 1*s, 1*s, 2*s), // 3 bottom.
		image.Rect(1*s, 1*s, 2*s, 2*s), // 4 top.
		image.Rect(2*s, 1*s, 3*s, 2*s), // 5 side.
	}
	// redirect must end with 3 and 4.  This places the floor at -z and the
	// sky at +z.
	// All the sides look properly oriented (grass down) on the +y side.
	// Considering that the sky is properly oriented the way it is, then
	// rectangle 5 contains the picture that matches it on the +y side.
	// Therefore, redirect must end with 5, 3, 4.
	// Problem now: all the other sides, and maybe the floor too, need to be
	// rotated in order to line up, that is having their sky on top.
	redirect := [...]int{0, 2, 1, 5, 3, 4}

	ID := matrix23{1, 0, 0, 1, 0, 0}
	ms := [...]matrix23{
		matrix23{0, -1, 1, 0, 0, s - 1},
		matrix23{0, 1, -1, 0, s - 1, 0},
		matrix23{-1, 0, 0, -1, s - 1, s - 1},
		ID,
		matrix23{-1, 0, 0, -1, s - 1, s - 1},
		ID,
	}

	rgba := forceRGBA(img)
	for i, target := range targets {
		subimage := rgba.SubImage(rs[redirect[i]])
		data := GlRgba(subimage, ms[i])
		gl.TexImage2D(
			target,
			0,                // Mipmap level.
			gl.SRGB8_ALPHA8,  // Format inside OpenGL.
			s,                // Width.
			s,                // Height.
			0,                // Border.  Doc says it must be 0.
			gl.RGBA,          // Format of the data that...
			gl.UNSIGNED_BYTE, // ... I give to OpenGL.
			data,             // And the data itself.
		)
		if err := CheckGlError(); err != nil {
			panic(err)
		}
	}

	const target = gl.TEXTURE_CUBE_MAP
	gl.GenerateMipmap(target)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	gl.TexParameteri(target, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
	if err := CheckGlError(); err != nil {
		panic(err)
	}
	gl.TexParameteri(target, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	if err := CheckGlError(); err != nil {
		panic(err)
	}
	gl.TexParameteri(target, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
	if err := CheckGlError(); err != nil {
		panic(err)
	}
	gl.TexParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	if err := CheckGlError(); err != nil {
		panic(err)
	}
	gl.TexParameteri(target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	t.Unbind(target)

	globaltexture = t
}
Пример #6
0
func LoadTexture() {
	gl.ActiveTexture(gl.TEXTURE0)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	t := gl.GenTexture()
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	const target = gl.TEXTURE_2D

	t.Bind(target)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	img, err := LoadImage("test.jpg")
	if err != nil {
		panic(err)
	}

	data := GlRgba(img, matrix23{1, 0, 0, 1, 0, 0})
	size := img.Bounds().Size()

	gl.TexImage2D(
		target,
		0,                // Mipmap level.
		gl.SRGB8_ALPHA8,  // Format inside OpenGL.
		size.X,           // Width.
		size.Y,           // Height.
		0,                // Border.  Doc says it must be 0.
		gl.RGBA,          // Format of the data that...
		gl.UNSIGNED_BYTE, // ... I give to OpenGL.
		data,             // And the data itself.
	)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	gl.GenerateMipmap(target)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	gl.TexParameteri(target, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
	if err := CheckGlError(); err != nil {
		panic(err)
	}
	gl.TexParameteri(target, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	if err := CheckGlError(); err != nil {
		panic(err)
	}
	gl.TexParameteri(target, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
	if err := CheckGlError(); err != nil {
		panic(err)
	}
	gl.TexParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	if err := CheckGlError(); err != nil {
		panic(err)
	}

	t.Unbind(target)

	globaltexture = t
}
Пример #7
0
func (t *Texture) BuildMipmaps() {
	t.Bind()
	gl.GenerateMipmap(t.target)
}