Ejemplo n.º 1
0
Archivo: gl.go Proyecto: 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)
}
Ejemplo n.º 2
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"))
}
Ejemplo n.º 3
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
}