Exemplo n.º 1
0
func (f *FrameBuffer) Bind() {
    gl.BindTexture(gl.TEXTURE_2D, 0) // why?

    // bind this frame buffer
    gl.BindFramebuffer(gl.FRAMEBUFFER, f.id)

    // set viewport size equal to buffer size
    gl.Viewport(0, 0, f.Width, f.Height)
}
Exemplo n.º 2
0
func main() {
	runtime.LockOSThread()

	initGlfw()
	setOpenGlVersion()
	win := makeWindow()
	printOpenGlVersionInfo()

	vao := createVao()

	gl.Viewport(0, 0, windowWidth, windowHeight)

	shader := createProgram()

	drawLoop(win, vao, shader)
}
Exemplo n.º 3
0
func (m *Manager) Draw() {
	/* create draw event args */
	p := m.Viewport
	v := mgl.Ident4()
	vp := p

	args := render.DrawArgs{
		Projection: p,
		View:       v,
		VP:         vp,
		MVP:        vp,
		Transform:  mgl.Ident4(),
	}

	gl.Viewport(0, 0, int32(m.Width), int32(m.Height))

	for _, el := range m.Children {
		el.Draw(args)
	}
}
Exemplo n.º 4
0
func (screen *Screen) Init(title string, onCloseHandler func(), chip *chip8.Chip8) {
	var err error

	screen.chip = chip

	err = glfw.Init()

	if err != nil {
		panic(err)
	}

	window, err := glfw.CreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, title, nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()

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

	version := gl.GoStr(gl.GetString(gl.VERSION))
	fmt.Println("OpenGL version", version)

	gl.ClearColor(255.0, 0.0, 0.0, 0.0)                          //Set the cleared screen colour to black
	gl.Viewport(0, 0, int32(SCREEN_WIDTH), int32(SCREEN_HEIGHT)) //This sets up the viewport so that the coordinates (0, 0) are at the top left of the window

	for !window.ShouldClose() {
		// Do OpenGL stuff
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		window.SwapBuffers()
		glfw.PollEvents()
	}

	onCloseHandler()

}
Exemplo n.º 5
0
func main() {
	var parser = flags.NewParser(&gOpts, flags.Default)

	var err error
	var args []string
	if args, err = parser.Parse(); err != nil {
		os.Exit(1)
	}

	if len(args) < 1 || len(args) > 2 {
		panic(fmt.Errorf("Too many or not enough arguments"))
	}

	gDiffFlag = len(args) == 2

	// make sure that we display any errors that are encountered
	//glfw.SetErrorCallback(errorCallback)

	// the GLFW library has to be initialized before any of the methods
	// can be invoked
	if err = glfw.Init(); err != nil {
		panic(err)
	}

	// to be tidy, make sure glfw.Terminate() is called at the end of
	// the program to clean things up by using `defer`
	defer glfw.Terminate()

	// hints are the way you configure the features requested for the
	// window and are required to be set before calling glfw.CreateWindow().

	// desired number of samples to use for mulitsampling
	//glfw.WindowHint(glfw.Samples, 4)

	// request a OpenGL 4.1 core context
	if runtime.GOOS == "darwin" {
		glfw.WindowHint(glfw.ContextVersionMajor, 3)
		glfw.WindowHint(glfw.ContextVersionMinor, 3)
	} else {
		glfw.WindowHint(glfw.ContextVersionMajor, 4)
		glfw.WindowHint(glfw.ContextVersionMinor, 1)
	}
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

	// do the actual window creation
	var window *glfw.Window
	window, err = glfw.CreateWindow(1024, 768, "goicmpgl", nil, nil)
	if err != nil {
		// we legitimately cannot recover from a failure to create
		// the window in this sample, so just bail out
		panic(err)
	}

	// set the callback function to get all of the key input from the user
	window.SetKeyCallback(keyCallback)
	window.SetMouseButtonCallback(mouseDownCallback)
	window.SetScrollCallback(mouseWheelCallback)
	window.SetCursorPosCallback(mouseMoveCallback)

	// GLFW3 can work with more than one window, so make sure we set our
	// new window as the current context to operate on
	window.MakeContextCurrent()

	// disable v-sync for max FPS if the driver allows it
	//glfw.SwapInterval(0)

	// make sure that GLEW initializes all of the GL functions
	if err = gl.Init(); err != nil {
		panic(err)
	}

	var attribs []string = []string{
		"position",
		"uvs",
	}

	// compile our shaders
	var progTex0 *Program
	if progTex0, err = LoadShaderProgram(vertShader, fragShaderTex0, attribs); err != nil {
		panic(err)
	}
	defer progTex0.DeleteProgram()

	var progGrid *Program
	if progGrid, err = LoadShaderProgram(vertShader, S_FragmentShader_Grid, attribs); err != nil {
		panic(err)
	}
	defer progGrid.DeleteProgram()

	var diffProg2 *Program
	if diffProg2, err = LoadShaderProgram(vertShader, sProgram2Src, attribs); err != nil {
		panic(err)
	}
	defer diffProg2.DeleteProgram()

	var diffProg3 *Program
	if diffProg3, err = LoadShaderProgram(vertShader, sProgram3Src, attribs); err != nil {
		panic(err)
	}
	defer diffProg3.DeleteProgram()

	var diffProg4 *Program
	if diffProg4, err = LoadShaderProgram(vertShader, sProgram4Src, attribs); err != nil {
		panic(err)
	}
	defer diffProg4.DeleteProgram()

	var diffProg5 *Program
	if diffProg5, err = LoadShaderProgram(vertShader, sProgram5Src, attribs); err != nil {
		panic(err)
	}
	defer diffProg5.DeleteProgram()

	var image1_path string = args[0]
	if strings.HasPrefix(image1_path, "http") {
		if err, image1_path = downloadImage(image1_path); err != nil {
			panic(err)
		}
	}

	var texture *Texture
	if err, texture, gImage1 = NewTexture(image1_path, false); err != nil {
		panic(err)
	}
	defer texture.DeleteTexture()

	var texture2 *Texture
	if gDiffFlag {
		var image2_path string = args[1]
		if strings.HasPrefix(image2_path, "http") {
			if err, image2_path = downloadImage(image2_path); err != nil {
				panic(err)
			}
		}

		if err, texture2, gImage2 = NewTexture(image2_path, false); err != nil {
			panic(err)
		}
		defer texture2.DeleteTexture()

		if texture.Size.X != texture2.Size.X || texture.Size.Y != texture2.Size.Y {
			fmt.Println("WARNING: image dimensions differ!")
		} else {
			fmt.Printf("image dimensions: %dx%d\n", texture.Size.X, texture.Size.Y)
		}
	} else {
		fmt.Printf("image dimensions: %dx%d\n", texture.Size.X, texture.Size.Y)
	}

	var font *Font
	if err, font = NewFont("Font.png", 16); err != nil {
		panic(err)
	}
	defer font.DeleteFont()

	var help1 *String = font.NewString("1: show only A")
	defer help1.DeleteString()
	var help2 *String = font.NewString("2: show only B")
	defer help2.DeleteString()
	var help3 *String = font.NewString("3: show diff A&B")
	defer help3.DeleteString()
	var helph *String = font.NewString("h: toggle this help")
	defer helph.DeleteString()
	var helparrows *String = font.NewString("<up>,<down>: go from A to B")
	defer helparrows.DeleteString()
	var helpzoom *String = font.NewString("[]: zoom in/out (also mouse wheel)")
	defer helpzoom.DeleteString()
	var helpclear *String = font.NewString("Z: reset zoom/view")
	defer helpclear.DeleteString()
	var helpescape *String = font.NewString("ESC: quit")
	defer helpescape.DeleteString()

	var vbo *VBO
	if vbo, err = NewVBOQuad(0, 0, float32(texture.Size.X), float32(texture.Size.Y)); err != nil {
		panic(err)
	}
	defer vbo.DeleteVBO()

	var cnt float32 = 0

	// while there's no request to close the window
	for !window.ShouldClose() {
		cnt += 1

		// get the texture of the window because it may have changed since creation
		width, height := window.GetFramebufferSize()
		wwidth, _ := window.GetSize()
		gRetinaScale = float32(width) / float32(wwidth)

		//fmt.Printf("x=%d y=%d wx=%d wy=%d\n", width, height, wwidth, wheight)

		if cnt >= float32(width) {
			cnt = 0
		}

		var matrix Matrix2x3 = IdentityMatrix2x3()
		matrix = matrix.Translate(-1.0, 1.0)
		matrix = matrix.Scale(2.0/float32(width), -2.0/float32(height))

		// clear it all out
		gl.Viewport(0, 0, int32(width), int32(height))
		gl.ClearColor(0.0, 0.0, 0.0, 1.0)
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
		gl.BlendEquation(gl.FUNC_ADD)

		var matrix3 Matrix2x3 = matrix.Scale(gZoom, gZoom)
		matrix3 = matrix3.Translate(gOffX, gOffY)

		// draw the grid
		if true {
			vbo.Bind()
			progGrid.UseProgram()

			color1 := [4]float32{.4, .4, .4, 1}
			color2 := [4]float32{.9, .9, .9, 1}
			grid := [3]float32{float32(texture.Size.X), float32(texture.Size.Y), 8 / gZoom}
			//fmt.Printf("%.2f %.2f %.2f %.2f\n", grid[0], grid[1], grid[2], grid[3])
			progGrid.ProgramUniformMatrix4fv("ModelviewMatrix", matrix3.Array())
			progGrid.ProgramUniform4fv("color1", color1)
			progGrid.ProgramUniform4fv("color2", color2)
			progGrid.ProgramUniform3fv("grid", grid)

			if err = progGrid.ValidateProgram(); err != nil {
				panic(err)
			}

			vbo.Draw()

			vbo.Unbind()
			progGrid.UnuseProgram()
		}

		// draw the texture
		if !gDiffFlag {
			vbo.Bind()
			progTex0.UseProgram()
			texture.BindTexture(0)

			progTex0.ProgramUniformMatrix4fv("ModelviewMatrix", matrix3.Array())
			progTex0.ProgramUniform1i("tex1", 0)
			progTex0.ProgramUniform1f("blend", gBlend)

			if err = progTex0.ValidateProgram(); err != nil {
				panic(err)
			}

			vbo.Draw()

			vbo.Unbind()
			progTex0.UnuseProgram()
			texture.UnbindTexture(0)
		} else {
			var diffBlend float32 = gBlend

			var diffProg *Program

			if diffBlend < 0.25 {
				diffBlend *= 4
				diffProg = diffProg2
			} else if diffBlend < 0.5 { // 0.25 -> 0.5
				diffBlend = 4*diffBlend - 1
				diffProg = diffProg4
			} else if diffBlend < 0.75 { // 0.5 -> 0.75
				diffBlend = 4*diffBlend - 2
				diffProg = diffProg5
			} else { // 0.75 -> 1.0=
				diffBlend = 4*diffBlend - 3
				diffProg = diffProg3
			}

			vbo.Bind()
			diffProg.UseProgram()
			texture.BindTexture(0)
			texture2.BindTexture(1)

			diffProg.ProgramUniformMatrix4fv("ModelviewMatrix", matrix3.Array())
			diffProg.ProgramUniform1i("decalA", 0)
			diffProg.ProgramUniform1i("decalB", 1)
			diffProg.ProgramUniform1f("diffBlend", diffBlend)

			if err = diffProg.ValidateProgram(); err != nil {
				panic(err)
			}

			vbo.Draw()

			vbo.Unbind()
			diffProg.UnuseProgram()
			texture.UnbindTexture(0)
			texture2.UnbindTexture(1)
		}

		// font
		if gHelp {
			color := [...]float32{0, 0, 1, 1}
			bg := [...]float32{0.5, 0.5, 0.5, 0.5}
			var line float32 = 0
			if err = helph.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128); err != nil {
				panic(err)
			}
			line += 1
			help1.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
			line += 1
			help2.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
			line += 1
			help3.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
			line += 1
			helparrows.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
			line += 1
			helpzoom.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
			line += 1
			helpclear.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
			line += 1
			helpescape.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
			line += 1
		}

		// swapping OpenGL buffers and polling events has been decoupled
		// in GLFW3, so make sure to invoke both here
		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Exemplo n.º 6
0
func (p *LightPass) DrawPass(scene *Scene) {
	/* use light pass shader */
	p.Material.Use()
	shader := p.Material.Shader

	/* compute camera view projection inverse */
	vp := scene.Camera.Projection.Mul4(scene.Camera.View)
	vp_inv := vp.Inv()
	shader.Matrix4f("cameraInverse", &vp_inv[0])

	/* clear */
	gl.ClearColor(0.9, 0.9, 0.9, 1.0)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	/* set blending mode to additive */

	gl.DepthMask(false)

	/* draw lights */
	lights := scene.FindLights()
	last := len(lights) - 1

	for i, light := range lights {
		if i == 1 {
			/* first light pass we want the shader to restore the depth buffer
			 * then, disable depth masking so that multiple lights can be drawn */
			gl.BlendFunc(gl.ONE, gl.ONE)
		}
		if i == last {
			gl.DepthMask(true)
		}

		/* draw shadow pass for this light into shadow map */
		p.Shadows.DrawPass(scene, &light)

		/* use light pass shader */
		p.Material.Use()

		/* compute world to lightspace (light view projection) matrix */
		lp := light.Projection
		lv := mgl.LookAtV(light.Position, mgl.Vec3{}, mgl.Vec3{0, 1, 0}) // only for directional light
		lvp := lp.Mul4(lv)
		shader.Matrix4f("light_vp", &lvp[0])

		/* set light uniform attributes */
		shader.Vec3("light.Position", &light.Position)
		shader.Vec3("light.Color", &light.Color)
		shader.Int32("light.Type", int32(light.Type))
		shader.Float("light.Range", light.Range)
		shader.Float("light.attenuation.Constant", light.Attenuation.Constant)
		shader.Float("light.attenuation.Linear", light.Attenuation.Linear)
		shader.Float("light.attenuation.Quadratic", light.Attenuation.Quadratic)

		/* render light */
		gl.Viewport(0, 0, int32(scene.Camera.Width), int32(scene.Camera.Height))
		p.quad.Draw()
	}

	/* reset GL state */
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
}