Beispiel #1
0
func (b *BasicMaterial) Load() error {
	program := b.Program
	vrt := gl.CreateShader(gl.VERTEX_SHADER)
	frg := gl.CreateShader(gl.FRAGMENT_SHADER)

	vrt.Source(b.vertexShader)
	frg.Source(b.fragmentShader)

	vrt.Compile()
	if vrt.Get(gl.COMPILE_STATUS) != 1 {
		return fmt.Errorf("Error in Compiling Vertex Shader:%s\n", vrt.GetInfoLog())
	}
	frg.Compile()
	if frg.Get(gl.COMPILE_STATUS) != 1 {
		return fmt.Errorf("Error in Compiling Fragment Shader:%s\n", frg.GetInfoLog())
	}

	program.AttachShader(vrt)
	program.AttachShader(frg)

	program.BindAttribLocation(0, "vertexPos")
	program.BindAttribLocation(1, "vertexUV")

	program.Link()

	b.Verts = program.GetAttribLocation("vertexPos")
	b.UV = program.GetAttribLocation("vertexUV")
	b.ViewMatrix = program.GetUniformLocation("MView")
	b.ProjMatrix = program.GetUniformLocation("MProj")
	b.ModelMatrix = program.GetUniformLocation("MModel")
	b.Texture = program.GetUniformLocation("mytexture")
	b.AddColor = program.GetUniformLocation("addcolor")
	b.Tiling = program.GetUniformLocation("tiling")
	b.Offset = program.GetUniformLocation("offset")

	b.Offset.Uniform2f(0, 0)
	b.Tiling.Uniform2f(1, 1)
	b.AddColor.Uniform4f(1, 1, 1, 1)

	return nil
}
func MakeShader(type_ gl.GLenum, filename string) gl.Shader {
	source := ReadShaderFile(filename)
	shader := gl.CreateShader(type_)
	shader.Source(source)

	shader.Compile()
	log := shader.GetInfoLog()
	if log != "" {
		panic(fmt.Sprint(filename, log))
	}

	return shader
}
Beispiel #3
0
func loadShader(shaderType gl.GLenum, source string) gl.Shader {
	shader := gl.CreateShader(shaderType)
	if err := gl.GetError(); err != gl.NO_ERROR {
		panic(fmt.Errorf("gl error: %v", err))
	}

	shader.Source(source)
	shader.Compile()

	if shader.Get(gl.COMPILE_STATUS) != gl.TRUE {
		log := shader.GetInfoLog()
		panic(fmt.Errorf("Failed to compile shader: %v, shader: %v", log, source))
	}

	return shader
}
Beispiel #4
0
func CreateProgram(filenames ...string) gl.Program {

	program := gl.CreateProgram()

	for _, fn := range filenames {

		var shaderType gl.GLenum

		switch {
		case strings.HasSuffix(fn, ".vert"):
			shaderType = gl.VERTEX_SHADER
		case strings.HasSuffix(fn, ".geom"):
			shaderType = gl.GEOMETRY_SHADER
		case strings.HasSuffix(fn, ".frag"):
			shaderType = gl.FRAGMENT_SHADER
		default:
			fmt.Println("Wrong suffix: " + fn)
		}

		shader := gl.CreateShader(shaderType)
		defer shader.Delete()

		shader.Source(ReadFile(fn))
		shader.Compile()
		if info := shader.GetInfoLog(); info != "" {
			log.Fatal(info)
		}

		program.AttachShader(shader)
		defer program.DetachShader(shader)
	}

	program.Link()
	if info := program.GetInfoLog(); info != "" {
		log.Fatal(info)
	}

	return program
}
Beispiel #5
0
func main() {
	glfw.SetErrorCallback(errorCallback)
	// lock glfw/gl calls to a single thread
	runtime.LockOSThread()
	runtime.GOMAXPROCS(8) // Render, read commands, send input, extra for file loading, etc

	if !glfw.Init() {
		panic("Could not init glfw!")
	}

	defer glfw.Terminate()

	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenglForwardCompatible, glfw.True)
	glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)

	window, err := glfw.CreateWindow(800, 600, "Example", nil, nil)
	if err != nil {
		panic(err)
	}

	window.SetFramebufferSizeCallback(func(w *glfw.Window, width, height int) {
		fmt.Printf("Framebuffer size is now %vx%v\n", width, height)
		// Keep aspect ratio from camwidth/camheight
		camRatio := camWidth / camHeight
		bufRatio := float32(width) / float32(height)
		var newWidth, newHeight float32

		switch {
		case camRatio > bufRatio:
			newHeight = float32(width) / camRatio
			newWidth = float32(width)
		case bufRatio > camRatio:
			newWidth = float32(height) * camRatio
			newHeight = float32(height)
		}

		fmt.Printf("Viewport size is now %vx%v; cam ratio is %v; viewport ratio is %v;\n", newWidth, newHeight, camRatio, newWidth/newHeight)

		gl.Viewport((width-int(newWidth))/2, (height-int(newHeight))/2, int(newWidth), int(newHeight))
	})

	defer window.Destroy()

	window.MakeContextCurrent()
	glfw.SwapInterval(1)

	gl.Init()

	// Enable blending
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	vao := gl.GenVertexArray()
	vao.Bind()

	vbo := gl.GenBuffer()
	vbo.Bind(gl.ARRAY_BUFFER)

	verticies := []float32{-0.5, 0.5,
		0.5, 0.5,
		0.5, -0.5,
		-0.5, -0.5}

	gl.BufferData(gl.ARRAY_BUFFER, len(verticies)*4, verticies, gl.STATIC_DRAW)

	vertex_shader := gl.CreateShader(gl.VERTEX_SHADER)
	vertex_shader.Source(vertex)
	vertex_shader.Compile()
	fmt.Println(vertex_shader.GetInfoLog())
	defer vertex_shader.Delete()

	fragment_shader := gl.CreateShader(gl.FRAGMENT_SHADER)
	fragment_shader.Source(fragment)
	fragment_shader.Compile()
	fmt.Println(fragment_shader.GetInfoLog())
	defer fragment_shader.Delete()

	program := gl.CreateProgram()
	program.AttachShader(vertex_shader)
	program.AttachShader(fragment_shader)

	program.BindFragDataLocation(0, "outColor")
	program.Link()
	program.Use()
	defer program.Delete()

	positionAttrib := program.GetAttribLocation("position")
	positionAttrib.AttribPointer(2, gl.FLOAT, false, 0, nil)
	positionAttrib.EnableArray()
	defer positionAttrib.DisableArray()

	modelMat := program.GetUniformLocation("modelView")
	projMat := program.GetUniformLocation("projection")
	spriteSize := program.GetUniformLocation("size")

	cmd := exec.Command(os.Args[1], os.Args[2:]...)
	stdoutReader, stdoutWriter := io.Pipe()
	cmd.Stdout = stdoutWriter
	input := bufio.NewReader(stdoutReader)

	stdinReader, stdinWriter := io.Pipe()
	cmd.Stdin = stdinReader

	stderr, err := cmd.StderrPipe()
	chkErr(err)

	go io.Copy(os.Stderr, stderr)

	err = cmd.Start()
	chkErr(err)

	window.SetKeyCallback(handleKey)

	go func() {
		runtime.LockOSThread()
		for !window.ShouldClose() {
			sendInput(stdinWriter)
			//fmt.Fprintf(stdinWriter, "T %v\n", time.Now())
			<-ticks
			readCommands(input)
		}
	}()

	frameCnt := 0
	queueDepth := 0
	then := time.Now()

	for !window.ShouldClose() {
		frameCnt++
		queueDepth += len(commandBus)
		if frameCnt%120 == 0 {
			fmt.Printf("Queue depth: %v. Render time: %v. Decode time: %v.\n", queueDepth/120, time.Since(then)/120, decodeTime/time.Duration(decodes))
			queueDepth = 0
			then = time.Now()
			decodeTime = time.Duration(0)
			decodes = 0
		}

		for len(commandBus) > 0 {
			(<-commandBus)()
		}

		halfwidth := camWidth / 2.0
		halfheight := camHeight / 2.0
		projection := mathgl.Ortho2D(camera.x-halfwidth, camera.x+halfwidth, camera.y+halfheight, camera.y-halfheight)
		projection = mathgl.Scale3D(camera.sx, camera.sy, 1).Mul4(projection)
		projection = mathgl.HomogRotate3DZ(camera.rot).Mul4(projection)

		projMat.UniformMatrix4f(false, (*[16]float32)(&projection))

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

		for _, sprite := range sprites {
			sMat := mathgl.Scale3D(sprite.sx, sprite.sy, 1)
			sMat = mathgl.HomogRotate3DZ(sprite.rot).Mul4(sMat)
			sMat = mathgl.Translate3D(sprite.x, sprite.y, sprite.z).Mul4(sMat)

			// temp hack bank in
			sprite.bank = 1

			sMap := findSMap(sprite.smap)

			spriteSize.Uniform2f(float32(sMap.getWidth()*sprite.cellwidth), float32(sMap.getHeight()*sprite.cellheight))

			modelMat.UniformMatrix4f(false, (*[16]float32)(&sMat))
			gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
		}

		window.SwapBuffers()
		glfw.PollEvents()

		if window.GetKey(glfw.KeyEscape) == glfw.Press {
			window.SetShouldClose(true)
		}
	}
}