Exemplo n.º 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
}
Exemplo n.º 2
0
func reinitScreen(width, height int) {
	winWidth, winHeight = gl.Sizei(width), gl.Sizei(height)
	if sdlScreen != nil {
		sdlScreen.Free()
	}
	sdlScreen = sdl.SetVideoMode(width, height, 24, sdl.OPENGL|sdl.RESIZABLE)
	if sdlScreen == nil {
		panic("SDL video init error")
	}
}
Exemplo n.º 3
0
Arquivo: main.go Projeto: vron/fm
func resize(w, h int) {

	// TODO: Fix the near and far limits
	persMat := gls.Perspective(45, float32(w)/float32(h), 1, 1000)
	//persMat := gls.Identity()
	gl.UseProgram(theProgram)
	gls.UniformMatrix4fv(ctcm_unif, 1, false, persMat)

	//log.Println("Resizing ", persMat)

	gl.Viewport(0, 0, gl.Sizei(w), gl.Sizei(h))
}
Exemplo n.º 4
0
func ShaderSource(name string, shader gl.Uint, source string, defines map[string]interface{}, logPrint bool) {
	var src []*gl.Char
	var i, l = 1, len(defines)
	var lines = make([]string, (l*5)+3)
	var joined string
	lines[0] = "#version 420 core\n"
	for dk, dv := range defines {
		lines[i+0] = "#define "
		lines[i+1] = dk
		lines[i+2] = " "
		lines[i+3] = fmt.Sprintf("%v", dv)
		lines[i+4] = "\n"
		i = i + 5
	}
	lines[i] = "#line 1\n"
	lines[i+1] = source
	joined = strings.Join(lines, "")
	src = gl.GLStringArray(lines...)
	defer gl.GLStringArrayFree(src)
	gl.ShaderSource(shader, gl.Sizei(len(src)), &src[0], nil)
	PanicIfErrors("ShaderSource(name=%v source=%v)", name, joined)
	if logPrint {
		log.Printf("\n\n------------------------------\n%s\n\n", joined)
	}
}
Exemplo n.º 5
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]))
}
Exemplo n.º 6
0
Arquivo: gl.go Projeto: vron/fm
func CreateProgram(shaders []g.Uint) g.Uint {
	p := g.CreateProgram()

	for _, v := range shaders {
		g.AttachShader(p, v)
	}
	g.LinkProgram(p)

	var status g.Int
	g.GetProgramiv(p, g.LINK_STATUS, &status)
	if status == g.FALSE {
		var length g.Int
		g.GetProgramiv(p, g.INFO_LOG_LENGTH, &length)
		log.Println(length)
		logStr := g.GLStringAlloc(g.Sizei(length + 1))
		g.GetProgramInfoLog(p, g.Sizei(length), nil, logStr)
		log.Println(g.GoString(logStr))
	}
	return p
}
Exemplo n.º 7
0
func glGetInfoLog(shaderOrProgram gl.Uint, isShader bool) string {
	var l = gl.Sizei(256)
	var s = gl.GLStringAlloc(l)
	defer gl.GLStringFree(s)
	if isShader {
		gl.GetShaderInfoLog(shaderOrProgram, l, nil, s)
	} else {
		gl.GetProgramInfoLog(shaderOrProgram, l, nil, s)
	}
	return gl.GoString(s)
}
Exemplo n.º 8
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
}
Exemplo n.º 9
0
func ShaderInfoLog(shaderOrProgram gl.Uint, isShader bool) string {
	var l = gl.Sizei(256)
	var s = gl.GLStringAlloc(l)
	defer gl.GLStringFree(s)
	if isShader {
		gl.GetShaderInfoLog(shaderOrProgram, l, nil, s)
	} else {
		gl.GetProgramInfoLog(shaderOrProgram, l, nil, s)
	}
	PanicIfErrors("ShaderInfoLog(s=%v)", isShader)
	return gl.GoString(s)
}
Exemplo n.º 10
0
Arquivo: gl.go Projeto: vron/fm
// Loads a shader from a string
func LoadShader(typ g.Enum, s string) g.Uint {
	shader := g.CreateShader(typ)
	cst := g.GLString(s)

	defer g.GLStringFree(cst)
	g.ShaderSource(shader, 1, &cst, nil)

	g.CompileShader(shader)

	var status g.Int
	g.GetShaderiv(shader, g.COMPILE_STATUS, &status)

	if status == g.FALSE {
		var length g.Int
		g.GetShaderiv(shader, g.INFO_LOG_LENGTH, &length)

		log.Println(int(length))
		logStr := g.GLStringAlloc(g.Sizei(length + 1))
		// TODO: defer
		g.GetShaderInfoLog(shader, g.Sizei(length), nil, logStr)
		log.Println(g.GoString(logStr))
	}
	return shader
}
Exemplo n.º 11
0
func handleResize(w, h int) {

	gl.Viewport(0, 0, gl.Sizei(w), gl.Sizei(h))
}
Exemplo n.º 12
0
func ReadAtomicCounterValues(ac gl.Uint, vals []gl.Uint) {
	var ptr *gl.Uint
	gl.BindBuffer(gl.ATOMIC_COUNTER_BUFFER, ac)
	for i := 0; i < len(vals); i++ {
		ptr = (*gl.Uint)(gl.MapBufferRange(gl.ATOMIC_COUNTER_BUFFER, gltypes.OffsetIntPtr(nil, gl.Sizei(i*4)), gltypes.SizeOfGlUint, gl.MAP_READ_BIT))
		vals[i] = *ptr
		gl.UnmapBuffer(gl.ATOMIC_COUNTER_BUFFER)
	}
	gl.BindBuffer(gl.ATOMIC_COUNTER_BUFFER, 0)
	PanicIfErrors("ReadAtomicCounter(%v)", ac)
}
Exemplo n.º 13
0
Arquivo: main.go Projeto: vron/fm
func display() {
	gl.ClearColor(0.19, 0.19, 0.21, 0)
	gl.ClearDepth(1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.UseProgram(theProgram)

	// Temporarily set the cam t clip
	id := gls.Identity()

	trans := getLookAtMatrix()
	//	log.Println(trans)
	gls.UniformMatrix4fv(wtcm_unif, 1, false, trans)

	gls.UniformMatrix4fv(mtwm_unif, 1, false, id)

	// And here we do the actual rendering

	// Generate a name for vufferobj and store it
	gl.GenBuffers(1, &positionBufferObject)
	// Bind it to a target
	gl.BindBuffer(gl.ARRAY_BUFFER, positionBufferObject)

	// Conver all nodes to a long array of vertexes!
	i := 0
	// TODO: Fix this, based on 4 per eleemnts
	vpos := make([]float32, len(elements)*4*4)
	elms := make(map[uint32]mS, len(elements))
	// Note that the order here is undefined!
	ii := 0
	for k, v := range elements {
		elms[k] = mS{ii, len(v.Nodes())}
		for _, vv := range v.Nodes() {
			ii++
			vpos[i] = float32(nodes[vv][0])
			i++
			vpos[i] = float32(nodes[vv][1])
			i++
			vpos[i] = float32(nodes[vv][2])
			i++
			vpos[i] = 1.0
			i++
		}
	}
	/*
		for _, v := range nodes {
			vpos[i] = float32(v[0])
			i++
			vpos[i] = float32(v[1])
			i++
			vpos[i] = float32(v[2])
			i++
		}
	*/
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(32/8*len(vpos)), (gl.Pointer)(&vpos[0]), gl.STATIC_DRAW)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	gl.BindBuffer(gl.ARRAY_BUFFER, positionBufferObject)
	gl.EnableVertexAttribArray(0)
	gl.VertexAttribPointer(0, 4, gl.FLOAT, gl.FALSE, 0, (gl.Pointer)(nil))

	/*	for _,v := range elms {

		}
	*/
	for _, v := range elms {
		gl.DrawArrays(gl.LINE_LOOP, gl.Int(v.Start), gl.Sizei(v.Length))
	}

	// Draw Coordinate acis
	/*for i := 9; i < 9+2*5; i += 2 {
		gl.DrawArrays(gl.LINE_STRIP, gl.Int(i), 2)
	}*/
}
Exemplo n.º 14
0
func (v *Vec2) Sizei() (sizeof gl.Sizei) {
	sizeof = gl.Sizei(unsafe.Sizeof(*v))
	return sizeof
}
Exemplo n.º 15
0
func glSetShaderSource(shader gl.Uint, source string) {
	var src = gl.GLStringArray(source)
	defer gl.GLStringArrayFree(src)
	gl.ShaderSource(shader, gl.Sizei(len(src)), &src[0], nil)
}
Exemplo n.º 16
0
func (mf *Mat4f) Sizei() (sizeof gl.Sizei) {
	sizeof = gl.Sizei(unsafe.Sizeof(*mf))
	return sizeof
}
Exemplo n.º 17
0
func (vf *Vec2f) Sizei() (sizeof gl.Sizei) {
	sizeof = gl.Sizei(unsafe.Sizeof(*vf))
	return sizeof
}
Exemplo n.º 18
0
func (m *Mat4) Sizei() (sizeof gl.Sizei) {
	sizeof = gl.Sizei(unsafe.Sizeof(*m))
	return sizeof
}