Example #1
0
// Creates a new system using (temorarily) hard-coded constants
func NewSystem(X, Y int) System {
	numStars := 1
	numPlanets := rand.Int() % 12

	// Make the slice of Planet

	planets := make([]*data.Planet, numPlanets+1)

	// TODO get rid of hard coded constants
	for i := 0; i <= numPlanets; i++ {
		orbitSizeX := rand.Float64()*4000 + 2000
		orbitSizeY := rand.Float64()*4000 + 2000
		orbitSpeed := rand.Float64() / ((orbitSizeX + orbitSizeY) / 10)

		size := gl.Float(rand.Float64() * 100)

		planets[i] = &data.Planet{data.GameObject{PlanetTextures[0], 0, 0, size, size, 0},
			orbitSizeX, orbitSizeY, orbitSpeed, float64(X), float64(Y)}
	}

	// Make the slice of Stars for each system
	stars := make([]*data.Star, numStars+1)

	for i := 0; i <= numStars; i++ {
		stars[i] = &data.Star{data.GameObject{PlanetTextures[0], gl.Float(X), gl.Float(Y), gl.Float(X + 300), gl.Float(Y + 300), 0}}
	}

	return System{X, Y, numStars, numPlanets, planets, stars}
}
Example #2
0
func Cylinder(r, h gl.Float, slices int, hollow, solid bool) {
	res := 2 * math.Pi / float64(slices)
	mode := gl.LINE_LOOP
	if solid {
		mode = gl.QUADS
	}
	gl.Begin(gl.Enum(mode))
	for a := 0.0; a < 2*math.Pi; a += res {
		gl.Normal3f(gl.Float(math.Cos(a)), gl.Float(math.Sin(a)), 0)
		gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), 0)
		gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), h)
		a += res
		gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), h)
		gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), 0)
	}
	gl.End()
	if !hollow {
		// X Y plane
		if h < 0 {
			gl.Normal3f(0, 0, 1)
		} else {
			gl.Normal3f(0, 0, -1)
		}
		Circle(r, slices, solid)
		// Top (or bottom)
		if h < 0 {
			gl.Normal3f(0, 0, -1)
		} else {
			gl.Normal3f(0, 0, 1)
		}
		gl.Translatef(0, 0, h)
		Circle(r, slices, solid)
	}
}
Example #3
0
func drawMatrix(m *[][]Point, xby, yby gl.Float) {
	for x, row := range *m {
		for y, p := range row {
			UseColor(PointColor(p))
			// fmt.Println(p, PointColor(p))
			// gl.Color4f(1, 0, 0, 1)
			gl.Vertex2f(gl.Float(x)*xby, gl.Float(y)*yby)
		}
	}
}
Example #4
0
/* Updates o's position using its movement function.

The movemet function looks something like
	(x,y) = (f(t),g(t)) where for elliptic orbits

	f = Acos(t)
	g = Bsin(t)
*/
func PlanetOrbit(o *data.Planet, t float64) {
	A := o.OrbitParam1
	B := o.OrbitParam2
	C := o.OrbitParam3

	x0 := o.OrbitParam4
	y0 := o.OrbitParam5

	o.AbsMove(gl.Float(A*math.Sin(t*C)+x0), gl.Float(B*math.Cos(t*C)+y0))
}
Example #5
0
func SetUniform4F(shader, variable string, vs []float32) error {
	prog, ok := shader_progs[shader]
	if !ok {
		return shaderError(fmt.Sprintf("Tried to set a uniform in an unknown shader '%s'", shader))
	}
	bvariable := []byte(fmt.Sprintf("%s\x00", variable))
	loc := gl.GetUniformLocation(prog, (*gl.Char)(unsafe.Pointer(&bvariable[0])))
	gl.Uniform4f(loc, gl.Float(vs[0]), gl.Float(vs[1]), gl.Float(vs[2]), gl.Float(vs[3]))
	return nil
}
Example #6
0
func SetUniformV2(shader, variable string, v linear.Vec2) {
	prog, ok := shader_progs[shader]
	if !ok {
		if !warned_names[shader] {
			Warn().Printf("Tried to set a uniform in an unknown shader '%s'", shader)
			warned_names[shader] = true
		}
		return
	}
	bvariable := []byte(fmt.Sprintf("%s\x00", variable))
	loc := gl.GetUniformLocation(prog, (*gl.Char)(unsafe.Pointer(&bvariable[0])))
	gl.Uniform2f(loc, gl.Float(v.X), gl.Float(v.Y))
}
Example #7
0
// SetVariable4f sets a specified variable to the four supplied integers to
// be passed into an effect.
func (effect *Effect) SetVariable4f(variable string, val1 float32, val2 float32, val3 float32, val4 float32) error {

	var currEffect gl.Int
	gl.GetIntegerv(gl.CURRENT_PROGRAM, &currEffect)
	if gl.Uint(currEffect) != effect.program {
		return errors.New("effect is not currently in use")
	}

	effect.checkUniformVariable(variable)

	gl.Uniform4f(effect.uniforms[variable], gl.Float(val1), gl.Float(val2), gl.Float(val3), gl.Float(val4))
	return nil
}
Example #8
0
func Ellipse(rX, rY gl.Float, slices int, solid bool) {
	mode := gl.LINE_LOOP
	if solid {
		mode = gl.POLYGON
	}
	res := 2 * math.Pi / float64(slices)
	gl.Begin(gl.Enum(mode))
	gl.Vertex2f(0, 0)
	for a := 0.0; a < 2*math.Pi; a += res {
		gl.Vertex2f(rX*gl.Float(math.Cos(a)), rY*gl.Float(math.Sin(a)))
	}
	gl.End()
}
Example #9
0
func Box(x, y, z float32, solid bool) {
	mode := gl.LINE_LOOP
	if solid {
		mode = gl.QUADS
	}
	X := gl.Float(x) / 2
	Y := gl.Float(y) / 2
	Z := gl.Float(z) / 2
	gl.Begin(gl.Enum(mode))

	gl.Normal3f(0, 0, 1) // Positive Z Face
	gl.Vertex3f(-X, -Y, Z)
	gl.Vertex3f(X, -Y, Z)
	gl.Vertex3f(X, Y, Z)
	gl.Vertex3f(-X, Y, Z)

	gl.Normal3f(0, 0, -1) // Negative Z Face
	gl.Vertex3f(-X, -Y, -Z)
	gl.Vertex3f(-X, Y, -Z)
	gl.Vertex3f(X, Y, -Z)
	gl.Vertex3f(X, -Y, -Z)

	gl.Normal3f(0, 1, 0) // Positive Y Face
	gl.Vertex3f(-X, Y, -Z)
	gl.Vertex3f(-X, Y, Z)
	gl.Vertex3f(X, Y, Z)
	gl.Vertex3f(X, Y, -Z)

	gl.Normal3f(0, -1, 0) // Negative Y Face
	gl.Vertex3f(-X, -Y, -Z)
	gl.Vertex3f(X, -Y, -Z)
	gl.Vertex3f(X, -Y, Z)
	gl.Vertex3f(-X, -Y, Z)

	gl.Normal3f(1, 0, 0) // Positive X Face
	gl.Vertex3f(X, -Y, -Z)
	gl.Vertex3f(X, Y, -Z)
	gl.Vertex3f(X, Y, Z)
	gl.Vertex3f(X, -Y, Z)

	gl.Normal3f(-1, 0, 0) // Negative X Face
	gl.Vertex3f(-X, -Y, -Z)
	gl.Vertex3f(-X, -Y, Z)
	gl.Vertex3f(-X, Y, Z)
	gl.Vertex3f(-X, Y, -Z)

	gl.End()
}
Example #10
0
func drawScene() {
	fps()
	gl.Clear(gl.COLOR_BUFFER_BIT)

	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	if input.Zoom > 0 {
		gl.Ortho(0, gl.Double(float64(*flagSize)/input.Zoom), gl.Double(float64(*flagSize)/input.Zoom), 0, -1, 1.0)
	}

	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translatef(gl.Float(input.TransX*1), gl.Float(input.TransY*1), 0)
	drawGrid()
	drawCells()
}
Example #11
0
func PointColor(p Point) Color {
	n := gl.Float(float64(p.Intensity) / 10.0)
	/*
	  if p.Color == WHITE {
	    return Color{1.0, 1.0, 1.0, 1}
	  }
	  if p.Color == RED {
	    return Color{1, 1-n, 1-n, 1}
	  }
	  if p.Color == GREEN {
	    return Color{1-n, 1, 1-n, 1}
	  }
	  return Color{1-n, 1-n, 1, 1}
	*/
	if p.Color == WHITE {
		return Color{0, 0, 0, 1}
	}
	if p.Color == RED {
		return Color{n, 0, 0, 1}
	}
	if p.Color == GREEN {
		return Color{0, n, 0, 1}
	}
	return Color{0, 0, n, 1}
}
Example #12
0
func factor(t time.Time, tc0 time.Time, tc1 time.Time) gl.Float {
	num := t.Sub(tc0)
	den := tc1.Sub(tc0)
	res := num.Seconds() / den.Seconds()
	res = math.Max(res, 0)
	res = math.Min(res, 1)
	return gl.Float(res)
}
Example #13
0
func SetUniformV2Array(shader, variable string, vs []linear.Vec2) {
	prog, ok := shader_progs[shader]
	if !ok {
		if !warned_names[shader] {
			Warn().Printf("Tried to set a uniform in an unknown shader '%s'", shader)
			warned_names[shader] = true
		}
		return
	}
	bvariable := []byte(fmt.Sprintf("%s\x00", variable))
	loc := gl.GetUniformLocation(prog, (*gl.Char)(unsafe.Pointer(&bvariable[0])))
	var fs []gl.Float
	for i := range vs {
		fs = append(fs, gl.Float(vs[i].X))
		fs = append(fs, gl.Float(vs[i].Y))
	}
	gl.Uniform2fv(loc, gl.Sizei(len(vs)), (*gl.Float)(unsafe.Pointer(&fs[0])))
}
Example #14
0
func (m *Matrix3) toGLFloat() Matrix3GLFloat {
	var glm Matrix3GLFloat

	for i := range m {
		glm[i] = gl.Float(m[i])
	}

	return glm
}
Example #15
0
func drawCells() {

	if input.Zoom > 0 {
		gl.PointSize(gl.Float(float64(Width) / float64(*flagSize) * input.Zoom))
	}

	gl.Color4f(1, 1, 1, 1)
	gl.Enable(gl.POINT_SMOOTH)
	gl.Begin(gl.POINTS)

	for i, j := 0, 0; i < (life.Size * life.Size); i, j = i+life.Size, j+1 {
		for k, v := range life.Cells[i : i+life.Size] {
			if v {
				gl.Vertex2f(gl.Float(k), gl.Float(j))
			}
		}
	}

	gl.End()
	gl.Disable(gl.POINT_SMOOTH)
}
Example #16
0
// Set the GL modelview matrix to match view position and orientation.
func UpdateViewpos() {
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translatef(gl.Float(Viewpos[0]), gl.Float(Viewpos[1]), gl.Float(Viewpos[2]))
	gl.Rotatef(gl.Float(Rot[0]*2), 1, 0, 0)
	gl.Rotatef(gl.Float(Rot[1]*2), 0, 1, 0)
	gl.Rotatef(gl.Float(Rot[2]*2), 0, 0, 1)
}
Example #17
0
func drawGrid() {
	gl.Color4f(0.2, 0.2, 0.2, 1)
	gl.Begin(gl.LINES)
	for i := -0.5; i < Width; i += 1 {
		gl.Vertex2f(gl.Float(-0.5), gl.Float(i))
		gl.Vertex2f(gl.Float(life.Size), gl.Float(i))

		gl.Vertex2f(gl.Float(i), -0.5)
		gl.Vertex2f(gl.Float(i), gl.Float(life.Size))
	}
	gl.End()
}
Example #18
0
func drawQuad(x, y, w, h int, u, v, u2, v2 float32) {
	gl.Begin(gl.QUADS)

	gl.TexCoord2f(gl.Float(u), gl.Float(v))
	gl.Vertex2i(gl.Int(x), gl.Int(y))

	gl.TexCoord2f(gl.Float(u2), gl.Float(v))
	gl.Vertex2i(gl.Int(x+w), gl.Int(y))

	gl.TexCoord2f(gl.Float(u2), gl.Float(v2))
	gl.Vertex2i(gl.Int(x+w), gl.Int(y+h))

	gl.TexCoord2f(gl.Float(u), gl.Float(v2))
	gl.Vertex2i(gl.Int(x), gl.Int(y+h))

	gl.End()
}
Example #19
0
func renderPts() {
	gl.MatrixMode(gl.MODELVIEW)
	for i := minPt; i <= maxPt; i++ {
		if Pts[i].is == false {
			continue
		}
		pt := &Pts[i]
		gl.PopMatrix()
		gl.PushMatrix()
		gl.Translatef(gl.Float(pt.X), gl.Float(pt.Y), -gl.Float(pt.Z))
		gl.Scalef(gl.Float(pt.R*2), gl.Float(pt.R*2), gl.Float(pt.R*2))
		gl.DrawArrays(gl.QUADS, 0, 24)
	}
}
Example #20
0
func DrawStarTex(t *data.Star) {

	// Find the center point of the texture to rotate around
	xav := (t.X1 + t.X2) / 2
	yav := (t.Y1 + t.Y2) / 2

	//Translate there, rotate, translate back
	gl.MatrixMode(gl.MODELVIEW)
	gl.Translatef(xav, yav, 0)
	gl.Rotatef(gl.Float(t.Theta), 0, 0, 1)
	gl.Translatef(-xav, -yav, 0)

	//Bind our texture to be drawn by id
	gl.Color3f(1, 1, 1)
	gl.Enable(gl.TEXTURE_2D)
	gl.BindTexture(gl.TEXTURE_2D, t.TexId)

	// Draw a rectangle with the texture stretched to the corners
	gl.Begin(gl.QUADS)

	// Stretch the texture to its 4 corners.
	gl.TexCoord2d(0, 0)
	gl.Vertex2f(t.X1, t.Y1)

	gl.TexCoord2d(0, 1)
	gl.Vertex2f(t.X1, t.Y2)

	gl.TexCoord2d(1, 1)
	gl.Vertex2f(t.X2, t.Y2)

	gl.TexCoord2d(1, 0)
	gl.Vertex2f(t.X2, t.Y1)

	gl.End()

	// Unbind the texture in case something else wants to draw
	gl.Disable(gl.TEXTURE_2D)

	// Reset the matrix
	gl.LoadIdentity()

}
Example #21
0
File: poly.go Project: shenyp09/mx3
// Wraps gl.Normal3f
func Normal3f(x, y, z float32) {
	gl.Normal3f(gl.Float(x), gl.Float(y), gl.Float(z))
}
Example #22
0
// SetClearColor sets the color the screen will become after a call to the
// Clear function.
func SetClearColor(r uint8, g uint8, b uint8) {

	gl.ClearColor(gl.Float(r)/255, gl.Float(g)/255, gl.Float(b)/255, 1.0)
}
Example #23
0
func (mr *ModelReader) readModel(path string, m *Model) {

	var num uint16
	mr.readuint16(&num)

	for i := 0; i < int(num); i++ {
		var x, y, z float32
		mr.readfloat32(&x)
		mr.readfloat32(&y)
		mr.readfloat32(&z)

		m.vertices = append(m.vertices, gl.Float(x), gl.Float(y), gl.Float(z))
		//fmt.Printf("vertices %f, %f, %f \n", x, y, z)
	}
	//fmt.Printf("m.vertices size %d \n", len(m.vertices))

	var faces uint16
	mr.readuint16(&faces)
	//fmt.Println("nb faces  : ", faces)

	for i := 0; i < int(faces); i++ {
		var x, y, z uint16
		mr.readuint16(&x)
		mr.readuint16(&y)
		mr.readuint16(&z)

		m.indices = append(m.indices, gl.Uint(x), gl.Uint(y), gl.Uint(z))
		//fmt.Printf("face indices :%d, %d, %d \n", x, y, z)
	}

	var normals_num uint16
	mr.readuint16(&normals_num)

	for i := 0; i < int(normals_num); i++ {
		var x, y, z float32
		mr.readfloat32(&x)
		mr.readfloat32(&y)
		mr.readfloat32(&z)

		m.normals = append(m.normals, gl.Float(x), gl.Float(y), gl.Float(z))
		//fmt.Printf("normal %f, %f, %f \n", x, y, z)
	}

	var uvs_num uint16
	mr.readuint16(&uvs_num)
	//fmt.Printf("uvs num  %d \n", uvs_num)

	m.has_uv = uvs_num > 0

	if m.has_uv {
		for i := 0; i < int(uvs_num); i++ {
			var x, y float32
			mr.readfloat32(&x)
			mr.readfloat32(&y)

			m.uvs = append(m.uvs, gl.Float(x), gl.Float(y))
			//fmt.Printf("uvs %f, %f \n", x, y)
		}
	}

}
Example #24
0
func (w *glfwBackend) SetScreenColor(color RGB) {
	r := gl.Float(float64(color.R) / 255.0)
	g := gl.Float(float64(color.G) / 255.0)
	b := gl.Float(float64(color.B) / 255.0)
	gl.ClearColor(r, g, b, gl.Float(1.0))
}
Example #25
0
File: poly.go Project: shenyp09/mx3
// Wraps gl.Vertex3f
func Vertex3f(x, y, z float32) {
	gl.Vertex3f(gl.Float(x), gl.Float(y), gl.Float(z))
}