Example #1
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
}
Example #2
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
}
Example #3
0
func onPaint(glctx gl.Context, sz size.Event) {

	glctx.Viewport(0, 0, sz.WidthPx, sz.HeightPx)
	glctx.ClearColor(0.5, 0.5, 0.5, 1)
	glctx.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	glctx.UseProgram(program)

	projectionMtx = mgl32.Perspective(45, float32(width)/float32(height), 0.1, 100)

	arcBallMtx := arcball.getMtx()

	glctx.UniformMatrix4fv(projection, projectionMtx[:])

	glctx.UniformMatrix4fv(view, arcBallMtx[:])

	glctx.BindBuffer(gl.ARRAY_BUFFER, triBuf)
	glctx.EnableVertexAttribArray(position)
	glctx.EnableVertexAttribArray(color)
	glctx.EnableVertexAttribArray(normals)

	vertSize := 4 * (coordsPerVertex + colorPerVertex + normalsPerVertex)

	glctx.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, vertSize, 0)
	glctx.VertexAttribPointer(color, colorPerVertex, gl.FLOAT, false, vertSize, 4*coordsPerVertex)
	glctx.VertexAttribPointer(normals, normalsPerVertex, gl.FLOAT, false, vertSize, 4*(coordsPerVertex+colorPerVertex))

	glctx.DepthMask(true)

	glctx.Uniform3fv(lightPos, light.Pos[:])
	glctx.Uniform3fv(lightIntensity, light.Intensities[:])

	for _, k := range piano.Keys {
		glctx.Uniform4fv(tint, k.Color[:])

		mtx := k.GetMtx()
		normMat := mtx.Mat3().Inv().Transpose()
		glctx.UniformMatrix3fv(normalMatrix, normMat[:])
		glctx.UniformMatrix4fv(model, mtx[:])
		glctx.DrawArrays(gl.TRIANGLES, 0, len(triangleData)/vertSize)
	}

	modelMtx := mgl32.Ident4()
	modelMtx = modelMtx.Mul4(mgl32.Translate3D(worldPos.X(), worldPos.Y(), worldPos.Z()))
	modelMtx = modelMtx.Mul4(mgl32.Scale3D(0.5, 0.5, 0.5))

	/*
		glctx.Uniform4fv(tint, red[:])
		// Disable depthmask so we dont get the pixel depth of the cursor cube
		glctx.DepthMask(false)
		glctx.UniformMatrix4fv(model, modelMtx[:])
		glctx.DepthMask(true)
	*/

	glctx.DisableVertexAttribArray(position)
	glctx.DisableVertexAttribArray(color)
	glctx.DisableVertexAttribArray(normals)

	fps.Draw(sz)
}
Example #4
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)
		}
	}
}
Example #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
}
Example #6
0
File: main.go Project: bmatsuo/rex
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)
}
Example #7
0
func NewDynamicShape(glctx gl.Context, bufSize int) *DynamicShape {
	shape := &DynamicShape{StaticShape{glctx: glctx}}
	shape.VBO = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, shape.VBO)
	glctx.BufferInit(gl.ARRAY_BUFFER, bufSize, gl.DYNAMIC_DRAW)

	return shape
}
Example #8
0
// newPianoKey creates a PianoKey with color and sound.
func newPianoKey(glctx gl.Context, keyColor util.RGBColor, note util.KeyNote) *PianoKey {
	key := new(PianoKey)
	key.keyColor = keyColor
	// Create buffer
	key.glBuf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, key.glBuf)
	// Generate sound
	_ = al.OpenDevice()
	key.soundBuffers = al.GenBuffers(1)
	key.soundSources = al.GenSources(1)
	key.soundBuffers[0].BufferData(al.FormatStereo8, audio.GenSound(note), audio.SampleRate)
	key.soundSources[0].QueueBuffers(key.soundBuffers)
	return key
}
Example #9
0
func onPaint(glctx gl.Context, sz size.Event) {
	//gl.Enable(gl.DEPTH_TEST)
	//gl.DepthFunc(gl.LESS)
	//清场
	glctx.ClearColor(1, 1, 1, 1) //设置背景颜色
	glctx.Clear(gl.COLOR_BUFFER_BIT)
	glctx.Clear(gl.DEPTH_BUFFER_BIT)

	//使用program
	glctx.UseProgram(program)

	//gl.Uniform4f(color, 0, 0.5, 0.8, 1)//设置color对象值,设置4个浮点数.
	//offset有两个值X,Y,窗口左上角为(0,0),右下角为(1,1)
	//gl.Uniform4f(offset,5.0,1.0,1.0,1.0 )
	//gl.Uniform2f(offset,offsetx,offsety )//为2参数的uniform变量赋值
	//log.Println("offset:",offsetx,offsety, 0, 0)
	glctx.UniformMatrix4fv(scan, []float32{
		touchLocX/float32(sz.WidthPt)*4 - 2, 0, 0, 0,
		0, touchLocY/float32(sz.HeightPt)*4 - 2, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 1,
	})
	/*glVertexAttribPointer 指定了渲染时索引值为 index 的顶点属性数组的数据格式和位置。调用gl.vertexAttribPointer()方法,
		把顶点着色器中某个属性相对应的通用属性索引连接到绑定的webGLBUffer对象上。
	index 指定要修改的顶点属性的索引值
	size    指定每个顶点属性的组件数量。必须为1、2、3或者4。初始值为4。(如position是由3个(x,y,z)组成,而颜色是4个(r,g,b,a))
	type    指定数组中每个组件的数据类型。可用的符号常量有GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT,GL_UNSIGNED_SHORT, GL_FIXED,
		和 GL_FLOAT,初始值为GL_FLOAT。
	normalized  指定当被访问时,固定点数据值是否应该被归一化(GL_TRUE)或者直接转换为固定点值(GL_FALSE)。
	stride  指定连续顶点属性之间的偏移量。如果为0,那么顶点属性会被理解为:它们是紧密排列在一起的。初始值为0。
	pointer 指定第一个组件在数组的第一个顶点属性中的偏移量。该数组与GL_ARRAY_BUFFER绑定,储存于缓冲区中。初始值为0;
	*/

	glctx.BindBuffer(gl.ARRAY_BUFFER, positionbuf)
	glctx.EnableVertexAttribArray(position)
	defer glctx.DisableVertexAttribArray(position)//必须全部缓存导入完成后再关闭
	glctx.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0) //导入position缓存
	glctx.DrawArrays(gl.TRIANGLES, 0, vertexCount)

	glctx.BindBuffer(gl.ARRAY_BUFFER, colorbuf)
	glctx.EnableVertexAttribArray(color)
	defer glctx.DisableVertexAttribArray(color)//必须全部缓存导入完成后再关闭
	glctx.VertexAttribPointer(color, colorsPerVertex, gl.FLOAT, false, 0, 0) //导入color缓存
	glctx.DrawArrays(gl.TRIANGLES, 0, vertexCount)



	fps.Draw(sz)
}
Example #10
0
func ParticleEmitter(glctx gl.Context, origin mgl.Vec3, num int, rate float32) Emitter {
	bufSize := num * particleLen * vecSize
	vbo := glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, vbo)
	glctx.BufferInit(gl.ARRAY_BUFFER, bufSize, gl.DYNAMIC_DRAW)

	return &particleEmitter{
		glctx:     glctx,
		VBO:       vbo,
		origin:    origin,
		rate:      rate,
		particles: make([]*particle, 0, num),
		num:       num,
	}
}
Example #11
0
// PaintKey will paint the key 1) its own color or 2) red if they key is pressed.
func (k *PianoKey) PaintKey(glctx gl.Context, frameData util.FrameData) {
	glctx.BindBuffer(gl.ARRAY_BUFFER, k.glBuf)
	glctx.VertexAttribPointer(frameData.Position, coordsPerVertex, gl.FLOAT, false, 0, 0)
	if k.pressed {
		// Paint Red if pressed
		glctx.Uniform4f(frameData.Color, 1, 0, 0, 1)
	} else {
		// Paint white if not pressed
		glctx.Uniform4f(frameData.Color, k.keyColor.Red, k.keyColor.Green, k.keyColor.Blue, 1)
	}
	if frameData.Orientation == util.Portrait {
		glctx.DrawArrays(gl.TRIANGLES, 6, vertexCount)
	} else if frameData.Orientation == util.Landscape {
		glctx.DrawArrays(gl.TRIANGLES, 0, vertexCount)
	}
}
Example #12
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)
}
Example #13
0
func onPaint(glctx gl.Context, sz size.Event) {
	glctx.ClearColor(1, 0, 0, 1)
	glctx.Clear(gl.COLOR_BUFFER_BIT)
	glctx.UseProgram(program)
	green += 0.01
	if green > 1 {
		green = 0
	}
	glctx.Uniform4f(color, 0, green, 0, 1)
	glctx.Uniform2f(offset, touchX/float32(sz.WidthPx), touchY/float32(sz.HeightPx))
	glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
	glctx.EnableVertexAttribArray(position)
	glctx.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0)
	glctx.DrawArrays(gl.TRIANGLES, 0, vertexCount)
	glctx.DisableVertexAttribArray(position)
	fps.Draw(sz)
}
Example #14
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
}
Example #15
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()
}
Example #16
0
func (wf *Waveform) Paint(ctx gl.Context, xps, yps, width, height float32) {
	// TODO this is racey and samples can be in the middle of changing
	// move the slice copy to Prepare and sync with playback, or feed over chan
	// TODO assumes mono

	var (
		xstep float32 = width / float32(len(wf.samples))
		xpos  float32 = xps
	)

	for i, x := range wf.samples {
		// clip
		if x > 1 {
			x = 1
		} else if x < -1 {
			x = -1
		}

		wf.verts[i*3] = float32(xpos)
		wf.verts[i*3+1] = yps + (height * float32((x+1)/2))
		wf.verts[i*3+2] = 0
		xpos += xstep
	}

	for i, x := range wf.verts {
		u := math.Float32bits(x)
		wf.data[4*i+0] = byte(u >> 0)
		wf.data[4*i+1] = byte(u >> 8)
		wf.data[4*i+2] = byte(u >> 16)
		wf.data[4*i+3] = byte(u >> 24)
	}

	ctx.UseProgram(wf.program)
	ctx.Uniform4f(wf.color, 1, 1, 1, 1)

	// update hw buf and draw
	ctx.BindBuffer(gl.ARRAY_BUFFER, wf.buf)
	ctx.EnableVertexAttribArray(wf.position)
	ctx.VertexAttribPointer(wf.position, 3, gl.FLOAT, false, 0, 0)
	ctx.BufferSubData(gl.ARRAY_BUFFER, 0, wf.data)
	ctx.DrawArrays(gl.LINE_STRIP, 0, len(wf.samples))
	ctx.DisableVertexAttribArray(wf.position)
}
Example #17
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)
}
Example #18
0
// This is the main rendering call that updates the current scene and all children in the scene
func appPaint(glctx gl.Context, sz size.Event, delta float32) {
	glctx.ClearColor(currentScene.BGColor.R, currentScene.BGColor.G, currentScene.BGColor.B, currentScene.BGColor.A)
	glctx.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	for _, child := range currentScene.Children {
		child.act(delta)
		child.draw(tempBatch, 1.0)
		if len(InputChannel) > 0 {
			for e := range InputChannel {
				if child.Input != nil {
					child.Input(child, e)
				}
				if len(InputChannel) == 0 {
					break
				}
			}
		}
	}

	glctx.UseProgram(program)

	green += 0.01
	if green > 1 {
		green = 0
	}
	glctx.Uniform4f(color, 0, green, 0, 1)

	glctx.Uniform2f(offset, touchX/float32(sz.WidthPx), touchY/float32(sz.HeightPx))

	glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
	glctx.EnableVertexAttribArray(position)
	glctx.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0)
	glctx.DrawArrays(gl.TRIANGLES, 0, vertexCount)
	glctx.DisableVertexAttribArray(position)

	fps.Draw(sz)
}
Example #19
0
func (btn *Button) Paint(ctx gl.Context) {
	for i, x := range btn.verts {
		u := math.Float32bits(x)
		btn.data[4*i+0] = byte(u >> 0)
		btn.data[4*i+1] = byte(u >> 8)
		btn.data[4*i+2] = byte(u >> 16)
		btn.data[4*i+3] = byte(u >> 24)
	}

	ctx.UseProgram(btn.program)
	if btn.active {
		ctx.Uniform4f(btn.color, btn.r, btn.g, btn.b, btn.a)
	} else {
		ctx.Uniform4f(btn.color, 0.4, 0.4, 0.4, 0.5)
	}

	// update hw buf and draw
	ctx.BindBuffer(gl.ARRAY_BUFFER, btn.buf)
	ctx.EnableVertexAttribArray(btn.position)
	ctx.VertexAttribPointer(btn.position, 3, gl.FLOAT, false, 0, 0)
	ctx.BufferSubData(gl.ARRAY_BUFFER, 0, btn.data)
	ctx.DrawArrays(gl.TRIANGLES, 0, len(btn.verts))
	ctx.DisableVertexAttribArray(btn.position)
}
Example #20
0
func (buf *floatBuffer) Bind(ctx gl.Context) { ctx.BindBuffer(gl.ARRAY_BUFFER, buf.Buffer) }
Example #21
0
func (buf *uintBuffer) Bind(ctx gl.Context) { ctx.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, buf.Buffer) }
Example #22
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??
}
Example #23
0
func (buf Buffer) Bind(ctx gl.Context, after ...func(gl.Context)) {
	ctx.BindBuffer(buf.target, buf.Buffer)
	for _, fn := range after {
		fn(ctx)
	}
}