예제 #1
0
func initResources() {
	var err error
	// Load shaders
	vs, err = createShader("triangle.v.glsl", VertexShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}
	fs, err = createShader("triangle.f.glsl", FragmentShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}

	// Create GLSL program with loaded shaders
	var compileOk gl.Int
	program = gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	gl.LinkProgram(program)
	gl.GetProgramiv(program, gl.LINK_STATUS, &compileOk)
	if compileOk == 0 {
		fmt.Printf("Error in program.\n")
	}

	// Generate a buffer for the VertexBufferObject
	gl.GenBuffers(1, &vboTriangle)
	gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangle)
	// Submit the vertices of the triangle to the graphic card
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangleVertices)*4), gl.Pointer(&triangleVertices[0]), gl.STATIC_DRAW)
	// Unset the active buffer
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	// Generate a buffer for the Color-VBO
	gl.GenBuffers(1, &vboTriangleColors)
	gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangleColors)
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangleColors)*4), gl.Pointer(&triangleColors[0]), gl.STATIC_DRAW)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	// Get the attribute location from the GLSL program (here from the vertex shader)
	attributeName := gl.GLString("coord2d")
	defer gl.GLStringFree(attributeName)
	attributeTemp := gl.GetAttribLocation(program, attributeName)
	if attributeTemp == -1 {
		fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName))
	}
	attributeCoord2d = gl.Uint(attributeTemp)

	attributeName = gl.GLString("v_color")
	defer gl.GLStringFree(attributeName)
	attributeTemp = gl.GetAttribLocation(program, attributeName)
	if attributeTemp == -1 {
		fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName))
	}
	attributeColor = gl.Uint(attributeTemp)
}
예제 #2
0
파일: opengl3.go 프로젝트: emlai/go-sdl2
func createprogram() gl.Uint {
	// VERTEX SHADER
	vs := gl.CreateShader(gl.VERTEX_SHADER)
	vs_source := gl.GLString(vertexShaderSource)
	gl.ShaderSource(vs, 1, &vs_source, nil)
	gl.CompileShader(vs)
	var vs_status gl.Int
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &vs_status)
	fmt.Printf("Compiled Vertex Shader: %v\n", vs_status)

	// FRAGMENT SHADER
	fs := gl.CreateShader(gl.FRAGMENT_SHADER)
	fs_source := gl.GLString(fragmentShaderSource)
	gl.ShaderSource(fs, 1, &fs_source, nil)
	gl.CompileShader(fs)
	var fstatus gl.Int
	gl.GetShaderiv(fs, gl.COMPILE_STATUS, &fstatus)
	fmt.Printf("Compiled Fragment Shader: %v\n", fstatus)

	// CREATE PROGRAM
	program := gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	fragoutstring := gl.GLString("outColor")
	defer gl.GLStringFree(fragoutstring)
	gl.BindFragDataLocation(program, gl.Uint(0), fragoutstring)

	gl.LinkProgram(program)
	var linkstatus gl.Int
	gl.GetProgramiv(program, gl.LINK_STATUS, &linkstatus)
	fmt.Printf("Program Link: %v\n", linkstatus)

	return program
}
예제 #3
0
func (p *Program) GetAttributeLocation(name string) (*AttributeLocation, error) {
	attributeName := gl.GLString(name)
	defer gl.GLStringFree(attributeName)
	attributeTemp := gl.GetAttribLocation(p.id, attributeName)
	if attributeTemp == -1 {
		return nil, errors.New(fmt.Sprintf("Could not bind attribute %s\n", gl.GoString(attributeName)))
	}

	return &AttributeLocation{id: gl.Uint(attributeTemp)}, nil
}
예제 #4
0
func initResources() {
	var compileOk gl.Int
	// Vertex Shader
	vs = gl.CreateShader(gl.VERTEX_SHADER)
	vsSrc := gl.GLStringArray(vsSource)
	defer gl.GLStringArrayFree(vsSrc)
	gl.ShaderSource(vs, gl.Sizei(len(vsSrc)), &vsSrc[0], nil)
	gl.CompileShader(vs)
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &compileOk)
	if compileOk == 0 {
		errNum := gl.GetError()
		fmt.Printf("Error in vertex shader: %d\n", errNum)
	}

	// Fragment Shader
	fs = gl.CreateShader(gl.FRAGMENT_SHADER)
	fsSrc := gl.GLStringArray(fsSource)
	defer gl.GLStringArrayFree(fsSrc)
	gl.ShaderSource(fs, gl.Sizei(1), &fsSrc[0], nil)
	gl.CompileShader(fs)
	gl.GetShaderiv(fs, gl.COMPILE_STATUS, &compileOk)
	if compileOk == 0 {
		errNum := gl.GetError()
		fmt.Printf("Error in fragment shader: %d\n", errNum)
	}

	// GLSL program
	program = gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	gl.LinkProgram(program)
	gl.GetProgramiv(program, gl.LINK_STATUS, &compileOk)
	if compileOk == 0 {
		fmt.Printf("Error in program.\n")

	}

	// Get the attribute location from the GLSL program (here from the vertex shader)
	attributeName := gl.GLString("coord2d")
	defer gl.GLStringFree(attributeName)
	attributeTemp := gl.GetAttribLocation(program, attributeName)
	if attributeTemp == -1 {
		fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName))
	}
	attributeCoord2d = gl.Uint(attributeTemp)
}
예제 #5
0
파일: mesh.go 프로젝트: klkblake/sge
func NewMesh(attrs interface{}, indicies []uint32) *Mesh {
	mesh := new(Mesh)
	mesh.Attrs = attrs
	mesh.Indicies = indicies
	gl33.GenVertexArrays(1, &mesh.vao)
	gl33.BindVertexArray(mesh.vao)
	attrsValue := reflect.ValueOf(attrs)
	mesh.vertexBO = NewBuffer(gl33.ARRAY_BUFFER, gl33.STATIC_DRAW, attrs)
	vertexSpec := attrsValue.Type().Elem()
	if vertexSpec.Kind() != reflect.Struct && vertexSpec.Kind() != reflect.Array {
		panic("attrs is not a slice of structs or arrays")
	}
	var num int
	if vertexSpec.Kind() == reflect.Struct {
		num = vertexSpec.NumField()
	} else {
		num = vertexSpec.Len()
	}
	for i, offset := 0, uintptr(0); i < num; i++ {
		var field reflect.Type
		var type_ gl33.Enum
		var dimensions int
		if vertexSpec.Kind() == reflect.Struct {
			field = vertexSpec.Field(i).Type
		} else {
			field = vertexSpec.Elem()
		}
		if field.Kind() == reflect.Array {
			type_ = glType(field.Elem().Kind())
			dimensions = field.Len()
		} else {
			type_ = glType(field.Kind())
			dimensions = 1
		}
		setupAttrib(gl33.Uint(i), type_, dimensions, offset, int(vertexSpec.Size()))
		offset += field.Size()
	}
	mesh.indexBO = NewBuffer(gl33.ELEMENT_ARRAY_BUFFER, gl33.STATIC_DRAW, indicies)
	return mesh
}