Esempio n. 1
0
func main() {
	// We need to lock the goroutine to one thread due time.Ticker
	runtime.LockOSThread()

	var err os.Error
	err = glfw.Init()
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.Terminate()

	// You could probably change the required versions down
	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, 1)

	// Open Window with 8 bit Alpha
	err = glfw.OpenWindow(ScreenWidth, ScreenHeight, 0, 0, 0, 8, 0, 0, glfw.Windowed)
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetWindowTitle(WindowTitle)

	major, minor, rev := glfw.GLVersion()
	if major < 3 {
		fmt.Printf("Error your graphic card does not support OpenGL 3.3\n Your GL-Version is: %d, %d, %d\n", major, minor, rev)
		fmt.Println("You can try to lower the settings in glfw.OpenWindowHint(glfw.OpenGLVersionMajor/Minor.")
	}

	initStatus := gl.Init() // Init glew
	if initStatus != 0 {
		fmt.Printf("Error-code: %d Init-Status: %d\n", gl.GetError(), initStatus)
	}

	// Enable transparency in OpenGL
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	initResources()
	// We are limiting the calls to display() (frames per second) to 60. This prevents the 100% cpu usage.
	ticker := time.NewTicker(int64(second) / 60) // max 60 fps
	for {
		<-ticker.C
		move := float32(math.Sin(glfw.Time()))
		angle := float32(glfw.Time())
		matrix = math3d.MakeTranslationMatrix(move, 0.0, 0.0)
		matrix = matrix.Multiply(math3d.MakeZRotationMatrix(angle)).Transposed()
		display()
	}

	// Free resources
	free()

	runtime.UnlockOSThread()
}
Esempio n. 2
0
func main() {
	var err os.Error
	err = glfw.Init()
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.WindowNoResize, 1)
	glfw.OpenWindowHint(glfw.OpenGLDebugContext, 1)

	// You could probably change the required versions down
	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, 1)

	// Open Window with 8 bit Alpha
	err = glfw.OpenWindow(ScreenWidth, ScreenHeight, 0, 0, 0, 8, 0, 0, glfw.Windowed)
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetWindowTitle(WindowTitle)

	major, minor, rev := glfw.GLVersion()
	if major < 3 {
		fmt.Printf("Error your graphic card does not support OpenGL 3.3\n Your GL-Version is: %d, %d, %d\n", major, minor, rev)
		fmt.Println("You can try to lower the settings in glfw.OpenWindowHint(glfw.OpenGLVersionMajor/Minor.")
	}

	initStatus := gl.Init() // Init glew
	if initStatus != 0 {
		fmt.Printf("Error-code: %d Init-Status: %d\n", gl.GetError(), initStatus)
	}

	// Enable transparency in OpenGL
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	initResources()

	for {
		display()
	}

	// Free resources
	free()
}
Esempio n. 3
0
func main() {
	if err := glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
		return
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.WindowNoResize, 1)

	if err := glfw.OpenWindow(Width, Height, 0, 0, 0, 0, 0, 0, glfw.Windowed); err != nil {
		fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetSwapInterval(1)
	glfw.SetWindowTitle(Title)

	if err := gl.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "gl: %s\n", err)
	}

	if err := initScene(); err != nil {
		fmt.Fprintf(os.Stderr, "init: %s\n", err)
		return
	}
	defer destroyScene()

	for glfw.WindowParam(glfw.Opened) == 1 {
		drawScene()
		glfw.SwapBuffers()
	}
}
Esempio n. 4
0
// OpenWindow opens a new window with the given size.
func OpenWindow(w, h int) error {
	glfw.OpenWindowHint(glfw.WindowNoResize, 1)

	r, g, b := 0, 0, 0 // defaults
	a := 8             // 8-bit alpha channel
	d, s := 0, 0       // no depth or stencil buffers
	m := glfw.Windowed
	if err := glfw.OpenWindow(w, h, r, g, b, a, d, s, m); err != nil {
		return err
	}

	if gl.Init() != 0 {
		return errors.New("Failed to initialize OpenGL")
	}

	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(w), 0, float64(-h), -1, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translated(0, float64(-h), 0)
	return nil
}
Esempio n. 5
0
func main() {
	if err := glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
		return
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.WindowNoResize, 0)

	if err := glfw.OpenWindow(Width, Height, 0, 0, 0, 0, 16, 0, glfw.Windowed); err != nil {
		fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetSwapInterval(60)
	glfw.SetWindowTitle(Title)

	if err := gl.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "gl: %s\n", err)
	}

	keybindings.BindKeyboard()

	gl.ClearColor(0, 0, 0, 1)

	GameLoop()

}
Esempio n. 6
0
func main() {

	// Setup the window

	if err := glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
		return
	}
	// Close glfw at the end of main()
	defer glfw.Terminate()

	// Set window properties
	glfw.OpenWindowHint(glfw.WindowNoResize, 0)
	//glfw.OpenWindowHint(glfw.OpenGLForwardCompat, gl.TRUE)
	glfw.OpenWindowHint(glfw.OpenGLDebugContext, gl.TRUE)

	// Actually open the window
	if err := glfw.OpenWindow(initial_width, initial_height, 0, 0, 0, 0, 16, 0, glfw.Windowed); err != nil {
		fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
		return
	}

	// Close the window at the end of main()
	defer glfw.CloseWindow()

	// Only swap buffer once/ draw cycle (30 or 60 fps)
	glfw.SetSwapInterval(1)
	glfw.SetWindowTitle(Title)

	// Apply those settings
	if err := gl.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "gl: %s\n", err)
	}

	// While glfw.Opened is 1 (the window is open), keep swapping buffers
	for glfw.WindowParam(glfw.Opened) == 1 {

		glfw.SwapBuffers()
	}

	setCallbacks()

	// Initialize the openGL settings
	Initialize()
}
Esempio n. 7
0
File: main.go Progetto: sycoso/glfw
func main() {
	var err error
	if err = glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "[e] %v\n", err)
		return
	}

	defer glfw.Terminate()

	// Open window with FSAA samples (if possible).
	glfw.OpenWindowHint(glfw.FsaaSamples, 4)

	if err = glfw.OpenWindow(400, 400, 0, 0, 0, 0, 0, 0, glfw.Windowed); err != nil {
		fmt.Fprintf(os.Stderr, "[e] %v\n", err)
		return
	}

	defer glfw.CloseWindow()

	glfw.SetWindowTitle("Aliasing Detector")
	glfw.SetSwapInterval(1)

	if samples := glfw.WindowParam(glfw.FsaaSamples); samples != 0 {
		fmt.Fprintf(os.Stdout, "Context reports FSAA is supported with %d samples\n", samples)
	} else {
		fmt.Fprintf(os.Stdout, "Context reports FSAA is unsupported\n")
	}

	gl.MatrixMode(gl.PROJECTION)
	glu.Perspective(0, 1, 0, 1)

	for glfw.WindowParam(glfw.Opened) == 1 {
		time := float32(glfw.Time())

		gl.Clear(gl.COLOR_BUFFER_BIT)

		gl.LoadIdentity()
		gl.Translatef(0.5, 0, 0)
		gl.Rotatef(time, 0, 0, 1)

		gl.Enable(GL_MULTISAMPLE_ARB)
		gl.Color3f(1, 1, 1)
		gl.Rectf(-0.25, -0.25, 0.25, 0.25)

		gl.LoadIdentity()
		gl.Translatef(-0.5, 0, 0)
		gl.Rotatef(time, 0, 0, 1)

		gl.Disable(GL_MULTISAMPLE_ARB)
		gl.Color3f(1, 1, 1)
		gl.Rectf(-0.25, -0.25, 0.25, 0.25)
		glfw.SwapBuffers()
	}
}
Esempio n. 8
0
func main() {
	fmt.Println("OpenGL Programming/Modern OpenGL Introduction")
	fmt.Println("Tutorial taken from http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Introduction")

	var err error
	err = glfw.Init()
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, 1)

	err = glfw.OpenWindow(ScreenWidth, ScreenHeight, 0, 0, 0, 0, 0, 0, glfw.Windowed)
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetWindowTitle(WindowTitle)

	// Init extension loading
	err = gl.Init()
	if err != nil {
		fmt.Printf("Init OpenGL extension loading failed with %s.\n", err)
	}

	initResources()

	// Render loop
	for glfw.WindowParam(glfw.Opened) == 1 {
		display()
	}

	// Free resources
	free()
}
Esempio n. 9
0
File: init.go Progetto: shenyp09/mx3
func InitWindow(w, h int, multisample int, vsync bool) {
	core.Fatal(glfw.Init())
	if multisample != 0 {
		glfw.OpenWindowHint(glfw.FsaaSamples, multisample)
	}
	Width, Height = w, h
	core.Fatal(glfw.OpenWindow(Width, Height, r, g, b, a, depth, stencil, glfw.Windowed))
	glfw.SetWindowTitle("mumax cubed")
	if vsync {
		glfw.SetSwapInterval(1)
	}
}
Esempio n. 10
0
func initWindow(caption string) {
	//glfw stuff
	err := glfw.Init()
	if err != nil {
		panic(err)
	}

	_w = 500
	_h = 500

	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 2)
	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, 0) //glfw.OpenGLCoreProfile)

	err = glfw.OpenWindow(_w, _h, 0, 0, 0, 0, 0, 0, glfw.Windowed)
	if err != nil {
		panic(err)
	}
	glfw.SetWindowTitle(caption)
	glfw.Disable(glfw.AutoPollEvents)

	glfw.SetWindowSizeCallback(onResize)
}
Esempio n. 11
0
func main() {
	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}

		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if err := readModel(); err != nil {
		log.Fatal(err)
	}

	if err := glfw.Init(); err != nil {
		log.Fatal(err)
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.WindowNoResize, 1)

	if err := glfw.OpenWindow(Width, Height, 0, 0, 0, 0, 16, 0, glfw.Windowed); err != nil {
		log.Fatal(err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetSwapInterval(1)
	glfw.SetWindowTitle(Title)
	glfw.SetKeyCallback(onKey)

	if err := gl.Init(); err != nil {
		log.Fatal(err)
	}

	initScene()
	defer destroyScene()

	for glfw.WindowParam(glfw.Opened) == 1 {
		applyMove()
		drawScene()
		glfw.SwapBuffers()
	}
}
Esempio n. 12
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()

	if *flagListRules {
		for _, name := range automata.Rulers() {
			fmt.Println(name)
		}
		return
	}

	if err := glfw.Init(); err != nil {
		log.Fatal(err)
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.WindowNoResize, 1)

	if err := glfw.OpenWindow(Width, Height, 0, 0, 0, 0, 0, 0, glfw.Windowed); err != nil {
		log.Fatal(err)
	}
	defer glfw.CloseWindow()

	glfw.SetSwapInterval(1)
	glfw.SetWindowTitle(Title)

	if err := gl.Init(); err != nil {
		log.Fatal(err)
	}

	initScene()
	defer destroyScene()

	for glfw.WindowParam(glfw.Opened) == 1 {
		drawScene()
		glfw.SwapBuffers()
	}
}
Esempio n. 13
0
func main() {
	log.SetFlags(log.Lshortfile)

	//parse flags
	flag.Parse()

	if flags.cpuprofile != "" {
		f, err := os.Create(flags.cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	loadSettingsFile()
	Settings.Paused = flags.startPaused

	if flags.buildExamples {
		allExamples()
		return
	}

	wx, wy := 800, 600

	//initialize opengl & glfw
	{
		//init glfw
		if err := glfw.Init(); err != nil {
			log.Printf("Error initializing glfw: %v\n", err)
			return
		}
		defer glfw.Terminate()

		//set window hints
		glfw.OpenWindowHint(glfw.WindowNoResize, 1)

		//create the window
		if err := glfw.OpenWindow(wx, wy, 8, 8, 8, 8, 0, 8, glfw.Windowed); err != nil {
			log.Printf("Error opening Window: %v\n", err)
			return
		}
		defer glfw.CloseWindow()

		//init opengl
		if gl.Init() != 0 {
			panic("gl error")
		}

		//glfw config
		{
			glfw.SetSwapInterval(1)
			glfw.SetWindowTitle("mater test")
		}

		//set additional opengl stuff
		{
			gl.ClearColor(0, 0, 0, 0)
			gl.Enable(gl.BLEND)
			//gl.BlendFunc (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
			gl.Enable(gl.TEXTURE_2D)
		}
	}

	//setup scene related stuff
	{
		//create empty space
		space = collision.NewSpace()
	}

	//reload settings so they take effect
	reloadSettings()
	saveSettingsFile()

	//set callbacks
	{
		glfw.SetWindowSizeCallback(OnResize)
		glfw.SetKeyCallback(OnKey)
	}

	//init debug console
	console.Init()

	//load savefile passed from the commandline if any
	if flags.file != "" {
		err := loadSpace(flags.file)
		Settings.Paused = true
		if err != nil {
			panic(err)
		}
	}

	//if set to true once a second
	printFPS := false

	//fix timestep to given fps
	const expectedFps = 30.0
	const expectedFrameTime = 1.0 / expectedFps

	//the time at the start of the last frame
	lastTime := 0.0

	acc := 0.0
	updateAcc := 0.0

	frameCount := 0
	updateFrameCount := 0

	fps := 0
	updateFps := 0

	Settings.Running = true

	for Settings.Running && glfw.WindowParam(glfw.Opened) == 1 {
		time := glfw.Time()
		//get the time elapsed since the last frame
		dt := time - lastTime
		lastTime = time

		//advance framecount and accumulators
		frameCount++
		acc += dt
		updateAcc += dt

		//execute console commands if any
		select {
		case command := <-console.Command:
			console.ExecuteCommand(command)
		default:
		}

		//update the scene at a fixed timestep
		for updateAcc >= expectedFrameTime {
			updateFrameCount++

			//if one second has passed update the fps and reset the framecount
			if acc > 1 {
				updateFps = updateFrameCount
				updateFrameCount = 0
			}

			//only update if not paused or if set to advance a single frame
			if !Settings.Paused || Settings.SingleStep {
				space.Step(expectedFrameTime)
				Settings.SingleStep = false
			}

			updateAcc -= expectedFrameTime
		}

		//draw debug data
		Draw()

		glfw.SwapBuffers()

		//if one second has passed update the fps and reset the framecount
		if acc > 1 {
			fps = frameCount
			frameCount = 0
			if printFPS {
				fmt.Printf("---\n")
				fmt.Printf("FPS: %v\n", fps)
				fmt.Printf("Update FPS: %v\n", updateFps)
				fmt.Printf("Average frametime: %v\n", acc/float64(fps))
				fmt.Printf("---\n")
			}
			acc -= 1
		}
	}
}
Esempio n. 14
0
func main() {
	if err := glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
		return
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.WindowNoResize, gl.TRUE)

	if err := glfw.OpenWindow(Width, Height, 0, 0, 0, 0, 32, 32, glfw.Windowed); err != nil {
		fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetSwapInterval(1)
	glfw.SetWindowTitle(Title)

	if err := gl.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "gl: %s\n", err)
	}

	var scene ry.Scene
	if err := scene.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "init: %s\n", err)
		return
	}
	defer scene.Destroy()

	var player, wall ry.Object
	wall.Init()
	player.Init()
	player.Position.Z = -7
	player.Position.X = 3
	player.Orientation = ry.QuatAngleAxis(-45*ry.DegToRad, ry.Vec3{0, 0, 1})

	cc := newControlComponent(&player)
	mesh := ry.NewMeshComponent("model/tex.bin", &player)
	mesh.Init()
	player.AddComponent(*cc)
	player.Mesh = mesh

	mesh2 := ry.NewMeshComponent("model/tex.bin", &wall)
	mesh2.Init()
	wall.Mesh = mesh2
	wall.Position.Z = -10
	box := ry.NewBoxComponent(ry.Vec3{0, 0, 0}, ry.Vec3{10, 10, 10})
	wall.Box = box
	wall.Orientation = ry.QuatAngleAxis(-90*ry.DegToRad, ry.Vec3{0, 0, 1})

	scene.AddObject(&player)
	scene.AddObject(&wall)

	//plane := ry.Plane{ry.Vec3{0,0,0}, ry.Vec3{1,1,0}}
	ray := ry.Ray{ry.Vec3{15, -5, -5}, ry.Vec3{-100, 0, 0}}

	/*
	   b, hv := ry.IntersectionRayPlane(ray, plane)
	   if b {
	     fmt.Println("hv : ", hv)
	   } else { fmt.Println("no collision : ", hv) }
	*/

	//aabox := ry.AABox{ry.Vec3{0,0,0}, ry.Vec3{10,10,10}}
	//hit, _, pos, nor := ry.IntersectionRayAABox(ray, aabox)
	hit, _, pos, nor := ry.IntersectionRayObject(ray, &wall)
	if hit {
		fmt.Println("pos and normal : ", pos, nor)
	} else {
		fmt.Println("no intersection with box")

	}

	last_time = time.Now()
	for glfw.WindowParam(glfw.Opened) == 1 && !exit {
		/*
		   objects, positions := ry.LaunchRay( ry.Vec3{0,0,0}, ry.Vec3{0,0,-1}, 100, scene.Objects)

		    if objects != nil {
		      fmt.Println("collision with one or more objects", positions[0])
		    }
		*/

		scene.Update()
		scene.Draw()
		glfw.SwapBuffers()
		since := time.Since(last_time).Seconds()
		if since > 0.02 {
			fmt.Println("frame under 50fps:", since)
		}
		last_time = time.Now()
	}
}
Esempio n. 15
0
// NewWindow initialize glfw and opens a new window with p's properties.
// The AdvancedProperties ap is optional.
func NewWindow(p Properties, ap *AdvancedProperties) (*window, error) {
	if err := glfw.Init(); err != nil {
		return nil, err
	}
	w := &window{t: time.Now()}
	if ap != nil {
		glfw.OpenWindowHint(glfw.RefreshRate, ap.RefreshRate)
		glfw.OpenWindowHint(glfw.AccumRedBits, ap.AccumRedBits)
		glfw.OpenWindowHint(glfw.AccumGreenBits, ap.AccumGreenBits)
		glfw.OpenWindowHint(glfw.AccumBlueBits, ap.AccumBlueBits)
		glfw.OpenWindowHint(glfw.AccumAlphaBits, ap.AccumAlphaBits)
		glfw.OpenWindowHint(glfw.AuxBuffers, ap.AuxBuffers)
		if ap.Stereo {
			glfw.OpenWindowHint(glfw.Stereo, 1)
		} else {
			glfw.OpenWindowHint(glfw.Stereo, 0)
		}
		if ap.NoWindowResize {
			glfw.OpenWindowHint(glfw.WindowNoResize, 1)
		} else {
			glfw.OpenWindowHint(glfw.WindowNoResize, 0)
		}
		glfw.OpenWindowHint(glfw.FsaaSamples, ap.FsaaSamples)
		glfw.OpenWindowHint(glfw.OpenGLVersionMajor, ap.OpenGLVersionMajor)
		glfw.OpenWindowHint(glfw.OpenGLVersionMinor, ap.OpenGLVersionMinor)
		if ap.OpenGLForwardCompat {
			glfw.OpenWindowHint(glfw.OpenGLForwardCompat, 1)
		} else {
			glfw.OpenWindowHint(glfw.OpenGLForwardCompat, 0)
		}
		if ap.OpenGLDebugContext {
			glfw.OpenWindowHint(glfw.OpenGLDebugContext, 1)
		} else {
			glfw.OpenWindowHint(glfw.OpenGLDebugContext, 0)
		}
		switch ap.OpenGLProfile {
		case OpenGLCompatProfile:
			glfw.OpenWindowHint(glfw.OpenGLProfile, glfw.OpenGLCompatProfile)
		case OpenGLCoreProfile:
			glfw.OpenWindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
		default:
			return nil, ErrUnknownOpenGLProfile
		}
	}
	mode := glfw.Windowed
	if p.Fullscreen {
		mode = glfw.Fullscreen
	}
	if err := glfw.OpenWindow(p.Width, p.Height, p.R, p.G, p.B, p.A, p.Depth, p.Stencil, mode); err != nil {
		return nil, err
	}
	return w, nil
}
Esempio n. 16
0
func main() {
	// We need to lock the goroutine to one thread due time.Ticker
	runtime.LockOSThread()

	var err os.Error
	err = glfw.Init()
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.Terminate()

	// You could probably change the required versions down
	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, 1)

	// Open Window with 8 bit Alpha
	err = glfw.OpenWindow(ScreenWidth, ScreenHeight, 0, 0, 0, 8, 8, 0, glfw.Windowed)
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetWindowTitle(WindowTitle)
	glfw.SetWindowSizeCallback(onResize)

	major, minor, rev := glfw.GLVersion()
	if major < 3 {
		fmt.Printf("Error your graphic card does not support OpenGL 3.3\n Your GL-Version is: %d, %d, %d\n", major, minor, rev)
		fmt.Println("You can try to lower the settings in glfw.OpenWindowHint(glfw.OpenGLVersionMajor/Minor.")
	}

	initStatus := gl.Init() // Init glew
	if initStatus != 0 {
		fmt.Printf("Error-code: %d Init-Status: %d\n", gl.GetError(), initStatus)
	}

	// Enable transparency in OpenGL
	gl.Enable(gl.BLEND)
	gl.Enable(gl.DEPTH_TEST)
	//gl.DepthFunc(gl.LESS)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	initResources()

	// We are limiting the calls to display() (frames per second) to 60. This prevents the 100% cpu usage.
	ticker := time.NewTicker(int64(second) / 60) // max 60 fps
	for {
		<-ticker.C
		angle := float32(glfw.Time())
		anim := math3d.MakeYRotationMatrix(angle)
		model := math3d.MakeTranslationMatrix(0, 0, -4)
		view := math3d.MakeLookAtMatrix(math3d.Vector3{0, 2, 0}, math3d.Vector3{0, 0, -4}, math3d.Vector3{0, 1, 0})
		projection := math3d.MakePerspectiveMatrix(45, float32(ScreenWidth)/float32(ScreenHeight), 0.1, 10.0)
		matrix = math3d.MakeIdentity().Multiply(projection).Multiply(view).Multiply(model).Multiply(anim)
		program.Use()
		uniformMTransform.UniformMatrix4fv(1, false, matrix.Transposed())
		display()
	}

	// Free resources
	free()

	runtime.UnlockOSThread()
}