Beispiel #1
0
Datei: tetris.go Projekt: dmac/gg
func NewTetris(vertShader, fragShader string) (*Tetris, error) {
	gg.Enable(gg.BLEND)
	gg.BlendFunc(gg.SRC_ALPHA, gg.ONE_MINUS_SRC_ALPHA)

	// Compile the global shader program
	vshader, err := gg.CreateShader([]byte(vertShader), gg.VERTEX_SHADER)
	if err != nil {
		return nil, err
	}
	fshader, err := gg.CreateShader([]byte(fragShader), gg.FRAGMENT_SHADER)
	if err != nil {
		return nil, err
	}
	program := gg.CreateProgram()
	gg.AttachShader(program, vshader)
	gg.AttachShader(program, fshader)
	if err := gg.LinkProgram(program); err != nil {
		return nil, err
	}

	// Set the global projection matrix
	gg.UseProgram(program)
	proj := mgl.Ortho(0, float32(WindowWidth), float32(WindowHeight), 0, 0, 1)
	projUniform, err := gg.GetUniformLocation(program, "proj")
	if err != nil {
		return nil, err
	}
	gg.UniformMatrix4fv(projUniform, proj[:])

	tetris := &Tetris{}

	tetris.textures, err = LoadTextures()
	if err != nil {
		return nil, err
	}

	tetris.bg = NewSprite(WindowWidth, WindowHeight, program, tetris.textures["bg"])
	tetris.board = NewBoard(WidthCells, HeightCells, program, tetris.textures)
	tetris.board.x = Padding
	tetris.board.y = Padding

	rand.Seed(time.Now().Unix())
	tetris.board.current = tetris.board.NewRandomPiece()

	go func() {
		t := time.NewTicker(time.Second)
		for range t.C {
			tetris.mu.Lock()
			if tetris.gameOver {
				return
			}
			tetris.mu.Unlock()
			tetris.HandleInput(inputDown)
		}
	}()

	return tetris, nil
}
Beispiel #2
0
func SetOrthographic(width, height int) {
	// Projection := mathgl.Perspective(45.0, winWidth/winHeight, 0.1, 100.0)
	// Projection := mathgl.Ortho2D(0.0, winWidth, winHeight, 0.0)
	// aspect := float32(s.width / s.height)
	Projection := mathgl.Ortho(0.0, float32(width), float32(height), 0.0, -5.0, 5.0)
	viewM = mathgl.LookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
	projectionM = Projection
	gl.Disable(gl.DEPTH_TEST)
}
Beispiel #3
0
// SetViewport will set the viewport to x, y, w, h. This is interfaced
// directly with opengl and used by the framework. Only use this if you know what
// you are doing
func SetViewport(x, y, w, h int32) {
	screen_width = w
	screen_height = h
	// Set the viewport to top-left corner.
	gl.Viewport(int(y), int(x), int(screen_width), int(screen_height))
	gl_state.viewport = []int32{y, x, screen_width, screen_height}
	gl_state.projectionStack.Load(mgl32.Ortho(float32(x), float32(screen_width), float32(screen_height), float32(y), -1, 1))
	SetScissor(states.back().scissorBox[0], states.back().scissorBox[1], states.back().scissorBox[2], states.back().scissorBox[3])
}
Beispiel #4
0
func NewManager(width, height float32) *Manager {
	m := &Manager{
		Width:    width,
		Height:   height,
		Viewport: mgl.Ortho(0, width, 0, height, 1000, -1000),
		Children: []render.Drawable{},
	}
	return m
}
Beispiel #5
0
func (c *Camera) SetWorldBounds(bounds Rectangle) (err error) {
	c.WorldBounds = bounds
	c.Projection = mgl32.Ortho(
		bounds.Min.X(),
		bounds.Max.X(),
		bounds.Min.Y(),
		bounds.Max.Y(),
		1,
		-1)
	c.Inverse, err = GetInverseMatrix(c.Projection)
	return
}
Beispiel #6
0
func NewScene(vertShader, fragShader string, texture *gg.Texture) (*Scene, error) {
	gg.Enable(gg.DEPTH_TEST)
	gg.Enable(gg.CULL_FACE)
	gg.DepthFunc(gg.LESS)

	gg.Enable(gg.BLEND)
	gg.BlendFunc(gg.SRC_ALPHA, gg.ONE_MINUS_SRC_ALPHA)

	// Compile the global shader program
	vshader, err := gg.CreateShader([]byte(vertShader), gg.VERTEX_SHADER)
	if err != nil {
		return nil, err
	}
	fshader, err := gg.CreateShader([]byte(fragShader), gg.FRAGMENT_SHADER)
	if err != nil {
		return nil, err
	}
	program := gg.CreateProgram()
	gg.AttachShader(program, vshader)
	gg.AttachShader(program, fshader)
	if err := gg.LinkProgram(program); err != nil {
		return nil, err
	}

	// Set the global projection matrix
	gg.UseProgram(program)
	proj := mgl.Ortho(0, float32(WindowWidth), float32(WindowHeight), 0, 0, 1)
	projUniform, err := gg.GetUniformLocation(program, "proj")
	if err != nil {
		return nil, err
	}
	gg.UniformMatrix4fv(projUniform, proj[:])

	vertices := []float32{
		float32(WindowWidth)/2 - 50, float32(WindowHeight)/2 - 50, 0,
		float32(WindowWidth)/2 - 50, float32(WindowHeight)/2 + 50, 0,
		float32(WindowWidth)/2 + 50, float32(WindowHeight)/2 + 50, 0,
		float32(WindowWidth)/2 + 50, float32(WindowHeight)/2 - 50, 0,
	}

	// Initialize sprite
	sprite, err := NewSprite(vertices, program, texture)
	if err != nil {
		return nil, err
	}

	return &Scene{sprite: sprite}, nil
}
Beispiel #7
0
func NewScene() *Scene {
	s := &Scene{
		Camera:  nil,
		Objects: []*Object{},
		//World: physics.NewWorld(),

		lights: []Light{
			/* temporary: test light */
			Light{
				Attenuation: Attenuation{
					Constant:  0.01,
					Linear:    0,
					Quadratic: 1.0,
				},
				Color: mgl.Vec3{1, 1, 1},
				Range: 4,
				Type:  PointLight,
			},
		},
	}

	/* add a few more test lights */
	/*
	   for i := 0; i < 1; i++ {
	       s.lights = append(s.lights, Light {
	           Attenuation: Attenuation {
	               Constant: 0.01,
	               Linear: 0.5,
	               Quadratic: 1.5,
	           },
	           Position: mgl.Vec3 { 0, float32(4*i), 0 },
	           Color: mgl.Vec3 { 1.0, 1.0, 0.65 },
	           Range: 4,
	           Type: PointLight,
	       })
	   }
	*/

	s.lights[0].Position = mgl.Vec3{-11, 11, -11}
	s.lights[0].Color = mgl.Vec3{0.95, 0.95, 0.96}
	s.lights[0].Type = DirectionalLight
	s.lights[0].Projection = mgl.Ortho(-32, 32, 0, 64, -32, 64)
	//s.lights[2].Position = mgl.Vec3 { 10, 40, 10 }
	//s.lights[2].Range = 10
	//s.lights[0].Type = 2
	return s
}
Beispiel #8
0
// startGrab will bind this canvas to grab all drawing operations
// multiple canvases can only be passed in on non mobile platforms
func (canvas *Canvas) startGrab(canvases ...*Canvas) error {
	if gl_state.currentCanvas == canvas {
		return nil // already grabbing
	}

	if canvases != nil && len(canvases) > 0 {
		// Whether the new canvas list is different from the old one.
		// A more thorough check is done below.
		if maxRenderTargets < 4 {
			return fmt.Errorf("Multi-canvas rendering is not supported on this system.")
		}

		if int32(len(canvases)+1) > maxRenderTargets {
			return fmt.Errorf("This system can't simultaneously render to %v canvases.", len(canvases)+1)
		}

		for i := 0; i < len(canvases); i++ {
			if canvases[i].width != canvas.width || canvases[i].height != canvas.height {
				return fmt.Errorf("All canvases must have the same dimensions.")
			}
		}
	}

	// cleanup after previous Canvas
	if gl_state.currentCanvas != nil {
		canvas.systemViewport = gl_state.currentCanvas.systemViewport
		gl_state.currentCanvas.stopGrab(true)
	} else {
		canvas.systemViewport = GetViewport()
	}

	// indicate we are using this Canvas.
	gl_state.currentCanvas = canvas
	// bind the framebuffer object.
	gl.BindFramebuffer(gl.FRAMEBUFFER, canvas.fbo)
	SetViewport(0, 0, canvas.width, canvas.height)
	// Set up the projection matrix
	gl_state.projectionStack.Push()
	gl_state.projectionStack.Load(mgl32.Ortho(0.0, float32(screen_width), 0.0, float32(screen_height), -1, 1))

	canvas.attacheExtra(canvases)
	canvas.attachedCanvases = canvases

	return nil
}
Beispiel #9
0
func (c *Camera) SetWorldBounds(worldCenter, worldSize mgl32.Vec3) (err error) {
	c.WorldCenter = worldCenter
	c.WorldSize = worldSize
	c.calcPxPerUnit()
	var (
		defaultMat4 mgl32.Mat4
		half        = c.WorldSize.Mul(0.5)
		min         = c.WorldCenter.Sub(half)
		max         = c.WorldCenter.Add(half)
	)
	c.Projection = mgl32.Ortho(
		min.X(),
		max.X(),
		min.Y(),
		max.Y(),
		max.Z(),
		min.Z())
	c.Inverse = c.Projection.Inv()
	if c.Inverse == defaultMat4 {
		err = fmt.Errorf("Projection matrix not invertible")
	}
	return
}
Beispiel #10
0
func (c *Camera) SetOrtho(left, right, bottom, top, near, far float32) {
	c.Projection = glm.Ortho(left, right, bottom, top, near, far)
}
Beispiel #11
0
// SetOrtho will set the camera to an orthogonal projection
func (cam *Camera) SetOrtho(w int, h int, zDepth int) {
	cam.zDepth = float32(zDepth)
	cam.Bounds = mgl32.Vec3{float32(w), float32(h), float32(zDepth)}
	cam.projection = mgl32.Ortho(0, float32(w), float32(h), 0, -1, cam.zDepth)
	cam.update()
}
Beispiel #12
0
//SetOrtho sets the projection matrix to be used.
func (sfbo *ShadowFBO) SetOrtho(left, right, bottom, top, near, far float32) {
	sfbo.projection = glm.Ortho(left, right, bottom, top, near, far)
}