예제 #1
0
파일: font.go 프로젝트: sonald/gotris
func uploadTexture_NRGBA32(img *image.NRGBA) gl.Texture {
	b := img.Bounds()
	data := make([]uint8, b.Max.X*b.Max.Y*4)
	for y := 0; y < b.Max.Y; y++ {
		for x := 0; x < b.Max.X; x++ {
			p := &img.Pix[y*img.Stride+x]
			offset := y*b.Max.X*4 + x*4
			data[offset+0] = p.R
			data[offset+1] = p.G
			data[offset+2] = p.B
			data[offset+3] = p.A
		}
	}

	id := gl.GenTexture()
	id.Bind(gl.TEXTURE_2D)
	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.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, b.Max.X, b.Max.Y, 0, gl.RGBA, data)

	if gl.GetError() != gl.NO_ERROR {
		id.Delete()
		panic(os.NewError("Failed to load a texture"))
		return 0
	}
	return id
}
// Load bitmap from path as GL texture
func LoadGLTexture(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")
	}

	texture = gl.GenTexture()

	// Typical texture generation using data from the bitmap
	gl.BindTexture(gl.TEXTURE_2D, uint(texture))

	// Generate the texture
	gl.TexImage2D(gl.TEXTURE_2D, 0, int(image.Format.BytesPerPixel),
		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.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	// free up memory we have used.
	image.Free()
}
예제 #3
0
파일: draw_font.go 프로젝트: jcd/go-gamelib
func (f *Font) setupTextRendering(color [3]byte, txt string) (gl.GLuint, *sdl.Surface, *sdl.Surface, *sdl.Surface, int, int) {

	//
	var texture gl.GLuint

	/* Use SDL_TTF to render our text */
	var col sdl.Color
	col.R = color[0]
	col.G = color[1]
	col.B = color[2]

	// get surface with text
	initial := ttf.RenderText_Blended(f.font, txt, col)

	/* Convert the rendered text to a known format */
	w := next_p2(int(initial.W))
	h := next_p2(int(initial.H))

	intermediarya := sdl.CreateRGBSurface(0, w, h, 32,
		0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000)

	var rr *sdl.Rect = nil
	intermediarya.Blit(rr, initial, rr)

	intermediary := intermediarya.DisplayFormatAlpha()

	/* Tell GL about our new texture */
	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexImage2D(gl.TEXTURE_2D, 0, 4, gl.GLsizei(w), gl.GLsizei(h), 0, gl.BGRA,
		gl.UNSIGNED_BYTE, unsafe.Pointer(intermediary.Pixels))

	gl.TexEnvi(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)

	/* GL_NEAREST looks horrible, if scaled... */
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	/* prepare to render our texture */
	gl.Enable(gl.TEXTURE_2D)

	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.Color4f(1.0, 1.0, 1.0, 1.0)

	gl.Enable(gl.BLEND)
	//	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_COLOR) // TODO : MAke real alpha work!

	return texture, initial, intermediarya, intermediary, w, h
}
예제 #4
0
파일: ogl_graphics.go 프로젝트: chsc/g3
func (gd *openGLGraphicsDevice) NewTexture2D(img image.Image) Texture2D {
	rect := img.Bounds()
	width, height := rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y
	id := gl.GenTexture()
	id.Bind(gl.TEXTURE_2D)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	switch i := img.(type) {
	case *image.RGBA:
		gl.TexImage2D(gl.TEXTURE_2D, 0, 4, width, height, 0, gl.RGBA, &i.Pix[0].R)
		//fmt.Println("teximg2d", i.Pix[0:4], i.Stride, width, height, gl.GetError(), id)
	default:
		panic("unknown format")
	}
	return &openGLTexture2D{id}
}
예제 #5
0
func uploadTexture_RGBA32(w, h int, data []byte) gl.Texture {

	id := gl.GenTexture()
	id.Bind(gl.TEXTURE_2D)
	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.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int(w), int(h), 0, gl.RGBA, data)

	if gl.GetError() != gl.NO_ERROR {
		id.Delete()
		panic("Failed to load a texture")
		return 0
	}
	return id
}
예제 #6
0
func uploadTexture_RGBA32(w, h int, data []byte) gl.GLuint {
	var id gl.GLuint

	gl.GenTextures(1, &id)
	gl.BindTexture(gl.TEXTURE_2D, id)
	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.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.GLsizei(w), gl.GLsizei(h), 0, gl.RGBA,
		gl.UNSIGNED_BYTE, unsafe.Pointer(&data[0]))

	if gl.GetError() != gl.NO_ERROR {
		gl.DeleteTextures(1, &id)
		panic("Failed to load a texture")
		return 0
	}
	return id
}
예제 #7
0
func loadSurface(img image.Image) gl.Texture {
	w := img.Bounds().Dx()
	h := img.Bounds().Dy()
	rgba := image.NewRGBA(w, h)
	for x := 0; x < w; x++ {
		for y := 0; y < h; y++ {
			rgba.Set(x, y, img.At(x, y))
		}
	}
	gl.ActiveTexture(gl.TEXTURE0)
	texture := gl.GenTexture()
	texture.Bind(gl.TEXTURE_2D)

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

	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, rgba.Rect.Dx(), rgba.Rect.Dy(), 0, gl.RGBA, gl.UNSIGNED_BYTE, rgba.Pix)

	texture.Unbind(gl.TEXTURE_2D)
	return texture
}
예제 #8
0
// 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,
  )
}
func genTexture(into gl.Texture, from *sdl.Surface, format gl.GLenum) {
	gl.BindTexture(gl.TEXTURE_2D, uint(into))
	gl.TexImage2D(gl.TEXTURE_2D, 0, 3, int(from.W), int(from.H),
		0, format, gl.UNSIGNED_BYTE, from.Pixels,
	)
}