示例#1
0
// TODO the switch number
func (t *SwitchModel) Draw() {
	modelViewBackup := t.modelView
	s := t.sw
	var rotatemv mathgl.Mat4f
	if s.rotate == 0 {
		rotatemv = mathgl.Ident4f()
	} else {
		rotatemv = mathgl.HomogRotate3D(t.sw.rotate, [3]float32{0, 0, 1})
	}
	var scalemv mathgl.Mat4f
	if s.scale == 0 {
		scalemv = mathgl.Ident4f()
	} else {
		scalemv = mathgl.Scale3D(s.scale, s.scale, 0)
	}
	blockmv := scalemv.Mul4(rotatemv)

	// Draw the associated blocks
	// top left block
	t.drawBlock(g.level.blocks[s.line][s.col], blockmv.Mul4(topLeftModelView))
	// top right block
	t.drawBlock(g.level.blocks[s.line][s.col+1], blockmv.Mul4(topRightModelView))
	// bottom right block
	t.drawBlock(g.level.blocks[s.line+1][s.col+1], blockmv.Mul4(bottomRightModelView))
	// bottom left block
	t.drawBlock(g.level.blocks[s.line+1][s.col], blockmv.Mul4(bottomLeftModelView))

	t.ModelBase.Draw()

	t.modelView = modelViewBackup
}
示例#2
0
func newWorld(width, height int) *world {
	// Load the font
	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/freesans.ttf", responseCh)
	response := <-responseCh
	fontBuffer := response.Buffer
	err := response.Error
	if err != nil {
		panic(err)
	}

	w := &world{
		width:      width,
		height:     height,
		projMatrix: mathgl.Ortho2D(0, float32(width), -float32(height/2), float32(height/2)),
		viewMatrix: mathgl.Ident4f(),
	}

	sans, err := gltext.LoadTruetype(bytes.NewBuffer(fontBuffer), w, 40, 32, 127, gltext.LeftToRight)
	if err != nil {
		panic(err)
	}

	w.font = sans

	return w
}
示例#3
0
func newWorld(width, height int) *world {
	return &world{
		width:      width,
		height:     height,
		projMatrix: mathgl.Ortho2D(0, float32(width), -float32(height/2), float32(height/2)),
		viewMatrix: mathgl.Ident4f(),
	}
}
示例#4
0
文件: model.go 项目: remogatto/mozaik
func (t *ModelBase) Init(mode gl.GLenum, vertices []Vertex, vshaderf, fshaderf string) {
	t.mode = mode
	t.vertices = vertices
	t.sizeVertices = len(t.vertices) * sizeVertex

	// Shaders
	t.vshader = compileShader(gl.VERTEX_SHADER, vshaderf)
	t.fshader = compileShader(gl.FRAGMENT_SHADER, fshaderf)
	t.prg = NewProgram(t.vshader, t.fshader)
	t.posLoc = gl.AttribLocation(0)
	t.colLoc = gl.AttribLocation(1)
	t.uniformMVP = t.prg.GetUniformLocation("modelViewProjection")

	// the projection matrix
	t.projection = mathgl.Ident4f()

	// the model view
	t.modelView = mathgl.Ident4f()

	// Create VBO
	t.buffer = gl.GenBuffer()
	t.buffer.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, t.sizeVertices, nil, gl.STATIC_DRAW)
	gl.BufferSubData(gl.ARRAY_BUFFER, 0, t.sizeVertices, t.vertices)
	t.buffer.Unbind(gl.ARRAY_BUFFER)

	// Create VAO
	t.vao = gl.GenVertexArray()
	t.vao.Bind()
	t.buffer.Bind(gl.ARRAY_BUFFER)

	// Attrib vertex data to VAO
	t.posLoc.AttribPointer(4, gl.FLOAT, false, sizeVertex, uintptr(0))
	t.posLoc.EnableArray()
	t.colLoc.AttribPointer(4, gl.FLOAT, false, sizeVertex, uintptr(sizeCoords))
	t.colLoc.EnableArray()

	t.buffer.Unbind(gl.ARRAY_BUFFER)
	t.vao.Unbind()
}
示例#5
0
func NewWorld(width, height int) *World {
	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))

	return &World{
		Width:      width,
		Height:     height,
		objects:    make([]*Cube, 0),
		projection: mathgl.Perspective(60, float32(width)/float32(height), 1, 20),
		view:       mathgl.Ident4f(),
	}
}
示例#6
0
// NewSegment returns a new segment object. It takes a program
// shader and segment coordinates as arguments.
func NewSegment(program shaders.Program, x1, y1, x2, y2 float32) *Segment {

	segment := new(Segment)

	// Set the geometry

	segment.x1, segment.x2 = x1, x2
	segment.y1, segment.y2 = y1, y2

	segment.vertices = []float32{
		segment.x1, segment.y1,
		segment.x2, segment.y2,
	}

	// Set the default color
	segment.SetColor(DefaultColor)

	// Size of the segment bounding box
	// segment.w = float32(math.Abs(float64(x1 - x2)))
	// segment.h = float32(math.Abs(float64(y1 - y2)))
	segment.bounds = image.Rect(int(x1), int(y1), int(x2), int(y2))

	// Center of the segment
	segment.x = (segment.x1 + segment.x2) / 2
	segment.y = (segment.y1 + segment.y2) / 2

	segment.program = program
	segment.program.Use()

	// Get variables IDs from shaders
	segment.posId = segment.program.GetAttribute("pos")
	segment.colorId = segment.program.GetAttribute("color")
	segment.projMatrixId = segment.program.GetUniform("projection")
	segment.modelMatrixId = segment.program.GetUniform("model")
	segment.viewMatrixId = segment.program.GetUniform("view")

	// Fill the model matrix with the identity.
	segment.modelMatrix = mathgl.Ident4f()

	return segment
}
示例#7
0
func newWorld(width, height int) *world {
	world := &world{
		width:      width,
		height:     height,
		projMatrix: mathgl.Ortho2D(0, float32(width), 0, float32(height)),
		viewMatrix: mathgl.Ident4f(),
		space:      chipmunk.NewSpace(),
	}

	world.space.Gravity = vect.Vect{0, Gravity}

	// Initialize the audio player
	var err error
	world.explosionPlayer, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	// Read the PCM audio samples

	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/explosion.pcm", responseCh)
	response := <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.explosionBuffer = response.Buffer

	responseCh = make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/impact.pcm", responseCh)
	response = <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.impactBuffer = response.Buffer

	return world
}
示例#8
0
文件: box.go 项目: remogatto/shapes
// NewBox creates a new box of given sizes. It takes as arguments a
// linked program and width and height values.
func NewBox(program shaders.Program, width, height float32) *Box {

	box := new(Box)

	// The box is built around its center at (0, 0)
	box.vertices = []float32{
		-width / 2, -height / 2,
		width / 2, -height / 2,
		-width / 2, height / 2,
		width / 2, height / 2,
	}

	// Set the default color
	box.SetColor(DefaultColor)

	box.program = program
	box.program.Use()

	// Get variables IDs from shaders
	box.posId = program.GetAttribute("pos")
	box.colorId = program.GetAttribute("color")
	box.projMatrixId = program.GetUniform("projection")
	box.modelMatrixId = program.GetUniform("model")
	box.viewMatrixId = program.GetUniform("view")
	box.texInId = program.GetAttribute("texIn")
	box.textureId = program.GetUniform("texture")
	box.texRatioId = program.GetUniform("texRatio")

	// Fill the model matrix with the identity.
	box.modelMatrix = mathgl.Ident4f()

	// Create the bounding rectangle for the shape.
	box.bounds = image.Rect(
		int(-width/2), int(-height/2),
		int(width/2), int(height/2),
	)

	return box
}
示例#9
0
	vv := float64(SwitchSize / 2)
	for i := float64(0); i <= SwitchSegments; i++ {
		a := 2 * math.Pi * i / SwitchSegments
		vs = append(vs, NewVertex(float32(math.Sin(a)*vv), float32(math.Cos(a)*vv), 0, WhiteColor))
	}
	model.Init(gl.TRIANGLE_FAN, vs, VShaderBasic, FShaderBasic)

	v := SwitchSize / 2
	model.modelView = mathgl.Ortho2D(0, WindowWidth, WindowHeight, 0).Mul4(mathgl.Translate3D(float32(sw.X+v), float32(sw.Y+v), 0))
	return model
}

