Exemple #1
0
// Create a vertex/fragment shader program
func createProgram(info *shaderInfo) (gl.Program, error) {
	vshader := gl.CreateShader(gl.VERTEX_SHADER)
	vshader.Source(info.vertCode)
	vshader.Compile()
	if vshader.Get(gl.COMPILE_STATUS) != gl.TRUE {
		return gl.Program(0), errors.New("Unable to compile vertex shader. " + vshader.GetInfoLog())
	}
	defer vshader.Delete() // no need to use it after linking

	fshader := gl.CreateShader(gl.FRAGMENT_SHADER)
	fshader.Source(info.fragCode)
	fshader.Compile()
	if fshader.Get(gl.COMPILE_STATUS) != gl.TRUE {
		return gl.Program(0), errors.New("Unable to compile fragment shader. " + fshader.GetInfoLog())
	}
	defer fshader.Delete() // no need to use it after linking

	program := gl.CreateProgram()
	program.AttachShader(vshader)
	program.AttachShader(fshader)
	program.Link()

	if program.Get(gl.LINK_STATUS) != gl.TRUE {
		return gl.Program(0), errors.New("Unable to link program. " + fshader.GetInfoLog())
	}

	return program, nil
}
Exemple #2
0
func main() {
	flag.Parse()

	loadMeshInfo()

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

	program, shaderInfo, err := loadShaders(gl.Program(0), nil)
	if err != nil {
		panic(err)
	}
	_ = program

	defer glfw.Terminate()

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

	// Perform the rendering.
	var angle float32
	reload := time.Tick(time.Duration(300 * time.Millisecond))
	for glfw.WindowParam(glfw.Opened) > 0 {
		select {
		case <-reload:
			oldInfo := shaderInfo
			program, shaderInfo, err = loadShaders(program, shaderInfo)
			if err != nil && lastErr == nil {
				lastErr = err
				println("Error loading shaders. Using old code.", lastErr)
			} else if err == nil && oldInfo != shaderInfo {
				lastErr = nil
				println("new shader code loaded")
			}
		default:
			// do nothing here
		}
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.LoadIdentity()
		gl.Translatef(0, 0, -zoom)

		if angle > 0 {
			gl.Rotatef(angle, rotVet[0], rotVet[1], rotVet[2])
		}

		program.Use()

		gl.Enable(gl.COLOR_MATERIAL)
		gl.Enable(gl.POLYGON_OFFSET_FILL)
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
		mb.Render(gl.TRIANGLES)

		if rotVet[0]+rotVet[1]+rotVet[2] != 0 {
			// only increment the angle if at least one of the rotate axis
			// is enabled
			angle += 0.5
		} else if angle != 0 {
			angle = 0
		}
		glfw.SwapBuffers()
	}
}