Пример #1
0
// Render renders the model
func (m *Model) Render() {

	gl.UseProgram(m.currentProgram)
	gl.UniformMatrix4fv(m.modelUniform, 1, false, &m.model[0])

	gl.BindVertexArray(m.vao)

	gl.ActiveTexture(gl.TEXTURE0)

	texture, isLoaded := m.textures.GetTexture(m.data.TextureFile)

	if isLoaded {
		gl.BindTexture(gl.TEXTURE_2D, texture)
	} else {
		if m.data.TextureFile != "" {
			go fmt.Printf("Unable to load texture %s", m.data.TextureFile)
		}
	}

	if m.data.Indexed {
		gl.DrawElements(gl.TRIANGLE_FAN, int32(len(m.data.Indices)), gl.UNSIGNED_INT, gl.PtrOffset(0))
	} else {
		gl.DrawArrays(gl.TRIANGLES, 0, int32(len(m.data.Verts))/m.data.VertSize)
	}

	gl.BindVertexArray(0)
}
Пример #2
0
func (v *VBO) DrawQuads(nquads int) {
	gl.DrawElements(gl.TRIANGLES, int32(nquads*6), gl.UNSIGNED_INT, nil)
	//gl.DrawElements(gl.LINE_STRIP, 6, gl.UNSIGNED_INT, nil)
}
Пример #3
0
func (v *VBO) Draw() {
	gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil)
	//gl.DrawElements(gl.LINE_STRIP, 6, gl.UNSIGNED_INT, nil)
}
func programLoop(window *glfw.Window) {

	// the linked shader program determines how the data will be rendered
	vertexShader := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
	fragmentShaderBlue := compileShader(fragmentShaderBlueSource, gl.FRAGMENT_SHADER)
	shaderProgramBlue := linkShaders([]uint32{vertexShader, fragmentShaderBlue})
	fragmentShaderRed := compileShader(fragmentShaderRedSource, gl.FRAGMENT_SHADER)
	shaderProgramRed := linkShaders([]uint32{vertexShader, fragmentShaderRed})

	// shader objects are not needed after they are linked into a program object
	gl.DeleteShader(vertexShader)
	gl.DeleteShader(fragmentShaderBlue)
	gl.DeleteShader(fragmentShaderRed)

	vertices1 := []float32{
		0.2, 0.2, 0.0, // top right
		0.2, -0.8, 0.0, // bottom right
		-0.8, -0.8, 0.0, // bottom left
		-0.8, 0.2, 0.0, // top left
	}

	indices1 := []uint32{
		0, 1, 3, // first triangle
		1, 2, 3, // second triangle
	}

	VAO1 := createTriangleVAO(vertices1, indices1)

	vertices2 := []float32{
		0.2, 0.6, 0.0, // top
		0.6, -0.2, 0.0, // bottom right
		-0.2, -0.2, 0.0, // bottom left
	}

	indices2 := []uint32{
		0, 1, 2, // only triangle
	}

	VAO2 := createTriangleVAO(vertices2, indices2)

	for !window.ShouldClose() {
		// poll events and call their registered callbacks
		glfw.PollEvents()

		// perform rendering
		gl.ClearColor(0.2, 0.5, 0.5, 1.0)
		gl.Clear(gl.COLOR_BUFFER_BIT)

		// draw loop

		// draw rectangle
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
		gl.UseProgram(shaderProgramRed)
		gl.BindVertexArray(VAO1)
		gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, unsafe.Pointer(nil))
		gl.BindVertexArray(0)

		// draw triangle
		gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
		gl.UseProgram(shaderProgramBlue)
		gl.BindVertexArray(VAO2)
		gl.DrawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, unsafe.Pointer(nil))
		gl.BindVertexArray(0)

		// end of draw loop

		// swap in the rendered buffer
		window.SwapBuffers()
	}
}
Пример #5
0
func programLoop(window *glfw.Window) error {

	// the linked shader program determines how the data will be rendered
	vertShader, err := gfx.NewShaderFromFile("shaders/basic.vert", gl.VERTEX_SHADER)
	if err != nil {
		return err
	}

	fragShader, err := gfx.NewShaderFromFile("shaders/basic.frag", gl.FRAGMENT_SHADER)
	if err != nil {
		return err
	}

	shaderProgram, err := gfx.NewProgram(vertShader, fragShader)
	if err != nil {
		return err
	}
	defer shaderProgram.Delete()

	vertices := []float32{
		// top
		0.0, 0.5, 0.0, // position
		1.0, 0.0, 0.0, // Color

		// bottom right
		0.5, -0.5, 0.0,
		0.0, 1.0, 0.0,

		// bottom left
		-0.5, -0.5, 0.0,
		0.0, 0.0, 1.0,
	}

	indices := []uint32{
		0, 1, 2, // only triangle
	}

	VAO := createTriangleVAO(vertices, indices)

	for !window.ShouldClose() {
		// poll events and call their registered callbacks
		glfw.PollEvents()

		// perform rendering
		gl.ClearColor(0.2, 0.5, 0.5, 1.0)
		gl.Clear(gl.COLOR_BUFFER_BIT)

		// draw loop

		// draw triangle
		shaderProgram.Use()
		gl.BindVertexArray(VAO)
		gl.DrawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, unsafe.Pointer(nil))
		gl.BindVertexArray(0)

		// end of draw loop

		// swap in the rendered buffer
		window.SwapBuffers()
	}

	return nil
}
Пример #6
0
func programLoop(window *glfw.Window) error {

	// the linked shader program determines how the data will be rendered
	vertShader, err := gfx.NewShaderFromFile("shaders/basic.vert", gl.VERTEX_SHADER)
	if err != nil {
		return err
	}

	fragShader, err := gfx.NewShaderFromFile("shaders/basic.frag", gl.FRAGMENT_SHADER)
	if err != nil {
		return err
	}

	shaderProgram, err := gfx.NewProgram(vertShader, fragShader)
	if err != nil {
		return err
	}
	defer shaderProgram.Delete()

	vertices := []float32{
		// top left
		-0.75, 0.75, 0.0, // position
		1.0, 0.0, 0.0, // Color
		1.0, 0.0, // texture coordinates

		// top right
		0.75, 0.75, 0.0,
		0.0, 1.0, 0.0,
		0.0, 0.0,

		// bottom right
		0.75, -0.75, 0.0,
		0.0, 0.0, 1.0,
		0.0, 1.0,

		// bottom left
		-0.75, -0.75, 0.0,
		1.0, 1.0, 1.0,
		1.0, 1.0,
	}

	indices := []uint32{
		// rectangle
		0, 1, 2, // top triangle
		0, 2, 3, // bottom triangle
	}

	VAO := createVAO(vertices, indices)
	texture0, err := gfx.NewTextureFromFile("../images/RTS_Crate.png",
		gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE)
	if err != nil {
		panic(err.Error())
	}

	texture1, err := gfx.NewTextureFromFile("../images/trollface.png",
		gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE)
	if err != nil {
		panic(err.Error())
	}

	for !window.ShouldClose() {
		// poll events and call their registered callbacks
		glfw.PollEvents()

		// background color
		gl.ClearColor(0.2, 0.5, 0.5, 1.0)
		gl.Clear(gl.COLOR_BUFFER_BIT)

		// draw vertices
		shaderProgram.Use()

		// set texture0 to uniform0 in the fragment shader
		texture0.Bind(gl.TEXTURE0)
		texture0.SetUniform(shaderProgram.GetUniformLocation("ourTexture0"))

		// set texture1 to uniform1 in the fragment shader
		texture1.Bind(gl.TEXTURE1)
		texture1.SetUniform(shaderProgram.GetUniformLocation("ourTexture1"))

		gl.BindVertexArray(VAO)
		gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, unsafe.Pointer(nil))
		gl.BindVertexArray(0)

		texture0.UnBind()
		texture1.UnBind()

		// end of draw loop

		// swap in the rendered buffer
		window.SwapBuffers()
	}

	return nil
}
Пример #7
0
func (glRenderer *OpenglRenderer) DrawGeometry(geometry *renderer.Geometry, transform mgl32.Mat4) {
	glRenderer.enableShader()
	glRenderer.enableMaterial()
	glRenderer.enableCubeMap()

	if glRenderer.activeShader == nil {
		panic("ERROR: No shader is configured.")
	}

	shader := glRenderer.activeShader
	program := shader.Program
	params := glRenderer.rendererParams

	glRenderer.enableDepthTest(params.DepthTest)
	glRenderer.enableDepthMask(params.DepthMask)
	glRenderer.enableCullFace(params.CullBackface)
	glRenderer.enableUnlit(params.Unlit)
	glRenderer.setTransparency(params.Transparency)

	// set buffers
	gl.BindBuffer(gl.ARRAY_BUFFER, geometry.VboId)
	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, geometry.IboId)

	// update buffers
	if geometry.VboDirty && len(geometry.Verticies) > 0 && len(geometry.Indicies) > 0 {
		gl.BufferData(gl.ARRAY_BUFFER, len(geometry.Verticies)*4, gl.Ptr(geometry.Verticies), gl.DYNAMIC_DRAW)
		gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(geometry.Indicies)*4, gl.Ptr(geometry.Indicies), gl.DYNAMIC_DRAW)
		geometry.VboDirty = false
	}

	// set uniforms
	modelNormal := transform.Inv().Transpose()
	shader.Uniforms["model"] = transform
	shader.Uniforms["modelNormal"] = modelNormal

	// set camera uniforms
	cam := glRenderer.camera
	win := glRenderer.WindowDimensions()
	shader.Uniforms["cameraTranslation"] = cam.Translation
	if cam.Ortho {
		shader.Uniforms["projection"] = mgl32.Ortho2D(0, win.X(), win.Y(), 0)
		shader.Uniforms["camera"] = mgl32.Ident4()
	} else {
		shader.Uniforms["projection"] = mgl32.Perspective(mgl32.DegToRad(cam.Angle), win.X()/win.Y(), cam.Near, cam.Far)
		shader.Uniforms["camera"] = mgl32.LookAtV(cam.Translation, cam.Lookat, cam.Up)
	}

	shader.Uniforms["unlit"] = glRenderer.unlit
	shader.Uniforms["useTextures"] = glRenderer.useTextures

	shader.Uniforms["ambientLightValue"] = glRenderer.ambientLightValue

	shader.Uniforms["nbPointLights"] = glRenderer.nbPointLights
	shader.Uniforms["pointLightValues"] = glRenderer.pointLightValues
	shader.Uniforms["pointLightPositions"] = glRenderer.pointLightPositions

	shader.Uniforms["nbDirectionalLights"] = glRenderer.nbDirectionalLights
	shader.Uniforms["directionalLightValues"] = glRenderer.directionalLightValues
	shader.Uniforms["directionalLightVectors"] = glRenderer.directionalLightVectors

	// set custom uniforms
	setupUniforms(shader)

	// set verticies attribute
	vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vert\x00")))
	gl.EnableVertexAttribArray(vertAttrib)
	gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, renderer.VertexStride*4, gl.PtrOffset(0))
	// set normals attribute
	normAttrib := uint32(gl.GetAttribLocation(program, gl.Str("normal\x00")))
	gl.EnableVertexAttribArray(normAttrib)
	gl.VertexAttribPointer(normAttrib, 3, gl.FLOAT, false, renderer.VertexStride*4, gl.PtrOffset(3*4))
	// set texture coord attribute
	texCoordAttrib := uint32(gl.GetAttribLocation(program, gl.Str("texCoord\x00")))
	gl.EnableVertexAttribArray(texCoordAttrib)
	gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, renderer.VertexStride*4, gl.PtrOffset(6*4))
	// vertex color attribute
	colorAttrib := uint32(gl.GetAttribLocation(program, gl.Str("color\x00")))
	gl.EnableVertexAttribArray(colorAttrib)
	gl.VertexAttribPointer(colorAttrib, 4, gl.FLOAT, false, renderer.VertexStride*4, gl.PtrOffset(8*4))

	gl.DrawElements(gl.TRIANGLES, (int32)(len(geometry.Indicies)), gl.UNSIGNED_INT, gl.PtrOffset(0))
}