Home  >  Article  >  Backend Development  >  How to make golang cube

How to make golang cube

王林
王林Original
2023-05-14 18:01:38463browse

With the rapid development of cloud computing, big data, artificial intelligence and other technologies, the demand for programming languages ​​is also getting higher and higher. Among them, Golang, as a new programming language launched by Google, has attracted much attention because of its efficiency, simplicity, security and other characteristics. The processing of cubes has also become one of the key issues in Golang development. This article will introduce the Golang cube processing method to help readers better understand Golang's development technology.

1. Introduction to Cube

In three-dimensional space, a cube is a hexahedron, and each face is a square. A standard cube has eight vertices and twelve edges. The formula for cubic volume is V=a³, where a represents the side length of the cube.

In computer graphics processing, the cube is a frequently used object. The cube can represent the basic shape of a 3D model and can also be used as the basic unit in the rendering process.

2. Golang cube processing method

1. Create a cube

In Golang, three keywords are needed to create a cube: mesh, geometry and material. Among them, mesh represents the object mesh model, geometry represents the object geometry, and material represents the material of the object (such as texture, color, etc.).

Here is the sample code to create a cube:

package main

import (

"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"

)

type Cube struct {

vao             uint32
vbo             uint32
vertexPositions []float32
shaderProgram   uint32

}

func (c *Cube) Init(shaderProgram uint32) {

c.vertexPositions = []float32{
    // Front
    -1.0, -1.0, 1.0,
    1.0, -1.0, 1.0,
    1.0, 1.0, 1.0,
    -1.0, 1.0, 1.0,
    // Back
    -1.0, -1.0, -1.0,
    1.0, -1.0, -1.0,
    1.0, 1.0, -1.0,
    -1.0, 1.0, -1.0,
}

indices := []uint32{
    // Front
    0, 1, 2,
    2, 3, 0,
    // Back
    4, 5, 6,
    6, 7, 4,
    // Top
    3, 2, 6,
    6, 7, 3,
    // Bottom
    0, 1, 5,
    5, 4, 0,
    // Left
    0, 3, 7,
    7, 4, 0,
    // Right
    1, 2, 6,
    6, 5, 1,
}

c.shaderProgram = shaderProgram
gl.GenVertexArrays(1, &c.vao)
gl.BindVertexArray(c.vao)

gl.GenBuffers(1, &c.vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, c.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(c.vertexPositions)*3*4, gl.Ptr(c.vertexPositions), gl.STATIC_DRAW)

gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0))
gl.EnableVertexAttribArray(0)

gl.GenBuffers(1, &ibo)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(indices)*3*4, gl.Ptr(indices), gl.STATIC_DRAW)

}

func (c *Cube) Draw() {

gl.UseProgram(c.shaderProgram)
gl.BindVertexArray(c.vao)
gl.DrawElements(gl.TRIANGLES, 6*2*3, gl.UNSIGNED_INT, gl.PtrOffset(0))

}

func (c *Cube) Destroy() {

gl.DeleteVertexArrays(1, &c.vao)
gl.DeleteBuffers(1, &c.vbo)
gl.DeleteProgram(c.shaderProgram)

}

2. Cube rotation

In Golang, The cube can be rotated in three dimensions by using the Rotate3D method in the math library glmath. Here is a sample code for a simple cube rotation:

package main

import (

"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"

)

func main() {

if err := gl.Init(); err != nil {
    panic(err)
}
defer gl.Terminate()

window := createWindow()

shaderProgram := createShaderProgram()

cube := &Cube{}
cube.Init(shaderProgram)

for !window.ShouldClose() {
    gl.ClearColor(0.2, 0.2, 0.3, 1.0)
    gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

    // 计算旋转矩阵
    angle := float32(glfw.GetTime()) * mgl32.DegToRad(45.0)
    axis := mgl32.Vec3{0, 1, 0}
    model := mgl32.Ident4()
    model = model.Mul4(mgl32.Translate3D(0, 0, -4)) // 平移
    model = model.Mul4(mgl32.HomogRotate3D(angle, axis)) // 旋转

    // 更新uniform值
    gl.UseProgram(shaderProgram)
    gl.UniformMatrix4fv(gl.GetUniformLocation(shaderProgram, gl.Str("model")), 1, false, &model[0])

    cube.Draw()

    window.SwapBuffers()
    glfw.PollEvents()
}

cube.Destroy()

}

3. Cube texture mapping

In Golang, you can use OpenGL methods to perform texture mapping operations. First, you need to load the texture file, and then perform mapping operations on the surface of the cube.

Here is a sample code for a simple cube texture mapping:

package main

import (

"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/mathgl/mgl32"
"image"
"image/draw"
_ "image/jpeg"
_ "image/png"
"os"

)

func LoadTextureFromFile (filepath string) (texture uint32, err error) {

// 加载纹理文件
file, err := os.Open(filepath)
if err != nil {
    return 0, err
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
    return 0, err
}

// 创建空白纹理
rgba := image.NewRGBA(img.Bounds())
if rgba.Stride != rgba.Rect.Size().X*4 {
    panic("unsupported stride")
}
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)

// 创建纹理
gl.GenTextures(1, &texture)
gl.BindTexture(gl.TEXTURE_2D, texture)

gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)

gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix))

return texture, nil

}

func main() {

if err := gl.Init(); err != nil {
    panic(err)
}
defer gl.Terminate()

window := createWindow()

shaderProgram := createShaderProgram()

cube := &Cube{}
cube.Init(shaderProgram)

// 加载纹理
texture, err := LoadTextureFromFile("texture.jpg")
if err == nil {
    gl.BindTexture(gl.TEXTURE_2D, texture)
}

for !window.ShouldClose() {
    gl.ClearColor(0.2, 0.2, 0.3, 1.0)
    gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

    // 计算旋转矩阵
    angle := float32(glfw.GetTime()) * mgl32.DegToRad(45.0)
    axis := mgl32.Vec3{0, 1, 0}
    model := mgl32.Ident4()
    model = model.Mul4(mgl32.Translate3D(0, 0, -4)) // 平移
    model = model.Mul4(mgl32.HomogRotate3D(angle, axis)) // 旋转

    // 更新uniform值
    gl.UseProgram(shaderProgram)
    gl.UniformMatrix4fv(gl.GetUniformLocation(shaderProgram, gl.Str("model")), 1, false, &model[0])

    cube.Draw()

    window.SwapBuffers()
    glfw.PollEvents()
}

cube.Destroy()

}

3. Summary

Golang, as a new programming language, has received widespread attention for its efficiency, simplicity, security and other characteristics. In terms of cube processing, Golang provides a wealth of processing methods, including cube creation, cube rotation, and cube texture mapping. Through the above sample code, readers can further understand Golang's development technology and cubic processing principles, so as to better apply Golang for development work.

The above is the detailed content of How to make golang cube. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn