Пример #1
0
// CreateProgram creates an OpenGL shader program from the
// passed shader sources. Panics if the shader is invalid.
func CreateProgram(vertex, fragment string) gl.Program {
	program := gl.CreateProgram()

	v := gl.CreateShader(gl.VertexShader)
	v.Source(vertex)
	v.Compile()

	if v.Parameter(gl.CompileStatus) == 0 {
		panic(v.InfoLog())
	} else {
		log := v.InfoLog()
		if len(log) > 0 {
			fmt.Println(log)
		}
	}

	f := gl.CreateShader(gl.FragmentShader)
	f.Source(fragment)
	f.Compile()

	if f.Parameter(gl.CompileStatus) == 0 {
		panic(f.InfoLog())
	} else {
		log := f.InfoLog()
		if len(log) > 0 {
			fmt.Println(log)
		}
	}

	program.AttachShader(v)
	program.AttachShader(f)
	program.Link()
	program.Use()
	return program
}
Пример #2
0
func (c *cloudState) init() {
	program := gl.CreateProgram()

	v := gl.CreateShader(gl.VertexShader)
	v.Source(glsl.Get("cloud_vertex"))
	v.Compile()

	if v.Parameter(gl.CompileStatus) == 0 {
		panic(v.InfoLog())
	} else {
		log := v.InfoLog()
		if len(log) > 0 {
			fmt.Println(log)
		}
	}

	g := gl.CreateShader(gl.GeometryShader)
	g.Source(glsl.Get("cloud_geo"))
	g.Compile()

	if g.Parameter(gl.CompileStatus) == 0 {
		panic(g.InfoLog())
	} else {
		log := g.InfoLog()
		if len(log) > 0 {
			fmt.Println(log)
		}
	}

	f := gl.CreateShader(gl.FragmentShader)
	f.Source(glsl.Get("cloud_fragment"))
	f.Compile()

	if f.Parameter(gl.CompileStatus) == 0 {
		panic(f.InfoLog())
	} else {
		log := f.InfoLog()
		if len(log) > 0 {
			fmt.Println(log)
		}
	}

	program.AttachShader(v)
	program.AttachShader(g)
	program.AttachShader(f)
	program.Link()
	program.Use()

	c.program = program

	InitStruct(&c.shader, c.program)

	c.array = gl.CreateVertexArray()
	c.array.Bind()
	c.buffer = gl.CreateBuffer()
	c.buffer.Bind(gl.ArrayBuffer)
	c.shader.Position.Enable()
	c.shader.Position.Pointer(3, gl.Float, false, 12, 0)

	data := builder.New(builder.Float, builder.Float, builder.Float)
	for x := -160; x < 160; x++ {
		for y := -160; y < 160; y++ {
			data.Float(float32(x))
			data.Float(128)
			data.Float(float32(y))
			c.numPoints++
		}
	}

	c.buffer.Data(data.Data(), gl.StaticDraw)

	c.texture = gl.CreateTexture()
	c.texture.Bind(gl.Texture2D)
	c.texture.Image2D(0, 512, 512, gl.Red, gl.UnsignedByte, c.data)
	c.texture.Parameter(gl.TextureMinFilter, gl.Nearest)
	c.texture.Parameter(gl.TextureMagFilter, gl.Nearest)
}