Example #1
0
func CreateMesh(verticies []float32, texturePaths []string, shader *Shader) (*Mesh, error) {

	modelView := mgl32.Ident4()

	// // Load the texture
	textures := make([]uint32, 0)
	// fmt.Println(texturePaths)
	for _, texturePath := range texturePaths {
		// fmt.Println("Paths", texturePath, texturePaths[i])
		texture, err := createTexture(texturePath)
		if err != nil {
			return nil, err
		}
		textures = append(textures, texture)
	}
	//
	// // Configure the vertex data
	var vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.BindVertexArray(vao)
	//
	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(verticies)*4, gl.Ptr(verticies), gl.STATIC_DRAW)
	//
	gl.EnableVertexAttribArray(shader.attributes["vert"])
	gl.VertexAttribPointer(shader.attributes["vert"], 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0))
	//
	gl.EnableVertexAttribArray(shader.attributes["vertTexCoord"])
	gl.VertexAttribPointer(shader.attributes["vertTexCoord"], 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))

	gl.BindVertexArray(0)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	return &Mesh{
		modelView: modelView,
		textures:  textures,
		verticies: verticies,
		vao:       vao,
		vbo:       vbo,
	}, nil
}
Example #2
0
func (r *Renderer) Render() {
	// defer glfw.Terminate()
	shader := r.Shaders.textureFlat
	program := shader.program
	//
	gl.UseProgram(program)
	//
	gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00"))
	// // Configure global settings
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)
	gl.ClearColor(1.0, 1.0, 1.0, 1.0)

	//
	// angle += elapsed
	// r.Mesh.modelView = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})

	// Render
	// gl.UniformMatrix4fv(shader.uniforms["modelView"], 1, false, &r.Mesh.modelView[0])

	time := glfw.GetTime()
	_ = time - r.PreviousTime
	r.PreviousTime = time

	// fmt.Println(elapsed * 100)

	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.UniformMatrix4fv(shader.uniforms["projection"], 1, false, &r.Projection[0])
	gl.UniformMatrix4fv(shader.uniforms["camera"], 1, false, &r.Camera[0])

	// TODO : batch triangles and use multiple textures
	for _, mesh := range r.Meshes {
		gl.UniformMatrix4fv(shader.uniforms["modelView"], 1, false, &mesh.modelView[0])
		gl.Uniform1i(shader.uniforms["tex"], 0)

		gl.BindVertexArray(mesh.vao)

		gl.ActiveTexture(gl.TEXTURE0)
		gl.BindTexture(gl.TEXTURE_2D, mesh.textures[0])

		gl.DrawArrays(gl.TRIANGLES, 0, int32(len(mesh.verticies)/5))
	}

	// Maintenance
	r.Window.SwapBuffers()
	glfw.PollEvents()
	if r.Ready == false {
		r.Ready = true
	}
}
Example #3
0
File: gl.go Project: gmacd/rt
func (glr *GlRenderer) initScreen() {
	var err error
	glr.program, err = createScreenShader()
	if err != nil {
		panic(err)
	}

	gl.UseProgram(glr.program)

	projection := mgl32.Ortho2D(-1, 1, 1, -1)
	projectionUniform := gl.GetUniformLocation(glr.program, gl.Str("projection\x00"))
	gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0])

	textureUniform := gl.GetUniformLocation(glr.program, gl.Str("tex\x00"))
	gl.Uniform1i(textureUniform, 0)

	glr.texture = createScreenTexture(glr.width, glr.height)

	gl.GenVertexArrays(1, &glr.vao)
	gl.BindVertexArray(glr.vao)

	var quadVertices = []float32{
		//  X, Y, Z, U, V
		1.0, -1.0, 0.0, 1.0, 0.0,
		-1.0, -1.0, 0.0, 0.0, 0.0,
		1.0, 1.0, 0.0, 1.0, 1.0,
		-1.0, -1.0, 0.0, 0.0, 0.0,
		-1.0, 1.0, 0.0, 0.0, 1.0,
		1.0, 1.0, 0.0, 1.0, 1.0,
	}

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(quadVertices)*4, gl.Ptr(quadVertices), gl.STATIC_DRAW)

	vertAttrib := uint32(gl.GetAttribLocation(glr.program, gl.Str("vert\x00")))
	gl.EnableVertexAttribArray(vertAttrib)
	gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0))

	texCoordAttrib := uint32(gl.GetAttribLocation(glr.program, gl.Str("vertTexCoord\x00")))
	gl.EnableVertexAttribArray(texCoordAttrib)
	gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))

	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(1.0, 1.0, 1.0, 1.0)
}
Example #4
0
//
// Init App
// This function initializes the variables and sets up the environment.
//
// @param wrapper (*wrapper.Glw) the window wrapper
//
func InitApp(glw *wrapper.Glw) {
	/* Set the object transformation controls to their initial values */
	x = 0.05
	y = 0
	z = 0
	angle_x = 0
	angle_y = 0
	angle_z = 0
	angle_inc_x = 0
	angle_inc_y = 0
	angle_inc_z = 0
	scale = 1.0
	aspect_ratio = 1.3333
	colourmode = objects.COLOR_SOLID
	var numLats uint32 = 20  // Number of latitudes in our sphere
	var numLongs uint32 = 20 // Number of longitudes in our sphere

	// Generate index (name) for one vertex array object
	gl.GenVertexArrays(1, &vertexArrayObject)

	// Create the vertex array object and make it current
	gl.BindVertexArray(vertexArrayObject)

	// Create the Cube Object
	cube = objects.NewCube(&vertexPositions, &vertexColours, &normals)
	cube.MakeVBO()

	// create the sphere object
	sphere = objects.NewSphere(numLats, numLongs)
	sphere.MakeSphereVBO()

	// Creates the Shader Program
	var err error
	shaderProgram, err = wrapper.LoadShader("./shaders/basic.vert", "./shaders/basic.frag")

	// If there is any error loading the shaders, it panics
	if err != nil {
		panic(err)
	}

	// Define uniforms to send to vertex shader
	modelUniform = gl.GetUniformLocation(shaderProgram, gl.Str("model\x00"))
	colourmodeUniform = gl.GetUniformLocation(shaderProgram, gl.Str("colourmode\x00"))
	viewUniform = gl.GetUniformLocation(shaderProgram, gl.Str("view\x00"))
	projectionUniform = gl.GetUniformLocation(shaderProgram, gl.Str("projection\x00"))
}
Example #5
0
File: gl.go Project: gmacd/rt
func (glr *GlRenderer) Start() {
	go func() {
		glr.initSystem()
		glr.initScreen()

		// Prepare 2 frames to double buffer
		for i := 0; i < 2; i++ {
			glr.freeFrames <- &Frame{
				glr.window.ShouldClose(),
				make([]maths.Rgba, glr.width*glr.height),
				glr.width, glr.height}
		}

		// Loop until stop is requested
		for nextFrame := range glr.framesToRender {
			if nextFrame.ShouldStop {
				break
			}

			gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

			gl.UseProgram(glr.program)
			gl.BindVertexArray(glr.vao)

			updateScreenTexture(glr.texture, nextFrame.Pixels, glr.width, glr.height)
			gl.DrawArrays(gl.TRIANGLES, 0, 6)

			glr.window.SwapBuffers()
			glfw.PollEvents()

			// Consider frame rendered, so push back to be reused
			nextFrame.ShouldStop = glr.window.ShouldClose()
			glr.freeFrames <- nextFrame
		}
	}()
}