func NewGmat4RotationZ(amount gl.Float) Gmat4 { var c, s = gl.Float(math.Cos(float64(amount))), gl.Float(math.Sin(float64(amount))) return Gmat4{ c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, } }
func NewGmat4Rotation(angle gl.Float, vec Gvec3) Gmat4 { var c, s = gl.Float(math.Cos(float64(angle))), gl.Float(math.Sin(float64(angle))) return Gmat4{ c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, } }
func NewGmat4Perspective(yFov, aspect, zNear, zFar gl.Float) Gmat4 { var f = gl.Float(math.Tan((math.Pi / 2) - float64(yFov))) return Gmat4{ f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, (zFar + zNear) / (zNear - zFar), (2 * zFar * zNear) / (zNear - zFar), 0, 0, -1, 0, } }
func MakeTextureFromImageFloatsFile(filePath string, w, h int) gl.Uint { var file, err = os.Open(filePath) var tex gl.Uint var pix = make([]gl.Float, w*h*3) var fVal float32 var raw []uint8 var buf *bytes.Buffer var i int if err != nil { panic(err) } defer file.Close() raw, err = ioutil.ReadAll(file) if err != nil { panic(err) } buf = bytes.NewBuffer(raw) for i = 0; (err == nil) && (i < len(pix)); i++ { if err = binary.Read(buf, binary.LittleEndian, &fVal); err == io.EOF { err = nil break } else if err == nil { pix[i] = gl.Float(fVal) } } if err != nil { panic(err) } sw, sh := gl.Sizei(w), gl.Sizei(h) gl.GenTextures(1, &tex) gl.BindTexture(gl.TEXTURE_2D, tex) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexStorage2D(gl.TEXTURE_2D, 1, gl.RGB16F, sw, sh) gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, sw, sh, gl.RGB, gl.FLOAT, gl.Pointer(&pix[0])) gl.BindTexture(gl.TEXTURE_2D, 0) PanicIfErrors("MakeTextureFromImageFloatsFile(%v)", filePath) return tex }
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") } }
func WriteFloatsVec4(vec num.Vec3, vecW float64, index int, glFloats []gl.Float) { glFloats[index+0], glFloats[index+1], glFloats[index+2], glFloats[index+3] = gl.Float(vec.X), gl.Float(vec.Y), gl.Float(vec.Z), gl.Float(vecW) }
func WriteFloatsVec3(vec num.Vec3, index int, glFloats []gl.Float) { glFloats[index+0], glFloats[index+1], glFloats[index+2] = gl.Float(vec.X), gl.Float(vec.Y), gl.Float(vec.Z) }
func WriteFloats4(vecX, vecY, vecZ, vecW float64, index int, glFloats []gl.Float) { glFloats[index+0], glFloats[index+1], glFloats[index+2], glFloats[index+3] = gl.Float(vecX), gl.Float(vecY), gl.Float(vecZ), gl.Float(vecW) }
func (me Gvec3) Magnitude() gl.Float { return gl.Float(math.Sqrt(float64(me.LenSqrt()))) }