Example #1
0
func MakeTextureFromImageFile(filePath string) gl.Uint {
	var file, err = os.Open(filePath)
	var img image.Image
	var tex gl.Uint
	if err != nil {
		panic(err)
	}
	defer file.Close()
	img, _, err = image.Decode(file)
	if err != nil {
		panic(err)
	}
	w, h := img.Bounds().Dx(), img.Bounds().Dy()
	sw, sh := gl.Sizei(w), gl.Sizei(h)
	rgba := image.NewRGBA(image.Rect(0, 0, w, h))
	for x := 0; x < w; x++ {
		for y := 0; y < h; y++ {
			rgba.Set(x, y, img.At(x, h-1-y))
		}
	}
	gl.GenTextures(1, &tex)
	gl.BindTexture(gl.TEXTURE_2D, tex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	gl.TexStorage2D(gl.TEXTURE_2D, 1, gl.RGBA8, sw, sh)
	gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, sw, sh, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&rgba.Pix[0]))
	gl.BindTexture(gl.TEXTURE_2D, 0)
	PanicIfErrors("MakeTextureFromImageFile(%v)", filePath)
	return tex
}
Example #2
0
func renderToScreen() {
	gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
	gl.Viewport(0, 0, winWidth, winHeight)
	gl.UseProgram(shaderTextureDisplay.glProg)
	gl.BindTexture(gl.TEXTURE_2D, rttFrameTex)
	gl.Uniform1i(shaderTextureDisplay.unifTex, 0)
	gl.BindBuffer(gl.ARRAY_BUFFER, dispVertBuf)
	gl.EnableVertexAttribArray(shaderTextureDisplay.attrPos)
	gl.VertexAttribPointer(shaderTextureDisplay.attrPos, 3, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil))
	gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
	gl.DisableVertexAttribArray(shaderTextureDisplay.attrPos)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
