Exemplo n.º 1
0
func uploadGeometry(mesh *geometry) {
	gl.GenVertexArrays(1, &mesh.glVao)
	gl.BindVertexArray(mesh.glVao)
	gl.GenBuffers(1, &mesh.glVertBuf)
	gl.BindBuffer(gl.ARRAY_BUFFER, mesh.glVertBuf)
	gl.EnableVertexAttribArray(progPosAttrLoc)
	gl.VertexAttribPointer(progPosAttrLoc, 3, gl.FLOAT, gl.FALSE, 0, gl.Ptr(nil))
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(4*len(mesh.glVerts)), gl.Ptr(&mesh.glVerts[0]), gl.STATIC_DRAW)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
	gl.BindVertexArray(0)
}
Exemplo n.º 2
0
func (me *Mesh) GpuUpload() (err error) {
	sizeVerts, sizeIndices := gl.Sizeiptr(4*len(me.raw.verts)), gl.Sizeiptr(4*len(me.raw.indices))
	me.GpuDelete()
	if sizeVerts > gl.Sizeiptr(me.meshBuffer.memSizeVertices) {
		err = errf("Cannot upload mesh '%v': vertex size (%vB) exceeds mesh buffer's available vertex memory (%vB)", me.Name, sizeVerts, me.meshBuffer.memSizeVertices)
	} else if sizeIndices > gl.Sizeiptr(me.meshBuffer.memSizeIndices) {
		err = errf("Cannot upload mesh '%v': index size (%vB) exceeds mesh buffer's available index memory (%vB)", me.Name, sizeIndices, me.meshBuffer.memSizeIndices)
	} else {
		me.meshBufOffsetBaseIndex, me.meshBufOffsetIndices, me.meshBufOffsetVerts = me.meshBuffer.offsetBaseIndex, me.meshBuffer.offsetIndices, me.meshBuffer.offsetVerts
		Diag.LogMeshes("Upload %v at voff=%v ioff=%v boff=%v", me.Name, me.meshBufOffsetVerts, me.meshBufOffsetIndices, me.meshBufOffsetBaseIndex)
		me.meshBuffer.glIbo.Bind()
		defer me.meshBuffer.glIbo.Unbind()
		me.meshBuffer.glVbo.Bind()
		defer me.meshBuffer.glVbo.Unbind()
		if err = me.meshBuffer.glVbo.SubData(gl.Intptr(me.meshBufOffsetVerts), sizeVerts, gl.Ptr(&me.raw.verts[0])); err == nil {
			me.meshBuffer.offsetVerts += int32(sizeVerts)
			if err = me.meshBuffer.glIbo.SubData(gl.Intptr(me.meshBufOffsetIndices), sizeIndices, gl.Ptr(&me.raw.indices[0])); err == nil {
				me.meshBuffer.offsetIndices += int32(sizeIndices)
				me.meshBuffer.offsetBaseIndex += int32(len(me.raw.indices))
				if err == nil {
					me.gpuSynced = true
				}
			}
		}
	}
	return
}
Exemplo n.º 3
0
func (me *TextureBase) prepFromImages(bgra, uintRev bool, images ...image.Image) (err error) {
	pixData, pfmt := &me.PixelData, Typed.Ife(bgra, gl.BGRA, gl.RGBA)
	pixData.Format = pfmt
	pixData.Type = Typed.Ife(uintRev, gl.UNSIGNED_INT_8_8_8_8_REV, gl.UNSIGNED_BYTE)
	for i, img := range images {
		switch pic := img.(type) {
		case *image.Alpha:
			me.SizedInternalFormat = gl.R8
			pixData.Format = gl.RED
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix[0])
		case *image.Alpha16:
			me.SizedInternalFormat = gl.R16
			pixData.Format = gl.RED
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix[0])
		case *image.Gray:
			me.SizedInternalFormat = gl.R8
			pixData.Format = gl.RED
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix[0])
		case *image.Gray16:
			me.SizedInternalFormat = gl.R16
			pixData.Format = gl.RED
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix[0])
		case *image.NRGBA:
			me.SizedInternalFormat = gl.RGBA8
			pixData.Format = pfmt
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix[0])
		case *image.NRGBA64:
			me.SizedInternalFormat = gl.RGBA16
			pixData.Format = pfmt
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix[0])
		case *image.RGBA:
			me.SizedInternalFormat = gl.RGBA8
			pixData.Format = pfmt
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix[0])
		case *image.RGBA64:
			me.SizedInternalFormat = gl.RGBA16
			pixData.Format = pfmt
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix[0])
		case interface {
			Pix() []byte
		}:
			pixData.Ptrs[i] = gl.Ptr(&pic.Pix()[0])
		default:
			err = errf("Unsupported image.Image type (%v) for use as OpenGL texture", reflect.TypeOf(pic))
		}
	}
	return
}
Exemplo n.º 4
0
func (me *TextureBase) subImage(glTarget gl.Enum, x, y gl.Int, width, height gl.Sizei, ptr gl.Ptr) (err error) {
	if ptr != PtrNil {
		if Support.Textures.StreamUpdatesViaPixelBuffer {
			buf := NewPixelUnpackBuffer()
			defer buf.Dispose()
			buf.Recreate(0, gl.Sizeiptr(width*height*4), PtrNil, gl.STREAM_DRAW)
			buf.Bind()
			defer buf.Unbind()
			pbo := buf.Map(true, true)
			*pbo = gl.Ptr(ptr)
			buf.Unmap()
			err = Try.TexSubImage2D(glTarget, 0, x, y, width, height, me.PixelData.Format, me.PixelData.Type, PtrNil)
		} else {
			err = Try.TexSubImage2D(glTarget, 0, x, y, width, height, me.PixelData.Format, me.PixelData.Type, ptr)
		}
	}
	return
}
Exemplo n.º 5
0
import (
	gl "github.com/go3d/go-opengl/core"

	"github.com/go-utils/unum"
)

const (
	//	The size of a gl.Float in bytes
	SizeOfGlFloat gl.Sizeiptr = 4

	//	The size of a gl.Uint in bytes
	SizeOfGlUint gl.Sizeiptr = 4
)

var (
	PtrNil = gl.Ptr(nil)
)

//	Represents a 3-dimensional vector (32-bit per component)
type GlVec3 [3]gl.Float

//	Sets me to the first 3-or-less vals.
func (me *GlVec3) Set(vals ...gl.Float) {
	for i := 0; (i < len(me)) && (i < len(vals)); i++ {
		me[i] = vals[i]
	}
}

//	Represents a quaternion or 4-dimensional vector (32-bit per component)
type GlVec4 [4]gl.Float