예제 #1
0
func New() (s *Space, err error) {
	s = new(Space)
	s.accelProgram, err = loadShader()
	if err != nil {
		return nil, err
	}
	gl.GenVertexArrays(1, &s.vao)
	gl.GenBuffers(1, &s.vbo)

	gl.UseProgram(s.accelProgram)
	gl.BindVertexArray(s.vao)
	gl.BindBufferBase(gl.SHADER_STORAGE_BUFFER, 0, s.vbo)

	return s, nil
}
예제 #2
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))
}
예제 #3
0
파일: demo.go 프로젝트: niksaak/goticles
func (s *MainState) Init(e *engine.E) error {
	// Init Glow
	if err := gl.Init(); err != nil {
		return err
	}

	// Enable debug output
	if glfw3.ExtensionSupported("GL_ARB_debug_output") {
		gl.Enable(gl.DEBUG_OUTPUT_SYNCHRONOUS_ARB)
		gl.DebugMessageCallbackARB(gl.DebugProc(glDebugCallback), nil)
	}

	// Init shaders
	shaderProg := engine.NewShaderProgram()
	if err := shaderProg.ReadShaderFile("vert.glsl", engine.VertexShader); err != nil {
		return err
	}
	if err := shaderProg.ReadShaderFile("geom.glsl", engine.GeometryShader); err != nil {
		return err
	}
	if err := shaderProg.ReadShaderFile("frag.glsl", engine.FragmentShader); err != nil {
		return err
	}
	program, err := shaderProg.Link()
	if err != nil {
		return err
	}
	s.program = program
	gl.UseProgram(program)

	// Manage GL features
	gl.Disable(gl.DEPTH_TEST)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.MULTISAMPLE)
	gl.Enable(gl.POLYGON_SMOOTH)
	gl.Enable(gl.PROGRAM_POINT_SIZE)

	// Init VAO
	gl.GenVertexArrays(1, &s.vao)
	gl.BindVertexArray(s.vao)
	e.Defer(func() {
		gl.DeleteVertexArrays(1, &s.vao)
	})

	s.projectionUniform = gl.GetUniformLocation(program, gl.Str("projection\x00"))

	// Init VBO
	gl.GenBuffers(1, &s.vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, s.vbo)
	e.Defer(func() {
		gl.DeleteBuffers(1, &s.vbo)
	})

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

	// Init space
	space, err := gputicles.New()
	if err != nil {
		return err
	}
	s.space = space
	/*
		for i := 0; i < PARTICLE_COUNT_DEFAULT; i++ {
			particle := space.MkParticle(PARTICLE_MASS_DEFAULT)
			particle.Position = randVect()
			//particle.Velocity = randVect().Div(8)
		}
	*/
	for i := 0; i < PARTICLE_COUNT_DEFAULT/2; i++ {
		particle := space.MkParticle(PARTICLE_MASS_DEFAULT)
		particle.Position = randVect().Div(2).Add(vect.V{-0.5, -0.5})
	}
	for i := PARTICLE_COUNT_DEFAULT / 2; i < PARTICLE_COUNT_DEFAULT; i++ {
		particle := space.MkParticle(PARTICLE_MASS_DEFAULT)
		particle.Position = randVect().Div(2).Add(vect.V{0.5, 0.5})
	}

	return nil
}
예제 #4
0
파일: demo.go 프로젝트: niksaak/goticles
func (s *MainState) Init(eng *engine.E) error {
	println("initializing state")
	// Init Glow
	if err := gl.Init(); err != nil {
		return err
	}

	// Enable debug output
	if glfw3.ExtensionSupported("GL_ARB_debug_output") {
		gl.Enable(gl.DEBUG_OUTPUT_SYNCHRONOUS_ARB)
		gl.DebugMessageCallbackARB(gl.DebugProc(glDebugCallback), nil)
	}

	shaderProg := engine.NewShaderProgram()
	if err := shaderProg.ReadShaderFile("vert.glsl", engine.VertexShader); err != nil {
		return err
	}
	if err := shaderProg.ReadShaderFile("geom.glsl", engine.GeometryShader); err != nil {
		return err
	}
	if err := shaderProg.ReadShaderFile("frag.glsl", engine.FragmentShader); err != nil {
		return err
	}
	program, err := shaderProg.Link()
	if err != nil {
		return err
	}
	s.program = program
	gl.UseProgram(program)

	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	//gl.Enable(gl.DITHER)
	gl.Enable(gl.MULTISAMPLE)
	gl.Enable(gl.POLYGON_SMOOTH)
	gl.Enable(gl.PROGRAM_POINT_SIZE)

	// Init VAO
	var vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.BindVertexArray(vao)
	s.vertexArray = vao
	eng.Defer(func() {
		gl.DeleteVertexArrays(1, &vao)
	})

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

	// Initialize vertex buffer
	var buffer uint32
	gl.GenBuffers(1, &buffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, buffer)
	s.vertexBuffer = buffer
	eng.Defer(func() {
		gl.DeleteBuffers(1, &buffer)
	})

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

	// Initialize space
	space := leapfrog.New()
	for i := 0; i < PARTICLE_COUNT_DEFAULT; i++ {
		particle := space.MkParticle(PARTICLE_MASS_DEFAULT)
		particle.Position = randVect().Div(2)
	}
	s.space = space

	// Deinit is not part of interface, but can be deferred manually:
	eng.Defer(s.Deinit)

	return nil
}