Example #3
0
func renderToTexture() {
	gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
	gl.Viewport(0, 0, texSize, texSize)
	gl.ClearColor(0.9, 0.6, 0.3, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.UseProgram(shaderTextureCreator.glProg)
	gl.BindBuffer(gl.ARRAY_BUFFER, rttVertBuf)
	gl.EnableVertexAttribArray(shaderTextureCreator.attrPos)
	gl.VertexAttribPointer(shaderTextureCreator.attrPos, 2, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil))
	gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
	gl.DisableVertexAttribArray(shaderTextureCreator.attrPos)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
Example #4
0
func renderToTexture() {
	gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
	gl.Viewport(0, 0, texWidth, texHeight)
	gl.ClearColor(0.1, 0.2, 0.3, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.UseProgram(shaderTextureCreator.glProg)
	gl.BindTexture(gl.TEXTURE_3D, volTex)
	gl.Uniform1i(shaderTextureCreator.unifTex, 0)
	gl.BindBuffer(gl.ARRAY_BUFFER, rttVertBuf)
	gl.EnableVertexAttribArray(shaderTextureCreator.attrPos)
	gl.VertexAttribPointer(shaderTextureCreator.attrPos, 2, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil))
	gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
	gl.DisableVertexAttribArray(shaderTextureCreator.attrPos)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
Example #5
0
func MakeTextureFromImageFloatsFile(filePath string, w, h int) gl.Uint {
	var file, err = os.Open(filePath)
	var tex gl.Uint
	var pix = make([]gl.Float, w*h*3)
	var fVal float32
	var raw []uint8
	var buf *bytes.Buffer
	var i int
	if err != nil {
		panic(err)
	}
	defer file.Close()
	raw, err = ioutil.ReadAll(file)
	if err != nil {
		panic(err)
	}
	buf = bytes.NewBuffer(raw)
	for i = 0; (err == nil) && (i < len(pix)); i++ {
		if err = binary.Read(buf, binary.LittleEndian, &fVal); err == io.EOF {
			err = nil
			break
		} else if err == nil {
			pix[i] = gl.Float(fVal)
		}
	}
	if err != nil {
		panic(err)
	}
	sw, sh := gl.Sizei(w), gl.Sizei(h)
	gl.GenTextures(1, &tex)
	gl.BindTexture(gl.TEXTURE_2D, tex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	gl.TexStorage2D(gl.TEXTURE_2D, 1, gl.RGB16F, sw, sh)
	gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, sw, sh, gl.RGB, gl.FLOAT, gl.Pointer(&pix[0]))
	gl.BindTexture(gl.TEXTURE_2D, 0)
	PanicIfErrors("MakeTextureFromImageFloatsFile(%v)", filePath)
	return tex
}
Example #6
0
func (m *Mat4) Pointer() (pointer gl.Pointer) {
	pointer = gl.Pointer(unsafe.Pointer(m))
	return pointer
}
Example #7
0
func (v *Vec2) Pointer() (pointer gl.Pointer) {
	pointer = gl.Pointer(unsafe.Pointer(v))
	return pointer
}
Example #8
0
func loadVolume() {
	var file, err = os.Open("/home/roxor/apps/ImageVis3D/Data/BostonTeapot.raw")
	var bytes []byte
	if err != nil {
		panic(err)
	}
	defer file.Close()
	if bytes, err = ioutil.ReadAll(file); err != nil {
		panic(err)
	}
	gl.GenTextures(1, &volTex)
	gl.BindTexture(gl.TEXTURE_3D, volTex)
	gl.TexParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, glTexFilter)
	gl.TexParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, glTexFilter)
	gl.TexParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_R, glTexClamp)
	gl.TexParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, glTexClamp)
	gl.TexParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_T, glTexClamp)
	gl.TexImage3D(gl.TEXTURE_3D, 0, gl.ALPHA, 256, 256, 178, 0, gl.ALPHA, gl.UNSIGNED_BYTE, gl.Pointer(&bytes[0]))
	gl.BindTexture(gl.TEXTURE_3D, 0)
}
Example #9
0
func glFillTextureFromImageFile(filePath string) {
	var file, err = os.Open(filePath)
	var img image.Image
	if err != nil {
		panic(err)
	}
	defer file.Close()
	if img, _, err = image.Decode(file); err != nil {
		panic(err)
	}
	w, h := img.Bounds().Dx(), img.Bounds().Dy()
	rgba := image.NewRGBA(image.Rect(0, 0, w, h))
	for x := 0; x < w; x++ {
		for y := 0; y < h; y++ {
			rgba.Set(x, y, img.At(x, y))
		}
	}
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(w), gl.Sizei(h), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&rgba.Pix[0]))
}
Example #10
0
func glFillBuffer(vals []gl.Float, buf *gl.Uint) {
	gl.GenBuffers(1, buf)
	gl.BindBuffer(gl.ARRAY_BUFFER, *buf)
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(vals)*4), gl.Pointer(&vals[0]), gl.STATIC_DRAW)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
Example #11
0
func MakeAtomicCounters(glPtr *gl.Uint, num gl.Sizei) {
	gl.GenBuffers(1, glPtr)
	gl.BindBuffer(gl.ATOMIC_COUNTER_BUFFER, *glPtr)
	gl.BufferData(gl.ATOMIC_COUNTER_BUFFER, gl.Sizeiptr(4*num), gl.Pointer(nil), BufMode)
	gl.BindBuffer(gl.ATOMIC_COUNTER_BUFFER, 0)
}
Example #12
0
func MakeArrayBuffer(glPtr *gl.Uint, size uint64, sl interface{}, isLen, makeTex bool) gl.Uint {
	var ptr = gl.Pointer(nil)
	var glTex gl.Uint = 0
	var glTexFormat gl.Enum = gl.R8UI
	var sizeFactor, sizeTotal uint64 = 1, 0
	var tm = false
	var handle = func(sf uint64, glPtr gl.Pointer, le int, tf gl.Enum) {
		tm = true
		if le > 1 {
			ptr = glPtr
		}
		if size == 0 {
			size = uint64(le)
			isLen = true
		}
		if isLen {
			sizeFactor = sf
		}
		if tf != 0 {
			glTexFormat = tf
		}
	}
	if (sl == nil) && FillWithZeroes {
		sl = make([]uint8, size)
	}
	gl.GenBuffers(1, glPtr)
	gl.BindBuffer(gl.ARRAY_BUFFER, *glPtr)
	if sl != nil {
		if tv, tb := sl.([]uint8); tb {
			handle(1, gl.Pointer(&tv[0]), len(tv), gl.R8UI)
		}
		if tv, tb := sl.([]uint16); tb {
			handle(2, gl.Pointer(&tv[0]), len(tv), gl.R16UI)
		}
		if tv, tb := sl.([]uint32); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32UI)
		}
		if tv, tb := sl.([]uint64); tb {
			handle(8, gl.Pointer(&tv[0]), len(tv), gl.RG32UI)
		}
		if tv, tb := sl.([]int8); tb {
			handle(1, gl.Pointer(&tv[0]), len(tv), gl.R8I)
		}
		if tv, tb := sl.([]int16); tb {
			handle(2, gl.Pointer(&tv[0]), len(tv), gl.R16I)
		}
		if tv, tb := sl.([]int32); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32I)
		}
		if tv, tb := sl.([]int64); tb {
			handle(8, gl.Pointer(&tv[0]), len(tv), gl.RG32I)
		}
		if tv, tb := sl.([]float32); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32F)
		}
		if tv, tb := sl.([]float64); tb {
			handle(8, gl.Pointer(&tv[0]), len(tv), gl.RG32F)
		}
		if tv, tb := sl.([]gl.Bitfield); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R8UI)
		}
		if tv, tb := sl.([]gl.Byte); tb {
			handle(1, gl.Pointer(&tv[0]), len(tv), gl.R8I)
		}
		if tv, tb := sl.([]gl.Ubyte); tb {
			handle(1, gl.Pointer(&tv[0]), len(tv), gl.R8UI)
		}
		if tv, tb := sl.([]gl.Ushort); tb {
			handle(2, gl.Pointer(&tv[0]), len(tv), gl.R16UI)
		}
		if tv, tb := sl.([]gl.Short); tb {
			handle(2, gl.Pointer(&tv[0]), len(tv), gl.R16I)
		}
		if tv, tb := sl.([]gl.Uint); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32UI)
		}
		if tv, tb := sl.([]gl.Uint64); tb {
			handle(8, gl.Pointer(&tv[0]), len(tv), gl.RG32UI)
		}
		if tv, tb := sl.([]gl.Int); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32I)
		}
		if tv, tb := sl.([]gl.Int64); tb {
			handle(8, gl.Pointer(&tv[0]), len(tv), gl.RG32I)
		}
		if tv, tb := sl.([]gl.Clampd); tb {
			handle(8, gl.Pointer(&tv[0]), len(tv), gl.RG32F)
		}
		if tv, tb := sl.([]gl.Clampf); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32F)
		}
		if tv, tb := sl.([]gl.Float); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32F)
		}
		if tv, tb := sl.([]gl.Half); tb {
			handle(2, gl.Pointer(&tv[0]), len(tv), gl.R16F)
		}
		if tv, tb := sl.([]gl.Double); tb {
			handle(8, gl.Pointer(&tv[0]), len(tv), gl.RG32F)
		}
		if tv, tb := sl.([]gl.Enum); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32I)
		}
		if tv, tb := sl.([]gl.Sizei); tb {
			handle(4, gl.Pointer(&tv[0]), len(tv), gl.R32UI)
		}
		if tv, tb := sl.([]gl.Char); tb {
			handle(1, gl.Pointer(&tv[0]), len(tv), gl.R8UI)
		}
		if !tm {
			log.Panicf("MakeArrayBuffer() -- slice type unsupported:\n%+v", sl)
		}
	}
	sizeTotal = size * sizeFactor
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(sizeTotal), ptr, BufMode)
	if makeTex {
		if sizeTotal > MaxTextureBufferSize() {
			log.Panicf("Texture buffer size (%vMB) would exceed your GPU's maximum texture buffer size (%vMB)", sizeTotal/MB, maxTexBufSize/MB)
		}
		gl.GenTextures(1, &glTex)
		gl.BindTexture(gl.TEXTURE_BUFFER, glTex)
		gl.TexBuffer(gl.TEXTURE_BUFFER, glTexFormat, *glPtr)
		gl.BindTexture(gl.TEXTURE_2D, 0)
	}
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
	PanicIfErrors("MakeArrayBuffer()")
	return glTex
}
Example #13
0
func OffsetPointer(ptr gl.Pointer, offset uint) gl.Pointer {
	return gl.Pointer(uintptr(ptr) + uintptr(offset))
}