Example #1
0
func main() {
	flag.Parse()

	go func() {
		http.ListenAndServe("localhost:6060", nil)
	}()

	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.True)
	glfw.WindowHint(glfw.Visible, glfw.False) // do not steal focus

	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)

	window, err := glfw.CreateWindow(800, 600, "Spector", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()
	window.Restore()
	window.SetPos(32, 64)

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

	state := NewState()
	for !window.ShouldClose() {
		if window.GetKey(glfw.KeyEscape) == glfw.Press {
			return
		}
		if window.GetKey(glfw.KeyR) == glfw.Press {
			state.Stop()
			state = NewState()
		}

		start := time.Now()
		state.Update(1.0 / 60.0)
		updateTime := time.Since(start)

		start = time.Now()
		state.Render(window)
		renderTime := time.Since(start)

		text := fmt.Sprintf("update: %.2fms render: %.2fms",
			float32(updateTime)/float32(time.Millisecond),
			float32(renderTime)/float32(time.Millisecond))
		w, h := window.GetSize()
		state.Backend.SetFontColor(ui.ColorHex(0xFF0000FF))
		size := state.Backend.Measure(text)
		state.Backend.Text(text, ui.Block(float32(w)-size.X, float32(h)-size.Y, size.X, size.Y))

		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Example #2
0
func main() {
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()
	width, height = 800, 800
	window, err := glfw.CreateWindow(width, height, "Show RoundedRect", nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()
	window.SetSizeCallback(reshape)
	window.SetKeyCallback(onKey)
	window.SetCharCallback(onChar)

	glfw.SwapInterval(1)

	err = gl.Init()
	if err != nil {
		panic(err)
	}

	reshape(window, width, height)
	for !window.ShouldClose() {
		if redraw {
			display()
			window.SwapBuffers()
			redraw = false
		}
		glfw.PollEvents()
		//		time.Sleep(2 * time.Second)
	}
}
Example #3
0
func main() {

	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(800, 600, "Create Window Example", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

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

	window.SetKeyCallback(keypress)
	window.SetMouseButtonCallback(mousepress)

	gl.Viewport(0, 0, 800, 600)

	for !window.ShouldClose() {
		draw()
		window.SwapBuffers()
		glfw.PollEvents()
	}

}
Example #4
0
func main() {
	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(800, 600, "Cube", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

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

	texture = newTexture("square.png")
	defer gl.DeleteTextures(1, &texture)

	setupScene()
	for !window.ShouldClose() {
		drawScene()
		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Example #5
0
func main() {
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()

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

	window.SetKeyCallback(game.OnKey)
	game.Init()
	for !window.ShouldClose() {
		game.Render()

		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Example #6
0
func main() {
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()

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

	// int width, height
	// glfw.GetFramebufferSize(window, &width, &height)

	for !window.ShouldClose() {
		// Do OpenGL stuff.
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.Color3f(1, 1, 0)
		gl.Rectf(-0.75, 0.75, 0.75, -0.75)
		gl.Color3f(1, 0, 1)
		gl.Rectf(-0.5, 0.5, 0.5, -0.5)
		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Example #7
0
func glInit() {
	window.Set("cls", false)
	if err = glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	checkMon()

	win.MakeContextCurrent()

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

	win.SetPos(projMonitor.GetPos())
	setupScene()

	qml.Func1 = func() int {
		if !win.ShouldClose() {
			//glfw.PollEvents()
			drawSlide()
			win.SwapBuffers()
			return 0

		}
		win.Hide()
		//win.Destroy()
		//glfw.Terminate()
		return 1

	}
}
Example #8
0
func initGraphics() error {
	if err := gl.Init(); err != nil {
		return err
	}

	if err := glfw.Init(); err != nil {
		return err
	}

	glfw.WindowHint(glfw.Resizable, glfw.False)

	var err error
	window, err = glfw.CreateWindow(64*scaleRatio, 32*scaleRatio, "CHIP-8 Emulator", nil, nil)

	if err != nil {
		return err
	}

	window.MakeContextCurrent()

	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, 64, 32, 0, 0, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()

	clear()
	window.SwapBuffers()

	return nil
}
Example #9
0
File: demo.go Project: rdterner/gl
func main() {
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	w, err := glfw.CreateWindow(640, 480, "Testing", nil, nil)
	if err != nil {
		panic(err)
	}

	w.MakeContextCurrent()
	glfw.SwapInterval(1)

	w.SetCharCallback(charCallBack)

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

	setupScene(w)
	fmt.Println("Press 'q' to quit")
	for !w.ShouldClose() {
		// Do OpenGL stuff.
		time.Sleep(10 * time.Millisecond)
		drawScene(w)

		w.SwapBuffers()
		glfw.PollEvents()
	}
}
Example #10
0
func main() {
	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(640, 480, "tut01", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

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

	program, coords := initResources()
	for {
		onDisplay(program, uint32(coords))
		window.SwapBuffers()
	}
}
Example #11
0
func main() {
	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(640, 480, "tut03", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

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

	t0 := time.Now()
	program := initResources()
	for {
		curFade = float32(math.Sin(time.Now().Sub(t0).Seconds()*2*math.Pi/5)/2 + 0.5)
		onDisplay(program)
		window.SwapBuffers()
		glfw.PollEvents()
	}

	gl.DeleteProgram(program)
	gl.DeleteBuffers(1, &vboTriangle)
}
Example #12
0
func (c *Context) init() {
	if err := gl.Init(); err != nil {
		panic(err)
	}
	// Textures' pixel formats are alpha premultiplied.
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
}
Example #13
0
func main() {
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()
	fp, err := os.Open("example.tmx")
	if err != nil {
		panic(err)
	}

	m, err := tmx.NewMap(fp)
	if err != nil {
		panic(err)
	}

	var monitor *glfw.Monitor
	window, err := glfw.CreateWindow(screenWidth, screenHeight, "Map Renderer", monitor, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()

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

	width, height := window.GetFramebufferSize()
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.ClearColor(1.0, 1.0, 1.0, 1.0)
	gl.Viewport(0, 0, int32(width), int32(height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(width), float64(height), 0, -1, 1)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	canvas := newOpenGLCanvas(width, height, float32(width)/float32(screenWidth), float32(height)/float32(screenHeight))
	renderer := tmx.NewRenderer(*m, canvas)
	fps := 0
	startTime := time.Now().UnixNano()
	timer := tmx.CreateTimer()
	timer.Start()
	for !window.ShouldClose() {
		elapsed := float64(timer.GetElapsedTime()) / (1000 * 1000)
		renderer.Render(int64(math.Ceil(elapsed)))
		fps++
		if time.Now().UnixNano()-startTime > 1000*1000*1000 {
			log.Println(fps)
			startTime = time.Now().UnixNano()
			fps = 0
		}

		window.SwapBuffers()
		glfw.PollEvents()
		timer.UpdateTime()
	}
}
Example #14
0
func (cw *contextWatcher) OnMakeCurrent(context interface{}) {
	if !cw.initGL {
		// Initialise gl bindings using the current context.
		err := gl.Init()
		if err != nil {
			log.Fatalln("gl.Init:", err)
		}
		cw.initGL = true
	}
}
Example #15
0
func main() {
	runtime.LockOSThread()
	if err := glfw.Init(); err != nil {
		log.Fatal(err)
	}
	defer glfw.Terminate()

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

	window, err := glfw.CreateWindow(WindowWidth, WindowHeight, "Texture Demo", nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	window.MakeContextCurrent()

	const vertShader = `#version 120

uniform mat4 proj;
attribute vec3 vertex_position;
attribute vec2 vertex_texture;
varying vec2 texture_coordinates;

void main() {
	gl_Position = proj * vec4(vertex_position, 1);
	texture_coordinates = vertex_texture;
}
`

	const fragShader = `#version 120

uniform sampler2D tex_loc;
varying vec2 texture_coordinates;

void main() {
	gl_FragColor = texture2D(tex_loc, texture_coordinates);
}
`

	texture, err := newImageTexture("sq.png")
	if err != nil {
		log.Fatal(err)
	}
	scene, err := NewScene(vertShader, fragShader, texture)
	if err != nil {
		log.Fatal(err)
	}

	for !window.ShouldClose() {
		scene.Draw()
		glfw.PollEvents()
		window.SwapBuffers()
	}
}
Example #16
0
func main() {
	runtime.LockOSThread()

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

	window, err := glfw.CreateWindow(800, 600, "fontstash example", nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()
	glfw.SwapInterval(1)
	gl.Init()

	data, err := ioutil.ReadFile(filepath.Join("..", "ClearSans-Regular.ttf"))
	if err != nil {
		panic(err)
	}

	gl.Enable(gl.TEXTURE_2D)

	tmpBitmap := make([]byte, 512*512)
	cdata, err, _, tmpBitmap := truetype.BakeFontBitmap(data, 0, 32, tmpBitmap, 512, 512, 32, 96)
	var ftex uint32
	gl.GenTextures(1, &ftex)
	gl.BindTexture(gl.TEXTURE_2D, ftex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, 512, 512, 0,
		gl.ALPHA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tmpBitmap[0]))

	gl.ClearColor(0.3, 0.3, 0.32, 1.)

	for !window.ShouldClose() {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.MatrixMode(gl.PROJECTION)
		gl.LoadIdentity()
		gl.Ortho(0, 800, 600, 0, 0, 1)
		gl.MatrixMode(gl.MODELVIEW)
		gl.LoadIdentity()
		gl.Disable(gl.DEPTH_TEST)
		gl.Color4ub(255, 255, 255, 255)
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

		my_print(100, 100, "The quick brown fox jumps over the fence", ftex, cdata)

		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Example #17
0
func main() {
	runtime.LockOSThread()
	if err := glfw.Init(); err != nil {
		log.Fatal(err)
	}
	defer glfw.Terminate()

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

	window, err := glfw.CreateWindow(WindowWidth, WindowHeight, "Tetris", nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	window.MakeContextCurrent()

	const vshader = `#version 120

uniform mat4 proj, model;
attribute vec3 vertex_position;
attribute vec2 vertex_texture;
varying vec2 texture_coordinates;

void main() {
	gl_Position = proj * model * vec4(vertex_position, 1);
	texture_coordinates = vertex_texture;
}
`

	const fshader = `#version 120

uniform sampler2D tex_loc;
varying vec2 texture_coordinates;

void main() {
	gl_FragColor = texture2D(tex_loc, texture_coordinates);
}
`

	tetris, err := NewTetris(vshader, fshader)
	if err != nil {
		log.Fatal(err)
	}

	window.SetKeyCallback(tetris.handleKeyInput)

	for !window.ShouldClose() {
		tetris.Draw()
		glfw.PollEvents()
		window.SwapBuffers()
	}
}
Example #18
0
func main() {
	runtime.LockOSThread()

	// initialize glfw
	if err := glfw.Init(); err != nil {
		log.Fatalln("Failed to initialize GLFW: ", err)
	}
	defer glfw.Terminate()

	// create window
	window, err := glfw.CreateWindow(1280, 720, os.Args[0], nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	window.SetFramebufferSizeCallback(onResize)
	window.MakeContextCurrent()

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

	// set up opengl context
	onResize(window, 600, 600)
	// player = createPlayer()
	// addBall()
	// set up physics

	game = engine.NewGame()

	game.Init()
	game.ReadLevelFromFile("level.json")

	//Init Controlls I think
	// glfw.KeyCallback(window)
	window.SetKeyCallback(keyCallback)

	runtime.LockOSThread()
	glfw.SwapInterval(1)

	ticker := time.NewTicker(time.Second / 60)
	for !window.ShouldClose() {

		game.Update(1.0 / 60.0)

		draw()
		window.SwapBuffers()
		glfw.PollEvents()

		<-ticker.C // wait up to 1/60th of a second
	}
}
Example #19
0
func main() {
	var winTitle string = "Go-SDL2 + Go-GL"
	var winWidth, winHeight int = 800, 600
	var window *sdl.Window
	var context sdl.GLContext
	var event sdl.Event
	var running bool
	var err error

	if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil {
		panic(err)
	}
	defer sdl.Quit()

	if err = gl.Init(); err != nil {
		panic(err)
	}

	window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_OPENGL)
	if err != nil {
		panic(err)
	}
	defer window.Destroy()
	context, err = sdl.GL_CreateContext(window)
	if err != nil {
		panic(err)
	}
	defer sdl.GL_DeleteContext(context)

	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(0.2, 0.2, 0.3, 1.0)
	gl.ClearDepth(1)
	gl.DepthFunc(gl.LEQUAL)
	gl.Viewport(0, 0, int32(winWidth), int32(winHeight))

	running = true
	for running {
		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseMotionEvent:
				fmt.Printf("[%d ms] MouseMotion\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Which, t.X, t.Y, t.XRel, t.YRel)
			}
		}
		drawgl()
		sdl.GL_SwapWindow(window)
	}
}
Example #20
0
func main() {
	runtime.LockOSThread()

	// initialize glfw
	if err := glfw.Init(); err != nil {
		log.Fatalln("Failed to initialize GLFW: ", err)
	}
	defer glfw.Terminate()

	// create window
	window, err := glfw.CreateWindow(600, 600, os.Args[0], nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	window.SetFramebufferSizeCallback(onResize)
	window.MakeContextCurrent()

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

	// set up opengl context
	onResize(window, 600, 600)

	// set up physics
	createBodies()

	runtime.LockOSThread()
	glfw.SwapInterval(1)

	ticksToNextBall := 10
	ticker := time.NewTicker(time.Second / 60)
	for !window.ShouldClose() {
		ticksToNextBall--
		if ticksToNextBall == 0 {
			ticksToNextBall = rand.Intn(100) + 1
			addBall()
		}
		draw()
		step(1.0 / 60.0)
		window.SwapBuffers()
		glfw.PollEvents()

		<-ticker.C // wait up to 1/60th of a second
	}
}
Example #21
0
func (v *Video) initGL() {
	if err := gl.Init(); err != nil {
		panic(err)
	}

	gl.Enable(gl.CULL_FACE)
	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)

	v.prog = createProgram(vertShaderSrcDef, fragShaderSrcDef)
	posAttrib := uint32(gl.GetAttribLocation(v.prog, gl.Str("vPosition"+"\x00")))
	texCoordAttr := uint32(gl.GetAttribLocation(v.prog, gl.Str("vTexCoord"+"\x00")))
	v.textureUni = gl.GetAttribLocation(v.prog, gl.Str("texture"+"\x00"))

	var texture uint32
	gl.GenTextures(1, &texture)
	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

	gl.UseProgram(v.prog)
	gl.EnableVertexAttribArray(posAttrib)
	gl.EnableVertexAttribArray(texCoordAttr)
	//posAttrib.EnableArray()
	//texCoordAttr.EnableArray()

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	verts := []float32{-1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0}
	gl.BufferData(gl.ARRAY_BUFFER, len(verts)*int(unsafe.Sizeof(verts[0])), gl.Ptr(verts), gl.STATIC_DRAW)

	var textCoorBuf uint32
	gl.GenBuffers(1, &textCoorBuf)
	gl.BindBuffer(gl.ARRAY_BUFFER, textCoorBuf)
	texVerts := []float32{0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}
	gl.BufferData(gl.ARRAY_BUFFER, len(texVerts)*int(unsafe.Sizeof(texVerts[0])), gl.Ptr(texVerts), gl.STATIC_DRAW)

	gl.VertexAttribPointer(posAttrib, 2, gl.FLOAT, false, 0, gl.PtrOffset(0))
	gl.VertexAttribPointer(texCoordAttr, 2, gl.FLOAT, false, 0, gl.PtrOffset(0))
	//posAttrib.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0))
	//texCoordAttr.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0))
}
Example #22
0
func main() {
	runtime.LockOSThread()

	// initialize glfw
	if err := glfw.Init(); err != nil {
		log.Fatalln("Failed to initialize GLFW: ", err)
	}
	defer glfw.Terminate()

	// create window
	window, err := glfw.CreateWindow(1280, 720, os.Args[0], nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	window.SetFramebufferSizeCallback(onResize)
	window.MakeContextCurrent()

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

	// set up opengl context
	onResize(window, 1280, 720)

	// glfw.KeyCallback(window)
	window.SetKeyCallback(keyCallback)

	runtime.LockOSThread()
	glfw.SwapInterval(1)

	player = NewBall(600, 600)

	ticker := time.NewTicker(time.Second / 60)
	for !window.ShouldClose() {

		player.update()
		//Output
		draw()

		window.SwapBuffers()
		glfw.PollEvents()

		<-ticker.C // wait up to 1/60th of a second
	}
}
Example #23
0
func main() {
	runtime.LockOSThread()
	if err := glfw.Init(); err != nil {
		log.Fatal(err)
	}
	defer glfw.Terminate()

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

	window, err := glfw.CreateWindow(WindowWidth, WindowHeight, "Triangle Demo", nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	window.MakeContextCurrent()

	const vertShader = `#version 120
uniform mat4 proj;
attribute vec3 vertex_position;

void main() {
	gl_Position = proj * vec4(vertex_position, 1);
}
`

	const fragShader = `#version 120
uniform vec4 color;

void main() {
	gl_FragColor = color;
}
`
	scene, err := NewScene(vertShader, fragShader)
	if err != nil {
		log.Fatal(err)
	}

	for !window.ShouldClose() {
		scene.Draw()
		glfw.PollEvents()
		window.SwapBuffers()
	}
}
Example #24
0
func main() {
	var done bool

	sdl.Init(sdl.INIT_VIDEO)

	var screen = sdl.SetVideoMode(300, 300, 18, sdl.OPENGL|sdl.RESIZABLE)

	if screen == nil {
		sdl.Quit()
		panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n")
	}
	gl.Init()
	//if gl.Init() != nil {
	//panic("gl error")
	//}
	init_()
	reshape(int32(screen.W), int32(screen.H))
	done = false
	for !done {
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch e.(type) {
			case *sdl.ResizeEvent:
				re := e.(*sdl.ResizeEvent)
				screen = sdl.SetVideoMode(int(re.W), int(re.H), 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen != nil {
					reshape(int32(screen.W), int32(screen.H))
				} else {
					panic("we couldn't set the new video mode??")
				}
				break
			case *sdl.QuitEvent:
				done = true
				break
			}
		}
		done = key_handler()
		draw()
	}
	sdl.Quit()
	return
}
Example #25
0
//TODO: how to handle multiple windows? Do we even want to allow that?
func CreateWindow(width int, height int, title string) *Window {
	handle, err := glfw.CreateWindow(width, height, title, nil, nil)
	if err != nil {
		panic(err)
	}

	w := new(Window)
	w.handle = handle
	w.width = width
	w.height = height

	handle.MakeContextCurrent()

	err = gl.Init()
	if err != nil {
		panic(err)
	}

	return w
}
Example #26
0
func main() {
	src, err := os.OpenFile("tiger.ps", 0, 0)
	if err != nil {
		log.Println("can't find postscript file.")
		return
	}
	defer src.Close()
	bytes, err := ioutil.ReadAll(src)
	postscriptContent = string(bytes)
	err = glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	window, err = glfw.CreateWindow(800, 800, "Show Tiger in OpenGL", nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()
	window.SetSizeCallback(reshape)
	window.SetKeyCallback(onKey)

	glfw.SwapInterval(1)

	err = gl.Init()
	if err != nil {
		panic(err)
	}
	reshape(window, 800, 800)
	for !window.ShouldClose() {
		display()
		window.SwapBuffers()
		glfw.PollEvents()
		//		time.Sleep(2 * time.Second)
	}
}
Example #27
0
func (c *Context) Reset() error {
	if err := c.runOnContextThread(func() error {
		if c.init {
			return nil
		}
		// Note that this initialization must be done after Loop is called.
		if err := gl.Init(); err != nil {
			return fmt.Errorf("opengl: initializing error %v", err)
		}
		c.init = true
		return nil
	}); err != nil {
		return nil
	}
	c.locationCache = newLocationCache()
	c.lastTexture = invalidTexture
	c.lastFramebuffer = invalidFramebuffer
	c.lastViewportWidth = 0
	c.lastViewportHeight = 0
	c.lastCompositeMode = CompositeModeUnknown
	if err := c.runOnContextThread(func() error {
		gl.Enable(gl.BLEND)
		return nil
	}); err != nil {
		return err
	}
	c.BlendFunc(CompositeModeSourceOver)
	if err := c.runOnContextThread(func() error {
		f := int32(0)
		gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f)
		c.screenFramebuffer = Framebuffer(f)
		return nil
	}); err != nil {
		return err
	}
	return nil
}
Example #28
0
File: run.go Project: jxwufan/nes
func Run(paths []string) {
	// initialize audio
	portaudio.Initialize()
	defer portaudio.Terminate()

	audio := NewAudio()
	if err := audio.Start(); err != nil {
		log.Fatalln(err)
	}
	defer audio.Stop()

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

	// create window
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(width*scale, height*scale, title, nil, nil)
	if err != nil {
		log.Fatalln(err)
	}
	window.MakeContextCurrent()

	// initialize gl
	if err := gl.Init(); err != nil {
		log.Fatalln(err)
	}
	gl.Enable(gl.TEXTURE_2D)

	// run director
	director := NewDirector(window, audio)
	director.Start(paths)
}
Example #29
0
func TestMain(m *testing.M) {
	if err := gl.Init(); err != nil {
		log.Fatalf(`can't init GL: %s`, err)
	}
	os.Exit(m.Run())
}
Example #30
0
func main() {
	if err := glfw.Init(nopContextWatcher{}); err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	window, err := glfw.CreateWindow(1536, 960, "", nil, nil)
	if err != nil {
		panic(err)
	}
	globalWindow = window
	window.MakeContextCurrent()

	window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)

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

	glfw.SwapInterval(1) // Vsync.

	framebufferSizeCallback := func(w *glfw.Window, framebufferSize0, framebufferSize1 int) {
		gl.Viewport(0, 0, int32(framebufferSize0), int32(framebufferSize1))

		var windowSize [2]int
		windowSize[0], windowSize[1] = w.GetSize()

		// Update the projection matrix
		gl.MatrixMode(gl.PROJECTION)
		gl.LoadIdentity()
		gl.Ortho(0, float64(windowSize[0]), float64(windowSize[1]), 0, -1, 1)
		gl.MatrixMode(gl.MODELVIEW)
	}
	{
		var framebufferSize [2]int
		framebufferSize[0], framebufferSize[1] = window.GetFramebufferSize()
		framebufferSizeCallback(window, framebufferSize[0], framebufferSize[1])
	}
	window.SetFramebufferSizeCallback(framebufferSizeCallback)

	var inputEventQueue []InputEvent
	mousePointer = &Pointer{VirtualCategory: POINTING}

	var lastMousePos mgl64.Vec2
	lastMousePos[0], lastMousePos[1] = window.GetCursorPos()
	MousePos := func(w *glfw.Window, x, y float64) {
		//fmt.Println("MousePos:", x, y)

		inputEvent := InputEvent{
			Pointer:    mousePointer,
			EventTypes: map[EventType]bool{SLIDER_EVENT: true, AXIS_EVENT: true},
			InputId:    0,
			Buttons:    nil,
			Sliders:    []float64{x - lastMousePos[0], y - lastMousePos[1]}, // TODO: Do this in a pointer general way?
			Axes:       []float64{x, y},
		}
		lastMousePos[0] = x
		lastMousePos[1] = y
		inputEventQueue = EnqueueInputEvent(inputEvent, inputEventQueue)
	}
	window.SetCursorPosCallback(MousePos)
	MousePos(window, lastMousePos[0], lastMousePos[1])

	gl.ClearColor(0.85, 0.85, 0.85, 1)

	rand.Seed(4)
	var widget = newMultitouchTestBoxWidget(mgl64.Vec2{600, 300}, rand.Intn(6))
	var widget2 = newMultitouchTestBoxWidget(mgl64.Vec2{600 + 210, 300 + 210}, rand.Intn(6))
	var widget3 = newMultitouchTestBoxWidget(mgl64.Vec2{600 + 210, 300}, rand.Intn(6))
	var widget4 = newMultitouchTestBoxWidget(mgl64.Vec2{600, 300 + 210}, rand.Intn(6))

	go func() {
		<-time.After(5 * time.Second)
		log.Println("trigger!")
		widget.color++ // HACK: Racy.

		glfw.PostEmptyEvent()
	}()

	for !window.ShouldClose() {
		//glfw.PollEvents()
		glfw.WaitEvents()

		// Process Input.
		inputEventQueue = ProcessInputEventQueue(inputEventQueue)

		gl.Clear(gl.COLOR_BUFFER_BIT)

		widget.Render()
		widget2.Render()
		widget3.Render()
		widget4.Render()

		mousePointer.Render()

		window.SwapBuffers()
		log.Println("swapped buffers")
		runtime.Gosched()
	}
}