Ejemplo n.º 1
0
// InitGraphics creates an OpenGL window and initializes the required graphics libraries.
// It will either succeed or panic.
func (app *ExampleApp) InitGraphics(title string, w int, h int) {
	err := glfw.Init()
	if err != nil {
		panic("Can't init glfw! " + err.Error())
	}

	// request a OpenGL 3.3 core context
	glfw.WindowHint(glfw.Samples, 0)
	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

	// do the actual window creation
	app.MainWindow, err = glfw.CreateWindow(w, h, title, nil, nil)
	if err != nil {
		panic("Failed to create the main window! " + err.Error())
	}
	app.MainWindow.MakeContextCurrent()
	glfw.SwapInterval(0)

	// make sure that all of the GL functions are initialized
	err = gl.Init()
	if err != nil {
		panic("Failed to initialize GL! " + err.Error())
	}

	// set the app window dimensions
	app.Width = w
	app.Height = h

	gl.FrontFace(gl.CCW)
	gl.CullFace(gl.BACK)
	gl.Enable(gl.CULL_FACE)
}
Ejemplo n.º 2
0
//BindForDrawing binds this fbo, change face culling for back face, start using the shadow program, calculate projection and clears the texture.
func (sfbo *ShadowFBO) BindForDrawing() {
	sfbo.framebuffer.Bind(gl2.FRAMEBUFFER)
	sfbo.program.Use()
	sfbo.vp = sfbo.projection.Mul4(sfbo.view)
	gl.Clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT)
	ViewportChange(sfbo.width, sfbo.height)
	gl.CullFace(gl.FRONT)
}
Ejemplo n.º 3
0
func Init() error {
	if err := gl.Init(); err != nil {
		return err
	}

	log.Printf("OpenGL version: %s", gl.GoStr(gl.GetString(gl.VERSION)))

	vs, err := asset.String("shader.vert")
	if err != nil {
		return err
	}

	fs, err := asset.String("shader.frag")
	if err != nil {
		return err
	}

	program, err := createProgram(vs, fs)
	if err != nil {
		return err
	}
	gl.UseProgram(program)

	var shaderErr error
	uniform := func(name string) int32 {
		var loc int32
		loc, shaderErr = getUniformLocation(program, name)
		return loc
	}

	projectionViewMatrixUniform = uniform("u_projectionViewMatrix")
	modelMatrixUniform = uniform("u_modelMatrix")
	normalMatrixUniform = uniform("u_normalMatrix")
	ambientLightColorUniform = uniform("u_ambientLightColor")
	directionalLightColorUniform = uniform("u_directionalLightColor")
	directionalVectorUniform = uniform("u_directionalVector")
	textureUniform = uniform("u_texture")
	grayscaleUniform = uniform("u_grayscale")
	brightnessUniform = uniform("u_brightness")
	alphaUniform = uniform("u_alpha")
	mixColorUniform = uniform("u_mixColor")
	mixAmountUniform = uniform("u_mixAmount")

	if shaderErr != nil {
		return shaderErr
	}

	vm := newViewMatrix(cameraPosition, targetPosition, up)
	nm := vm.inverse().transpose()
	gl.UniformMatrix4fv(normalMatrixUniform, 1, false, &nm[0])

	gl.Uniform3fv(ambientLightColorUniform, 1, &ambientLightColor[0])
	gl.Uniform3fv(directionalLightColorUniform, 1, &directionalLightColor[0])
	gl.Uniform3fv(directionalVectorUniform, 1, &directionalVector[0])

	SizeCallback = func(width, height int) {
		if winWidth == width && winHeight == height {
			return
		}

		log.Printf("window size changed (%dx%d -> %dx%d)", int(winWidth), int(winHeight), width, height)
		gl.Viewport(0, 0, int32(width), int32(height))

		// Calculate new perspective projection view matrix.
		winWidth, winHeight = width, height
		fw, fh := float32(width), float32(height)
		aspect := fw / fh
		fovRadians := float32(math.Pi) / 3
		perspectiveProjectionViewMatrix = vm.mult(newPerspectiveMatrix(fovRadians, aspect, 1, 2000))

		// Calculate new ortho projection view matrix.
		orthoProjectionViewMatrix = newOrthoMatrix(fw, fh, fw /* use width as depth */)
	}

	if err := initMeshes(); err != nil {
		return err
	}

	if err := initTextures(); err != nil {
		return err
	}

	gl.Enable(gl.CULL_FACE)
	gl.CullFace(gl.BACK)

	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)

	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0, 0, 0, 0)

	return nil
}
Ejemplo n.º 4
0
//Unbind return cull face to front and unbind this fbo.
func (sfbo *ShadowFBO) Unbind() {
	sfbo.framebuffer.Unbind(gl2.FRAMEBUFFER)
	gl.CullFace(gl.BACK)
}
Ejemplo n.º 5
0
func InitGL() error {
	defer tlog.FuncLog(tlog.Func("InitGL"))

	err := gl.Init()
	if err != nil {
		return err
	}
	gl.GetError() // cleanup gl error state

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

	gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
	Check()
	gl.ReadBuffer(gl.BACK)
	Check()
	gl.DrawBuffer(gl.BACK)
	Check()

	gl.FrontFace(gl.CW)
	Check()
	gl.CullFace(gl.BACK)
	Check()
	gl.Disable(gl.CULL_FACE)
	Check()

	gl.ClearColor(1, 1, 0.5, 0)
	Check()
	gl.ClearDepth(1.0)
	Check()
	gl.ClearStencil(0)
	Check()

	gl.Disable(gl.STENCIL_TEST)
	Check()
	gl.StencilMask(0xFFFFFFFF)
	Check()
	gl.StencilFunc(gl.EQUAL, 0x00000000, 0x00000001)
	Check()
	gl.StencilOp(gl.KEEP, gl.KEEP, gl.KEEP)
	Check()

	gl.Disable(gl.DITHER)
	Check()

	gl.Enable(gl.DEPTH_TEST)
	Check()
	gl.DepthFunc(gl.LEQUAL)
	Check()
	gl.DepthMask(true)
	Check()
	gl.DepthRange(0., 1.)
	Check()
	gl.Enable(gl.DEPTH_CLAMP)
	Check()

	gl.Enable(gl.BLEND)
	Check()
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	ForceCheck()
	return nil
}