Пример #1
0
// initGL initializes GLFW and OpenGL.
func initGL() error {
	err := glfw.Init()
	if err != nil {
		return err
	}

	err = glfw.OpenWindow(AtlasSize, AtlasSize, 8, 8, 8, 8, 0, 0, glfw.Windowed)
	if err != nil {
		glfw.Terminate()
		return err
	}

	glfw.SetWindowTitle("Texture atlas example")
	glfw.SetSwapInterval(1)
	glfw.SetWindowSizeCallback(onResize)
	glfw.SetKeyCallback(onKey)

	gl.Init()
	if err = glh.CheckGLError(); err != nil {
		return err
	}

	gl.Disable(gl.DEPTH_TEST)
	gl.Disable(gl.LIGHTING)
	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0.2, 0.2, 0.23, 1.0)
	return nil
}
Пример #2
0
// initGL initializes GLFW and OpenGL.
func initGL() error {
	err := glfw.Init()
	if err != nil {
		return err
	}

	glfw.OpenWindowHint(glfw.FsaaSamples, 4)

	err = glfw.OpenWindow(512, 512, 8, 8, 8, 8, 0, 0, glfw.Windowed)
	if err != nil {
		glfw.Terminate()
		return err
	}

	glfw.SetWindowTitle("Meshbuffer 2D example")
	glfw.SetSwapInterval(1)
	glfw.SetWindowSizeCallback(onResize)
	glfw.SetKeyCallback(onKey)

	gl.Init()
	if err = glh.CheckGLError(); err != nil {
		return err
	}

	gl.Disable(gl.DEPTH_TEST)
	gl.Enable(gl.MULTISAMPLE)
	gl.Disable(gl.LIGHTING)
	gl.Enable(gl.COLOR_MATERIAL)
	gl.ClearColor(0.2, 0.2, 0.23, 1.0)
	return nil
}
Пример #3
0
// initGL initializes GLFW and OpenGL.
func initGL(c *cpu.CPU, width, height int) error {
	err := glfw.Init()
	if err != nil {
		return err
	}

	glfw.OpenWindowHint(glfw.WindowNoResize, 1)

	err = glfw.OpenWindow(width, height, 8, 8, 8, 8, 8, 0, glfw.Windowed)
	if err != nil {
		glfw.Terminate()
		return err
	}

	glfw.SetWindowTitle("DCPU")
	glfw.SetSwapInterval(1)
	glfw.SetWindowSizeCallback(onResize)

	gl.Init()
	err = glh.CheckGLError()
	if err != nil {
		return err
	}

	gl.Disable(gl.LIGHTING)
	gl.Disable(gl.DEPTH_TEST)
	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0, 0, 0, 1)
	return err
}
Пример #4
0
func initGL() error {
	gl.Init()
	if err := glh.CheckGLError(); err != nil {
		return err
	}

	gl.Disable(gl.DEPTH_TEST)
	gl.Enable(gl.MULTISAMPLE)
	gl.Disable(gl.LIGHTING)
	gl.Enable(gl.COLOR_MATERIAL)
	gl.ClearColor(0, 0, 0, 1)

	return nil
}
Пример #5
0
// initGL initializes GLFW and OpenGL.
func initGL() error {
	err := glfw.Init()
	if err != nil {
		return err
	}

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

	err = glfw.OpenWindow(512, 512, 8, 8, 8, 8, 32, 0, glfw.Windowed)
	if err != nil {
		glfw.Terminate()
		return err
	}

	glfw.SetWindowTitle("Meshbuffer 3D example")
	glfw.SetSwapInterval(1)
	glfw.SetWindowSizeCallback(onResize)
	glfw.SetKeyCallback(onKey)

	gl.Init()
	if err = glh.CheckGLError(); err != nil {
		return err
	}

	gl.Enable(gl.DEPTH_TEST)
	gl.Enable(gl.MULTISAMPLE)
	gl.Disable(gl.LIGHTING)

	//gl.ClearColor(0.2, 0.2, 0.23, 1.0)
	gl.ClearColor(0, 0, 0, 1.0)
	gl.ShadeModel(gl.SMOOTH)
	gl.LineWidth(2)
	gl.ClearDepth(1)
	gl.DepthFunc(gl.LEQUAL)
	gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
	gl.ColorMaterial(gl.FRONT_AND_BACK, gl.AMBIENT_AND_DIFFUSE)
	return nil
}
Пример #6
0
func (v *video) init() error {
	// Disclaimer: I'm an absolute noob in openGL and I just slammed the
	// keyboard with my text editor open, until it started to produce usable
	// results. I don't fully understand everything I'm doing here.
	if err := glfw.Init(); err != nil {
		return err
	}

	err := glfw.OpenWindow(screenWidth*pixelSize, screenHeight*pixelSize, 0, 0, 0, 0, 0, 0, glfw.Windowed)
	if err != nil {
		return err
	}

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

	glfw.SetWindowTitle("nictuku's CHIP-8 emulator")

	gl.Init()
	if err = glh.CheckGLError(); err != nil {
		return err
	}

	gl.ClearColor(0, 0, 0, 0)
	gl.MatrixMode(gl.PROJECTION)

	// Change coordinates to range from [0, 64] and [0,32].
	gl.Ortho(0, screenWidth, screenHeight, 0, 0, 1)

	// Unnecessary sanity check. :-P
	if glfw.WindowParam(glfw.Opened) == 0 {
		return fmt.Errorf("No window opened")
	}

	return nil
}
Пример #7
0
// Printf draws the given string at the specified coordinates.
// It expects the string to be a single line. Line breaks are not
// handled as line breaks and are rendered as glyphs.
//
// In order to render multi-line text, it is up to the caller to split
// the text up into individual lines of adequate length and then call
// this method for each line seperately.
func (f *Font) Printf(x, y float32, fs string, argv ...interface{}) error {
	indices := []rune(fmt.Sprintf(fs, argv...))

	if len(indices) == 0 {
		return nil
	}

	// Runes form display list indices.
	// For this purpose, they need to be offset by -FontConfig.Low
	low := f.Config.Low
	for i := range indices {
		indices[i] -= low
	}

	var vp [4]int32
	gl.GetIntegerv(gl.VIEWPORT, vp[:])

	gl.PushAttrib(gl.TRANSFORM_BIT)
	gl.MatrixMode(gl.PROJECTION)
	gl.PushMatrix()
	gl.LoadIdentity()
	gl.Ortho(float64(vp[0]), float64(vp[2]), float64(vp[1]), float64(vp[3]), 0, 1)
	gl.PopAttrib()

	gl.PushAttrib(gl.LIST_BIT | gl.CURRENT_BIT | gl.ENABLE_BIT | gl.TRANSFORM_BIT)
	{
		gl.MatrixMode(gl.MODELVIEW)
		gl.Disable(gl.LIGHTING)
		gl.Disable(gl.DEPTH_TEST)
		gl.Enable(gl.BLEND)
		gl.Enable(gl.TEXTURE_2D)

		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)
		f.Texture.Bind(gl.TEXTURE_2D)

		var mv [16]float32
		gl.GetFloatv(gl.MODELVIEW_MATRIX, mv[:])

		gl.PushMatrix()
		{
			gl.LoadIdentity()

			mgw := float32(f.maxGlyphWidth)
			mgh := float32(f.maxGlyphHeight)

			switch f.Config.Dir {
			case LeftToRight, TopToBottom:
				gl.Translatef(x, float32(vp[3])-y-mgh, 0)
			case RightToLeft:
				gl.Translatef(x-mgw, float32(vp[3])-y-mgh, 0)
			}

			gl.MultMatrixf(&mv)
			gl.CallLists(len(indices), gl.UNSIGNED_INT, indices)
		}
		gl.PopMatrix()
		f.Texture.Unbind(gl.TEXTURE_2D)
	}
	gl.PopAttrib()

	gl.PushAttrib(gl.TRANSFORM_BIT)
	gl.MatrixMode(gl.PROJECTION)
	gl.PopMatrix()
	gl.PopAttrib()
	return glh.CheckGLError()
}
Пример #8
0
// loadFont loads the given font data. This does not deal with font scaling.
// Scaling should be handled by the independent Bitmap/Truetype loaders.
// We therefore expect the supplied image and charset to already be adjusted
// to the correct font scale.
//
// The image should hold a sprite sheet, defining the graphical layout for
// every glyph. The config describes font metadata.
func loadFont(img *image.RGBA, config *FontConfig) (f *Font, err error) {
	f = new(Font)
	f.config = config

	// Resize image to next power-of-two.
	img = glh.Pow2Image(img).(*image.RGBA)
	ib := img.Bounds()

	// Create the texture itself. It will contain all glyphs.
	// Individual glyph-quads display a subset of this texture.
	f.texture = gl.GenTexture()
	f.texture.Bind(gl.TEXTURE_2D)
	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.RGBA, ib.Dx(), ib.Dy(), 0,
		gl.RGBA, gl.UNSIGNED_BYTE, img.Pix)

	// Create display lists for each glyph.
	f.listbase = gl.GenLists(len(config.Glyphs))

	texWidth := float32(ib.Dx())
	texHeight := float32(ib.Dy())

	for index, glyph := range config.Glyphs {
		// Update max glyph bounds.
		if glyph.Width > f.maxGlyphWidth {
			f.maxGlyphWidth = glyph.Width
		}

		if glyph.Height > f.maxGlyphHeight {
			f.maxGlyphHeight = glyph.Height
		}

		// Quad width/height
		vw := float32(glyph.Width)
		vh := float32(glyph.Height)

		// Texture coordinate offsets.
		tx1 := float32(glyph.X) / texWidth
		ty1 := float32(glyph.Y) / texHeight
		tx2 := (float32(glyph.X) + vw) / texWidth
		ty2 := (float32(glyph.Y) + vh) / texHeight

		// Advance width (or height if we render top-to-bottom)
		adv := float32(glyph.Advance)

		gl.NewList(f.listbase+uint(index), gl.COMPILE)
		{
			gl.Begin(gl.QUADS)
			{
				gl.TexCoord2f(tx1, ty2)
				gl.Vertex2f(0, 0)
				gl.TexCoord2f(tx2, ty2)
				gl.Vertex2f(vw, 0)
				gl.TexCoord2f(tx2, ty1)
				gl.Vertex2f(vw, vh)
				gl.TexCoord2f(tx1, ty1)
				gl.Vertex2f(0, vh)
			}
			gl.End()

			switch config.Dir {
			case LeftToRight:
				gl.Translatef(adv, 0, 0)
			case RightToLeft:
				gl.Translatef(-adv, 0, 0)
			case TopToBottom:
				gl.Translatef(0, -adv, 0)
			}
		}
		gl.EndList()
	}

	err = glh.CheckGLError()
	return
}