예제 #1
0
func (this *WorldRenderer) Render(ww *gamestate.World, options *settings.BoolOptions, window *sdl.Window) {
	camera := ww.Player.Camera
	p0 := camera.Pos4f()

	w, h := window.GetSize()

	camera.MoveRelative(mgl.Vec4{-0.1, 0, 0, 0})
	this.View = (ww.PortalTransform(p0, camera.Pos4f()).Mul4(camera.Model())).Inv()
	viewport := Viewport{0, 0, w / 2, h}
	viewport.Activate()
	this.render(ww, options, viewport, 0, nil)

	camera.MoveRelative(mgl.Vec4{+0.2, 0, 0, 0})
	this.View = (ww.PortalTransform(p0, camera.Pos4f()).Mul4(camera.Model())).Inv()
	viewport.X = w / 2
	viewport.Activate()
	this.render(ww, options, viewport, 0, nil)

	gl.Viewport(0, 0, w, h)

	gl.ActiveTexture(gl.TEXTURE0)
	this.Framebuffer[0].RenderTexture.Bind(gl.TEXTURE_RECTANGLE)
	this.ScreenQuadRenderer.Render(this.ScreenQuad, this.Proj, this.View, this.ClippingPlane_ws, nil)

	if this.screenShot {
		this.screenShot = false
		helpers.SaveTexture(gl.TEXTURE_RECTANGLE, 0, "screenshot.png")
	}
}
예제 #2
0
func NewGameState(window *sdl.Window, world *World) (gamestate *GameState) {
	gl.ClearColor(0., 0., 0.4, 0.0)

	gl.Enable(gl.DEPTH_TEST)

	bar := tw.NewBar("TweakBar")

	gamestate = &GameState{
		Window:  window,
		Camera:  nil,
		Bar:     bar,
		World:   world,
		Fps:     0,
		Options: settings.BoolOptions{},
	}

	opt := &gamestate.Options
	opt.Load()
	gamestate.Camera = gamestate.World.Player.GetCamera()

	tw.Define(" GLOBAL help='This example shows how to integrate AntTweakBar with SDL2 and OpenGL.' ")
	bar.AddVarRO("fps", tw.TYPE_FLOAT, unsafe.Pointer(&gamestate.Fps), "")
	opt.CreateGui(bar)

	for i, portal := range gamestate.World.KdTree {
		ptr := &(portal.(*Portal).Orientation)
		bar.AddVarRW(fmt.Sprintf("Rotation %d", i), tw.TYPE_QUAT4F, unsafe.Pointer(ptr), "")
	}

	//window.GetSize(w, h)
	w, h := window.GetSize()
	tw.WindowSize(w, h)

	return
}
예제 #3
0
func GetMouseDirection(window *sdl.Window, mx, my int) (dir_cs mgl.Vec4) {
	if !sdl.GetRelativeMouseMode() {
		W, H := window.GetSize()
		x := (2*float32(mx) - float32(W)) / float32(H)
		y := (float32(H) - 2*float32(my)) / float32(H)
		dir_cs = mgl.Vec4{x, y, -1, 0}
	} else {
		dir_cs = mgl.Vec4{0, 0, -1, 0}
	}
	return
}
예제 #4
0
func NewWorldRenderer(window *sdl.Window, w *gamestate.World) *WorldRenderer {
	width, height := window.GetSize()

	return &WorldRenderer{
		Proj:               mgl.Perspective(90, float32(width)/float32(height), 0.3, 1000),
		View:               mgl.Ident4(),
		ClippingPlane_ws:   mgl.Vec4{1, 0, 0, -1000000},
		Textures:           NewTextures(w.HeightMap),
		HeightMapRenderer:  NewHeightMapRenderer(),
		WaterRendererA:     NewSurfaceWaterRenderer(),
		WaterRendererB:     NewDebugWaterRenderer(),
		MeshRenderer:       NewMeshRenderer(),
		PortalRenderer:     NewPortalRenderer(),
		TreeRenderer:       NewTreeRenderer(),
		SkyboxRenderer:     NewSkyboxRenderer(),
		Skybox:             &Skybox{},
		ParticleSystem:     particles.NewParticleSystem(w, 10000, mgl.Vec3{32, 32, 32}, 1, 250),
		Framebuffer:        [2]*FrameBuffer{NewFrameBuffer(window.GetSize()), NewFrameBuffer(window.GetSize())},
		ScreenQuad:         &ScreenQuad{},
		ScreenQuadRenderer: NewScreenQuadRenderer(),
		DebugRenderer:      NewLineRenderer(),
		MaxRecursion:       1,
	}
}
예제 #5
0
// win==nil requests a full-screen window
func Run(win WindowSpec) int {
	// All SDL calls must come from same thread.
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	err := sdl.Init(sdl.INIT_EVERYTHING)
	if err != nil {
		panic(err)
	}
	defer sdl.Quit()

	// Install audio callback
	spec := &sdl.AudioSpec{
		Freq:     SampleRate,
		Format:   sdl.AUDIO_F32SYS,
		Channels: 1,
		Samples:  4096,
		Callback: sdl.AudioCallback(C.getSoundSamplesAdaptor),
	}
	audioDevice, err := sdl.OpenAudioDevice("", false, spec, nil, 0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to open audio device: %v\n", err)
		panic(err)
	}
	if audioDevice < 2 {
		fmt.Fprintf(os.Stderr, "Audio device=%v < 2 contrary to SDL-2 documentation\n", audioDevice, err)
	}
	sdl.PauseAudioDevice(audioDevice, false)

	// Create window
	var window *sdl.Window
	if win != nil {
		// Partial screen
		winWidth, winHeight := win.Size()
		window, err = sdl.CreateWindow(win.Title(), sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
			int(winWidth), int(winHeight), sdl.WINDOW_SHOWN)
	} else {
		// Full screen
		if isMacOS {
			// Contrary to https://wiki.libsdl.org/SDL_CreateWindow, on MacOS 10.11.1
			// a call to sdl.CreateWindow in fullscreen mode *does* use the w and h parameters.
			// So ask what the display size is.
			var mode sdl.DisplayMode
			err = sdl.GetDesktopDisplayMode(0, &mode)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Cannot get desktop display mode")
				panic(err)
			}
			window, err = sdl.CreateWindow("", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
				int(mode.W), int(mode.H), sdl.WINDOW_SHOWN|sdl.WINDOW_FULLSCREEN_DESKTOP)
		} else {
			window, err = sdl.CreateWindow("", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
				0, 0, sdl.WINDOW_SHOWN|sdl.WINDOW_FULLSCREEN_DESKTOP)
		}

	}
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %v\n", err)
		panic(err)
	}
	defer window.Destroy()

	// Create renderer
	width, height := window.GetSize()
	renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %v\n", err)
		panic(err)
	}
	defer renderer.Destroy()

	var nTex = 1
	if isMacOS {
		// Work around MacOS bug via double buffering
		nTex = 2
	}
	// Create texture
	tex := make([]*sdl.Texture, nTex)
	for i := range tex {
		tex[i], err = renderer.CreateTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_STREAMING, width, height)
		if err != nil {
			fmt.Fprintf(os.Stderr, "renderer.CreateTexture: %v\n", err)
			panic(err)
		}
		defer tex[i].Destroy()
	}
	for _, r := range renderClientList {
		r.Init(int32(width), int32(height))
	}

	// Loop until quit
	for i := 0; ; i ^= nTex - 1 {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch e := event.(type) {
			case *sdl.QuitEvent:
				return 0
			case *sdl.MouseMotionEvent:
				// Go equivalent of SDL_PRESSED seems to be missing, so compare with zero.
				if e.State != 0 {
					forwardMouseEvent(MouseDrag, int32(e.X), int32(e.Y))
				} else {
					forwardMouseEvent(MouseMove, int32(e.X), int32(e.Y))
				}
			case *sdl.MouseButtonEvent:
				switch e.Type {
				case sdl.MOUSEBUTTONDOWN:
					forwardMouseEvent(MouseDown, int32(e.X), int32(e.Y))
				case sdl.MOUSEBUTTONUP:
					forwardMouseEvent(MouseUp, int32(e.X), int32(e.Y))
				}
			case *sdl.KeyDownEvent:
				var k Key
				if 0x20 <= e.Keysym.Sym && e.Keysym.Sym < 0x7F {
					// Printable ASCII
					k = Key(e.Keysym.Sym)
				} else {
					// Try special character table
					k = keyMap[e.Keysym.Sym]
				}
				if k != 0 {
					forwardKeyEvent(k)
				}
			}
		}
		pixels, pitch := lockTexture(tex[i], width, height)
		pm := MakePixMap(int32(width), int32(height), pixels, int32(pitch))
		for _, r := range renderClientList {
			r.Render(pm)
		}
		tex[i].Unlock()

		err := renderer.Clear()
		if err != nil {
			fmt.Fprintf(os.Stderr, "renderer.Clear: %v", err)
			panic(err)
		}
		renderer.Copy(tex[i], nil, nil)
		if err != nil {
			fmt.Fprintf(os.Stderr, "renderer.Copy: %v", err)
			panic(err)
		}
		renderer.Present()
	}
}