コード例 #1
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
}
コード例 #2
0
ファイル: demo.go プロジェクト: niksaak/goticles
	"math/rand"
	"os"
	"runtime"
	"runtime/pprof"
	"unsafe"
)

import (
	"github.com/niksaak/goticles/bnticles"
	"github.com/niksaak/goticles/leapfrog"
	"github.com/niksaak/goticles/rk4"
)

var _ Spacer = rk4.New()
var _ Spacer = bnticles.New()
var _ Spacer = leapfrog.New()

func randFloat() float64 {
	return rand.Float64() - 0.5
}

func randVect() vect.V {
	return vect.V{randFloat(), randFloat()}.Ulen().Mul(rand.Float64())
}

type Spacer interface {
	Particle(id int) *goticles.P
	MkParticle(mass float64) *goticles.P
	Step(dt float64)
}