예제 #1
0
파일: demo.go 프로젝트: niksaak/goticles
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
}
예제 #2
0
파일: demo.go 프로젝트: niksaak/goticles
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
}