Example #1
0
func main() {
	var err error
	if err = glfw.Init(); err != nil {
		log.Fatalf("%v\n", err)
		return
	}

	defer glfw.Terminate()

	if err = glfw.OpenWindow(640, 480, 8, 8, 8, 8, 0, 0, glfw.Windowed); err != nil {
		log.Fatalf("%v\n", err)
		return
	}

	defer glfw.CloseWindow()

	glfw.SetWindowTitle("Draw")
	glfw.SetSwapInterval(1)
	glfw.SetKeyCallback(onKey)
	glfw.SetMouseButtonCallback(onMouseBtn)
	glfw.SetWindowSizeCallback(onResize)

	running = true
	for running && glfw.WindowParam(glfw.Opened) == 1 {
		if mouse[0] != 0 {
			pen.lineTo(glfw.MousePos())
		} else {
			pen.moveTo(glfw.MousePos())
		}

		glfw.SwapBuffers()
	}
}
Example #2
0
func mouseButtonCallback(button, state int) {
	if currExX != -1 || currExY != -1 || mouseLock || len(selectedHex) < 3 {
		return
	}
	x, y := glfw.MousePos()

	if state == glfw.KeyPress {
		switch button {
		case glfw.MouseLeft:
			// fmt.Println(x, y)
			currExX = int(math.Floor((float64(x) - 80) / 33))
			currExY = int(math.Floor((float64(y) - 80 - 19) / 38))
			if currExX%2 == 1 {
				currExY = (y - 80) / 36
			}
			if currExX > 9 || currExY > 8 || currExX < 0 || currExY < 0 {
				currExX = -1
				currExY = -1
				return
			}
			if hexMap[currExX][currExY] == 6 && currExX > 0 && currExX < 9 && currExY > 0 && (currExX%2 == 0 && currExY < 7 || currExX%2 == 1 && currExY < 8) {
				starRotate = true
			}
			timesToRotate = 2
			mouseLock = true
			fmt.Println("Mouse locked")
			// fmt.Println(currExX, currExY)
			// renderHexMap(currExX, currExY)
		}
	}
}
Example #3
0
// colorize changes the color of the quad, currently
// underneath the mouse cursor.
func colorize(px, py int, attr *glh.Attr) {
	// Fetch color data from mesh buffer.
	data := attr.Data().([]uint8)

	// We will be changing the color of the quad under the mouse cursor.
	// So first determine which quad the mouse is currently hovering over.
	mx, my := glfw.MousePos()
	mx = (mx - px) / CellWidth
	my = (my - py) / CellHeight
	index := my*Cols + mx

	// Ignore coordinates outside of the quad grid.
	// Make sure the index targets a valid quad.
	if mx < 0 || mx >= Cols || my < 0 || my >= Rows || index < 0 || index > Cols*Rows-1 {
		selected = -1
		return
	}

	if index == selected {
		return // No change.
	}

	// Set color for newly selected tile.
	if index != -1 {
		newcolor := colors[rng.Int31n(PaletteSize)]
		setColor(data[index*3*4:], newcolor[0], newcolor[1], newcolor[2])

		// Mark color data as stale, so it will be re-committed to the GPU
		// on the next Render call.
		//
		// This is technically only necessary for the RenderBuffer
		// and RenderShader modes, but it is good practice to always
		// use this call once data has changed.
		attr.Invalidate()
	}

	selected = index
}
Example #4
0
// Since go has multiple return values, I just went ahead and made it return the view and perspective matrices (in that order) rather than messing with getter methods
func (c *Camera) ComputeViewPerspective() (mathgl.Mat4f, mathgl.Mat4f) {
	if mathgl.FloatEqual(-1.0, c.time) {
		c.time = glfw.Time()
	}

	currTime := glfw.Time()
	deltaT := currTime - c.time

	xPos, yPos := glfw.MousePos()
	glfw.SetMousePos(width/2.0, height/2.0)

	c.hAngle += mouseSpeed * ((width / 2.0) - float64(xPos))
	c.vAngle += mouseSpeed * ((height / 2.0) - float64(yPos))

	dir := mathgl.Vec3f{
		float32(math.Cos(c.vAngle) * math.Sin(c.hAngle)),
		float32(math.Sin(c.vAngle)),
		float32(math.Cos(c.vAngle) * math.Cos(c.hAngle))}

	right := mathgl.Vec3f{
		float32(math.Sin(c.hAngle - math.Pi/2.0)),
		0.0,
		float32(math.Cos(c.hAngle - math.Pi/2.0))}

	up := right.Cross(dir)

	if glfw.Key(glfw.KeyUp) == glfw.KeyPress || glfw.Key('W') == glfw.KeyPress {
		c.pos = c.pos.Add(dir.Mul(float32(deltaT * speed)))
	}

	if glfw.Key(glfw.KeyDown) == glfw.KeyPress || glfw.Key('S') == glfw.KeyPress {
		c.pos = c.pos.Sub(dir.Mul(float32(deltaT * speed)))
	}

	if glfw.Key(glfw.KeyRight) == glfw.KeyPress || glfw.Key('D') == glfw.KeyPress {
		c.pos = c.pos.Add(right.Mul(float32(deltaT * speed)))
	}

	if glfw.Key(glfw.KeyLeft) == glfw.KeyPress || glfw.Key('A') == glfw.KeyPress {
		c.pos = c.pos.Sub(right.Mul(float32(deltaT * speed)))
	}

	// Adding to the original tutorial, Space goes up
	if glfw.Key(glfw.KeySpace) == glfw.KeyPress {
		c.pos = c.pos.Add(up.Mul(float32(deltaT * speed)))
	}

	// Adding to the original tutorial, left control goes down
	if glfw.Key(glfw.KeyLctrl) == glfw.KeyPress {
		c.pos = c.pos.Sub(up.Mul(float32(deltaT * speed)))
	}

	fov := initialFOV - 5.0*float64(glfw.MouseWheel())

	proj := mathgl.Perspective(fov, 4.0/3.0, 0.1, 100.0)
	view := mathgl.LookAtV(c.pos, c.pos.Add(dir), up)

	c.time = currTime

	return view, proj
}