Exemplo n.º 1
0
func (img *glImage) DrawColoredAtXY(x, y int, color [4]float32) {
	// TODO have the state knwon globally somewhere so this does not need to be
	// called all the time
	gl.Enable(gl.TEXTURE_2D)
	gl.BindTexture(gl.TEXTURE_2D, img.id)

	gl.Begin(gl.QUADS)

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.left, img.top)
	gl.Vertex2i(int32(x), int32(y))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.right, img.top)
	gl.Vertex2i(int32(x+img.Width), int32(y))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.right, img.bottom)
	gl.Vertex2i(int32(x+img.Width), int32(y+img.Height))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.left, img.bottom)
	gl.Vertex2i(int32(x), int32(y+img.Height))

	gl.End()
}
func drawRoom(room *Room) {
	h := float64(room.Box.GetAsBox().Height)
	w := float64(room.Box.GetAsBox().Width)
	x1 := float64(0 - w*0.5)
	y1 := float64(0 - h*0.5)
	x2 := float64(0 + w*0.5)
	y2 := float64(0 - h*0.5)
	x3 := float64(0 + w*0.5)
	y3 := float64(0 + h*0.5)
	x4 := float64(0 - w*0.5)
	y4 := float64(0 + h*0.5)

	gl.Begin(gl.POLYGON)
	gl.Color4f(
		room.Color.X(),
		room.Color.Y(),
		room.Color.Z(),
		room.Color.W(),
	)
	gl.Vertex2d(x1, y1)
	gl.Vertex2d(x2, y2)
	gl.Vertex2d(x3, y3)
	gl.Vertex2d(x4, y4)
	gl.End()
	gl.Begin(gl.LINE_LOOP)
	gl.Color4f(.3, .3, 1, .9)
	gl.LineWidth(1.0)
	gl.Vertex2d(x1, y1)
	gl.Vertex2d(x2, y2)
	gl.Vertex2d(x3, y3)
	gl.Vertex2d(x4, y4)
	gl.End()
}
Exemplo n.º 3
0
func glRect(x, y, w, h, r, g, b, a float32) {
	gl.Disable(gl.TEXTURE_2D) // TODO do this once and remember the state
	gl.Begin(gl.QUADS)
	gl.Color4f(r, g, b, a)
	gl.Vertex2f(x, y)
	gl.Color4f(r, g, b, a)
	gl.Vertex2f(x+w, y)
	gl.Color4f(r, g, b, a)
	gl.Vertex2f(x+w, y+h)
	gl.Color4f(r, g, b, a)
	gl.Vertex2f(x, y+h)
	gl.End()
}
Exemplo n.º 4
0
func (m *menu) draw() {
	return // TODO
	gl.Disable(gl.TEXTURE_2D)
	gl.Begin(gl.QUADS)
	gl.Color4f(m.color.r, m.color.g, m.color.b, m.color.a)
	gl.Vertex2f(m.pos.x, m.pos.y)
	gl.Color4f(m.color.r, m.color.g, m.color.b, m.color.a)
	gl.Vertex2f(m.pos.x+m.pos.w, m.pos.y)
	gl.Color4f(m.color.r, m.color.g, m.color.b, m.color.a)
	gl.Vertex2f(m.pos.x+m.pos.w, m.pos.y+m.pos.h)
	gl.Color4f(m.color.r, m.color.g, m.color.b, m.color.a)
	gl.Vertex2f(m.pos.x, m.pos.y+m.pos.h)
	gl.End()
}
Exemplo n.º 5
0
// OpenGL draw function
func draw() {
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.Enable(gl.BLEND)
	gl.Enable(gl.POINT_SMOOTH)
	gl.Enable(gl.LINE_SMOOTH)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.LoadIdentity()

	player := game.Player

	//Transform screen.
	gl.PushMatrix()
	gl.Translatef((1280/2)-float32((player.Body.Position().X)), 0, 0.0)

	gl.Begin(gl.LINES)
	gl.Color3f(.2, .5, .2)
	for _, segment := range game.Level.GetChipmunkSegments() {
		x := segment.GetAsSegment().A.X
		y := segment.GetAsSegment().A.Y
		gl.Vertex3f(float32(x), float32(y), 0)
		x = segment.GetAsSegment().B.X
		y = segment.GetAsSegment().B.Y
		gl.Vertex3f(float32(x), float32(y), 0)
	}
	gl.End()

	gl.Color4f(.9, .1, 1, .9)
	// draw balls
	for _, enemy := range game.Enemies {
		gl.PushMatrix()
		pos := enemy.Body.Position()
		rot := enemy.Body.Angle() * game.DegreeConst
		gl.Translatef(float32(pos.X), float32(pos.Y), 0.0)
		gl.Rotatef(float32(rot), 0, 0, 1)
		drawCircle(float64(enemy.Radius), 60)
		gl.PopMatrix()
	}
	gl.Color4f(.3, .3, 1, .8)
	//Draw Player
	gl.PushMatrix()
	pos := player.Body.Position()
	rot := player.Body.Angle() * game.DegreeConst
	gl.Translatef(float32(pos.X), float32(pos.Y), 0.0)
	gl.Rotatef(float32(rot), 0, 0, 1)
	drawCircle(float64(player.Radius), 60)
	gl.PopMatrix()

	gl.PopMatrix()
}
Exemplo n.º 6
0
// OpenGL draw function
func draw() {
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.Enable(gl.BLEND)
	gl.Enable(gl.POINT_SMOOTH)
	gl.Enable(gl.LINE_SMOOTH)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.LoadIdentity()

	gl.Begin(gl.LINES)
	gl.Color3f(.2, .2, .2)
	for i := range staticLines {
		x := staticLines[i].GetAsSegment().A.X
		y := staticLines[i].GetAsSegment().A.Y
		gl.Vertex3f(float32(x), float32(y), 0)
		x = staticLines[i].GetAsSegment().B.X
		y = staticLines[i].GetAsSegment().B.Y
		gl.Vertex3f(float32(x), float32(y), 0)
	}
	gl.End()

	gl.Color4f(.3, .3, 1, .8)
	// draw balls
	for _, ball := range balls {
		gl.PushMatrix()
		pos := ball.Body.Position()
		rot := ball.Body.Angle() * chipmunk.DegreeConst
		gl.Translatef(float32(pos.X), float32(pos.Y), 0.0)
		gl.Rotatef(float32(rot), 0, 0, 1)
		drawCircle(float64(ballRadius), 60)
		gl.PopMatrix()
	}
}
Exemplo n.º 7
0
func draw() {
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.Enable(gl.BLEND)
	gl.Enable(gl.POINT_SMOOTH)
	gl.Enable(gl.LINE_SMOOTH)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.LoadIdentity()

	//Transform screen to keep player in middle. Added intentation to make obvious the push matrix is like a block
	gl.PushMatrix()
	// gl.Translatef((1280/2)-float32(player.x), 0, 0.0)

	// gl.Begin(gl.LINES)
	// gl.Color3f(.2, .5, .2)
	// for i := range staticLines {
	// 	x := staticLines[i].GetAsSegment().A.X
	// 	y := staticLines[i].GetAsSegment().A.Y
	// 	gl.Vertex3f(float32(x), float32(y), 0)
	// 	x = staticLines[i].GetAsSegment().B.X
	// 	y = staticLines[i].GetAsSegment().B.Y
	// 	gl.Vertex3f(float32(x), float32(y), 0)
	// }
	// gl.End()

	gl.Color4f(player.color_r, player.color_g, player.color_b, player.color_a)

	//Draw Player
	gl.PushMatrix()
	rot := player.rot
	pos_x := player.x
	pos_y := player.y

	gl.Translatef(pos_x, pos_y, 0.0)
	gl.Rotatef(float32(rot), 0, 0, 1)
	drawCircle(float64(BALL_RADIUS), 20)
	gl.PopMatrix()

	//Draw the grapple
	gl.PushMatrix()
	gl.Translatef(player.hook.x_end, player.hook.y_end, 0.0)
	drawCircle(float64(5), 5)
	gl.PopMatrix()

	//Grapple Line
	gl.LineWidth(2.5)
	gl.Color3f(1.0, 0.0, 0.0)
	gl.Begin(gl.LINES)
	gl.Vertex3f(player.x, player.y, 0.0)
	gl.Vertex3f(player.hook.x_end, player.hook.y_end, 0)
	gl.End()

	//Second Pop
	gl.PopMatrix()
}
Exemplo n.º 8
0
func Render() {
	gl.ClearColor(0, 0, 0, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.Color4f(1, 1, 1, 1)
	gl.BindTexture(gl.TEXTURE_2D, game.Texture)

	gl.PushMatrix()
	gl.Translatef(20-float32(game.PlayerX), 15-float32(game.PlayerY), 0)
	gl.Begin(gl.TRIANGLES)
	for i := 0; i < game.Width; i++ {
		for j := 0; j < game.Height; j++ {
			renderTile(game.Tiles[(j*game.Width)+i], float32(i), float32(j))
		}
	}
	// Player
	gl.Color4f(1, 0, 0, 1)
	renderTile(4, float32(game.PlayerX), float32(game.PlayerY))
	gl.End()
	gl.PopMatrix()
}
Exemplo n.º 9
0
func (e *Element) Render() {

	gl.Color4f(e.color.R, e.color.G, e.color.B, e.color.A)
	gl.Begin(gl.QUADS)

	gl.Vertex3f(e.x, e.y, 0)
	gl.Vertex3f(e.x, e.y+e.height, 0)
	gl.Vertex3f(e.x+e.width, e.y+e.height, 0)
	gl.Vertex3f(e.x+e.width, e.y, 0)

	gl.End()
}
Exemplo n.º 10
0
func (o openGLCanvas) FillRect(what color.Color, where image.Rectangle) {
	return //this does not work at all... makes the whole image red
	drawX := float32(where.Min.X) * o.scaleX
	drawY := float32(where.Min.Y) * o.scaleY
	endX := float32(where.Max.X) * o.scaleX
	endY := float32(where.Max.Y) * o.scaleY
	r, g, b, a := what.RGBA()
	gl.Color4f(float32(r)/0xFF, float32(g)/0xFF, float32(b)/0xFF, float32(a)/0xFF)
	gl.Begin(gl.QUADS)
	{
		gl.Vertex3f(drawX, drawY, 0)
		gl.Vertex3f(drawX, endX, 0)
		gl.Vertex3f(endX, endY, 0)
		gl.Vertex3f(endX, drawY, 0)
	}
	gl.End()
}
Exemplo n.º 11
0
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translatef(0, 0, -3.0)
	gl.Rotatef(rotationX, 1, 0, 0)
	gl.Rotatef(rotationY, 0, 1, 0)

	rotationX += 0.5
	rotationY += 0.5

	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.Color4f(1, 1, 1, 1)

	gl.Begin(gl.QUADS)

	gl.Normal3f(0, 0, 1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, -1, 1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, -1, 1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, 1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, 1)

	gl.Normal3f(0, 0, -1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, -1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, 1, -1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, 1, -1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, -1)

	gl.Normal3f(0, 1, 0)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, -1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, 1, 1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, 1, 1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, -1)

	gl.Normal3f(0, -1, 0)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, -1, -1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, -1, -1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, 1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, 1)

	gl.Normal3f(1, 0, 0)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, -1, -1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, -1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, 1, 1)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, 1)

	gl.Normal3f(-1, 0, 0)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, -1, -1)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, 1)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, 1, 1)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, -1)

	gl.End()
}
Exemplo n.º 12
0
func MakeRect(X, Y, Width, Height int32) Rect {
	rect := Rect{X, Y, Width, Height, MakeColor(255, 255, 255), NumPropertySet{0, 0, 0, 0}}
	x := rect.X
	y := rect.Y
	width := rect.X2
	height := rect.Y2
	radius := rect.BorderRadius.LEFT

	//TODO edit corner radius builder to use NumPropertySet for each corner
	if radius > 0 {
		gl.Color4f(1, 0, 0, 0.5)
		gl.Begin(gl.QUADS)

		gl.Vertex2i(x+radius, y+height)         //4
		gl.Vertex2i(x+(width-radius), y+height) //5

		gl.Vertex2i(x+(width-radius), y) //10
		gl.Vertex2i(x+radius, y)         //11

		gl.End()

		gl.Begin(gl.QUADS)
		gl.Vertex2i(x, y+radius)                 //1
		gl.Vertex2i(x, y+(height-radius))        //2
		gl.Vertex2i(x+radius, y+(height-radius)) //3
		gl.Vertex2i(x+radius, y+radius)          //12
		gl.End()

		gl.Begin(gl.QUADS)
		gl.Vertex2i(x+(width-radius), y+(height-radius)) //6
		gl.Vertex2i(x+width, y+(height-radius))          //7
		gl.Vertex2i(x+width, y+radius)                   //8
		gl.Vertex2i(x+(width-radius), y+radius)          //9
		gl.End()

		gl.Begin(gl.TRIANGLE_FAN)
		orginx := x
		orginy := y

		x = orginx + radius
		y = orginy + radius

		//gl.Translatef(float32(-x), float32(-y), 0)
		var triangleAmount = float64(48.0) //# of triangles used to draw circle
		//GLfloat radius = 0.8f; //radius
		twicePi := float64(0.5) * float64(3.14)
		gl.Vertex2f(float32(x), float32(y)) // center of circle
		for i := float64(144); i >= triangleAmount+48; i-- {

			gl.Vertex2f(float32(float64(x)+(float64(radius)*Cos(i*twicePi/triangleAmount))), float32(float64(y)+(float64(radius)*Sin(i*twicePi/triangleAmount))))
		}

		gl.End()

		y = orginy + height - radius

		gl.Begin(gl.TRIANGLE_FAN)
		gl.Vertex2f(float32(x), float32(y)) // center of circle
		for i := float64(96); i >= triangleAmount; i-- {

			gl.Vertex2f(float32(float64(x)+(float64(radius)*Cos(i*twicePi/triangleAmount))), float32(float64(y)+(float64(radius)*Sin(i*twicePi/triangleAmount))))
		}
		gl.End()

		x = orginx + width - radius
		gl.Begin(gl.TRIANGLE_FAN)
		gl.Vertex2f(float32(x), float32(y)) // center of circle
		for i := float64(0); i <= triangleAmount; i++ {

			gl.Vertex2f(float32(float64(x)+(float64(radius)*Cos(i*twicePi/triangleAmount))), float32(float64(y)+(float64(radius)*Sin(i*twicePi/triangleAmount))))
		}
		gl.End()

		y = orginy + radius

		gl.Begin(gl.TRIANGLE_FAN)
		gl.Vertex2f(float32(x), float32(y)) // center of circle
		for i := float64(192); i >= triangleAmount+96; i-- {

			gl.Vertex2f(float32(float64(x)+(float64(radius)*Cos(i*twicePi/triangleAmount))), float32(float64(y)+(float64(radius)*Sin(i*twicePi/triangleAmount))))
		}
		gl.End()
	} else {
		gl.Begin(gl.QUADS)
		gl.Vertex2i(x, y)
		gl.Vertex2i(x, y+height)
		gl.Vertex2i(x+width, y+height)
		gl.Vertex2i(x+width, y)
		gl.End()
	}

	// if rect.Stroke.BOTTOM.Width < 1 || rect.Stroke.LEFT.Width < 1 || rect.Stroke.RIGHT.Width < 1 || rect.Stroke.TOP.Width < 1 {
	// 	//BOTTOM
	// 	if rect.Stroke.BOTTOM.Width < 1 {
	// 		gl.Begin(gl.LINE)
	// 		gl.Color4f(float32(float32(rect.Stroke.BOTTOM.Fill.R)*0.0039), float32(float32(rect.Stroke.BOTTOM.Fill.G)*0.0039), float32(float32(rect.Stroke.BOTTOM.Fill.B)*0.0039), float32(float32(rect.Stroke.BOTTOM.Fill.A)*0.0039))
	// 		gl.Vertex2i(x, y+height)
	// 		gl.Vertex2i(x+width, y+height)
	// 		gl.End()
	// 	} else if rect.Stroke.LEFT.Width < 1 {
	// 		gl.Begin(gl.LINE)
	// 		gl.Color4f(float32(float32(rect.Stroke.LEFT.Fill.R)*0.0039), float32(float32(rect.Stroke.LEFT.Fill.G)*0.0039), float32(float32(rect.Stroke.LEFT.Fill.B)*0.0039), float32(float32(rect.Stroke.LEFT.Fill.A)*0.0039))
	// 		gl.Vertex2i(x, y)
	// 		gl.Vertex2i(x, y+height)
	// 		gl.End()
	// 	} else if rect.Stroke.TOP.Width < 1 {
	// 		gl.Begin(gl.LINE)
	// 		gl.Color4f(float32(float32(rect.Stroke.TOP.Fill.R)*0.0039), float32(float32(rect.Stroke.TOP.Fill.G)*0.0039), float32(float32(rect.Stroke.TOP.Fill.B)*0.0039), float32(float32(rect.Stroke.TOP.Fill.A)*0.0039))
	// 		gl.Vertex2i(x, y)
	// 		gl.Vertex2i(x+width, y)
	// 		gl.End()
	// 	} else if rect.Stroke.RIGHT.Width < 1 {
	// 		gl.Begin(gl.LINE)
	// 		gl.Color4f(float32(float32(rect.Stroke.RIGHT.Fill.R)*0.0039), float32(float32(rect.Stroke.RIGHT.Fill.G)*0.0039), float32(float32(rect.Stroke.RIGHT.Fill.B)*0.0039), float32(float32(rect.Stroke.RIGHT.Fill.A)*0.0039))
	// 		gl.Vertex2i(x+width, y)
	// 		gl.Vertex2i(x+width, y+height)
	// 		gl.End()
	// 	}

	// 	//LEFT

	// 	//TOP

	// 	//RIGHT
	// 	gl.Begin(gl.LINES)
	// 	gl.Vertex2i(x, y)
	// 	gl.Vertex2i(x, y+height)
	// 	gl.Vertex2i(x+width, y+height)
	// 	gl.Vertex2i(x+width, y)
	// }

	return rect
}
Exemplo n.º 13
0
func MakeColorRGBA(r, g, b, a byte) Color {
	color := Color{r, g, b, a}
	gl.Color4f(float32(float32(color.R)*0.0039), float32(float32(color.G)*0.0039), float32(float32(color.B)*0.0039), float32(float32(color.A)*0.0039))
	return color
}
Exemplo n.º 14
0
func Color(r, g, b byte) XColor {
	color := XColor{r, g, b, 255}
	gl.Color4f(float32(float32(color.R)*0.0039), float32(float32(color.G)*0.0039), float32(float32(color.B)*0.0039), float32(float32(color.A)*0.0039))
	return color
}
// OpenGL draw function
func draw(window *glfw.Window) {
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.Enable(gl.BLEND)
	gl.Enable(gl.POINT_SMOOTH)
	gl.Enable(gl.LINE_SMOOTH)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.LoadIdentity()

	gl.PushMatrix()

	gl.Disable(gl.LIGHTING)

	width, height := window.GetSize()
	x := float64(width)
	y := float64(height)
	h := 0

	gl.Color4f(.1, .1, .1, .8)
	gl.LineWidth(1.0)

	// x方向
	var x0, x1, y0, y1 float64
	var deltaX, deltaY float64
	d := width / 2

	x0 = -x
	x1 = -x
	y0 = -y
	y1 = y
	deltaX = ((2 * x) / float64(d))

	for i := 0; i < d; i++ {
		x0 = x0 + deltaX
		gl.Begin(gl.LINES)
		gl.Vertex3f(float32(x0), float32(y0), float32(h))
		gl.Vertex3f(float32(x0), float32(y1), float32(h))
		gl.End()
	}

	// y方向
	x0 = -x
	x1 = x
	deltaY = ((2 * y) / float64(d))

	for i := 0; i < d; i++ {
		y0 = y0 + deltaY
		gl.Begin(gl.LINES)
		gl.Vertex3f(float32(x0), float32(y0), float32(h))
		gl.Vertex3f(float32(x1), float32(y0), float32(h))
		gl.End()
	}

	gl.PopMatrix()

	// draw boxes
	for _, room := range rooms {
		gl.PushMatrix()
		rot := room.Box.Body.Angle() * chipmunk.DegreeConst
		gl.Rotatef(float32(rot), 0, 0, 1.0)
		x := roundm(float64(room.Box.Body.Position().X), 4.0)
		y := roundm(float64(room.Box.Body.Position().Y), 4.0)
		gl.Translated(x, y, 0.0)
		drawRoom(room)
		gl.PopMatrix()
	}
}