Exemplo n.º 1
0
Arquivo: line.go Projeto: num5/steven
func initLineDraw() {
	lineState.program = CreateProgram(vertexLine, fragmentLine)
	lineState.shader = &lineShader{}
	InitStruct(lineState.shader, lineState.program)

	lineState.array = gl.CreateVertexArray()
	lineState.array.Bind()
	lineState.buffer = gl.CreateBuffer()
	lineState.buffer.Bind(gl.ArrayBuffer)
	lineState.shader.Position.Enable()
	lineState.shader.Color.Enable()
	lineState.shader.Position.Pointer(3, gl.Float, false, 16, 0)
	lineState.shader.Color.Pointer(4, gl.UnsignedByte, true, 16, 12)
}
Exemplo n.º 2
0
Arquivo: chunk.go Projeto: num5/steven
// Upload uploads the passed vertex data to the buffer.
func (cb *ChunkBuffer) Upload(data []byte, indices int, cullBits uint64) {
	if cb.invalid {
		return
	}
	cb.cullBits = cullBits
	var n bool

	if indices == 0 {
		if cb.array.IsValid() {
			cb.array.Delete()
			cb.buffer.Delete()
		}
		return
	}

	if !cb.array.IsValid() {
		cb.array = gl.CreateVertexArray()
		cb.buffer = gl.CreateBuffer()
		n = true
	}

	cb.array.Bind()
	shaderChunk.Position.Enable()
	shaderChunk.TextureInfo.Enable()
	shaderChunk.TextureOffset.Enable()
	shaderChunk.Color.Enable()
	shaderChunk.Lighting.Enable()

	ensureElementBuffer(indices)
	elementBuffer.Bind(gl.ElementArrayBuffer)

	cb.buffer.Bind(gl.ArrayBuffer)
	if n || len(data) > cb.bufferSize {
		cb.bufferSize = len(data)
		cb.buffer.Data(data, gl.DynamicDraw)
	} else {
		target := cb.buffer.Map(gl.WriteOnly, len(data))
		copy(target, data)
		cb.buffer.Unmap()
	}
	shaderChunk.Position.Pointer(3, gl.Float, false, 40, 0)
	shaderChunk.TextureInfo.Pointer(4, gl.UnsignedShort, false, 40, 12)
	shaderChunk.TextureOffset.Pointer(3, gl.Short, false, 40, 20)
	shaderChunk.Color.Pointer(3, gl.UnsignedByte, true, 40, 28)
	shaderChunk.Lighting.Pointer(2, gl.UnsignedShort, false, 40, 32)

	cb.count = indices
}
Exemplo n.º 3
0
Arquivo: model.go Projeto: num5/steven
func NewModelCollection(parts [][]*ModelVertex, c *ModelCollection) *Model {
	model := &Model{
		c:          c,
		BlockLight: 1.0,
		SkyLight:   1.0,
	}

	model.array = gl.CreateVertexArray()
	model.array.Bind()
	modelState.indexBuffer.Bind(gl.ElementArrayBuffer)
	model.buffer = gl.CreateBuffer()
	model.buffer.Bind(gl.ArrayBuffer)
	c.shader.Position.Enable()
	c.shader.TextureInfo.Enable()
	c.shader.TextureOffset.Enable()
	c.shader.Color.Enable()
	c.shader.ID.Enable()
	c.shader.Position.Pointer(3, gl.Float, false, 36, 0)
	c.shader.TextureInfo.Pointer(4, gl.UnsignedShort, false, 36, 12)
	c.shader.TextureOffset.PointerInt(3, gl.Short, 36, 20)
	c.shader.Color.Pointer(4, gl.UnsignedByte, true, 36, 28)
	c.shader.ID.PointerInt(4, gl.UnsignedByte, 36, 32)

	model.Matrix = make([]mgl32.Mat4, len(parts))
	model.Colors = make([][4]float32, len(parts))
	model.counts = make([]int32, len(parts))
	model.offsets = make([]uintptr, len(parts))
	var all []*ModelVertex
	for i, p := range parts {
		model.Matrix[i] = mgl32.Ident4()
		model.Colors[i] = [4]float32{1.0, 1.0, 1.0, 1.0}
		model.counts[i] = int32((len(p) / 4) * 6)
		model.offsets[i] = uintptr((len(all) / 4) * 6)
		for _, pp := range p {
			pp.id = byte(i)
		}
		all = append(all, p...)
	}
	model.Verts = all
	model.data()

	c.models = append(c.models, model)
	return model
}
Exemplo n.º 4
0
Arquivo: ui.go Projeto: num5/steven
func initUI() {
	uiState.program = CreateProgram(glsl.Get("ui_vertex"), glsl.Get("ui_frag"))
	uiState.shader = &uiShader{}
	InitStruct(uiState.shader, uiState.program)

	uiState.array = gl.CreateVertexArray()
	uiState.array.Bind()
	uiState.buffer = gl.CreateBuffer()
	uiState.buffer.Bind(gl.ArrayBuffer)
	uiState.shader.Position.Enable()
	uiState.shader.TextureInfo.Enable()
	uiState.shader.TextureOffset.Enable()
	uiState.shader.Color.Enable()
	uiState.shader.Position.PointerInt(3, gl.Short, 28, 0)
	uiState.shader.TextureInfo.Pointer(4, gl.UnsignedShort, false, 28, 8)
	uiState.shader.TextureOffset.PointerInt(3, gl.Short, 28, 16)
	uiState.shader.Color.Pointer(4, gl.UnsignedByte, true, 28, 24)

	uiState.indexBuffer = gl.CreateBuffer()
	uiState.indexBuffer.Bind(gl.ElementArrayBuffer)
}
Exemplo n.º 5
0
Arquivo: trans.go Projeto: num5/steven
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)
}
Exemplo n.º 6
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)
}