Beispiel #1
0
func drawChunk(chunkX, chunkY int64) {
	chunk := terrain.GetChunkAt(chunkX, chunkY)

	if chunk.DisplayList == 0 {
		chunk.DisplayList = gl.GenLists(1)

		gl.NewList(chunk.DisplayList, gl.COMPILE)
		for x := 0; x < config.ChunkArraySize()-1; x++ {
			for y := 0; y < config.ChunkArraySize()-1; y++ {
				gl.Normal3d(chunk.Normals.Get(x, y))
				gl.Vertex3d(chunk.Vertices.Get(x, y))
				x++

				gl.Normal3d(chunk.Normals.Get(x, y))
				gl.Vertex3d(chunk.Vertices.Get(x, y))
				y++

				gl.Normal3d(chunk.Normals.Get(x, y))
				gl.Vertex3d(chunk.Vertices.Get(x, y))
				x--

				gl.Normal3d(chunk.Normals.Get(x, y))
				gl.Vertex3d(chunk.Vertices.Get(x, y))
				y--
			}
		}
		gl.EndList()
	}

	gl.CallList(chunk.DisplayList)
}
Beispiel #2
0
func gear(inner_radius, outer_radius, width float64, teeth int, tooth_depth float64) {
	var i int
	var r0, r1, r2 float64
	var angle, da float64
	var u, v, len float64

	r0 = inner_radius
	r1 = outer_radius - tooth_depth/2.0
	r2 = outer_radius + tooth_depth/2.0

	da = 2.0 * math.Pi / float64(teeth) / 4.0

	gl.ShadeModel(gl.FLAT)

	gl.Normal3d(0.0, 0.0, 1.0)

	/* draw front face */
	gl.Begin(gl.QUAD_STRIP)
	for i = 0; i <= teeth; i++ {
		angle = float64(i) * 2.0 * math.Pi / float64(teeth)
		gl.Vertex3d(r0*math.Cos(angle), r0*math.Sin(angle), width*0.5)
		gl.Vertex3d(r1*math.Cos(angle), r1*math.Sin(angle), width*0.5)
		if i < teeth {
			gl.Vertex3d(r0*math.Cos(angle), r0*math.Sin(angle), width*0.5)
			gl.Vertex3d(r1*math.Cos(angle+3*da), r1*math.Sin(angle+3*da), width*0.5)
		}
	}
	gl.End()

	/* draw front sides of teeth */
	gl.Begin(gl.QUADS)
	da = 2.0 * math.Pi / float64(teeth) / 4.0
	for i = 0; i < teeth; i++ {
		angle = float64(i) * 2.0 * math.Pi / float64(teeth)

		gl.Vertex3d(r1*math.Cos(angle), r1*math.Sin(angle), width*0.5)
		gl.Vertex3d(r2*math.Cos(angle+da), r2*math.Sin(angle+da), width*0.5)
		gl.Vertex3d(r2*math.Cos(angle+2*da), r2*math.Sin(angle+2*da), width*0.5)
		gl.Vertex3d(r1*math.Cos(angle+3*da), r1*math.Sin(angle+3*da), width*0.5)
	}
	gl.End()

	gl.Normal3d(0.0, 0.0, -1.0)

	/* draw back face */
	gl.Begin(gl.QUAD_STRIP)
	for i = 0; i <= teeth; i++ {
		angle = float64(i) * 2.0 * math.Pi / float64(teeth)
		gl.Vertex3d(r1*math.Cos(angle), r1*math.Sin(angle), -width*0.5)
		gl.Vertex3d(r0*math.Cos(angle), r0*math.Sin(angle), -width*0.5)
		if i < teeth {
			gl.Vertex3d(r1*math.Cos(angle+3*da), r1*math.Sin(angle+3*da), -width*0.5)
			gl.Vertex3d(r0*math.Cos(angle), r0*math.Sin(angle), -width*0.5)
		}
	}
	gl.End()

	/* draw back sides of teeth */
	gl.Begin(gl.QUADS)
	da = 2.0 * math.Pi / float64(teeth) / 4.0
	for i = 0; i < teeth; i++ {
		angle = float64(i) * 2.0 * math.Pi / float64(teeth)

		gl.Vertex3d(r1*math.Cos(angle+3*da), r1*math.Sin(angle+3*da), -width*0.5)
		gl.Vertex3d(r2*math.Cos(angle+2*da), r2*math.Sin(angle+2*da), -width*0.5)
		gl.Vertex3d(r2*math.Cos(angle+da), r2*math.Sin(angle+da), -width*0.5)
		gl.Vertex3d(r1*math.Cos(angle), r1*math.Sin(angle), -width*0.5)
	}
	gl.End()

	/* draw outward faces of teeth */
	gl.Begin(gl.QUAD_STRIP)
	for i = 0; i < teeth; i++ {
		angle = float64(i) * 2.0 * math.Pi / float64(teeth)

		gl.Vertex3d(r1*math.Cos(angle), r1*math.Sin(angle), width*0.5)
		gl.Vertex3d(r1*math.Cos(angle), r1*math.Sin(angle), -width*0.5)
		u = r2*math.Cos(angle+da) - r1*math.Cos(angle)
		v = r2*math.Sin(angle+da) - r1*math.Sin(angle)
		len = math.Sqrt(u*u + v*v)
		u /= len
		v /= len
		gl.Normal3d(v, -u, 0.0)
		gl.Vertex3d(r2*math.Cos(angle+da), r2*math.Sin(angle+da), width*0.5)
		gl.Vertex3d(r2*math.Cos(angle+da), r2*math.Sin(angle+da), -width*0.5)
		gl.Normal3d(math.Cos(angle), math.Sin(angle), 0.0)
		gl.Vertex3d(r2*math.Cos(angle+2*da), r2*math.Sin(angle+2*da), width*0.5)
		gl.Vertex3d(r2*math.Cos(angle+2*da), r2*math.Sin(angle+2*da), -width*0.5)
		u = r1*math.Cos(angle+3*da) - r2*math.Cos(angle+2*da)
		v = r1*math.Sin(angle+3*da) - r2*math.Sin(angle+2*da)
		gl.Normal3d(v, -u, 0.0)
		gl.Vertex3d(r1*math.Cos(angle+3*da), r1*math.Sin(angle+3*da), width*0.5)
		gl.Vertex3d(r1*math.Cos(angle+3*da), r1*math.Sin(angle+3*da), -width*0.5)
		gl.Normal3d(math.Cos(angle), math.Sin(angle), 0.0)
	}

	gl.Vertex3d(r1*math.Cos(0), r1*math.Sin(0), width*0.5)
	gl.Vertex3d(r1*math.Cos(0), r1*math.Sin(0), -width*0.5)

	gl.End()

	gl.ShadeModel(gl.SMOOTH)

	/* draw inside radius cylinder */
	gl.Begin(gl.QUAD_STRIP)
	for i = 0; i <= teeth; i++ {
		angle = float64(i) * 2.0 * math.Pi / float64(teeth)
		gl.Normal3d(-math.Cos(angle), -math.Sin(angle), 0.0)
		gl.Vertex3d(r0*math.Cos(angle), r0*math.Sin(angle), -width*0.5)
		gl.Vertex3d(r0*math.Cos(angle), r0*math.Sin(angle), width*0.5)
	}
	gl.End()

}
Beispiel #3
0
func (this *App) Run() {
	fmt.Println("running")

	var done bool
	cam := camera.New()
	cam.Pos = V3(-0, 0, 3)
	cam.Dir = V3(0, -0, -1)

	pos := []float32{5.0, 5.0, 10.0, 0.0}
	gl.Lightfv(gl.LIGHT0, gl.POSITION, pos)
	gl.Enable(gl.LIGHT0)

	elapsed := 0.0
	rotateSpeed := DegToRad(100.0)
	moveSpeed := 3.0
	texture.SetDefaultTexturePaths("res/textures/", ".png")
	texture.Load("ground")
	texture.Load("ground2")
	defer texture.ClearAllData()
	tex1 := texture.Get("ground2")
	//tex2 := texture.Get("ground2")
	model.SetDefaultModelPaths("res/models/", ".obj")
	model.Load("teapot")
	model.Load("cuboid")
	model.Load("sphere")
	model.Load("bunny")
	defer model.ClearAllData()
	mod1 := model.Get("bunny") //*/

	//*
	prog1 := program.New("test", "test")
	defer prog1.Destroy()
	prog := prog1
	println(prog.InfoLog())
	var mView, mProjection, mModel *mat4.Mat4
	mModel = mat4.New() //*/

	done = false
	for !done {
		time1 := time.Now()
		this.win.ClearBuffers()
		this.win.ResetModelViewMatrix()

		//input
		if this.il.KeyDown(glfw.KeyLeft) {
			cam.RotateY(-rotateSpeed * elapsed)
		}
		if this.il.KeyDown(glfw.KeyRight) {
			cam.RotateY(rotateSpeed * elapsed)
		}
		if this.il.KeyDown(glfw.KeyUp) {
			cam.RotateX(-rotateSpeed * elapsed)
		}
		if this.il.KeyDown(glfw.KeyDown) {
			cam.RotateX(rotateSpeed * elapsed)
		}
		if this.il.KeyDown(KeyX) {
			cam.MoveZ(-moveSpeed * elapsed)
		}
		if this.il.KeyDown(KeyC) {
			cam.MoveZ(moveSpeed * elapsed)
		}
		if this.il.KeyPressed(KeyR) {
			prog.Reload()
			println(prog.InfoLog())
		}

		this.win.ApplyCamera(cam)
		//*
		mView = cam.Matrix()
		mProjection = this.win.ProjectionMatrix()
		mModel.SetIdentity()
		prog.SetModelMatrix(mModel)
		prog.SetViewMatrix(mView)
		prog.SetProjectionMatrix(mProjection)
		prog.UniformColor("uColor", Col(1, .1, 0.5, 1))
		prog.UniformMat4("uMat", mModel)
		prog.Use() //*/
		material.White.Use()
		//tex1.Unbind()
		mod1.Render()
		tex1.Bind2(Col(1, 1, 1, 1))
		gl.Begin(gl.TRIANGLES)
		gl.Normal3d(0, 0, 1)
		gl.TexCoord2d(11.0, 10.0)
		gl.Vertex3d(1.0, 0.0, -15.0)
		gl.TexCoord2d(0.5, 11.0)
		gl.Vertex3d(0.0, 1.0, -15.0)
		gl.TexCoord2d(0.0, 0.0)
		gl.Vertex3d(-1.0, 0.0, -15.0)
		gl.TexCoord2d(1.0, 0.0)
		gl.Vertex3d(1.0, 0.0, -5.0)
		gl.TexCoord2d(0.5, 1.0)
		gl.Vertex3d(0.0, 1.0, -5.0)
		gl.TexCoord2d(0.0, 0.0)
		gl.Vertex3d(-1.0, 0.0, -5.0)
		gl.End() //*/
		this.win.Flip()
		done = this.il.MBQuit() || this.il.KeyPressed(glfw.KeyEsc)
		elapsed = time.Since(time1).Seconds()

		printFPS()

	}
}