Exemple #1
0
func (c *cloudState) init() {
	program := gl.CreateProgram()

	v := gl.CreateShader(gl.VertexShader)
	v.Source(glsl.Get("cloud_vertex"))
	v.Compile()

	if v.Parameter(gl.CompileStatus) == 0 {
		panic(v.InfoLog())
	} else {
		log := v.InfoLog()
		if len(log) > 0 {
			fmt.Println(log)
		}
	}

	g := gl.CreateShader(gl.GeometryShader)
	g.Source(glsl.Get("cloud_geo"))
	g.Compile()

	if g.Parameter(gl.CompileStatus) == 0 {
		panic(g.InfoLog())
	} else {
		log := g.InfoLog()
		if len(log) > 0 {
			fmt.Println(log)
		}
	}

	f := gl.CreateShader(gl.FragmentShader)
	f.Source(glsl.Get("cloud_fragment"))
	f.Compile()

	if f.Parameter(gl.CompileStatus) == 0 {
		panic(f.InfoLog())
	} else {
		log := f.InfoLog()
		if len(log) > 0 {
			fmt.Println(log)
		}
	}

	program.AttachShader(v)
	program.AttachShader(g)
	program.AttachShader(f)
	program.Link()
	program.Use()

	c.program = program

	InitStruct(&c.shader, c.program)

	c.array = gl.CreateVertexArray()
	c.array.Bind()
	c.buffer = gl.CreateBuffer()
	c.buffer.Bind(gl.ArrayBuffer)
	c.shader.Position.Enable()
	c.shader.Position.Pointer(3, gl.Float, false, 12, 0)

	data := builder.New(builder.Float, builder.Float, builder.Float)
	for x := -160; x < 160; x++ {
		for y := -160; y < 160; y++ {
			data.Float(float32(x))
			data.Float(128)
			data.Float(float32(y))
			c.numPoints++
		}
	}

	c.buffer.Data(data.Data(), gl.StaticDraw)

	c.texture = gl.CreateTexture()
	c.texture.Bind(gl.Texture2D)
	c.texture.Image2D(0, 512, 512, gl.Red, gl.UnsignedByte, c.data)
	c.texture.Parameter(gl.TextureMinFilter, gl.Nearest)
	c.texture.Parameter(gl.TextureMagFilter, gl.Nearest)
}
Exemple #2
0
func initTrans() {
	if transCreated {
		accum.Delete()
		revealage.Delete()
		transFramebuffer.Delete()
		fbColor.Delete()
		fbDepth.Delete()
		mainFramebuffer.Delete()
	}
	transCreated = true
	transFramebuffer = gl.NewFramebuffer()
	transFramebuffer.Bind()

	accum = gl.CreateTexture()
	accum.Bind(gl.Texture2D)
	accum.Image2DEx(0, lastWidth, lastHeight, gl.RGBA16F, gl.RGBA, gl.Float, nil)
	accum.Parameter(gl.TextureMinFilter, gl.Linear)
	accum.Parameter(gl.TextureMagFilter, gl.Linear)
	transFramebuffer.Texture2D(gl.ColorAttachment0, gl.Texture2D, accum, 0)

	revealage = gl.CreateTexture()
	revealage.Bind(gl.Texture2D)
	revealage.Image2DEx(0, lastWidth, lastHeight, gl.R16F, gl.Red, gl.Float, nil)
	revealage.Parameter(gl.TextureMinFilter, gl.Linear)
	revealage.Parameter(gl.TextureMagFilter, gl.Linear)
	transFramebuffer.Texture2D(gl.ColorAttachment1, gl.Texture2D, revealage, 0)

	transDepth = gl.CreateTexture()
	transDepth.Bind(gl.Texture2D)
	transDepth.Image2DEx(0, lastWidth, lastHeight, gl.DepthComponent24, gl.DepthComponent, gl.UnsignedByte, nil)
	transDepth.Parameter(gl.TextureMinFilter, gl.Linear)
	transDepth.Parameter(gl.TextureMagFilter, gl.Linear)
	transFramebuffer.Texture2D(gl.DepthAttachment, gl.Texture2D, transDepth, 0)

	chunkProgramT.Use()
	gl.BindFragDataLocation(chunkProgramT, 0, "accum")
	gl.BindFragDataLocation(chunkProgramT, 1, "revealage")

	gl.DrawBuffers([]gl.Attachment{
		gl.ColorAttachment0,
		gl.ColorAttachment1,
	})

	mainFramebuffer = gl.NewFramebuffer()
	mainFramebuffer.Bind()

	fbColor = gl.CreateTexture()
	fbColor.Bind(gl.Texture2DMultisample)
	fbColor.Image2DSample(rSamples.Value(), lastWidth, lastHeight, gl.RGBA8, false)
	mainFramebuffer.Texture2D(gl.ColorAttachment0, gl.Texture2DMultisample, fbColor, 0)

	fbDepth = gl.CreateTexture()
	fbDepth.Bind(gl.Texture2DMultisample)
	fbDepth.Image2DSample(rSamples.Value(), lastWidth, lastHeight, gl.DepthComponent24, false)
	mainFramebuffer.Texture2D(gl.DepthAttachment, gl.Texture2DMultisample, fbDepth, 0)

	gl.UnbindFramebuffer()

	transState.program = CreateProgram(vertexTrans, fragmentTrans)
	transState.shader = &transShader{}
	InitStruct(transState.shader, transState.program)

	transState.array = gl.CreateVertexArray()
	transState.array.Bind()
	transState.buffer = gl.CreateBuffer()
	transState.buffer.Bind(gl.ArrayBuffer)

	data := builder.New()
	for _, f := range []float32{-1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1} {
		data.Float(f)
	}

	transState.buffer.Data(data.Data(), gl.StaticDraw)
	transState.shader.Position.Enable()
	transState.shader.Position.Pointer(2, gl.Float, false, 8, 0)
}
Exemple #3
0
// LoadTextures (re)loads all the block textures from the resource pack(s)
// TODO(Think) better error handling (if possible to recover?)
func LoadTextures() {
	textureLock.Lock()

	if texturesCreated {
		glTexture.Bind(gl.Texture2DArray)
		data := make([]byte, AtlasSize*AtlasSize*textureCount*4)
		glTexture.Image3D(0, AtlasSize, AtlasSize, textureCount, gl.RGBA, gl.UnsignedByte, data)
	} else {
		glTexture = gl.CreateTexture()
		glTexture.Bind(gl.Texture2DArray)
		textureDepth = len(textures)
		glTexture.Image3D(0, AtlasSize, AtlasSize, len(textures), gl.RGBA, gl.UnsignedByte, make([]byte, AtlasSize*AtlasSize*len(textures)*4))
		glTexture.Parameter(gl.TextureMagFilter, gl.Nearest)
		glTexture.Parameter(gl.TextureMinFilter, gl.Nearest)
		glTexture.Parameter(gl.TextureWrapS, gl.ClampToEdge)
		glTexture.Parameter(gl.TextureWrapT, gl.ClampToEdge)
		for i, tex := range textures {
			glTexture.SubImage3D(0, 0, 0, i, AtlasSize, AtlasSize, 1, gl.RGBA, gl.UnsignedByte, tex.Buffer)
			textures[i] = nil
		}
		texturesCreated = true
	}
	freeTextures = nil
	animatedTextures = nil
	textures = nil
	pix := []byte{
		0, 0, 0, 255,
		255, 0, 255, 255,
		255, 0, 255, 255,
		0, 0, 0, 255,
	}
	info := addTexture(pix, 2, 2)
	if t, ok := textureMap["missing_texture"]; ok {
		t.atlas = info.atlas
		t.rect = info.rect
	} else {
		textureMap["missing_texture"] = info
	}

	for _, t := range textureMap {
		t.atlas = info.atlas
		t.rect = info.rect
	}

	for _, s := range loadedTextures {
		func() {
			r, err := resource.Open(s.Plugin, s.File)
			if err == nil {
				defer r.Close()
				img, err := png.Decode(r)
				if err != nil {
					panic(fmt.Sprintf("(%s): %s", s.File, err))
				}
				s.Image = img
				loadTexFile(s)
			}
		}()
	}

	pix = []byte{255, 255, 255, 255}
	info = addTexture(pix, 1, 1)
	if t, ok := textureMap["solid"]; ok {
		t.atlas = info.atlas
		t.rect = info.rect
	} else {
		textureMap["solid"] = info
	}

	for _, skin := range skins {
		info := getFreeTexture(skin.info.W, skin.info.H)
		uploadTexture(info.info, skin.data)
		i := skin.info.info
		skin.info = info
		i.rect = info.info.rect
		i.atlas = info.info.atlas
		skin.info.info = i
	}

	textureLock.Unlock()

	loadFontInfo()
	loadFontPage(0)

	for i := range isFontLoaded {
		if i == 0 || !isFontLoaded[i] {
			continue
		}
		isFontLoaded[i] = false
		loadFontPage(i)
	}
}