Example #1
0
func init() {
	go func() {
		runtime.LockOSThread()

		if err := glfw.Init(); err != nil {
			log.Panic("glfw Error:", err)
		}

		w, h := 100, 100
		err := glfw.OpenWindow(w, h, 0, 0, 0, 0, 0, 0, glfw.Windowed)
		if err != nil {
			log.Panic("Error:", err)
		}

		if gl.Init() != 0 {
			log.Panic("gl error")
		}

		glfw.SetWindowSizeCallback(Reshape)
		glfw.SwapBuffers()

		for {
			(<-main_thread_setup)()
			glfw.SwapBuffers()
			(<-main_thread_after)()
			main_thread_work_done <- true
		}
	}()
}
Example #2
0
func (w *glfwBackend) Render(console *Console) {
	if w.Running() {
		w.mouse.Left.Released = false
		w.mouse.Right.Released = false
		w.mouse.Middle.Released = false
		w.key = NOKEY

		gl.Clear(gl.COLOR_BUFFER_BIT)

		gl.BindTexture(gl.TEXTURE_2D, textures[1])
		for y := 0; y < console.Height(); y++ {
			for x := 0; x < console.Width(); x++ {
				_, bg, _ := console.Get(x, y)
				w.letter(x, y, 0, bg)
			}
		}

		gl.BindTexture(gl.TEXTURE_2D, textures[0])
		for y := 0; y < console.Height(); y++ {
			for x := 0; x < console.Width(); x++ {
				fg, _, ch := console.Get(x, y)
				if position, ok := w.font.Map(ch); ok {
					w.letter(x, y, position, fg)
				}
			}
		}

		glfw.SwapBuffers()
	}
}
Example #3
0
func main() {
	if err := glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
		return
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.FsaaSamples, 4)
	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

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

	gl.Init()
	gl.GetError() // Ignore error

	glfw.SetWindowTitle("Tutorial 01")
	glfw.Enable(glfw.StickyKeys)

	gl.ClearColor(0., 0., 0.4, 0.)
	// Equivalent to a do... while
	for ok := true; ok; ok = (glfw.Key(glfw.KeyEsc) != glfw.KeyPress && glfw.WindowParam(glfw.Opened) == gl.TRUE) {
		glfw.SwapBuffers()
	}

}
Example #4
0
func main() {
	var err error
	if err = glfw.Init(); err != nil {
		log.Fatalf("%v\n", err)
		return
	}

	defer glfw.Terminate()

	if err = glfw.OpenWindow(640, 480, 8, 8, 8, 8, 0, 0, glfw.Windowed); err != nil {
		log.Fatalf("%v\n", err)
		return
	}

	defer glfw.CloseWindow()

	glfw.SetWindowTitle("Draw")
	glfw.SetSwapInterval(1)
	glfw.SetKeyCallback(onKey)
	glfw.SetMouseButtonCallback(onMouseBtn)
	glfw.SetWindowSizeCallback(onResize)

	running = true
	for running && glfw.WindowParam(glfw.Opened) == 1 {
		if mouse[0] != 0 {
			pen.lineTo(glfw.MousePos())
		} else {
			pen.moveTo(glfw.MousePos())
		}

		glfw.SwapBuffers()
	}
}
Example #5
0
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.LoadIdentity()
	gl.Translatef(-1.5, 0, -6)
	gl.Rotatef(trisAngle, 0, 1, 0)

	gl.Begin(gl.TRIANGLES)
	gl.Color3f(1, 0, 0)
	gl.Vertex3f(0, 1, 0)
	gl.Color3f(0, 1, 0)
	gl.Vertex3f(-1, -1, 0)
	gl.Color3f(0, 0, 1)
	gl.Vertex3f(1, -1, 0)
	gl.End()

	gl.LoadIdentity()
	gl.Translatef(1.5, 0, -6)
	gl.Rotatef(quadAngle, 1, 0, 0)
	gl.Color3f(0.5, 0.5, 1.0)

	gl.Begin(gl.QUADS)
	gl.Vertex3f(-1, 1, 0)
	gl.Vertex3f(1, 1, 0)
	gl.Vertex3f(1, -1, 0)
	gl.Vertex3f(-1, -1, 0)
	gl.End()

	trisAngle += 0.2
	quadAngle -= 0.15

	glfw.SwapBuffers()
}
Example #6
0
func RunGame(title string, width, height int, init, draw func()) {
	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, 16, 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(width, height, init); err != nil {
		fmt.Fprintf(os.Stderr, "init: %s\n", err)
		return
	}
	// defer destroyScene()

	for glfw.WindowParam(glfw.Opened) == 1 {
		drawScene(draw)
		glfw.SwapBuffers()
	}
}
Example #7
0
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) /// 清除屏幕及深度缓存
	gl.LoadIdentity()                                   /// 重置模型观察矩阵

	gl.Translatef(-1.5, 0, -6) /// 左移 1.5 单位,并移入屏幕 6.0

	gl.Begin(gl.TRIANGLES) /// 绘制三角形
	gl.Color3f(1, 0, 0)    ///  设置当前色为红色
	gl.Vertex3f(0, 1, 0)   ///上顶点
	gl.Color3f(0, 1, 0)    /// 设置当前色为绿色
	gl.Vertex3f(-1, -1, 0) /// 左下
	gl.Color3f(0, 0, 1)    ///设置当前色为蓝色
	gl.Vertex3f(1, -1, 0)  ///右下
	gl.End()               ///三角形绘制结束,三角形将被填充。
	//但是因为每个顶点有不同的颜色,因此看起来颜色从每个角喷出,并刚好在三角形的中心汇合,三种颜色相互混合。这就是平滑着色。

	gl.Translatef(3, 0, 0)    ///右移3单位
	gl.Color3f(0.5, 0.5, 1.0) ///一次性将当前色设置为蓝色

	/// @note 顺时针绘制的正方形意味着我们所看见的是四边形的背面
	gl.Begin(gl.QUADS)     ///绘制正方形
	gl.Vertex3f(-1, 1, 0)  /// 左上
	gl.Vertex3f(1, 1, 0)   /// 右上
	gl.Vertex3f(1, -1, 0)  /// 右下
	gl.Vertex3f(-1, -1, 0) /// 左下
	gl.End()               ///正方形绘制结束

	glfw.SwapBuffers() ///必须交换显示区才能展现
}
Example #8
0
func main() {
	if err := glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
		return
	}

	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.FsaaSamples, 4)
	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

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

	gl.Init()     // Can't find gl.GLEW_OK or any variation, not sure how to check if this worked
	gl.GetError() // Ignore error

	glfw.SetWindowTitle("Tutorial 02")

	glfw.Enable(glfw.StickyKeys)
	gl.ClearColor(0., 0., 0.4, 0.)

	prog := helper.MakeProgram("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader")

	vBufferData := [...]float32{
		-1., -1., 0.,
		1., -1., 0.,
		0., 1., 0.}

	vertexArray := gl.GenVertexArray()
	vertexArray.Bind()
	buffer := gl.GenBuffer()
	buffer.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, len(vBufferData)*4, &vBufferData, gl.STATIC_DRAW)

	// Equivalent to a do... while
	for ok := true; ok; ok = (glfw.Key(glfw.KeyEsc) != glfw.KeyPress && glfw.WindowParam(glfw.Opened) == gl.TRUE) {
		gl.Clear(gl.COLOR_BUFFER_BIT)

		prog.Use()

		attribLoc := gl.AttribLocation(0)
		attribLoc.EnableArray()
		buffer.Bind(gl.ARRAY_BUFFER)
		attribLoc.AttribPointer(3, gl.FLOAT, false, 0, nil)

		gl.DrawArrays(gl.TRIANGLES, 0, 3)

		attribLoc.DisableArray()

		glfw.SwapBuffers()
	}

}
Example #9
0
func StartEngine() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	runtime.LockOSThread()
	fmt.Println("Enginge started!")
	var err error
	if err = glfw.Init(); err != nil {
		panic(err)
	}
	fmt.Println("GLFW Initialized!")

	glfw.OpenWindowHint(glfw.Accelerated, 1)

	if err = glfw.OpenWindow(Width, Height, 8, 8, 8, 8, 8, 8, glfw.Windowed); err != nil {
		panic(err)
	}

	glfw.SetSwapInterval(1) //0 to disable vsync, 1 to enable it
	glfw.SetWindowTitle(windowTitle)
	glfw.SetWindowSizeCallback(onResize)
	glfw.SetKeyCallback(input.OnKey)
	glfw.SetCharCallback(input.OnChar)
	glfw.SetMouseButtonCallback(input.ButtonPress)
	glfw.SetMouseWheel(0)
	glfw.SetMouseWheelCallback(input.MouseWheelCallback)

	input.MouseWheelPosition = glfw.MouseWheel
	input.MousePosition = glfw.MousePos

	if err = initGL(); err != nil {
		panic(err)
	}
	fmt.Println("Opengl Initialized!")

	TextureMaterial = NewBasicMaterial(spriteVertexShader, spriteFragmentShader)
	err = TextureMaterial.Load()
	if err != nil {
		fmt.Println(err)
	}

	SDFMaterial = NewBasicMaterial(sdfVertexShader, sdfFragmentShader)
	err = SDFMaterial.Load()
	if err != nil {
		fmt.Println(err)
	}

	internalMaterial = NewBasicMaterial(spriteVertexShader, spriteFragmentShader)
	err = internalMaterial.Load()
	if err != nil {
		fmt.Println(err)
	}

	initDefaultPlane()
	glfw.SwapBuffers()

	gameTime = time.Time{}
	lastTime = time.Now()
	dl = glfw.Time()
}
Example #10
0
// This should be used to resize the window during tests, to ensure that it is
// correctly resized before execution continues.
// Causes buffers to swap.
func SetWindowSize(width, height int) {
	glfw.SetWindowSize(width, height)
	// Need to wait for the reshape event, otherwise it happens at an arbitrary
	// point in the future (some unknown number of SwapBuffers())
	//glfw.PollEvents() // Doesn't work
	//glfw.WaitEvents() // might be racy (ideally, we'd need to send an event)
	time.Sleep(10 * time.Millisecond)
	glfw.SwapBuffers()
}
Example #11
0
File: emu.go Project: jteeuwen/dcpu
// Render renders the OpenGL backend.
func (e *Emu) Render() {
	for glfw.WindowParam(glfw.Opened) != 0 {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.LoadIdentity()

		e.disp.Render()

		glfw.SwapBuffers()
	}
}
Example #12
0
func main() {
	var err error
	if err = glfw.Init(); err != nil {
		log.Fatalf("%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 {
		log.Fatalf("%v\n", err)
		return
	}

	defer glfw.CloseWindow()

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

	if samples := glfw.WindowParam(glfw.FsaaSamples); samples != 0 {
		fmt.Printf("Context reports FSAA is supported with %d samples\n", samples)
	} else {
		fmt.Printf("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()
	}
}
Example #13
0
func main() {
	fmt.Print("GLFW Init Start.")
	glfw.Init()
	fmt.Println(" GLFW Init Done.")

	defer glfw.Terminate()
	defer fmt.Println("example Terminating.")

	fmt.Println("GLFW Set Hints.")
	glfw.OpenWindowHint(glfw.WindowNoResize, 1)

	fmt.Print("GLFW Open Window Start.")
	glfw.OpenWindow(640, 480, 8, 8, 8, 0, 0, 0, glfw.Windowed)
	glfw.SetWindowTitle("example")
	fmt.Println(" GLFW Open Window Done.")

	v1, v2, v3 := glfw.GLVersion()
	fmt.Printf("OpenGL version: %d.%d.%d\n", v1, v2, v3)
	fmt.Printf("GLFW version: %d.%d.%d\n", glfw.VersionMajor, glfw.VersionMinor, glfw.VersionRevision)

	glfw.SetSwapInterval(1)

	fps := time.Duration(30)
	fmt.Printf("Creating %d Hz Ticker.", fps)
	ticker := time.NewTicker(time.Second / fps)
	fmt.Printf(" %d Hz Ticker Created\n", fps)

	closedWindow := glfwHelper.WindowCloseChan()
	mouseButtons := glfwHelper.MouseButtonChan()
	mousePos := glfwHelper.MousePosChan()
	mouseWheel := glfwHelper.MouseWheelChan()
	keyButtons := glfwHelper.KeyChan()
	charButtons := glfwHelper.CharChan()

	for {
		select {
		case <-ticker.C:
			glfw.SwapBuffers()
		case <-closedWindow:
			return
		case input := <-mouseButtons:
			fmt.Println(input)
		case input := <-mousePos:
			fmt.Println(input)
		case input := <-mouseWheel:
			fmt.Println(input)
		case input := <-keyButtons:
			fmt.Println(input)
		case input := <-charButtons:
			fmt.Println(string(input))
		}
	}
}
Example #14
0
func Test(t *testing.T) {
	err := glfw.Init()
	if err != nil {
		t.Fatal(err)
	}

	defer glfw.Terminate()

	err = glfw.OpenWindow(800, 600, 8, 8, 8, 0, 0, 0, glfw.Windowed)
	if err != nil {
		t.Fatal(err)
	}

	defer glfw.CloseWindow()

	kb := New()
	kb.Bind(func() {
		fmt.Println("pressed s")
	}, "s")

	kb.Bind(func() {
		fmt.Println("pressed ctrl+s or command+s")
	}, "ctrl+s", "command+s")

	kb.Bind(func() {
		fmt.Println("pressed 't e s t'")
	}, "t e s t")

	kb.Bind(func() {
		fmt.Println("pressed '3'")
	}, "3")

	kb.Bind(func() {
		fmt.Println("pressed '#'")
	}, "#")

	kb.Bind(func() {
		glfw.CloseWindow()
	}, "escape")

	// Print available keybindings.
	fmt.Printf("Known key bindings:\n")
	for _, b := range kb.Bindings() {
		fmt.Printf(" - %q\n", b)
	}

	// Go into GLFW's main event loop.
	for glfw.WindowParam(glfw.Opened) == 1 {
		glfw.SwapBuffers()
	}
}
Example #15
0
func main() {
	var err error
	if err = glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "[e] %v\n", err)
		return
	}

	// Ensure glfw is cleanly terminated on exit.
	defer glfw.Terminate()

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

	// Ensure window is cleanly closed on exit.
	defer glfw.CloseWindow()

	// Enable vertical sync on cards that support it.
	glfw.SetSwapInterval(1)

	// Set window title
	glfw.SetWindowTitle("Simple GLFW window")

	// Hook some events to demonstrate use of callbacks.
	// These are not necessary if you don't need them.
	glfw.SetWindowSizeCallback(onResize)
	glfw.SetWindowCloseCallback(onClose)
	glfw.SetMouseButtonCallback(onMouseBtn)
	glfw.SetMouseWheelCallback(onMouseWheel)
	glfw.SetKeyCallback(onKey)
	glfw.SetCharCallback(onChar)

	// Start loop
	running := true
	for running {
		// OpenGL rendering goes here.

		// Swap front and back rendering buffers. This also implicitly calls
		// glfw.PollEvents(), so we have valid key/mouse/joystick states after
		// this. This behavior can be disabled by calling glfw.Disable with the
		// argument glfw.AutoPollEvents. You must be sure to manually call
		// PollEvents() or WaitEvents() in this case.
		glfw.SwapBuffers()

		// Break out of loop when Escape key is pressed, or window is closed.
		running = glfw.Key(glfw.KeyEsc) == 0 && glfw.WindowParam(glfw.Opened) == 1
	}
}
Example #16
0
func (_ *NgCore) showSplash() (err error) {
	splash := &Core.Libs.Images.SplashScreen
	splash.init()
	splash.Preprocess.FlipY, splash.Preprocess.ToLinear, splash.Preprocess.ToBgra = false, false, false
	splash.Load()
	if err = splash.GpuSync(); err != nil {
		return
	}
	thrRend.quadTex = splash.glTex.GlHandle
	Core.onRender()
	splash.Unload()
	splash.InitFrom.RawData = nil
	glfw.SwapBuffers()
	return
}
Example #17
0
func main() {
	// This file holds the actual glyph shapes.
	imgFile, err := gas.Abs("github.com/go-gl/examples/data/bitmap_font.png")
	if err != nil {
		log.Printf("Find font image file: %v", err)
		return
	}

	// This file is a JSON encoded dataset which describes the font
	// and contains the pixel offsets and sizes for each glyph in
	// bitmap_font.png. Both files are needed to load a bitmap font.
	configFile, err := gas.Abs("github.com/go-gl/examples/data/bitmap_font.js")
	if err != nil {
		log.Printf("Find font config file: %v", err)
		return
	}

	err = initGL()
	if err != nil {
		log.Printf("InitGL: %v", err)
		return
	}

	defer glfw.Terminate()

	// Load the same bitmap font at different scale factors.
	for i := range fonts {
		fonts[i], err = loadFont(imgFile, configFile, i+1)
		if err != nil {
			log.Printf("LoadFont: %v", err)
			return
		}

		defer fonts[i].Release()
	}

	for glfw.WindowParam(glfw.Opened) > 0 {
		gl.Clear(gl.COLOR_BUFFER_BIT)

		err = drawString(10, 10, "0 1 2 3 4 5 6 7 8 9 A B C D E F")
		if err != nil {
			log.Printf("Printf: %v", err)
			return
		}

		glfw.SwapBuffers()
	}
}
Example #18
0
func (s *Display) drawFrame(screenData *types.Screen) {
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.Disable(gl.DEPTH_TEST)
	gl.PointSize(float32(s.ScreenSizeMultiplier) + 1.0)
	gl.Begin(gl.POINTS)
	for y := 0; y < SCREEN_HEIGHT; y++ {
		for x := 0; x < SCREEN_WIDTH; x++ {
			var pixel types.RGB = screenData[y][x]
			gl.Color3ub(pixel.Red, pixel.Green, pixel.Blue)
			gl.Vertex2i(x*s.ScreenSizeMultiplier, y*s.ScreenSizeMultiplier)
		}
	}

	gl.End()
	glfw.SwapBuffers()
}
Example #19
0
func (rw *RenderWindow) draw() {
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.Enable(gl.LINE_SMOOTH)
	for chanIdx, channel := range rw.Channels {
		color := channel.GetColor()
		gl.Color4f(color[0], color[1], color[2], color[3])
		gl.Begin(gl.LINES)
		lastY := int64(0)
		for i := 0; i < len(rw.renderBuffers[chanIdx]); i += 1 {
			y := rw.Height - (rw.renderBuffers[chanIdx][i] * rw.Height / 1024)
			gl.Vertex2i(i-1, int(lastY))
			gl.Vertex2i(i, int(y))
			lastY = y
		}
		gl.Flush()
		gl.End()
	}
	glfw.SwapBuffers()
}
Example #20
0
func main() {
	err := initGL()
	if err != nil {
		log.Printf("InitGL: %v", err)
		return
	}

	defer glfw.Terminate()

	mb := createBuffer()
	defer mb.Release()

	// Perform the rendering.
	var angle float32
	for glfw.WindowParam(glfw.Opened) > 0 {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.LoadIdentity()
		gl.Translatef(0, 0, -6)
		gl.Rotatef(angle, 1, 1, 1)

		// Render a solid cube at half the scale.
		gl.Scalef(0.2, 0.2, 0.2)
		gl.Enable(gl.COLOR_MATERIAL)
		gl.Enable(gl.POLYGON_OFFSET_FILL)
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
		mb.Render(gl.QUADS)

		// Render wireframe cubes, with incremental size.
		gl.Disable(gl.COLOR_MATERIAL)
		gl.Disable(gl.POLYGON_OFFSET_FILL)
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)

		for i := 0; i < 50; i++ {
			scale := 0.004*float32(i) + 1.0
			gl.Scalef(scale, scale, scale)
			mb.Render(gl.QUADS)
		}

		angle += 0.5
		glfw.SwapBuffers()
	}
}
Example #21
0
func Tick() {
	timer = float32(glfw.Time())

	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	program.Use()

	camera.Tick()

	modelViewMatrixUniform.UniformMatrix4fv(camera.modelViewMatrix[:])
	projectionMatrixUniform.UniformMatrix4fv(camera.projectionMatrix[:])
	timerUniform.Uniform1f(timer)

	positionAttrib.EnableArray()
	texcoordAttrib.EnableArray()

	gl.ActiveTexture(gl.TEXTURE0)
	textures[0].Bind(gl.TEXTURE_2D)
	textureUniforms[0].Uniform1i(0)

	gl.ActiveTexture(gl.TEXTURE1)
	textures[1].Bind(gl.TEXTURE_2D)
	textureUniforms[1].Uniform1i(1)

	sizeOfVertex := int(unsafe.Sizeof(Vertex{}))
	posoffset := uintptr(0)
	texoffset := unsafe.Offsetof(Vertex{}.texcoord)
	for _, renderObject := range renderObjects {
		renderObject.vertexBuffer.Bind(gl.ARRAY_BUFFER)
		positionAttrib.AttribPointer(3, gl.FLOAT, false, sizeOfVertex, posoffset)
		texcoordAttrib.AttribPointer(2, gl.FLOAT, false, sizeOfVertex, texoffset)

		gl.DrawArrays(gl.TRIANGLES, 0, renderObject.numVerticies)
	}

	positionAttrib.DisableArray()
	texcoordAttrib.DisableArray()
	gl.ProgramUnuse()

	glfw.SwapBuffers()
}
Example #22
0
func (v *Video) Render(frame []byte, frame_w int, frame_h int) {
	gl.Clear(gl.COLOR_BUFFER_BIT)
	v.Texture.Bind(gl.TEXTURE_2D)

	gl.TexImage2D(gl.TEXTURE_2D, 0, 3, frame_w, frame_h, 0, gl.RGB, gl.UNSIGNED_BYTE, frame)

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

	gl.Begin(gl.QUADS)
	gl.TexCoord2f(0.0, 1.0)
	gl.Vertex3f(-1.0, -1.0, 0.0)
	gl.TexCoord2f(1.0, 1.0)
	gl.Vertex3f(1.0, -1.0, 0.0)
	gl.TexCoord2f(1.0, 0.0)
	gl.Vertex3f(1.0, 1.0, 0.0)
	gl.TexCoord2f(0.0, 0.0)
	gl.Vertex3f(-1.0, 1.0, 0.0)
	gl.End()

	glfw.SwapBuffers()
}
Example #23
0
func drawScene() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) /// 清除屏幕及深度缓存

	gl.LoadIdentity()              /// 重置模型观察矩阵
	gl.Translatef(-1.5, 0, -6)     /// 左移 1.5 单位,并移入屏幕 6.0
	gl.Rotatef(trisAngle, 0, 1, 0) ///以y为轴 参数:角度,X,Y,Z

	gl.Begin(gl.TRIANGLES) /// 绘制三角形
	gl.Color3f(1, 0, 0)    ///  设置当前色为红色
	gl.Vertex3f(0, 1, 0)   ///上顶点
	gl.Color3f(0, 1, 0)    /// 设置当前色为绿色
	gl.Vertex3f(-1, -1, 0) /// 左下
	gl.Color3f(0, 0, 1)    ///设置当前色为蓝色
	gl.Vertex3f(1, -1, 0)  ///右下
	gl.End()               ///三角形绘制结束,三角形将被填充。
	//但是因为每个顶点有不同的颜色,因此看起来颜色从每个角喷出,并刚好在三角形的中心汇合,三种颜色相互混合。这就是平滑着色。

	/// 要让对象绕自身的轴旋转,必须让对象的中心坐标总是(0.0f,0,0f,0,0f),因此这里的四边形是满屏跑的
	gl.LoadIdentity() ///将当前点移到了屏幕中心,X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。
	///OpenGL屏幕中心的坐标值是X和Y轴上的0.0f点。中心左面的坐标值是负值,右面是正值。移向屏幕顶端是正值,移向屏幕底端是负值。移入屏幕深处是负值,移出屏幕则是正值。
	gl.Rotatef(quadAngle, 1, 0, 0) /// 以x为轴
	gl.Translatef(1.5, 0, -6)      ///以当前点为起始点移动?(是的)-6为距离
	//gl.Translatef(3, 0, -6); ///右移3单位,看不见?(因为loadIdentity()置中了)
	gl.Color3f(0.5, 0.5, 1.0) ///一次性将当前色设置为蓝色

	/// @note 顺时针绘制的正方形意味着我们所看见的是四边形的背面
	gl.Begin(gl.QUADS)     ///绘制正方形
	gl.Vertex3f(-1, 1, 0)  /// 左上
	gl.Vertex3f(1, 1, 0)   /// 右上
	gl.Vertex3f(1, -1, 0)  /// 右下
	gl.Vertex3f(-1, -1, 0) /// 左下
	gl.End()               ///正方形绘制结束

	trisAngle += 0.2
	quadAngle -= 0.15

	glfw.SwapBuffers() ///必须交换显示区才能展现
}
Example #24
0
func main() {
	err := initGL()
	if err != nil {
		log.Printf("InitGL: %v", err)
		return
	}

	defer glfw.Terminate()

	// Create our texture atlas.
	atlas := glh.NewTextureAtlas(AtlasSize, AtlasSize, 4)
	defer atlas.Release()

	// Fill the altas with image data.
	fillAtlas(atlas)

	// Display the atlas texture on a quad, so we can see
	// what it looks like.
	for glfw.WindowParam(glfw.Opened) > 0 {
		gl.Clear(gl.COLOR_BUFFER_BIT)

		// Bind the atlas texture and render a quad with it.
		atlas.Bind(gl.TEXTURE_2D)
		gl.Begin(gl.QUADS)
		gl.TexCoord2f(0, 0)
		gl.Vertex2f(0, 0)
		gl.TexCoord2f(1, 0)
		gl.Vertex2f(AtlasSize, 0)
		gl.TexCoord2f(1, 1)
		gl.Vertex2f(AtlasSize, AtlasSize)
		gl.TexCoord2f(0, 1)
		gl.Vertex2f(0, AtlasSize)
		gl.End()
		atlas.Unbind(gl.TEXTURE_2D)

		glfw.SwapBuffers()
	}
}
Example #25
0
func main() {
	file, err := gas.Abs("code.google.com/p/freetype-go/luxi-fonts/luxisr.ttf")
	if err != nil {
		log.Printf("Find font file: %v", err)
		return
	}

	err = initGL()
	if err != nil {
		log.Printf("InitGL: %v", err)
		return
	}

	defer glfw.Terminate()

	// Load the same font at different scale factors and directions.
	for i := range fonts {
		fonts[i], err = loadFont(file, int32(12+i))
		if err != nil {
			log.Printf("LoadFont: %v", err)
			return
		}

		defer fonts[i].Release()
	}

	for glfw.WindowParam(glfw.Opened) > 0 {
		gl.Clear(gl.COLOR_BUFFER_BIT)

		err = drawString(10, 10, SampleString)
		if err != nil {
			log.Printf("Printf: %v", err)
			return
		}

		glfw.SwapBuffers()
	}
}
Example #26
0
func (v *video) draw(pixels []byte) {

	// No need to clear the screen since I explicitly redraw all pixels, at
	// least currently.

	gl.MatrixMode(gl.POLYGON)

	for yline := 0; yline < screenHeight; yline++ {

		for xline := 0; xline < screenWidth; xline++ {

			x, y := float32(xline), float32(yline)
			if pixels[xline+yline*64] == 0 {
				gl.Color3f(0, 0, 0)
			} else {
				gl.Color3f(1, 1, 1)
			}
			gl.Rectf(x, y, x+1, y+1)
		}
	}

	glfw.SwapBuffers()
}
func Run() {

	running = true

	for running {

		start := time.Now()

		escPressed := glfw.Key(glfw.KeyEsc)
		windowOpen := glfw.WindowParam(glfw.Opened)

		running = escPressed == 0 && windowOpen == 1

		select {
		case data := <-ScreenOutputChan:

			drawFrame(data.Pixels)
			drawInfoBox(3, 3, 12, data.IterNr, data.NThreads, data.ExecTime)
			glfw.SwapBuffers()

		default: // Non blocking!
			glfw.PollEvents()

		}

		// Limit to 60hz

		elapsed := time.Since(start)

		timeToSleep := 16*time.Millisecond - elapsed

		if timeToSleep > 0 {
			time.Sleep(timeToSleep)
		}

	}
}
Example #28
0
func main() {
	err := initGL()
	if err != nil {
		log.Printf("InitGL: %v", err)
		return
	}

	defer glfw.Terminate()

	mb := createBuffer()
	defer mb.Release()

	attr := mb.Colors()

	// Perform the rendering.
	for glfw.WindowParam(glfw.Opened) > 0 {
		gl.Clear(gl.COLOR_BUFFER_BIT)
		gl.LoadIdentity()

		// Center mesh on screen.
		wx, wy := glfw.WindowSize()
		px := (wx / 2) - ((Cols * CellWidth) / 2)
		py := (wy / 2) - ((Rows * CellHeight) / 2)
		gl.Translatef(float32(px), float32(py), 0)

		// Change the color of the quad under the mouse cursor.
		colorize(px, py, attr)

		// Render the mesh.
		mb.Render(gl.QUADS)

		glfw.SwapBuffers()
	}

	attr = nil
}
Example #29
0
func main() {
	err := InitWindow(Width, Height)
	if err != nil {
		panic(err)
	}

	RegisterEvent(NewKeyboardEvent(glfw.KeyEsc, glfw.KeyPress), CloseWindow)
	RegisterEvent(NewKeyboardEvent('W', glfw.KeyPress), func() {
		DefaultCamera.Move(0, 0.5, 0)
	})
	RegisterEvent(NewKeyboardEvent('S', glfw.KeyPress), func() {
		DefaultCamera.Move(0, -0.5, 0)
	})
	RegisterEvent(NewKeyboardEvent('A', glfw.KeyPress), func() {
		DefaultCamera.Move(-0.5, 0, 0)
	})
	RegisterEvent(NewKeyboardEvent('D', glfw.KeyPress), func() {
		DefaultCamera.Move(0.5, 0, 0)
	})
	RegisterEvent(NewKeyboardEvent('Z', glfw.KeyPress), func() {
		DefaultCamera.Move(0, 0, 0.5)
	})
	RegisterEvent(NewKeyboardEvent('X', glfw.KeyPress), func() {
		DefaultCamera.Move(0, 0, -0.5)
	})
	RegisterEvent(NewKeyboardEvent('Q', glfw.KeyPress), func() {
		DefaultCamera.Rotate(45, 0, 0, 1)
	})
	RegisterEvent(NewKeyboardEvent('E', glfw.KeyPress), func() {
		DefaultCamera.Rotate(-45.0, 0, 0, 1)
	})
	go PullEvents()
	// Temporary model
	m, err := NewModel([]Vertex{
		Vertex{X: -0.5, Y: -0.5, Z: -0.5, R: 1.0, G: 0.0, B: 0.0, A: 1.0},
		Vertex{X: 0.5, Y: -0.5, Z: -0.5, R: 0.0, G: 1.0, B: 0.0, A: 1.0},
		Vertex{X: -0.5, Y: -0.5, Z: 0.5, R: 1.0, G: 1.0, B: 0.0, A: 1.0},
		Vertex{X: 0.5, Y: -0.5, Z: 0.5, R: 0.0, G: 0.0, B: 1.0, A: 1.0},
		Vertex{X: -0.5, Y: 0.5, Z: -0.5, R: 1.0, G: 0.0, B: 1.0, A: 1.0},
		Vertex{X: 0.5, Y: 0.5, Z: -0.5, R: 0.0, G: 1.0, B: 1.0, A: 1.0},
		Vertex{X: -0.5, Y: 0.5, Z: 0.5, R: 1.0, G: 1.0, B: 1.0, A: 1.0},
		Vertex{X: 0.5, Y: 0.5, Z: 0.5, R: 1.0, G: 1.0, B: 1.0, A: 1.0}},
		[]uint32{
			0, 1, 3, 0, 3, 2,
			1, 5, 7, 1, 7, 3,
			5, 4, 6, 5, 6, 7,
			4, 0, 2, 4, 2, 6,
			2, 3, 7, 2, 7, 6,
			4, 5, 1, 4, 1, 0,
		})
	defer m.Destroy()
	if err != nil {
		panic(err)
	}
	w, err := NewWorld()
	defer w.Destroy()
	if err != nil {
		panic(err)
	}
	for WindowOpened {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
		DefaultCamera.SetView()
		w.Draw()
		m.Draw()
		glfw.SwapBuffers()
	}
}
Example #30
0
func main() {
	err := initGL()
	if err != nil {
		log.Printf("InitGL: %v", err)
		return
	}

	defer glfw.Terminate()

	mb := createBuffer()
	defer mb.Release()

	gl.Enable(gl.BLEND)
	//gl.BlendFunc(gl.SRC_ALPHA, gl.ONE)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	f, err := os.Create("test.mpeg")
	if err != nil {
		log.Panicf("Unable to open output file: %q", err)
	}

	im := image.NewRGBA(image.Rect(0, 0, 512, 512))
	e, err := ffmpeg.NewEncoder(ffmpeg.CODEC_ID_H264, im, f)
	if err != nil {
		log.Panicf("Unable to start encoder: %q", err)
	}

	// Perform the rendering.
	var angle float64
	for glfw.WindowParam(glfw.Opened) > 0 {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.LoadIdentity()
		gl.Translatef(0, 0, -6)
		gl.Rotated(angle/10, 0, 1, 0)
		gl.Rotated(angle, 1, 1, 1)

		// Render a solid cube at half the scale.
		gl.Scalef(0.1, 0.1, 0.1)
		gl.Enable(gl.COLOR_MATERIAL)
		gl.Enable(gl.POLYGON_OFFSET_FILL)
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
		mb.Render(gl.QUADS)

		// Render wireframe cubes, with incremental size.
		gl.Disable(gl.COLOR_MATERIAL)
		gl.Disable(gl.POLYGON_OFFSET_FILL)
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)

		for i := 0; i < 1000; i++ {
			ifl := float64(i)
			scale := 0.000005*ifl + 1.0 + 0.01*math.Sin(angle/10+ifl/10)
			gl.Scaled(scale, scale, scale)
			gl.Rotated((ifl+angle)/1000, 1, 1, 1)
			gl.Rotated((ifl+angle)/1100, -1, 1, 1)

			mb.Render(gl.QUADS)
		}

		glh.ClearAlpha(1)
		glh.CaptureRGBA(im)

		err := e.WriteFrame()
		if err != nil {
			panic(err)
		}

		angle += 0.5
		//glh.CaptureToPng("test.png")
		glfw.SwapBuffers()
	}
	e.Close()
}