Exemple #1
0
func main() {
	initialWidth, initialHeight := 1280, 720
	runtime.LockOSThread()
	if sdlInit := sdl.Init(sdl.INIT_VIDEO); sdlInit != 0 {
		panic("SDL init error")
	}
	reinitScreen(initialWidth, initialHeight)
	defer cleanExit(true, false)
	if err := gl.Init(); err != nil {
		panic(err)
	}
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.DEPTH)
	defer cleanExit(false, true)
	glSetupShaderProg(&shaderTextureCreator, false)
	glSetupShaderProg(&shaderTextureDisplay, true)
	glFillBuffer(rttVerts, &rttVertBuf)
	glFillBuffer(dispVerts, &dispVertBuf)
	gl.GenTextures(1, &rttFrameTex)
	gl.BindTexture(gl.TEXTURE_2D, rttFrameTex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	if doRtt {
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texSize, texSize, 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
		gl.GenFramebuffers(1, &rttFrameBuf)
		gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
		gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rttFrameTex, 0)
		gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
	} else {
		glFillTextureFromImageFile("texture.jpg")
	}
	gl.BindTexture(gl.TEXTURE_2D, 0)
	gl.ClearColor(0.3, 0.6, 0.9, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.ActiveTexture(gl.TEXTURE0)
	for {
		if evt := sdl.PollEvent(); evt != nil {
			switch event := evt.(type) {
			case *sdl.ResizeEvent:
				reinitScreen(int(event.W), int(event.H))
			case *sdl.QuitEvent:
				return
			}
		} else {
			if doRtt {
				renderToTexture()
			}
			renderToScreen()
			sdl.GL_SwapBuffers()
		}
	}

	sdl.Quit()
}
Exemple #2
0
Fichier : main.go Projet : vron/fm
func initScene() {
	initProgram()

	//gl.Enable(gl.CULL_FACE)
	//gl.CullFace(gl.BACK)
	gl.FrontFace(gl.CW)
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthMask(gl.TRUE)
	gl.DepthFunc(gl.LEQUAL)
	gl.DepthRange(0.0, 1.0)
	gl.Enable(gl.DEPTH_CLAMP)
}
Exemple #3
0
func Initialize() {
	fmt.Println("INFO: OpenGL Version", gl.GetString(gl.VERSION))

	// Render things which are closer on top.
	gl.Init()
	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)
	CreateShaders()
	CreateVOB()
}
Exemple #4
0
func Loop(onKeyDown, onKeyUp func(*sdl.KeyboardEvent)) {
	var nowTime = time.Now()
	var lastSecond, lastTime = nowTime, nowTime
	var nowNano int64 = 0
	var durSec time.Duration
	var sdlEvt sdl.Event
	var winw, winh int
	var retro int = int(Canvas0.RetroFactor)
	var canvas0RetroFactor = 1 / Canvas0.RetroFactor
	gl.Enable(gl.FRAMEBUFFER_SRGB)
	for Looping {
		nowTime = time.Now()
		pipeline.TimeLast, pipeline.TimeNow = lastTime, nowTime
		pipeline.TimeSecsElapsed = nowTime.Sub(lastTime).Seconds()
		lastTime = nowTime
		nowNano = nowTime.UnixNano()
		Canvas0.TimeElapsed = nowNano - StartTime
		durSec = nowTime.Sub(lastSecond)
		if sdlEvt = sdl.PollEvent(); sdlEvt != nil {
			switch event := sdlEvt.(type) {
			case *sdl.ResizeEvent:
				winw, winh = int(event.W), int(event.H)
				if retro > 0 {
					for (winw % retro) != 0 {
						winw--
					}
					for (winh % retro) != 0 {
						winh--
					}
					if (winw / retro) < 32 {
						winw = 32 * retro
					}
					if (winh / retro) < 32 {
						winh = 32 * retro
					}
				}
				ReinitVideo(winw, winh, true, true)
				RefreshWindowCaption()
			case *sdl.QuitEvent:
				Looping = false
				return
			case *sdl.KeyboardEvent:
				if event.Type == sdl.KEYUP {
					KeysPressed[event.Keysym.Sym] = false
					if event.Keysym.Sym == KeySymTmpTexUp {
						LoadTmpTex(TmpTexIndex + 1)
					}
					if event.Keysym.Sym == KeySymTmpTexDn {
						LoadTmpTex(TmpTexIndex - 1)
					}
					// if event.Keysym.Sym == KeySymOctreeLevelUp { softpipeline.OctreeMaxLevel++ }
					// if event.Keysym.Sym == KeySymOctreeLevelDown { softpipeline.OctreeMaxLevel-- }
					onKeyUp(event)
				} else if event.Type == sdl.KEYDOWN {
					KeysPressed[event.Keysym.Sym] = true
					onKeyDown(event)
				}
			case *sdl.MouseMotionEvent:
				MouseX, MouseY = float64(event.X)*canvas0RetroFactor, float64(event.Y)*canvas0RetroFactor
				Canvas0.MouseX, Canvas0.MouseY = gl.Float(MouseX*Canvas0.InvWidth), gl.Float(MouseY*Canvas0.InvHeight)
			}
		}
		pipeline.CamMove, pipeline.CamTurn = false, false
		if KeysPressed[sdl.K_UP] {
			pipeline.CamMove, pipeline.CamMoveFwd = true, 1
		} else {
			pipeline.CamMoveFwd = 0
		}
		if KeysPressed[sdl.K_DOWN] {
			pipeline.CamMove, pipeline.CamMoveBack = true, 1
		} else {
			pipeline.CamMoveBack = 0
		}
		if KeysPressed[sdl.K_LEFT] {
			pipeline.CamTurn, pipeline.CamTurnLeft = true, 1
		} else {
			pipeline.CamTurnLeft = 0
		}
		if KeysPressed[sdl.K_RIGHT] {
			pipeline.CamTurn, pipeline.CamTurnRight = true, 1
		} else {
			pipeline.CamTurnRight = 0
		}
		if KeysPressed[sdl.K_PAGEUP] {
			pipeline.CamTurn, pipeline.CamTurnUp = true, 1
		} else {
			pipeline.CamTurnUp = 0
		}
		if KeysPressed[sdl.K_PAGEDOWN] {
			pipeline.CamTurn, pipeline.CamTurnDown = true, 1
		} else {
			pipeline.CamTurnDown = 0
		}
		// if KeysPressed[sdl.K_RSHIFT] { pipeline.SpeedUp = 10 } else { pipeline.SpeedUp = 1 }
		if KeysPressed[sdl.K_a] {
			pipeline.CamMove, pipeline.CamMoveLeft = true, 1
		} else {
			pipeline.CamMoveLeft = 0
		}
		if KeysPressed[sdl.K_d] {
			pipeline.CamMove, pipeline.CamMoveRight = true, 1
		} else {
			pipeline.CamMoveRight = 0
		}
		if KeysPressed[sdl.K_w] {
			pipeline.CamMove, pipeline.CamMoveUp = true, 1
		} else {
			pipeline.CamMoveUp = 0
		}
		if KeysPressed[sdl.K_s] {
			pipeline.CamMove, pipeline.CamMoveDown = true, 1
		} else {
			pipeline.CamMoveDown = 0
		}
		if durSec.Seconds() < 1 {
			Fps++
		} else {
			RefreshWindowCaption()
			Fps = 0
			lastSecond = nowTime
		}

		pipeline.PreRender()
		// gl.Disable(gl.FRAMEBUFFER_SRGB)
		Canvas0.RenderContent()
		// Canvas0.RenderPostFx()
		// gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
		// gl.Viewport(0, 0, gl.Sizei(Screen.W), gl.Sizei(Screen.H))
		// Canvas0.RenderSelf()

		sdl.GL_SwapBuffers()
		glutil.PanicIfErrors("Post Render Loop")
	}
}
Exemple #5
0
func main() {
	var now = time.Now()
	initialWidth, initialHeight := 1280, 720
	runtime.LockOSThread()
	if sdlInit := sdl.Init(sdl.INIT_VIDEO); sdlInit != 0 {
		panic("SDL init error")
	}
	reinitScreen(initialWidth, initialHeight)
	defer cleanExit(true, false)
	if err := gl.Init(); err != nil {
		panic(err)
	}
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.DEPTH)
	sdl.WM_SetCaption("Loading volume...", "")
	loadVolume()
	defer cleanExit(false, true)
	sdl.WM_SetCaption("Compiling shaders...", "")
	glSetupShaderProg(&shaderTextureCreator)
	glSetupShaderProg(&shaderTextureDisplay)
	glFillBuffer(rttVerts, &rttVertBuf)
	glFillBuffer(dispVerts, &dispVertBuf)
	gl.GenTextures(1, &rttFrameTex)
	gl.BindTexture(gl.TEXTURE_2D, rttFrameTex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glTexFilter)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glTexFilter)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, glTexClamp)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, glTexClamp)
	if doRtt && !noRtt {
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texWidth, texHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
		gl.GenFramebuffers(1, &rttFrameBuf)
		gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
		gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rttFrameTex, 0)
		gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
	} else {
		if noRtt {
			rttFrameBuf = 0
		} else {
			glFillTextureFromImageFile("texture.jpg")
		}
	}
	gl.BindTexture(gl.TEXTURE_2D, 0)
	gl.ClearColor(0.3, 0.2, 0.1, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	var looping = true
	var fps = 0
	var durSec time.Duration
	startTime, lastLoopTime, lastSecond = now, now, now
	sdl.WM_SetCaption("Up & running!", "")
	for looping {
		now = time.Now()
		if durSec = now.Sub(lastSecond); durSec.Seconds() >= 1 {
			sdl.WM_SetCaption(fmt.Sprintf("%v×%v @ %vfps", sdlScreen.W, sdlScreen.H, fps), "")
			fps = 0
			lastSecond = now
		} else {
			fps++
		}
		if evt := sdl.PollEvent(); evt != nil {
			switch event := evt.(type) {
			case *sdl.ResizeEvent:
				reinitScreen(int(event.W), int(event.H))
			case *sdl.QuitEvent:
				looping = false
			}
		}
		if doRtt || noRtt {
			renderToTexture()
		}
		if !noRtt {
			renderToScreen()
		}
		sdl.GL_SwapBuffers()
		lastLoopTime = now
	}
	sdl.Quit()
}