Exemplo n.º 1
0
Arquivo: opengl.go Projeto: toophy/vu
// Render implementation.
// FUTURE: all kinds of possible optimizations that would need to be
//         profiled before implementing.
//           • group by vao to avoid switching vao's.
//           • group by texture to avoid switching textures.
//           • use interleaved vertex data.
//           • uniform buffers http://www.opengl.org/wiki/Uniform_Buffer_Object.
//           • ... lots more possiblities... leave your fav here.
func (gc *opengl) Render(dr Draw) {
	d, ok := dr.(*draw)
	if !ok || d == nil {
		return
	}

	// switch state only if necessary.
	if gc.depthTest != d.depth {
		if d.depth {
			gl.Enable(gl.DEPTH_TEST)
		} else {
			gl.Disable(gl.DEPTH_TEST)
		}
		gc.depthTest = d.depth
	}

	// switch shaders only if necessary.
	if gc.shader != d.shader {
		gl.UseProgram(d.shader)
		gc.shader = d.shader
	}

	// Ask the model to bind its provisioned uniforms.
	// FUTURE: only need to bind uniforms that have changed.
	gc.bindUniforms(d)

	// bind the data buffers and render.
	gl.BindVertexArray(d.vao)
	switch d.mode {
	case LINES:
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
		gl.DrawElements(gl.LINES, d.numFaces, gl.UNSIGNED_SHORT, 0)
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
	case POINTS:
		gl.Enable(gl.PROGRAM_POINT_SIZE)
		gl.DrawArrays(gl.POINTS, 0, d.numVerts)
		gl.Disable(gl.PROGRAM_POINT_SIZE)
	case TRIANGLES:
		if len(d.texs) > 1 && d.texs[0].fn > 0 {
			// Multiple textures on one model specify which verticies they apply to.
			for _, tex := range d.texs {
				// Use the same texture unit and sampler. Just update which
				// image is being sampled.
				gl.BindTexture(gl.TEXTURE_2D, tex.tid)
				// fn is the number of triangles, 3 indicies per triangle.
				// f0 is the offset in triangles where each triangle has 3 indicies
				//    of 2 bytes (uShort) each.
				gl.DrawElements(gl.TRIANGLES, tex.fn*3, gl.UNSIGNED_SHORT, int64(3*2*tex.f0))
			}
		} else {
			// Single textures are handled with a standard bindUniforms
			gl.DrawElements(gl.TRIANGLES, d.numFaces, gl.UNSIGNED_SHORT, 0)
		}
	}
}
Exemplo n.º 2
0
// Renderer implementation.
func (gc *opengl) Enable(attribute uint32, enabled bool) {
	switch attribute {
	case CULL, DEPTH:
		if enabled {
			gl.Enable(attribute)
		} else {
			gl.Disable(attribute)
		}
	case BLEND:
		if enabled {
			gl.Enable(attribute)

			// Using non pre-multiplied alpha colour data so...
			gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
		} else {
			gl.Disable(attribute)
		}
	}
}