Ejemplo n.º 1
0
func (img image) Draw() {

	if img.data == nil {
		return
	}

	// gl.ActiveTexture(texId2)

	gl.BindTexture(gl.TEXTURE_2D, img.textureId)

}
Ejemplo n.º 2
0
func NewImage(path string) (retImg image, err error) {

	imgFile, err := os.Open(path)
	if err != nil {
		return image{}, err
	}
	img, _, err := Image.Decode(imgFile)
	if err != nil {
		return image{}, err
	}

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

	var texture uint32

	gl.GenTextures(1, &texture)
	// gl.ActiveTexture(gl.TEXTURE1)
	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.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, 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))

	if gl.GetError() != gl.NO_ERROR {

		return image{}, fmt.Errorf("Failed to load texture: " + path)
	}

	return image{data: &img, textureId: texture, width: rgba.Rect.Size().X, height: rgba.Rect.Size().Y}, nil
}
Ejemplo n.º 3
0
func newOctree(file string) (uint32, []uint32, error) {
	var (
		header  pack.OctreeHeader
		texture uint32
		data    []uint32
	)

	fp, err := os.Open(file)
	if err != nil {
		return 0, nil, err
	}
	defer fp.Close()

	if err := pack.DecodeHeader(fp, &header); err != nil {
		return 0, nil, err
	}

	if header.Format != pack.MipR8G8B8A8UnpackUI32 {
		return 0, nil, errors.New("invalid octree format")
	}
	nodeSize := uint64(1 + 8)

	var maxSize int32
	gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &maxSize)

	numInts := header.NumNodes * nodeSize
	if maxSize*maxSize < int32(numInts) {
		panic("octree does not fit on GPU")
	}

	var (
		height int32
		width  = maxSize
	)

	for height < width {
		maxSize = width
		height = int32(numInts/uint64(width)) + 1
		width /= 2
	}

	fmt.Printf("Octree is loaded in an %vx%v, R32UI, 2D texture.\n", maxSize, height)

	textureSize := maxSize * height
	data = make([]uint32, textureSize)

	for i := uint64(0); i < header.NumNodes; i++ {
		start := i * nodeSize
		if err := binary.Read(fp, binary.BigEndian, data[start:start+nodeSize]); err != nil {
			return 0, nil, err
		}

		// Recalculate alpha value.
		data[start] &= 0xffffff00
		for j := uint64(1); j < 9; j++ {
			if data[start+j] > 0 {
				data[start] |= uint32(0x100) >> j
			}
		}
	}

	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.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.R32UI,
		maxSize,
		height,
		0,
		gl.RED_INTEGER,
		gl.UNSIGNED_INT,
		gl.Ptr(data))

	if glErr := gl.GetError(); glErr != gl.NO_ERROR {
		err = fmt.Errorf("GL error, loading octree: %x", glErr)
	}

	return texture, data, err
}
Ejemplo n.º 4
0
// BindTexture implements the opengl.OpenGl interface.
func (native *OpenGl) BindTexture(target uint32, texture uint32) {
	gl.BindTexture(target, texture)
}
Ejemplo n.º 5
0
func (s *BaseScene) Draw() {

	if len(s.entities) == 0 {
		fmt.Println("No Sprites to draw.")
		return
	}

	idx := uint32(0)

	vertexInfo := &Opengl.OpenGLVertexInfo{}

	// clone := &Opengl.OpenGLVertexInfo{}
	for _, v := range s.spriteDraw {

		//TODO: move this to sprite class and use interface instead.. what if i have "shape"
		// fmt.Printf("Img name: %s\n", k)
		// glInfo := v.getGLVertexInfo()
		w, h := v.GetImageSection().GetDimensions()

		for i := 0; i < 4; i++ {
			vertexInfo.Translations = append(vertexInfo.Translations, float32(v.x), float32(v.y), 0)
			vertexInfo.Rotations = append(vertexInfo.Rotations, 0, 0, 1, v.rot)
			vertexInfo.Scales = append(vertexInfo.Scales, v.xS, v.yS, 0)
			vertexInfo.Colors = append(vertexInfo.Colors, v.r, v.g, v.b, v.a)

		}

		vertexInfo.Vertices = append(vertexInfo.Vertices, -0.5*w, 0.5*h, 1.0, 0.5*w, 0.5*h, 1.0, 0.5*w, -0.5*h, 1.0, -0.5*w, -0.5*h, 1.0)

		// TODO: maybe put this inside struct to store since this will really never change
		x, y := s.spriteSheet.GetUVFromPosition(v.GetImageSection().BottomLeft())
		vertexInfo.Uvs = append(vertexInfo.Uvs, x, y)
		x, y = s.spriteSheet.GetUVFromPosition(v.GetImageSection().BottomRight())
		vertexInfo.Uvs = append(vertexInfo.Uvs, x, y)
		x, y = s.spriteSheet.GetUVFromPosition(v.GetImageSection().TopRight())
		vertexInfo.Uvs = append(vertexInfo.Uvs, x, y)
		x, y = s.spriteSheet.GetUVFromPosition(v.GetImageSection().TopLeft())
		vertexInfo.Uvs = append(vertexInfo.Uvs, x, y)

		vertexInfo.Elements = append(vertexInfo.Elements, uint32(idx*4), uint32(idx*4+1), uint32(idx*4+2), uint32(idx*4), uint32(idx*4+2), uint32(idx*4+3))

		// glInfo.AdjustElements(idx)

		// idx += 4
		// clone.Append(glInfo)
		idx = idx + 1
	}

	// vertexInfo.Print()
	Opengl.BindBuffers(vertexInfo)

	gl.BindTexture(gl.TEXTURE_2D, s.spriteSheet.textureId)

	Opengl.Draw(vertexInfo)

	Opengl.Cleanup()

	// calc fps
	if (int32(time.Now().Unix()) - s.timestart) >= 1 {
		fmt.Printf("FPS: %d \n", s.fps)
		s.fps = 0
		s.timestart = int32(time.Now().Unix())

	}

	s.fps = s.fps + 1

}