Exemplo n.º 1
0
func initResources() {
	var err os.Error
	// Load shaders
	vs, err = createShader("triangle.v.glsl", VertexShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}
	fs, err = createShader("triangle.f.glsl", FragmentShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}

	// Create GLSL program with loaded shaders
	program = gl.CreateProgram()
	program.AttachShader(vs)
	program.AttachShader(fs)
	program.Link()
	infoLog := program.GetInfoLog()
	if len(infoLog) != 0 {
		fmt.Printf("Program: %s\n", infoLog)
	}

	// Generate a buffer for the VertexBufferObject
	vboTriangle = gl.GenBuffer()
	vboTriangle.Bind(gl.ARRAY_BUFFER)
	// Submit the vertices of the triangle to the graphic card
	gl.BufferData(gl.ARRAY_BUFFER, len(triangleAttributes)*4, triangleAttributes, gl.STATIC_DRAW)
	// Unset the active buffer
	gl.Buffer(0).Bind(gl.ARRAY_BUFFER)

	// Get the attribute location from the GLSL program (here from the vertex shader)
	attributeName := "coord2d"
	attributeCoord2d = program.GetAttribLocation(attributeName)
	if attributeCoord2d == -1 {
		fmt.Printf("Could not bind attribute %s\n", attributeName)
	}

	attributeName = "v_color"
	attributeColor = program.GetAttribLocation(attributeName)
	if attributeColor == -1 {
		fmt.Printf("Could not bind attribute %s\n", attributeName)
	}

	uniformName := "fade"
	uniformFade = program.GetUniformLocation(uniformName)
	if uniformFade == -1 {
		fmt.Printf("Could not bind uniform %s\n", uniformName)
	}
}
Exemplo n.º 2
0
func (gd *openGLGraphicsDevice) NewShader(vertexShaderStr, fragmentShaderStr string) Shader {
	vertexShader := gl.CreateShader(gl.VERTEX_SHADER)
	vertexShader.Source(vertexShaderStr)
	vertexShader.Compile()
	fmt.Println(vertexShader.GetInfoLog())

	fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER)
	fragmentShader.Source(fragmentShaderStr)
	fragmentShader.Compile()
	fmt.Println(fragmentShader.GetInfoLog())

	program := gl.CreateProgram()
	program.AttachShader(vertexShader)
	program.AttachShader(fragmentShader)
	program.Link()
	program.Validate()
	fmt.Println(program.GetInfoLog())

	return &openGLShader{vertexShader, fragmentShader, program}
}
Exemplo n.º 3
0
func initResources() {
	var err os.Error
	// Load shaders
	vs, err = createShader("cube.v.glsl", VertexShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}
	fs, err = createShader("cube.f.glsl", FragmentShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}

	// Create GLSL program with loaded shaders
	program = gl.CreateProgram()
	program.AttachShader(vs)
	program.AttachShader(fs)
	program.Link()
	infoLog := program.GetInfoLog()
	if len(infoLog) != 0 {
		fmt.Printf("Program: %s\n", infoLog)
	}
	for i := 1; i < 6; i++ {
		cubeTexCoords = append(cubeTexCoords, cubeTexCoords...)
	}

	vboCubeTexCoords = gl.GenBuffer()
	vboCubeTexCoords.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, len(cubeTexCoords)*4, cubeTexCoords, gl.STATIC_DRAW)
	gl.Buffer(0).Bind(gl.ARRAY_BUFFER)

	vboCubeVertices = gl.GenBuffer()
	vboCubeVertices.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, len(cubeVertices)*4, cubeVertices, gl.STATIC_DRAW)
	gl.Buffer(0).Bind(gl.ARRAY_BUFFER)

	// Generate a buffer for the IndexBufferObject
	iboCubeElements = gl.GenBuffer()
	iboCubeElements.Bind(gl.ELEMENT_ARRAY_BUFFER)
	// Submit the indexes to the graphic card
	gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(cubeElements)*2, cubeElements, gl.STATIC_DRAW)
	// Unset the active buffer
	gl.Buffer(0).Bind(gl.ELEMENT_ARRAY_BUFFER)

	// Get the attribute location from the GLSL program (here from the vertex shader)
	attributeName := "coord3d"
	attributeCoord3d = program.GetAttribLocation(attributeName)
	if attributeCoord3d == -1 {
		fmt.Printf("Could not bind attribute %s\n", attributeName)
	}

	attributeName = "texcoord"
	attributeTexCoord = program.GetAttribLocation(attributeName)
	if attributeTexCoord == -1 {
		fmt.Printf("Could not bind attribute %s\n", attributeName)
	}

	uniformName := "mvp"
	uniformMTransform = program.GetUniformLocation(uniformName)
	if uniformMTransform == -1 {
		fmt.Printf("Could not bind attribute %s\n", uniformName)
	}

	uniformName = "mytexture"
	uniformTexture = program.GetUniformLocation(uniformName)
	if uniformTexture == -1 {
		fmt.Printf("Could not bind attribute %s\n", uniformName)
	}

	// Load texture
	texture, err = openSurface("texture.jpg")
	if err != nil {
		fmt.Printf("Texture: %s\n", err)
		return
	}
}
Exemplo n.º 4
0
func main() {
	//ABSPTest()
	//return
	glfw.Init(800, 600)
	InitPhysics()

	world := new(World)
	world.Init()
	world.GameObjects = new(list.List)
	player := MakeMan(Vec3{10, 20, 10})

	world.Add(player)
	world.Add(ropetest(Vec3{0, 40, 0}, 4, 4))
	world.Add(treeThing(Vec3{20, 20, 20}, 3))
	world.Add(MakePlayer(Vec3{-20, 20, 0}).Body)
	world.Add(MakePlayer(Vec3{-20, 50, 0}).Body)

	world.Add(MakePlayer(Vec3{-20, 70, 0}).Body)
	world.Add(MakePlayer(Vec3{-20, 90, 0}).Body)
	world.Add(MakePlayer(Vec3{-20, 110, 0}).Body)
	/*for i := 0; i < 100; i++ {
		world.Add(SetPlayerAnimation(MakePlayer(Vec3{float32(int(i%10))*50,0,float32(i*2) - 200})).Body)
	}*/
	world.Add(NewGameObj(Vec3{0, -20, 0}, Vec3{10000, 10, 10000}, Vec3{0, 0.5, 0.1}, float32(math.Inf(1)), 10, nil))

	//world.Add(SetPlayerAnimation(MakePlayer(Vec3{-20,120,0})).Body)

	qtn := new(ABSPNode)
	qtn.Root = qtn
	//qtn.Position = Vec3{-10000,-10000,-10000}
	//qtn.Size = Vec3{20000,20000,20000}
	for i := world.GameObjects.Front(); i != nil; i = i.Next() {
		gobj := i.Value.(*GameObj)
		all := gobj.GetSubs()
		for i := 0; i < len(all); i++ {
			qtn.Insert(all[i])
		}
	}
	world.GameObjectTree = qtn
	fmt.Println("Total:", len(qtn.Data))
	qtn.Divide()
	cols := 0
	qtn.cd(func(obj1, obj2 SPData) {
		cols += 1
	})
	fmt.Println(cols)
	//qtn.Traverse(0)
	//return
	gl.Init()
	vs := gl.CreateShader(gl.VERTEX_SHADER)
	vs.Source(
		LoadFileToString("s1.vert"))
	vs.Compile()

	fs := gl.CreateShader(gl.FRAGMENT_SHADER)
	fs.Source(LoadFileToString("s1.frag"))
	fs.Compile()

	pg := gl.CreateProgram()
	pg.AttachShader(vs)
	pg.AttachShader(fs)
	pg.Link()
	pg.Validate()

	pg.Use()
	fmt.Println("**Shader log**")
	fmt.Println(fs.GetInfoLog())
	fmt.Println(vs.GetInfoLog())
	fmt.Println(pg.GetInfoLog())
	fmt.Println("******END*****")

	gl.ClearColor(0.5, 0.5, 1, 0)
	gl.Enable(gl.DEPTH_TEST)
	gl.Enable(gl.BLEND)
	gl.Enable(gl.POLYGON_SMOOTH)
	gl.Hint(gl.POLYGON_SMOOTH_HINT, gl.NICEST)

	var t float64
	t = float64(time.Nanoseconds()) / 1000000000
	cam1 := Camera{player, 100, Vec3{0, 0, 0}}
	glfw.AddListener(func(m glfw.MouseMoveEvent) {
		cam1.Angle.X = float32(m.X-400) / 400 * 3.14 * 2
		cam1.Angle.Y = float32(m.Y-300) / 300 * 3.14 * 2
		//player.Rotation = Vec3{cam1.Angle.X,cam1.Angle.Y,0}
	})
	glfw.AddListener(func(mw glfw.MouseWheelEvent) {
		cam1.Distance = 100 + float32(mw.Pos*mw.Pos*mw.Pos)
	})

	world.CompilePhysicsObjects()
	for it := 0; it < 10000; it += 1 {
		cam1.Setup()
		dt := float32(0.005)
		t = float64(float64(time.Nanoseconds()) / 1000000000)
		UpdatePositions(world.PhysicsObjects, dt)

		UpdateModelStates(world.PhysicsObjects)

		UpdateCollisions(world.GameObjectTree, dt)
		UpdateModelStates(world.PhysicsObjects)
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		DrawWorld(world.GameObjects, dt, pg)
		glfw.SwapBuffers()
		pt := float64(float64(time.Nanoseconds()) / 1000000000)
		sleeptime := (float64(dt) - (pt - t)) * 10e8
		fmt.Println("Sleep for:", sleeptime)
		time.Sleep(int64(sleeptime))
	}

}