Example #1
0
func (r *Renderer) Render() {
	// defer glfw.Terminate()
	shader := r.Shaders.textureFlat
	program := shader.program
	//
	gl.UseProgram(program)
	//
	gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00"))
	// // Configure global settings
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)
	gl.ClearColor(1.0, 1.0, 1.0, 1.0)

	//
	// angle += elapsed
	// r.Mesh.modelView = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})

	// Render
	// gl.UniformMatrix4fv(shader.uniforms["modelView"], 1, false, &r.Mesh.modelView[0])

	time := glfw.GetTime()
	_ = time - r.PreviousTime
	r.PreviousTime = time

	// fmt.Println(elapsed * 100)

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

	gl.UniformMatrix4fv(shader.uniforms["projection"], 1, false, &r.Projection[0])
	gl.UniformMatrix4fv(shader.uniforms["camera"], 1, false, &r.Camera[0])

	// TODO : batch triangles and use multiple textures
	for _, mesh := range r.Meshes {
		gl.UniformMatrix4fv(shader.uniforms["modelView"], 1, false, &mesh.modelView[0])
		gl.Uniform1i(shader.uniforms["tex"], 0)

		gl.BindVertexArray(mesh.vao)

		gl.ActiveTexture(gl.TEXTURE0)
		gl.BindTexture(gl.TEXTURE_2D, mesh.textures[0])

		gl.DrawArrays(gl.TRIANGLES, 0, int32(len(mesh.verticies)/5))
	}

	// Maintenance
	r.Window.SwapBuffers()
	glfw.PollEvents()
	if r.Ready == false {
		r.Ready = true
	}
}
//
// Create Window
// This creates a window, initiates GL and sets the event listeners
//
// @return window (*glfw.Window) pointer to the window
//
func (glw *Glw) CreateWindow() *glfw.Window {
	// Init GLFW
	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}

	// Sets the OpenGL Version
	setOpenGlVersion()

	// Creates the Window
	win, err := glfw.CreateWindow(glw.Width, glw.Height, glw.Title, nil, nil)
	if err != nil {
		panic(err)
	}

	// Prints the OpenGL Versions at the end
	defer printOpenGlVersionInfo()

	// Sets this context as the current context
	win.MakeContextCurrent()

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

	// Enables Depth
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)

	win.SetInputMode(glfw.StickyKeysMode, 1)

	// Sets the Window to the Wrapper
	glw.SetWindow(win)
	return win
}