Exemplo n.º 1
0
func sliceMap2Float32Buffer(mode uint32, length int) ([][2]float32, error) {
	data := gl.MapBuffer(gl.ARRAY_BUFFER, mode)
	if data == nil {
		return nil, fmt.Errorf("buffer data pointer is nil")
	}
	header := reflect.SliceHeader{
		Data: uintptr(data),
		Len:  length,
		Cap:  length,
	}
	return *((*[][2]float32)(unsafe.Pointer(&header))), nil
}
Exemplo n.º 2
0
func sliceMapParticlesBuffer(mode uint32, length int) ([]gpuParticle, error) {
	data := gl.MapBuffer(gl.SHADER_STORAGE_BUFFER, mode)
	if data == nil {
		return nil, fmt.Errorf("buffer data pointer is nil")
	}
	header := reflect.SliceHeader{
		Data: uintptr(data),
		Len:  length,
		Cap:  length,
	}
	slice := (*[]gpuParticle)(unsafe.Pointer(&header))
	return *slice, nil
}
Exemplo n.º 3
0
func (s *MainState) Render() error {
	program := s.program
	gl.UseProgram(program)

	projectionUniform := gl.GetUniformLocation(program, gl.Str("projection\x00"))
	gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0])

	gl.Flush()
	buffer := s.vertexBuffer
	gl.BindBuffer(gl.ARRAY_BUFFER, buffer)
	gl.BufferData(
		gl.ARRAY_BUFFER,
		int(PARTICLE_COUNT_DEFAULT*unsafe.Sizeof([2]float32{})),
		nil,
		gl.STREAM_DRAW)
	dataPointer := gl.MapBuffer(gl.ARRAY_BUFFER, gl.WRITE_ONLY)
	if dataPointer == nil {
		return fmt.Errorf("buffer data pointer is nil")
	}
	data := (*[PARTICLE_COUNT_DEFAULT][2]float32)(dataPointer)

	gl.ClearColor(0, 0, 0, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)

	for i := range data {
		p := s.space.Particle(i)
		if p == nil {
			return fmt.Errorf("no particle of id %d", i)
		}
		data[i][0] = float32(p.Position.X)
		data[i][1] = float32(p.Position.Y)
	}
	gl.UnmapBuffer(gl.ARRAY_BUFFER)

	gl.DrawArrays(gl.POINTS, 0, PARTICLE_COUNT_DEFAULT)

	return nil
}