Esempio n. 1
0
File: 09.go Progetto: nzlov/gogl
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	texture.Bind(gl.TEXTURE_2D)

	for loop = 0; loop < num; loop++ {
		gl.LoadIdentity()                            // 绘制每颗星星之前,重置模型观察矩阵
		gl.Translatef(0.0, 0.0, zoom)                // 深入屏幕里面
		gl.Rotatef(tilt, 1.0, 0.0, 0.0)              // 倾斜视角
		gl.Rotatef(ztilt, 0.0, 0.0, 1.0)             // 倾斜视角
		gl.Rotatef(star[loop].angle, 0.0, 1.0, 0.0)  // 旋转至当前所画星星的角度
		gl.Translatef(star[loop].dist, 0.0, 0.0)     // 沿X轴正向移动
		gl.Rotatef(-star[loop].angle, 0.0, 1.0, 0.0) // 取消当前星星的角度
		gl.Rotatef(-ztilt, 0.0, 0.0, 1.0)            // 取消屏幕倾斜
		gl.Rotatef(-tilt, 1.0, 0.0, 0.0)             // 取消屏幕倾斜
		if twinkle {                                 // 启用闪烁效果
			// 使用byte型数值指定一个颜色
			gl.Color4ub(star[(num-loop)-1].r, star[(num-loop)-1].g, star[(num-loop)-1].b, 255)
			gl.Begin(gl.QUADS) // 开始绘制纹理映射过的四边形
			gl.TexCoord2f(0.0, 0.0)
			gl.Vertex3f(-1.0, -1.0, 0.0)
			gl.TexCoord2f(1.0, 0.0)
			gl.Vertex3f(1.0, -1.0, 0.0)
			gl.TexCoord2f(1.0, 1.0)
			gl.Vertex3f(1.0, 1.0, 0.0)
			gl.TexCoord2f(0.0, 1.0)
			gl.Vertex3f(-1.0, 1.0, 0.0)
			gl.End() // 四边形绘制结束

		}
		gl.Rotatef(spin, 0.0, 0.0, 1.0) // 绕z轴旋转星星
		// 使用byte型数值指定一个颜色
		gl.Color4ub(star[loop].r, star[loop].g, star[loop].b, 255)
		gl.Begin(gl.QUADS) // 开始绘制纹理映射过的四边形
		gl.TexCoord2f(0.0, 0.0)
		gl.Vertex3f(-1.0, -1.0, 0.0)
		gl.TexCoord2f(1.0, 0.0)
		gl.Vertex3f(1.0, -1.0, 0.0)
		gl.TexCoord2f(1.0, 1.0)
		gl.Vertex3f(1.0, 1.0, 0.0)
		gl.TexCoord2f(0.0, 1.0)
		gl.Vertex3f(-1.0, 1.0, 0.0)
		gl.End() // 四边形绘制结束

		spin += 0.01                            // 星星的公转
		star[loop].angle += float32(loop) / num // 改变星星的自转角度
		star[loop].dist -= 0.01                 // 改变星星离中心的距离
		if star[loop].dist < 0.0 {              // 星星到达中心了么
			star[loop].dist += 5 // 往外移5个单位
			//fmt.Println(loop, star[loop].dist)
			star[loop].r = uint8(rand.Int() % 256) // 赋一个新红色分量
			star[loop].g = uint8(rand.Int() % 256) // 赋一个新绿色分量
			star[loop].b = uint8(rand.Int() % 256) // 赋一个新蓝色分量
		}
	}
}
Esempio n. 2
0
func (h *Hex) Render(alpha float32, drawFromCenter bool) {
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	if h.Kind == HexFlower {
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE)
		starTex.Bind(gl.TEXTURE_2D)
	} else {
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)
		hexTex.Bind(gl.TEXTURE_2D)
		gl.GetError()
		var r, g, b uint8
		r = uint8(colors.Colors[h.Kind-1][0])
		g = uint8(colors.Colors[h.Kind-1][1])
		b = uint8(colors.Colors[h.Kind-1][2])
		if alpha < 1 {
			gl.Color4ub(r, g, b, uint8(alpha*255))
		} else {
			gl.Color3ub(r, g, b)
		}
	}
	gl.Begin(gl.QUADS)
	gl.TexCoord2f(0, 0)
	if drawFromCenter {
		gl.Vertex2i(HEX_WIDTH/2, HEX_HEIGHT/2)
	} else {
		gl.Vertex2i(HEX_WIDTH, HEX_HEIGHT)
	}
	gl.TexCoord2f(0, 1)
	if drawFromCenter {
		gl.Vertex2i(HEX_WIDTH/2, -HEX_HEIGHT/2)
	} else {
		gl.Vertex2i(HEX_WIDTH, 0)
	}
	gl.TexCoord2f(1, 1)
	if drawFromCenter {
		gl.Vertex2i(-HEX_WIDTH/2, -HEX_HEIGHT/2)
	} else {
		gl.Vertex2i(0, 0)
	}
	gl.TexCoord2f(1, 0)
	if drawFromCenter {
		gl.Vertex2i(-HEX_WIDTH/2, HEX_HEIGHT/2)
	} else {
		gl.Vertex2i(0, HEX_HEIGHT)
	}
	gl.End()
}
Esempio n. 3
0
// Used in classic render mode.
// Defines vertex colors.
func (a *Attr) color(i int) {
	i *= a.size

	switch a.size {
	case 3:
		switch v := a.data.(type) {
		case []int8:
			gl.Color3b(v[i], v[i+1], v[i+2])
		case []uint8:
			gl.Color3ub(v[i], v[i+1], v[i+2])
		case []int16:
			gl.Color3s(v[i], v[i+1], v[i+2])
		case []int32:
			gl.Color3i(int(v[i]), int(v[i+1]), int(v[i+2]))
		case []float32:
			gl.Color3f(v[i], v[i+1], v[i+2])
		case []float64:
			gl.Color3d(v[i], v[i+1], v[i+2])
		}
	case 4:
		switch v := a.data.(type) {
		case []int8:
			gl.Color4b(v[i], v[i+1], v[i+2], v[i+3])
		case []uint8:
			gl.Color4ub(v[i], v[i+1], v[i+2], v[i+3])
		case []int16:
			gl.Color4s(v[i], v[i+1], v[i+2], v[i+3])
		case []int32:
			gl.Color4i(int(v[i]), int(v[i+1]), int(v[i+2]), int(v[i+3]))
		case []float32:
			gl.Color4f(v[i], v[i+1], v[i+2], v[i+3])
		case []float64:
			gl.Color4d(v[i], v[i+1], v[i+2], v[i+3])
		}
	}
}