Esempio n. 1
0
File: tetris.js.go Progetto: dmac/gg
func main() {
	document := js.Global.Get("document")
	canvas := document.Call("createElement", "canvas")
	document.Get("body").Call("appendChild", canvas)
	canvas.Call("setAttribute", "id", "canvas")
	canvas.Call("setAttribute", "width", WindowWidth)
	canvas.Call("setAttribute", "height", WindowHeight)

	attrs := webgl.DefaultAttributes()
	gl, err := webgl.NewContext(canvas, attrs)
	if err != nil {
		log.Fatal(err)
	}
	ggwebgl.Init(gl)

	const vshader = `#version 100

uniform mat4 proj, model;
attribute vec3 vertex_position;
attribute vec2 vertex_texture;
varying highp vec2 texture_coordinates;

void main() {
	gl_Position = proj * model * vec4(vertex_position, 1);
	texture_coordinates = vertex_texture;
}
`

	const fshader = `#version 100

uniform sampler2D tex_loc;
varying highp vec2 texture_coordinates;

void main() {
	gl_FragColor = texture2D(tex_loc, texture_coordinates);
}
`

	tetris, err := NewTetris(vshader, fshader)
	if err != nil {
		log.Fatal(err)
	}

	dom.GetWindow().Document().AddEventListener("keypress", false, tetris.handleKeyPress)

	for {
		tetris.Draw()
		time.Sleep(16 * time.Millisecond)
	}
}
Esempio n. 2
0
func main() {
	document := js.Global.Get("document")
	canvas := document.Call("createElement", "canvas")
	document.Get("body").Call("appendChild", canvas)
	canvas.Call("setAttribute", "id", "canvas")
	canvas.Call("setAttribute", "width", WindowWidth)
	canvas.Call("setAttribute", "height", WindowHeight)

	attrs := webgl.DefaultAttributes()
	gl, err := webgl.NewContext(canvas, attrs)
	if err != nil {
		log.Fatal(err)
	}
	ggwebgl.Init(gl)

	const vertShader = `

uniform mat4 proj;
attribute vec3 vertex_position;
attribute vec2 vertex_texture;
varying highp vec2 texture_coordinates;

void main() {
	gl_Position = proj * vec4(vertex_position, 1);
	texture_coordinates = vertex_texture;
}
`

	const fragShader = `#version 100

uniform sampler2D tex_loc;
varying highp vec2 texture_coordinates;

void main() {
	gl_FragColor = texture2D(tex_loc, texture_coordinates);
}
`

	texture := newImageTexture("sq.png")
	scene, err := NewScene(vertShader, fragShader, texture)
	if err != nil {
		log.Fatal(err)
	}

	for {
		scene.Draw()
		time.Sleep(16 * time.Millisecond)
	}
}
Esempio n. 3
0
func main() {
	document := js.Global.Get("document")
	canvas := document.Call("createElement", "canvas")
	document.Get("body").Call("appendChild", canvas)
	canvas.Call("setAttribute", "id", "canvas")
	canvas.Call("setAttribute", "width", WindowWidth)
	canvas.Call("setAttribute", "height", WindowHeight)

	attrs := webgl.DefaultAttributes()
	gl, err := webgl.NewContext(canvas, attrs)
	if err != nil {
		log.Fatal(err)
	}
	ggwebgl.Init(gl)

	const vertShader = `#version 100

uniform mat4 proj;
attribute vec3 vertex_position;

void main() {
	gl_Position = proj * vec4(vertex_position, 1);
}
`

	const fragShader = `#version 100

uniform highp vec4 color;

void main() {
	gl_FragColor = color;
}
`
	scene, err := NewScene(vertShader, fragShader)
	if err != nil {
		log.Fatal(err)
	}

	for {
		scene.Draw()
		time.Sleep(16 * time.Millisecond)
	}
}