Пример #1
0
func onStart(glctx gl.Context) {
	var err error
	program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
	if err != nil {
		log.Printf("[ERR] Failed creating GL program: %v", err)
		return
	}

	buf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
	glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)

	position = glctx.GetAttribLocation(program, "position")
	color = glctx.GetUniformLocation(program, "color")
	offset = glctx.GetUniformLocation(program, "offset")

	images = glutil.NewImages(glctx)
	fps = debug.NewFPS(images)

	statusFont, statusFace, err = exfont.LoadAsset("Tuffy.ttf", statusFaceOpt)
	if err != nil {
		log.Printf("[ERR] Failed to load status font: %v", err)
	}
	statusPainter = rexdemo.NewStatusPainter(demo, statusFont, statusBG, images)

	ifaces, err := net.Interfaces()
	if err != nil {
		log.Printf("[ERR] Failed to retreived interfaces")
	} else {
		log.Printf("[DEBUG] %d network interfaces", len(ifaces))
		for _, iface := range ifaces {
			log.Printf("[DEBUG] IFACE %d %s", iface.Index, iface.Name)
		}
	}
}
Пример #2
0
func onStart(glctx gl.Context) {
	var err error
	program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
	if err != nil {
		log.Printf("[ERR] Failed creating GL program: %v", err)
		return
	}

	buf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
	glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)

	position = glctx.GetAttribLocation(program, "position")
	color = glctx.GetUniformLocation(program, "color")
	offset = glctx.GetUniformLocation(program, "offset")

	images = glutil.NewImages(glctx)
	fps = debug.NewFPS(images)

	statusFont, statusFace, err = exfont.LoadAsset("Tuffy.ttf", statusFaceOpt)
	if err != nil {
		log.Printf("[ERR] Failed to load status font: %v", err)
	}
	statusPainter = rexdemo.NewStatusPainter(demo, statusFont, _color.White, images)
}
Пример #3
0
// TODO just how many samples do we want/need to display something useful?
func NewWaveform(ctx gl.Context, n int, in Sound) (*Waveform, error) {
	wf := &Waveform{Sound: in}

	wf.outs = make([][]float64, n)
	for i := range wf.outs {
		wf.outs[i] = make([]float64, in.BufferLen()*in.Channels())
	}
	wf.samples = make([]float64, in.BufferLen()*in.Channels()*n)
	// wf.aligned = make([]float64, in.BufferLen()*in.Channels()*n)

	wf.verts = make([]float32, len(wf.samples)*3)
	wf.data = make([]byte, len(wf.verts)*4)

	if ctx == nil {
		return wf, nil
	}

	var err error
	wf.program, err = glutil.CreateProgram(ctx, vertexShader, fragmentShader)
	if err != nil {
		return nil, fmt.Errorf("error creating GL program: %v", err)
	}

	// create and alloc hw buf
	wf.buf = ctx.CreateBuffer()
	ctx.BindBuffer(gl.ARRAY_BUFFER, wf.buf)
	ctx.BufferData(gl.ARRAY_BUFFER, make([]byte, len(wf.samples)*12), gl.STREAM_DRAW)

	wf.position = ctx.GetAttribLocation(wf.program, "position")
	wf.color = ctx.GetUniformLocation(wf.program, "color")
	return wf, nil
}
Пример #4
0
// NewImages creates an *Images.
func NewImages(glctx gl.Context) *Images {
	program, err := CreateProgram(glctx, vertexShader, fragmentShader)
	if err != nil {
		panic(err)
	}

	p := &Images{
		glctx:         glctx,
		quadXY:        glctx.CreateBuffer(),
		quadUV:        glctx.CreateBuffer(),
		program:       program,
		pos:           glctx.GetAttribLocation(program, "pos"),
		mvp:           glctx.GetUniformLocation(program, "mvp"),
		uvp:           glctx.GetUniformLocation(program, "uvp"),
		inUV:          glctx.GetAttribLocation(program, "inUV"),
		textureSample: glctx.GetUniformLocation(program, "textureSample"),
	}

	glctx.BindBuffer(gl.ARRAY_BUFFER, p.quadXY)
	glctx.BufferData(gl.ARRAY_BUFFER, quadXYCoords, gl.STATIC_DRAW)
	glctx.BindBuffer(gl.ARRAY_BUFFER, p.quadUV)
	glctx.BufferData(gl.ARRAY_BUFFER, quadUVCoords, gl.STATIC_DRAW)

	return p
}
Пример #5
0
func NewButton(ctx gl.Context, x, y float32, w, h float32) *Button {
	btn := &Button{r: 1, g: 1, b: 1, a: 1}

	btn.x = (-(-1 - x)) / 2
	btn.y = (1 - y) / 2
	btn.w, btn.h = (w)/2, (-h)/2

	btn.verts = []float32{
		x, y, 0,
		x + w, y, 0,
		x, y + h, 0,
		x, y + h, 0,
		x + w, y, 0,
		x + w, y + h, 0,
	}

	btn.data = make([]byte, len(btn.verts)*4)

	var err error
	btn.program, err = glutil.CreateProgram(ctx, vertexShader, fragmentShader)
	if err != nil {
		panic(fmt.Errorf("error creating GL program: %v", err))
	}

	// create and alloc hw buf
	btn.buf = ctx.CreateBuffer()
	ctx.BindBuffer(gl.ARRAY_BUFFER, btn.buf)
	ctx.BufferData(gl.ARRAY_BUFFER, make([]byte, len(btn.verts)*4), gl.STATIC_DRAW)

	btn.position = ctx.GetAttribLocation(btn.program, "position")
	btn.color = ctx.GetUniformLocation(btn.program, "color")
	return btn
}
Пример #6
0
// NewWhiteKey is a constructor that creates and returns a WhiteKey
func NewWhiteKey(glctx gl.Context, note util.KeyNote, sz size.Event, count int) *WhiteKey {
	newWhiteKey := new(WhiteKey)
	// Create the coloring and sound for the white key.
	newWhiteKey.PianoKey = newPianoKey(glctx, whiteKeyRGBColor, note)

	// Create coordinates for this specific white key
	newWhiteKey.openGLCoords, newWhiteKey.keyOutline, newWhiteKey.keyOuterBoundary = makeWhiteKeyVector(float32(sz.WidthPx), count)
	glctx.BufferData(gl.ARRAY_BUFFER, newWhiteKey.openGLCoords, gl.STATIC_DRAW)

	return newWhiteKey
}
Пример #7
0
// NewBlackKey is a constructor that creates and returns a BlackKey
func NewBlackKey(leftWhiteKey Key, glctx gl.Context, note util.KeyNote, sz size.Event) *BlackKey {
	newBlackKey := new(BlackKey)
	// Create the coloring and sound for the black key.
	newBlackKey.PianoKey = newPianoKey(glctx, blackKeyRGBColor, note)

	// Create coordinates for this specific black key
	newBlackKey.openGLCoords, newBlackKey.keyOutline, newBlackKey.keyOuterBoundary =
		makeBlackKeyVector(leftWhiteKey)
	glctx.BufferData(gl.ARRAY_BUFFER, newBlackKey.openGLCoords, gl.STATIC_DRAW)

	return newBlackKey
}
Пример #8
0
func onStart(glctx gl.Context) {
	var err error
	program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
	if err != nil {
		log.Printf("error creating GL program: %v", err)
		return
	}
	buf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
	glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)
	position = glctx.GetAttribLocation(program, "position")
	color = glctx.GetUniformLocation(program, "color")
	offset = glctx.GetUniformLocation(program, "offset")
	images = glutil.NewImages(glctx)
	fps = debug.NewFPS(images)
}
Пример #9
0
func (buf *uintBuffer) Update(ctx gl.Context, data []uint32) {
	buf.count = len(data)
	subok := len(buf.bin) > 0 && len(data)*4 <= len(buf.bin)
	if !subok {
		buf.bin = make([]byte, len(data)*4)
	}
	for i, u := range data {
		buf.bin[4*i+0] = byte(u >> 0)
		buf.bin[4*i+1] = byte(u >> 8)
		buf.bin[4*i+2] = byte(u >> 16)
		buf.bin[4*i+3] = byte(u >> 24)
	}
	if subok {
		ctx.BufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, buf.bin)
	} else {
		ctx.BufferData(gl.ELEMENT_ARRAY_BUFFER, buf.bin, buf.usage)
	}
}
Пример #10
0
// NewKey initializes a new Key
func NewKey(glctx gl.Context, x, y, z, length, width, height float32,
	color Color, idColor Color, tex []byte) *Key {

	r := &Key{
		glctx, x, y, z, length, width, height,
		[]byte{},
		color,
		idColor,
		glctx.CreateBuffer(),
		tex,
	}
	r.chart()

	glctx.BindBuffer(gl.ARRAY_BUFFER, r.buf)
	glctx.BufferData(gl.ARRAY_BUFFER, r.data, gl.STATIC_DRAW)

	return r
}
Пример #11
0
func onStart(glctx gl.Context) {
	var err error
	program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
	if err != nil {
		log.Printf("error creating GL program: %v", err)
		return
	}

	glctx.Enable(gl.DEPTH_TEST)

	triBuf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, triBuf)
	glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)

	position = glctx.GetAttribLocation(program, "vPos")
	color = glctx.GetAttribLocation(program, "vCol")
	normals = glctx.GetAttribLocation(program, "vNorm")

	projection = glctx.GetUniformLocation(program, "proj")
	view = glctx.GetUniformLocation(program, "view")
	model = glctx.GetUniformLocation(program, "model")
	tint = glctx.GetUniformLocation(program, "tint")
	normalMatrix = glctx.GetUniformLocation(program, "normalMatrix")
	lightIntensity = glctx.GetUniformLocation(program, "light.intensities")
	lightPos = glctx.GetUniformLocation(program, "light.position")

	arcball = NewArcBall(mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 10, 10}, mgl32.Vec3{0, 1, 0})

	white = mgl32.Vec4{1.0, 1.0, 1.0, 1.0}
	red = mgl32.Vec4{1.0, 0.0, 0.0, 1.0}

	lastUpdate = time.Now()

	images = glutil.NewImages(glctx)
	fps = debug.NewFPS(images)

	err = al.OpenDevice()
	if err != nil {
		log.Printf("Err: %+v", err)
	}
	al.SetListenerPosition(al.Vector{0, 0, 0})
	al.SetListenerGain(1.0)
	piano = NewPiano()
}
Пример #12
0
func appStart(glctx gl.Context) {
	println("Starting")
	program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
	if err != nil {
		panic("error creating GL program: " + err.Error())
		return
	}

	buf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
	glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)

	position = glctx.GetAttribLocation(program, "position")
	color = glctx.GetUniformLocation(program, "color")
	offset = glctx.GetUniformLocation(program, "offset")
	images = glutil.NewImages(glctx)
	fps = debug.NewFPS(images)

	SetScene(currentScene.Name)
}
Пример #13
0
func onStart(glctx gl.Context) {
	var err error
	program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
	if err != nil {
		log.Printf("error creating GL program: %v", err)
		return
	}

	/*opengl中三种变量
	uniform变量是外部application程序传递给(vertex和fragment)shader的变量。因此它是application通过函数glUniform**()函数赋值的。
	在(vertex和fragment)shader程序内部,uniform变量就像是C语言里面的常量(const ),它不能被shader程序修改。(shader只能用,不能改)

	attribute变量是只能在vertex shader中使用的变量。(它不能在fragment shader中声明attribute变量,也不能被fragment shader中使用)
	一般用attribute变量来表示一些顶点的数据,如:顶点坐标,法线,纹理坐标,顶点颜色等。
	在application中,一般用函数glBindAttribLocation()来绑定每个attribute变量的位置,然后用函数glVertexAttribPointer()为每个attribute变量赋值。

	varying变量是vertex和fragment shader之间做数据传递用的。一般vertex shader修改varying变量的值,然后fragment shader使用该varying变量的值。
	因此varying变量在vertex和fragment shader二者之间的声明必须是一致的。application不能使用此变量。
	*/

	position = glctx.GetAttribLocation(program, "position") //获取位置对象(索引)
	color = glctx.GetAttribLocation(program, "color")       // 获取颜色对象(索引)
	scan = glctx.GetUniformLocation(program, "scan")        // 获取缩放对象(索引)

	/*
	VBO允许usage标示符取以下9种值:

		gl.STATIC_DRAW
		gl.STATIC_READ
		gl.STATIC_COPY

		gl.DYNAMIC_DRAW
		gl.DYNAMIC_READ
		gl.DYNAMIC_COPY

		gl.STREAM_DRAW
		gl.STREAM_READ
		gl.STREAM_COPY

	"Static”意味着VBO中的数据不会被改变(一次修改,多次使用),
	"dynamic”意味着数据可以被频繁修改(多次修改,多次使用),
	"stream”意味着数据每帧都不同(一次修改,一次使用)。

	"Draw”意味着数据将会被送往GPU进行绘制,
	"read”意味着数据会被用户的应用读取,
	"copy”意味着数据会被用于绘制和读取。

	注意在使用VBO时,只有draw是有效的,而copy和read主要将会在像素缓冲区(PBO)和帧缓冲区(FBO)中发挥作用。
	系统会根据usage标示符为缓冲区对象分配最佳的存储位置,比如系统会为gl.STATIC_DRAW和gl.STREAM_DRAW分配显存,
	gl.DYNAMIC_DRAW分配AGP,以及任何_READ_相关的缓冲区对象都会被存储到系统或者AGP中因为这样数据更容易读写
	 */
	positionbuf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, positionbuf)
	glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)

	colorbuf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, colorbuf)
	glctx.BufferData(gl.ARRAY_BUFFER, colorData, gl.STATIC_DRAW)

	images = glutil.NewImages(glctx)
	fps = debug.NewFPS(images)

	// fmt.Println(position.String(),color.String(),offset.String())//Attrib(0) Uniform(1) Uniform(0)
	// TODO(crawshaw): the debug package needs to put GL state init here
	// Can this be an event.Register call now??
}