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() }
func display() { // Clear the background as white gl.ClearColor(1.0, 1.0, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) // Use the GLSL program program.Use() // Faster fade in and out than in the wikibook curFade := math.Sin(glfw.Time()) uniformFade.Uniform1f(float32(curFade)) vboTriangle.Bind(gl.ARRAY_BUFFER) attributeCoord2d.EnableArray() // Describe our vertices array to OpenGL (it can't guess its format automatically) attributeCoord2d.AttribPointerOffset(2, gl.FLOAT, false, 5*4, 0) attributeColor.EnableArray() attributeColor.AttribPointerOffset(3, gl.FLOAT, false, 5*4, 2*4) // Push each element in buffer_vertices to the vertex shader gl.DrawArrays(gl.TRIANGLES, 0, 3) attributeCoord2d.DisableArray() attributeColor.DisableArray() // Display the result glfw.SwapBuffers() }
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() } }
func fps() { t1 := glfw.Time() if t1-t0 >= 1.0 { log.Print(frameCount / (t1 - t0)) log.Print(life.Generation) t0 = t1 frameCount = 0 } else { frameCount++ } }
//############################ func printFPS() { Frames++ { t := glfw.Time() if t-T0 >= 5 { seconds := (t - T0) fps := float64(Frames) / seconds print(Frames, " frames in ", int(seconds), " seconds = ", int(fps), " FPS\n") T0 = t Frames = 0 } } }
func keyPressListener(key int, action int) { if !running { return } if action == glfw.KeyPress { width := appWidth height := appHeight switch key { case glfw.KeyEsc: running = false case glfw.KeyF1: app.release() glfw.CloseWindow() // Toggle fullscreen mode fullScreen = !fullScreen if fullScreen { mode := glfw.DesktopMode() aspect := float32(mode.W) / float32(mode.H) if int(aspect*100) == 133 || int(aspect*100) == 125 { // Standard width = 1280 height = 1024 } else if int(aspect*100) == 177 { // Widescreen 16:9 width = 1280 height = 720 } else if int(aspect*100) == 160 { // Widescreen 16:10 width = 1280 height = 800 } else { // Unknown // Use desktop resolution width = mode.W height = mode.H } } if !setupWindow(width, height, fullScreen) { glfw.Terminate() //exit( -1 ); } app.init() app.resize(width, height) t0 = glfw.Time() } } }
func draw() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.PushMatrix() gl.Rotated(view_rotx, 1.0, 0.0, 0.0) gl.Rotated(view_roty, 0.0, 1.0, 0.0) gl.Rotated(view_rotz, 0.0, 0.0, 1.0) gl.PushMatrix() gl.Translated(-3.0, -2.0, 0.0) gl.Rotated(angle, 0.0, 0.0, 1.0) gl.CallList(gear1) gl.PopMatrix() gl.PushMatrix() gl.Translated(3.1, -2.0, 0.0) gl.Rotated(-2.0*angle-9.0, 0.0, 0.0, 1.0) gl.CallList(gear2) gl.PopMatrix() gl.PushMatrix() gl.Translated(-3.1, 4.2, 0.0) gl.Rotated(-2.0*angle-25.0, 0.0, 0.0, 1.0) gl.CallList(gear3) gl.PopMatrix() gl.PopMatrix() glfw.SwapBuffers() Frames++ { t := glfw.Time() if t-T0 >= 5 { seconds := (t - T0) fps := float64(Frames) / seconds print(Frames, " frames in ", int(seconds), " seconds = ", int(fps), " FPS\n") T0 = t Frames = 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 } } }
func main() { // Initialize GLFW glfw.Init() glfw.Enable(glfw.StickyKeys) if !setupWindow(appWidth, appHeight, fullScreen) { return } // Initialize application and engine app = new(Application) if !app.init() { fmt.Println("Error starting Horde3d") horde3d.DumpMessages() } if !fullScreen { glfw.SetWindowTitle(app.title) } app.resize(appWidth, appHeight) glfw.Disable(glfw.MouseCursor) var frames float32 = 0 var fps float32 = 30.0 t0 = glfw.Time() running = true // Game loop for running { // Calc FPS frames++ if frames >= 3 { t := glfw.Time() fps = frames / float32(t-t0) if fps < 5 { fps = 30 // Handle breakpoints } frames = 0 t0 = t } // Update key states for i := 0; i < 320; i++ { app.setKeyState(i, glfw.Key(i) == glfw.KeyPress) } app.keyStateHandler() // Render app.mainLoop(fps) glfw.SwapBuffers() } glfw.Enable(glfw.MouseCursor) // Quit app.release() glfw.Terminate() return }
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() }
func drawGrid() { gl.Color4f(0.2, 0.2, 0.2, 1) gl.Begin(gl.LINES) for i := -0.5; i < Width; i += 1 { gl.Vertex2f(gl.Float(-0.5), gl.Float(i)) gl.Vertex2f(gl.Float(life.Size), gl.Float(i)) gl.Vertex2f(gl.Float(i), -0.5) gl.Vertex2f(gl.Float(i), gl.Float(life.Size)) } gl.End() } var frameCount float64 var t0 = glfw.Time() func fps() { t1 := glfw.Time() if t1-t0 >= 1.0 { log.Print(frameCount / (t1 - t0)) log.Print(life.Generation) t0 = t1 frameCount = 0 } else { frameCount++ } } func drawScene() { fps()