Beispiel #1
0
func loadTextures() (err error) {
	gl.GenTextures(textures)

	// Texture 1
	textures[0].Bind(gl.TEXTURE_2D)

	if !glfw.LoadTexture2D(texturefile, 0) {
		return errors.New("Failed to load texture: " + texturefile)
	}

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

	// Texture 2
	textures[1].Bind(gl.TEXTURE_2D)

	if !glfw.LoadTexture2D(texturefile, 0) {
		return errors.New("Failed to load texture: " + texturefile)
	}

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

	// Texture 3
	textures[2].Bind(gl.TEXTURE_2D)

	if !glfw.LoadTexture2D(texturefile, glfw.BuildMipmapsBit) {
		return errors.New("Failed to load texture: " + texturefile)
	}

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

	return
}
Beispiel #2
0
func loadTextures() (err error) {
	textures = make([]gl.Texture, len(texturefiles))
	gl.GenTextures(textures)

	for i := range texturefiles {
		textures[i].Bind(gl.TEXTURE_2D)

		if glfw.LoadTexture2D(texturefiles[i], 0) {
			return errors.New("Failed to load texture: " + texturefiles[i])
		}

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

	return
}
// load in bitmap as a GL texture
func LoadGLTextures(path string) {
	// storage space for the textures
	image, format := LoadImage(path)

	// Create the textures
	gl.GenTextures(textures[:])

	genTexture(textures[0], image, format)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

	genTexture(textures[1], image, format)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	genTexture(textures[2], image, format)
	gl.TexParameteri(gl.TEXTURE_2D, gl.GENERATE_MIPMAP, gl.TRUE)
}
// load in bitmap as a GL texture
func LoadGLTextures(path string) {
	image := sdl.Load(path)
	if image == nil {
		panic(sdl.GetError())
	}

	// Check that the image's width is a power of 2
	if image.W&(image.W-1) != 0 {
		fmt.Println("warning:", path, "has a width that is not a power of 2")
	}

	// Also check if the height is a power of 2
	if image.H&(image.H-1) != 0 {
		fmt.Println("warning:", path, "has an height that is not a power of 2")
	}

	// get the number of channels in the SDL surface
	nOfColors := image.Format.BytesPerPixel
	var textureFormat gl.GLenum

	if nOfColors == 4 { // contains alpha channel
		if image.Format.Rmask == 0x000000ff {
			textureFormat = gl.RGBA
		} else {
			textureFormat = gl.BGRA
		}
	} else if nOfColors == 3 { // no alpha channel
		if image.Format.Rmask == 0x000000ff {
			textureFormat = gl.RGB
		} else {
			textureFormat = gl.BGR
		}
	} else {
		fmt.Println("warning:", path, "is not truecolor, this will probably break")
	}

	// Create the textures
	gl.GenTextures(textures[:])

	// First texture
	gl.BindTexture(gl.TEXTURE_2D, uint(textures[0]))
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		3,
		int(image.W),
		int(image.H),
		0,
		textureFormat,
		gl.UNSIGNED_BYTE,
		image.Pixels,
	)

	// linear filtering
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

	// Second texture
	gl.BindTexture(gl.TEXTURE_2D, uint(textures[1]))
	gl.TexImage2D(gl.TEXTURE_2D, 0,
		3,
		int(image.W),
		int(image.H),
		0, textureFormat, gl.UNSIGNED_BYTE,
		image.Pixels,
	)

	// Mipmapped filtering
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	// Third texture
	gl.BindTexture(gl.TEXTURE_2D, uint(textures[2]))
	gl.TexImage2D(gl.TEXTURE_2D, 0,
		3,
		int(image.W),
		int(image.H),
		0, textureFormat, gl.UNSIGNED_BYTE,
		image.Pixels,
	)

	// Mipmapped filtering
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	glu.Build2DMipmaps(
		gl.TEXTURE_2D,
		3,
		int(image.W),
		int(image.H),
		textureFormat,
		image.Pixels,
	)
}