Exemplo n.º 1
0
func NewGLRenderer(view *View, title string) *GLRenderer {
	r := new(GLRenderer)
	r.View = view
	GL <- func() {
		if sdl.Init(sdl.INIT_VIDEO) < 0 {
			panic(sdl.GetError())
		}
		sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
		sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1)
		r.Screen = sdl.SetVideoMode(view.Width, view.Height, 32, sdl.OPENGL)
		if r.Screen == nil {
			panic(sdl.GetError())
		}
		sdl.WM_SetCaption(title, title)
		err := gl33.Init()
		if err != nil {
			panic(err)
		}
		gl33.Enable(gl33.BLEND)
		gl33.Enable(gl33.CULL_FACE)
		gl33.Enable(gl33.DEPTH_TEST)
		gl33.ClearColor(1.0, 1.0, 1.0, 1.0)
		gl33.BlendFunc(gl33.SRC_ALPHA, gl33.ONE_MINUS_SRC_ALPHA)
		gl33.DepthFunc(gl33.LEQUAL)
		gl33.Viewport(0, 0, gl33.Sizei(view.Width), gl33.Sizei(view.Height))
	}
	r.PerspectiveMatrix = s3dm.PerspectiveMatrix(view.Fovy, view.Aspect, view.Near, view.Far)
	r.OrthographicMatrix = s3dm.OrthographicMatrix(float64(view.Width), float64(view.Height), 0, 1)
	return r
}
Exemplo n.º 2
0
func LoadTextureCubeMap(filenames *[6]string, minFilter int, magFilter int) *Texture {
	ch := make(chan *Texture)
	GL <- func() {
		tex := NewTextureCubeMap()
		tex.SetFilters(minFilter, magFilter)
		tex.Bind(0)
		var surfaces [6]*sdl.Surface
		for i, filename := range filenames {
			surfaces[i] = sdl.Load(filename)
			if surfaces[i] == nil {
				panic(sdl.GetError())
			}
			defer surfaces[i].Free()
		}
		tex.Width = int(surfaces[0].W)
		tex.Height = int(surfaces[0].H)
		uploadSurface(gl33.TEXTURE_CUBE_MAP_POSITIVE_X, surfaces[0])
		uploadSurface(gl33.TEXTURE_CUBE_MAP_NEGATIVE_X, surfaces[1])
		uploadSurface(gl33.TEXTURE_CUBE_MAP_POSITIVE_Y, surfaces[2])
		uploadSurface(gl33.TEXTURE_CUBE_MAP_NEGATIVE_Y, surfaces[3])
		uploadSurface(gl33.TEXTURE_CUBE_MAP_POSITIVE_Z, surfaces[4])
		uploadSurface(gl33.TEXTURE_CUBE_MAP_NEGATIVE_Z, surfaces[5])
		gl33.GenerateMipmap(gl33.TEXTURE_CUBE_MAP)
		ch <- tex
	}
	return <-ch
}
Exemplo n.º 3
0
func LoadTexture2D(filename string, minFilter int, magFilter int) *Texture {
	ch := make(chan *Texture)
	GL <- func() {
		tex := NewTexture2D()
		tex.SetFilters(minFilter, magFilter)
		tex.Bind(0)
		surface := sdl.Load(filename)
		if surface == nil {
			panic(sdl.GetError())
		}
		defer surface.Free()
		tex.Width = int(surface.W)
		tex.Height = int(surface.H)
		uploadSurface(gl33.TEXTURE_2D, surface)
		gl33.GenerateMipmap(gl33.TEXTURE_2D)
		ch <- tex
	}
	return <-ch
}
Exemplo n.º 4
0
func LoadTextureArray(filenames []string, minFilter int, magFilter int) *Texture {
	ch := make(chan *Texture)
	GL <- func() {
		tex := NewTextureArray()
		tex.SetFilters(minFilter, magFilter)
		tex.Bind(0)
		surfaces := make([]*sdl.Surface, len(filenames))
		for i, filename := range filenames {
			surfaces[i] = sdl.Load(filename)
			if surfaces[i] == nil {
				panic(sdl.GetError())
			}
			defer surfaces[i].Free()
		}
		tex.Width = int(surfaces[0].W)
		tex.Height = int(surfaces[0].H)
		var internalFormat gl33.Int
		var format gl33.Enum
		var size int
		if surfaces[0].Format.BitsPerPixel == 32 {
			internalFormat = gl33.RGBA8
			format = gl33.RGBA
			size = 4
		} else {
			internalFormat = gl33.RGB8
			format = gl33.RGB
			size = 3
		}
		pixels := make([]byte, tex.Width*tex.Height*len(surfaces)*size)
		for i, surface := range surfaces {
			p := uintptr(surface.Pixels)
			for j := 0; j < tex.Width*tex.Height*size; j++ {
				pixels[i*tex.Width*tex.Height*size+j] = *(*byte)(unsafe.Pointer(p + uintptr(j)))
			}
		}
		gl33.TexImage3D(gl33.TEXTURE_2D_ARRAY, 0, internalFormat, gl33.Sizei(tex.Width), gl33.Sizei(tex.Height), gl33.Sizei(len(surfaces)), 0, format, gl33.UNSIGNED_BYTE, gl33.Pointer(&pixels[0]))
		gl33.GenerateMipmap(gl33.TEXTURE_2D_ARRAY)
		ch <- tex
	}
	return <-ch
}