Exemplo n.º 1
0
Arquivo: demo.go Projeto: rdterner/gl
func drawScene(w *glfw.Window) {
	width, height := w.GetFramebufferSize()
	ratio := float32(width) / float32(height)
	var x1, x2, y1, y2 float32
	if ratio > 1 {
		x1, x2, y1, y2 = -ratio, ratio, -1, 1
	} else {
		x1, x2, y1, y2 = -1, 1, -1/ratio, 1/ratio
	}

	gl.Viewport(0, 0, int32(width), int32(height))
	gl.Clear(gl.COLOR_BUFFER_BIT)

	// Applies subsequent matrix operations to the projection matrix stack
	gl.MatrixMode(gl.PROJECTION)

	gl.LoadIdentity()                                                   // replace the current matrix with the identity matrix
	gl.Ortho(float64(x1), float64(x2), float64(y1), float64(y2), 1, -1) // multiply the current matrix with an orthographic matrix

	// Applies subsequent matrix operations to the modelview matrix stack
	gl.MatrixMode(gl.MODELVIEW)

	gl.LoadIdentity()

	gl.LineWidth(1)
	gl.Begin(gl.LINE)   // delimit the vertices of a primitive or a group of like primitives
	gl.Color3f(0, 0, 0) // set the current color
	gl.Vertex3f(0, y1, 0)
	gl.Vertex3f(0, y2, 0)
	gl.Vertex3f(x1, 0, 0)
	gl.Vertex3f(x2, 0, 0)
	gl.End()

	gl.Rotatef(float32(glfw.GetTime()*50), 0, 0, 1) // multiply the current matrix by a rotation matrix

	s := float32(.95)

	gl.Begin(gl.TRIANGLES)
	gl.Color3f(1, 0, 0)  // set the current color
	gl.Vertex3f(0, s, 0) // specify a vertex
	gl.Color3f(0, 1, 0)
	gl.Vertex3f(s*.866, s*-0.5, 0)
	gl.Color3f(0, 0, 1)
	gl.Vertex3f(s*-.866, s*-0.5, 0)
	gl.End()

	gl.LineWidth(5)
	gl.Begin(gl.LINE_LOOP)
	for i := float64(0); i < 2*math.Pi; i += .05 {
		r, g, b := hsb2rgb(float32(i/(2*math.Pi)), 1, 1)
		gl.Color3f(r, g, b)
		gl.Vertex3f(s*float32(math.Sin(i)), s*float32(math.Cos(i)), 0)
	}
	gl.End()

}
Exemplo n.º 2
0
// onResize sets up a simple 2d ortho context based on the window size
func onResize(window *glfw.Window, w, h int) {
	w, h = window.GetSize() // query window to get screen pixels
	width, height := window.GetFramebufferSize()
	gl.Viewport(0, 0, int32(width), int32(height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(w), 0, float64(h), -1, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.ClearColor(1, 1, 1, 1)
}
func initOpenGl(window *glfw.Window, w, h int) {
	w, h = window.GetSize() // query window to get screen pixels
	width, height := window.GetFramebufferSize()
	gl.Viewport(0, 0, width, height)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(w), 0, float64(h), -1, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.ClearColor(.25, .88, .83, 1) // turquoise
}
Exemplo n.º 4
0
func onMouseMove(w *glfw.Window, xpos float64, ypos float64) {
	width, height := w.GetSize()
	if currentScreen != nil {
		fw, fh := w.GetFramebufferSize()
		currentScreen.hover(xpos*(float64(fw)/float64(width)), ypos*(float64(fh)/float64(height)), fw, fh)
		return
	}
	if !lockMouse {
		return
	}
	ww, hh := float64(width/2), float64(height/2)
	w.SetCursorPos(ww, hh)

	s := float64(10000-mouseSensitivity.Value()) + 0.01
	rotate((xpos-ww)/s, (ypos-hh)/s)
}
Exemplo n.º 5
0
// createRenderer creates a new graphics Renderer engine and does
// any initializiation necessary.
//
// NOTE: in the future, this can create either a forward or deferred
// renderer with more advanced options.
func createRenderer(mainWindow *glfw.Window) (*GameRenderer, error) {
	gr := new(GameRenderer)
	gr.Shaders = make(map[string]*fizzle.RenderShader)

	// create the renderer itself
	windowW, windowH := mainWindow.GetFramebufferSize()
	forwardRenderer := fizzle.NewForwardRenderer(mainWindow)
	forwardRenderer.Init(int32(windowW), int32(windowH))
	gr.Renderer = forwardRenderer
	gr.MainWindow = mainWindow

	// load the landscape diffuse shader
	shader, err := fizzle.LoadShaderProgramFromFiles(landscapeShaderPath, nil)
	if err != nil {
		return nil, fmt.Errorf("Failed to compile and link the diffuse shader program!\n%v", err)
	}
	gr.Shaders["landscape"] = shader

	// load the diffuse shader
	shader, err = fizzle.LoadShaderProgramFromFiles(diffuseShaderPath, nil)
	if err != nil {
		return nil, fmt.Errorf("Failed to compile and link the diffuse shader program!\n%v", err)
	}
	gr.Shaders["diffuse"] = shader

	// put a test light in the renderer
	light := fizzle.NewLight()
	//light.Position = mgl.Vec3{8.0, 8.0, 8.0}
	light.DiffuseColor = mgl.Vec4{1.0, 1.0, 1.0, 1.0}
	light.Direction = mgl.Vec3{0.1, -1.0, -0.1}
	light.DiffuseIntensity = 0.6
	light.AmbientIntensity = 0.4
	//light.Attenuation = 1.0
	forwardRenderer.ActiveLights[0] = light

	// setup the camera to look at the cube
	gr.camera = fizzle.NewOrbitCamera(mgl.Vec3{0.0, 0.0, 0.0}, mgl.DegToRad(35.0), 16.0, mgl.DegToRad(90.0))

	// set some OpenGL flags
	gl.Enable(gl.CULL_FACE)
	gl.Enable(gl.DEPTH_TEST)

	return gr, nil
}
Exemplo n.º 6
0
func onMouseClick(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
	if currentScreen != nil {
		if button != glfw.MouseButtonLeft || action == glfw.Repeat {
			return
		}
		width, height := w.GetSize()
		xpos, ypos := w.GetCursorPos()
		fw, fh := w.GetFramebufferSize()
		currentScreen.click(action == glfw.Press, xpos*(float64(fw)/float64(width)), ypos*(float64(fh)/float64(height)), fw, fh)
		return
	}
	if !Client.chat.enteringText && lockMouse && action != glfw.Repeat {
		Client.MouseAction(button, action == glfw.Press)
	}
	if button == glfw.MouseButtonLeft && action == glfw.Press && !Client.chat.enteringText {
		lockMouse = true
		w.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
	}
}
Exemplo n.º 7
0
func drawBuffer(window *glfw.Window) {
	w, h := window.GetFramebufferSize()
	s1 := float32(w) / 256
	s2 := float32(h) / 240
	f := float32(1 - padding)
	var x, y float32
	if s1 >= s2 {
		x = f * s2 / s1
		y = f
	} else {
		x = f
		y = f * s1 / s2
	}
	gl.Begin(gl.QUADS)
	gl.TexCoord2f(0, 1)
	gl.Vertex2f(-x, -y)
	gl.TexCoord2f(1, 1)
	gl.Vertex2f(x, -y)
	gl.TexCoord2f(1, 0)
	gl.Vertex2f(x, y)
	gl.TexCoord2f(0, 0)
	gl.Vertex2f(-x, y)
	gl.End()
}
Exemplo n.º 8
0
Arquivo: demo.go Projeto: rdterner/gl
func render(w *glfw.Window, r *gResources) {

	width, height := w.GetFramebufferSize()
	ratio := float32(width) / float32(height)

	var xmul, ymul float32
	if ratio > 1 {
		xmul, ymul = ra/ratio, ra
	} else {
		xmul, ymul = ra, ra*ratio
	}

	d := time.Since(start).Seconds()
	sin := float32(math.Sin(d))
	cos := float32(math.Cos(d))

	gl.Viewport(0, 0, int32(width), int32(height))
	gl.Clear(gl.COLOR_BUFFER_BIT)

	////////////////

	// axes

	gl.UseProgram(r.program1)

	gl.BindBuffer(gl.ARRAY_BUFFER, r.vertexBuffer1)

	gl.VertexAttribPointer(
		uint32(r.attributes1.position), // attribute
		2,               // size
		gl.FLOAT,        // type
		false,           // normalized?
		8,               // stride
		gl.PtrOffset(0)) // array buffer offset
	gl.EnableVertexAttribArray(uint32(r.attributes1.position))

	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.elementBuffer1)

	gl.LineWidth(1)
	gl.DrawElements(
		gl.LINES,        // mode
		4,               // count
		gl.UNSIGNED_INT, // type
		gl.PtrOffset(0)) // element array buffer offset

	gl.DisableVertexAttribArray(uint32(r.attributes1.position))

	////////////////

	// triangle

	gl.UseProgram(r.program2)

	gl.Uniform1f(r.uniforms2.xmul, xmul)
	gl.Uniform1f(r.uniforms2.ymul, ymul)
	gl.Uniform1f(r.uniforms2.sin, sin)
	gl.Uniform1f(r.uniforms2.cos, cos)

	gl.BindBuffer(gl.ARRAY_BUFFER, r.vertexBuffer2)

	gl.VertexAttribPointer(
		uint32(r.attributes2.position), // attribute
		2,               // size
		gl.FLOAT,        // type
		false,           // normalized?
		8,               // stride
		gl.PtrOffset(0)) // array buffer offset
	gl.EnableVertexAttribArray(uint32(r.attributes2.position))

	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.elementBuffer2)

	gl.EnableVertexAttribArray(uint32(r.attributes2.color))
	gl.BindBuffer(gl.ARRAY_BUFFER, r.colorBuffer2)
	gl.VertexAttribPointer(
		uint32(r.attributes2.color), // attribute
		3,               // size
		gl.FLOAT,        // type
		false,           // normalized?
		0,               // stride
		gl.PtrOffset(0)) // array buffer offset

	gl.DrawElements(
		gl.TRIANGLES,    // mode
		3,               // count
		gl.UNSIGNED_INT, // type
		gl.PtrOffset(0)) // element array buffer offset

	////////////////

	// circle

	gl.BindBuffer(gl.ARRAY_BUFFER, r.vertexBuffer3)

	gl.VertexAttribPointer(
		uint32(r.attributes2.position), // attribute
		2,               // size
		gl.FLOAT,        // type
		false,           // normalized?
		8,               // stride
		gl.PtrOffset(0)) // array buffer offset

	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.elementBuffer3)

	gl.BindBuffer(gl.ARRAY_BUFFER, r.colorBuffer3)

	gl.VertexAttribPointer(
		uint32(r.attributes2.color), // attribute
		3,               // size
		gl.FLOAT,        // type
		false,           // normalized?
		0,               // stride
		gl.PtrOffset(0)) // array buffer offset

	gl.LineWidth(5)
	gl.DrawElements(
		gl.LINE_LOOP,    // mode
		r.len3,          // count
		gl.UNSIGNED_INT, // type
		gl.PtrOffset(0)) // element array buffer offset

	gl.DisableVertexAttribArray(uint32(r.attributes2.color))
	gl.DisableVertexAttribArray(uint32(r.attributes2.position))

}