var (
	topLeftModelView     = mathgl.Translate3D(-BlockSize, -BlockSize, 0)
	topRightModelView    = mathgl.Translate3D(0, -BlockSize, 0)
	bottomRightModelView = mathgl.Ident4f()
	bottomLeftModelView  = mathgl.Translate3D(-BlockSize, 0, 0)
)

// TODO the switch number
func (t *SwitchModel) Draw() {
	modelViewBackup := t.modelView
	s := t.sw
	var rotatemv mathgl.Mat4f
	if s.rotate == 0 {
		rotatemv = mathgl.Ident4f()
	} else {
		rotatemv = mathgl.HomogRotate3D(t.sw.rotate, [3]float32{0, 0, 1})
	}
	var scalemv mathgl.Mat4f
	if s.scale == 0 {
示例#10
0
func NewCube() *Cube {
	cube := new(Cube)
	cube.model = mathgl.Ident4f()

	cube.Vertices = []float32{
		// Front
		1, -1, 1, 1, TEX_COORD_MAX, 0,
		1, 1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, 1, 1, 0, TEX_COORD_MAX,
		-1, -1, 1, 1, 0, 0,
		// Back
		1, 1, -1, 1, TEX_COORD_MAX, 0,
		-1, -1, -1, 1, 0, TEX_COORD_MAX,
		1, -1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, 0,
		// Left
		-1, -1, 1, 1, TEX_COORD_MAX, 0,
		-1, 1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, TEX_COORD_MAX,
		-1, -1, -1, 1, 0, 0,
		// Right
		1, -1, -1, 1, TEX_COORD_MAX, 0,
		1, 1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		1, 1, 1, 1, 0, TEX_COORD_MAX,
		1, -1, 1, 1, 0, 0,
		// Top
		1, 1, 1, 1, TEX_COORD_MAX, 0,
		1, 1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, TEX_COORD_MAX,
		-1, 1, 1, 1, 0, 0,
		// Bottom
		1, -1, -1, 1, TEX_COORD_MAX, 0,
		1, -1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, -1, 1, 1, 0, TEX_COORD_MAX,
		-1, -1, -1, 1, 0, 0,
	}
	cube.indices = []byte{
		// Front
		0, 1, 2,
		2, 3, 0,
		// Back
		4, 5, 6,
		4, 5, 7,
		// Left
		8, 9, 10,
		10, 11, 8,
		// Right
		12, 13, 14,
		14, 15, 12,
		// Top
		16, 17, 18,
		18, 19, 16,
		// Bottom
		20, 21, 22,
		22, 23, 20,
	}

	fragmentShader := (FragmentShader)(`
        precision mediump float;
	varying vec2 texOut;
        uniform sampler2D texture;

	void main() {
		gl_FragColor = texture2D(texture, texOut);
	}
        `)
	vertexShader := (VertexShader)(`
        uniform mat4 model;
        uniform mat4 projection_view;
        attribute vec4 pos;
        attribute vec2 texIn;
        varying vec2 texOut;

        void main() {
          gl_Position = projection_view*model*pos;
          texOut = texIn;
        }
        `)

	fsh := fragmentShader.Compile()
	vsh := vertexShader.Compile()
	cube.Program.Link(fsh, vsh)

	cube.Program.Use()
	cube.attrPos = cube.Program.GetAttribute("pos")
	cube.attrColor = cube.Program.GetAttribute("color")
	cube.attrTexIn = cube.Program.GetAttribute("texIn")
	cube.uniformTexture = cube.Program.GetUniform("texture")

	cube.uniformModel = cube.Program.GetUniform("model")
	cube.uniformProjectionView = cube.Program.GetUniform("projection_view")

	gl.EnableVertexAttribArray(cube.attrPos)
	gl.EnableVertexAttribArray(cube.attrColor)
	gl.EnableVertexAttribArray(cube.attrTexIn)

	return cube
}
示例#11
0
func NewWorld(width, height int) *World {
	world := &World{
		width:      width,
		height:     height,
		projMatrix: mathgl.Ortho2D(0, float32(width), 0, float32(height)),
		viewMatrix: mathgl.Ident4f(),
		space:      chipmunk.NewSpace(),
	}

	world.space.Gravity = vect.Vect{0, Gravity}

	// Initialize the audio players
	var err error
	world.explosionPlayer, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	world.impactPlayer, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	// Read the PCM audio samples

	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/explosion.pcm", responseCh)
	response := <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.explosionBuffer = response.Buffer

	responseCh = make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/impact.pcm", responseCh)
	response = <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.impactBuffer = response.Buffer

	// Compile the shaders

	world.boxProgramShader = shaders.NewProgram(shapes.DefaultBoxFS, shapes.DefaultBoxVS)
	world.segmentProgramShader = shaders.NewProgram(shapes.DefaultSegmentFS, shapes.DefaultSegmentVS)

	// Load the font
	responseCh = make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/freesans.ttf", responseCh)
	response = <-responseCh
	fontBuffer := response.Buffer
	err = response.Error
	if err != nil {
		panic(err)
	}

	world.font, err = gltext.LoadTruetype(bytes.NewBuffer(fontBuffer), world, 12, 32, 127, gltext.LeftToRight)
	if err != nil {
		panic(err)
	}

	return world
}