Exemplo n.º 1
0
func (s *Space) Step(dt float64) {
	var prevProgram int32
	gl.GetIntegerv(gl.CURRENT_PROGRAM, &prevProgram)
	gl.UseProgram(s.accelProgram)
	gl.BindVertexArray(s.vao)
	gl.BindBufferBase(gl.SHADER_STORAGE_BUFFER, 0, s.vbo)

	gl.BufferData(
		gl.SHADER_STORAGE_BUFFER,
		len(s.Particles)*int(unsafe.Sizeof(gpuParticle{})),
		nil,
		gl.DYNAMIC_COPY)
	{ // send
		mappedParticles, err := sliceMapParticlesBuffer(gl.WRITE_ONLY, len(s.Particles))
		if err != nil {
			panic(err)
		}
		for i, p := range s.Particles {
			mappedParticles[i] = gpuParticle{
				position: [2]float32{float32(p.Position.X), float32(p.Position.Y)},
				mass:     float32(p.Mass),
			}
		}
		gl.UnmapBuffer(gl.SHADER_STORAGE_BUFFER)
	}
	gl.DispatchCompute(uint32(len(s.Particles))/WORK_GROUP_LOCAL_SIZE, 1, 1)
	gl.MemoryBarrier(gl.SHADER_STORAGE_BARRIER_BIT)
	{ // receive
		mappedParticles, err := sliceMapParticlesBuffer(gl.READ_ONLY, len(s.Particles))
		if err != nil {
			panic(err)
		}
		for i, q := range mappedParticles {
			p := &s.Particles[i]
			p.Position = p.Position.Add(p.Velocity.Mul(dt)).Add(
				p.Acceleration.Mul(dt * dt).Mul(0.5))
			oldAcceleration := p.Acceleration
			p.Acceleration = vect.V{
				float64(q.acceleration[0]), float64(q.acceleration[1]),
			}
			p.Velocity = p.Velocity.Add(
				p.Acceleration.Add(oldAcceleration).Mul(0.5).Mul(dt))
		}
		gl.UnmapBuffer(gl.SHADER_STORAGE_BUFFER)
	}

	gl.UseProgram(uint32(prevProgram))
}
Exemplo n.º 2
0
func (s *MainState) Render() error {
	gl.UseProgram(s.program)
	gl.UniformMatrix4fv(s.projectionUniform, 1, false, &s.projection[0])

	/*
		gl.BindVertexArray(s.vao)
		gl.BindBuffer(gl.ARRAY_BUFFER, s.vbo)
	*/
	gl.BufferData(
		gl.ARRAY_BUFFER,
		int(PARTICLE_COUNT_DEFAULT*unsafe.Sizeof([2]float32{})),
		nil,
		gl.STREAM_DRAW)
	data, err := sliceMap2Float32Buffer(gl.WRITE_ONLY, PARTICLE_COUNT_DEFAULT)
	if err != nil {
		return err
	}
	for i := range data {
		p := &s.space.Particles[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)
	data = nil

	positionAttrib := uint32(gl.GetAttribLocation(s.program, gl.Str("position\x00")))
	gl.EnableVertexAttribArray(positionAttrib)
	gl.VertexAttribPointer(positionAttrib, 2, gl.FLOAT, false, 0, gl.PtrOffset(0))

	gl.ClearColor(0, 0, 0, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.DrawArrays(gl.POINTS, 0, PARTICLE_COUNT_DEFAULT)

	return 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
}