Exemplo n.º 1
0
Arquivo: 10.go Projeto: nzlov/gogl
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.LoadIdentity() // 重置当前矩阵

	texture.Bind(gl.TEXTURE_2D)

	var x_m, y_m, z_m, u_m, v_m float32
	xtrans := -xpos
	ztrans := -zpos
	ytrans := -walkbias - 0.25
	sceneroty := 360.0 - yrot

	var numtriangles int

	gl.Rotatef(lookupdown, 1.0, 0, 0)
	gl.Rotatef(sceneroty, 0, 1.0, 0)

	gl.Translatef(xtrans, ytrans, ztrans)

	numtriangles = sector1.numtriangles

	// Process Each Triangle
	for loop_m := 0; loop_m < numtriangles; loop_m++ {
		gl.Begin(gl.TRIANGLES)
		gl.Normal3f(0.0, 0.0, 1.0)
		x_m = sector1.triangles[loop_m].vertex[0].x
		y_m = sector1.triangles[loop_m].vertex[0].y
		z_m = sector1.triangles[loop_m].vertex[0].z
		u_m = sector1.triangles[loop_m].vertex[0].u
		v_m = sector1.triangles[loop_m].vertex[0].v
		gl.TexCoord2f(u_m, v_m)
		gl.Vertex3f(x_m, y_m, z_m)

		x_m = sector1.triangles[loop_m].vertex[1].x
		y_m = sector1.triangles[loop_m].vertex[1].y
		z_m = sector1.triangles[loop_m].vertex[1].z
		u_m = sector1.triangles[loop_m].vertex[1].u
		v_m = sector1.triangles[loop_m].vertex[1].v
		gl.TexCoord2f(u_m, v_m)
		gl.Vertex3f(x_m, y_m, z_m)

		x_m = sector1.triangles[loop_m].vertex[2].x
		y_m = sector1.triangles[loop_m].vertex[2].y
		z_m = sector1.triangles[loop_m].vertex[2].z
		u_m = sector1.triangles[loop_m].vertex[2].u
		v_m = sector1.triangles[loop_m].vertex[2].v
		gl.TexCoord2f(u_m, v_m)
		gl.Vertex3f(x_m, y_m, z_m)
		gl.End()
	}
}
Exemplo n.º 2
0
// normal defines surface normals.
// Used in classic render mode.
func (a *Attr) normal(i int) {
	if a.size != 3 {
		return
	}

	i *= a.size

	switch v := a.data.(type) {
	case []int8:
		gl.Normal3b(v[i], v[i+1], v[i+2])
	case []int16:
		gl.Normal3s(v[i], v[i+1], v[i+2])
	case []int32:
		gl.Normal3i(int(v[i]), int(v[i+1]), int(v[i+2]))
	case []float32:
		gl.Normal3f(v[i], v[i+1], v[i+2])
	case []float64:
		gl.Normal3d(v[i], v[i+1], v[i+2])
	}
}
Exemplo n.º 3
0
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) //清除屏幕和深度缓存
	gl.LoadIdentity()                                   //重置当前的模型观察矩阵

	gl.Translatef(0, 0, z) //移入/移出屏幕 z 个单位

	gl.Rotatef(rotation[0], 1, 0, 0) //绕X轴旋转
	gl.Rotatef(rotation[1], 0, 1, 0) //绕Y轴旋转

	rotation[0] += speed[0]
	rotation[1] += speed[1]

	textures[filter].Bind(gl.TEXTURE_2D) //绑定的纹理

	/*
	   Normal就是法线的意思,所谓法线是指经过面(多边形)上的一点且垂直于这个面(多边形)的直线。
	   使用光源的时候必须指定一条法线。法线告诉OpenGL这个多边形的朝向,并指明多边形的正面和背面。
	   如果没有指定法线,什么怪事情都可能发生:不该照亮的面被照亮了,多边形的背面也被照亮....。对了,法线应该指向多边形的外侧。

	   看着木箱的前面您会注意到法线与Z轴正向同向。这意味着法线正指向观察者-您自己。这正是我们所希望的。
	   对于木箱的背面,也正如我们所要的,法线背对着观察者。
	   如果立方体沿着X或Y轴转个180度的话,前侧面的法线仍然朝着观察者,背面的法线也还是背对着观察者。
	   换句话说,不管是哪个面,只要它朝着观察者这个面的法线就指向观察者。由于光源紧邻观察者,任何时候法线对着观察者时,这个面就会被照亮。
	   并且法线越朝着光源,就显得越亮一些。如果您把观察点放到立方体内部,你就会法线里面一片漆黑。因为法线是向外指的。
	   如果立方体内部没有光源的话,当然是一片漆黑。
	*/
	gl.Begin(gl.QUADS)
	// Front Face 前面
	gl.Normal3f(0, 0, 1) // Normal Pointing Towards Viewer 法线指向观察者
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, -1, 1) // Point 1 (Front)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, -1, 1) // Point 2 (Front)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, 1) // Point 3 (Front)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, 1) // Point 4 (Front)
	// Back Face 后面
	gl.Normal3f(0, 0, -1) // Normal Pointing Away From Viewer 法线背向观察者
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, -1) // Point 1 (Back)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, 1, -1) // Point 2 (Back)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, 1, -1) // Point 3 (Back)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, -1) // Point 4 (Back)
	// Top Face 顶面
	gl.Normal3f(0, 1, 0) // Normal Pointing Up  法线向上
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, -1) // Point 1 (Top)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, 1, 1) // Point 2 (Top)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, 1, 1) // Point 3 (Top)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, -1) // Point 4 (Top)
	// Bottom Face 底面
	gl.Normal3f(0, -1, 0) // Normal Pointing Down 法线朝下
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, -1, -1) // Point 1 (Bottom)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, -1, -1) // Point 2 (Bottom)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, 1) // Point 3 (Bottom)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, 1) // Point 4 (Bottom)
	// Right face 右面
	gl.Normal3f(1, 0, 0) // Normal Pointing Right 法线朝右
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, -1, -1) // Point 1 (Right)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, -1) // Point 2 (Right)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, 1, 1) // Point 3 (Right)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, 1) // Point 4 (Right)
	// Left Face 左面
	gl.Normal3f(-1, 0, 0) // Normal Pointing Left  法线朝左
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, -1, -1) // Point 1 (Left)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, 1) // Point 2 (Left)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, 1, 1) // Point 3 (Left)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, -1)
	gl.End()

	glfw.SwapBuffers()
}
Exemplo n.º 4
0
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.LoadIdentity()

	gl.Translatef(0, 0, z)

	gl.Rotatef(rotation[0], 1, 0, 0)
	gl.Rotatef(rotation[1], 0, 1, 0)

	rotation[0] += speed[0]
	rotation[1] += speed[1]

	textures[filter].Bind(gl.TEXTURE_2D)

	gl.Begin(gl.QUADS)
	// Front Face
	gl.Normal3f(0, 0, 1) // Normal Pointing Towards Viewer
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, -1, 1) // Point 1 (Front)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, -1, 1) // Point 2 (Front)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, 1) // Point 3 (Front)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, 1) // Point 4 (Front)
	// Back Face
	gl.Normal3f(0, 0, -1) // Normal Pointing Away From Viewer
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, -1) // Point 1 (Back)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, 1, -1) // Point 2 (Back)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, 1, -1) // Point 3 (Back)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, -1) // Point 4 (Back)
	// Top Face
	gl.Normal3f(0, 1, 0) // Normal Pointing Up
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, -1) // Point 1 (Top)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, 1, 1) // Point 2 (Top)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, 1, 1) // Point 3 (Top)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, -1) // Point 4 (Top)
	// Bottom Face
	gl.Normal3f(0, -1, 0) // Normal Pointing Down
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, -1, -1) // Point 1 (Bottom)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, -1, -1) // Point 2 (Bottom)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, 1) // Point 3 (Bottom)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, 1) // Point 4 (Bottom)
	// Right face
	gl.Normal3f(1, 0, 0) // Normal Pointing Right
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(1, -1, -1) // Point 1 (Right)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(1, 1, -1) // Point 2 (Right)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(1, 1, 1) // Point 3 (Right)
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(1, -1, 1) // Point 4 (Right)
	// Left Face
	gl.Normal3f(-1, 0, 0) // Normal Pointing Left
	gl.TexCoord2f(0, 0)
	gl.Vertex3f(-1, -1, -1) // Point 1 (Left)
	gl.TexCoord2f(1, 0)
	gl.Vertex3f(-1, -1, 1) // Point 2 (Left)
	gl.TexCoord2f(1, 1)
	gl.Vertex3f(-1, 1, 1) // Point 3 (Left)
	gl.TexCoord2f(0, 1)
	gl.Vertex3f(-1, 1, -1)
	gl.End()

	glfw.SwapBuffers()
}
Exemplo n.º 5
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(rotx, 1, 0, 0)
	gl.Rotatef(roty, 0, 1, 0)

	rotx += 0.5
	roty += 0.5

	texture.Bind(gl.TEXTURE_2D)

	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()
}