【golang/g3n】3D游戏引擎G3N的windows安装与测试

目录

说在前面

  • 操作系统:win 11
  • go version:go1.21.5 windows/amd64
  • g3n版本:github.com/g3n/engine v0.2.0
  • 其他:找了下golang 3d相关的库,目前好像就这个比较活跃

安装

  • 按照官方教程所说,需要mingw环境,首先按照教程去下载 We tested the Windows build using the mingw-w64 toolchain (you can download this file in particular).

  • 下载后将文件解压,并添加环境变量(主要是binincludelib)

  • 然后下载g3n源代码

    shell 复制代码
    git clone https://github.com/g3n/engine g3n-engine

    同样添加下环境变量

  • 重启

  • 然后可以开始install

    shell 复制代码
    cd g3n-engine
    go install ./...

测试

  • 创建一个空文件加,创建一个main.go 文件

    go 复制代码
    package main
    
    import (
    	"time"
    
    	"github.com/g3n/engine/app"
    	"github.com/g3n/engine/camera"
    	"github.com/g3n/engine/core"
    	"github.com/g3n/engine/geometry"
    	"github.com/g3n/engine/gls"
    	"github.com/g3n/engine/graphic"
    	"github.com/g3n/engine/gui"
    	"github.com/g3n/engine/light"
    	"github.com/g3n/engine/material"
    	"github.com/g3n/engine/math32"
    	"github.com/g3n/engine/renderer"
    	"github.com/g3n/engine/util/helper"
    	"github.com/g3n/engine/window"
    )
    
    func main() {
    
    	// 创建app
    	a := app.App()
    	// 创建scene
    	scene := core.NewNode()
    
    	// 设置gui控制器
    	gui.Manager().Set(scene)
    
    	// 创建相机
    	cam := camera.New(1)
    	cam.SetPosition(0, 0, 3)
    	scene.Add(cam)
    
    	// 初始化相机控制器
    	camera.NewOrbitControl(cam)
    
    	// 设置窗口回调
    	onResize := func(evname string, ev interface{}) {
    		// Get framebuffer size and update viewport accordingly
    		width, height := a.GetSize()
    		a.Gls().Viewport(0, 0, int32(width), int32(height))
    		// Update the camera's aspect ratio
    		cam.SetAspect(float32(width) / float32(height))
    	}
    	a.Subscribe(window.OnWindowSize, onResize)
    	onResize("", nil)
    
    	// 添加一个蓝色甜甜圈
    	geom := geometry.NewTorus(1, .4, 12, 32, math32.Pi*2)
    	mat := material.NewStandard(math32.NewColor("DarkBlue"))
    	mesh := graphic.NewMesh(geom, mat)
    	scene.Add(mesh)
    
    	// 添加一个改变甜甜圈颜色的按钮
    	btn := gui.NewButton("Make Red")
    	btn.SetPosition(100, 40)
    	btn.SetSize(40, 40)
    	btn.Subscribe(gui.OnClick, func(name string, ev interface{}) {
    		mat.SetColor(math32.NewColor("DarkRed"))
    	})
    	scene.Add(btn)
    
    	// 添加光照
    	scene.Add(light.NewAmbient(&math32.Color{1.0, 1.0, 1.0}, 0.8))
    	pointLight := light.NewPoint(&math32.Color{1, 1, 1}, 5.0)
    	pointLight.SetPosition(1, 0, 2)
    	scene.Add(pointLight)
    
    	// 添加坐标系显示
    	scene.Add(helper.NewAxes(0.5))
    
    	// 设置灰色背景色
    	a.Gls().ClearColor(0.5, 0.5, 0.5, 1.0)
    
    	// 运行应用
    	a.Run(func(renderer *renderer.Renderer, deltaTime time.Duration) {
    		a.Gls().Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
    		renderer.Render(scene, cam)
    	})
    }
  • 初始化

    shell 复制代码
    go mod init test
    go mod tidy
  • 运行

    shell 复制代码
    go run main.go
  • 内存和cpu

相关推荐
年少无知且疯狂2 小时前
【Unity】实现小地图
unity·游戏引擎
AA陈超2 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 #06-11:游戏后效果执行
c++·游戏·ue5·游戏引擎·虚幻
Hello.Reader5 小时前
用 Go Typed Client 快速上手 Elasticsearch —— 从建索引到聚合的完整实战
elasticsearch·golang·jenkins
魔都吴所谓6 小时前
【go】语言的匿名变量如何定义与使用
开发语言·后端·golang
中科米堆6 小时前
中科米堆CASAIM金属件自动3d测量外观尺寸三维检测解决方案
人工智能·3d·视觉检测
徐子竣7 小时前
[学习记录]Unity-Shader-常量缓冲区(CBUFFER)
学习·unity·游戏引擎
大飞pkz7 小时前
【Unity】Application类常见路径
unity·游戏引擎·游戏开发·application常用路径·数据路径·数据存储位置
向宇it7 小时前
【unity小技巧】封装一套 Unity 的植物生长系统
游戏·unity·c#·游戏引擎
山楂树の7 小时前
模型优化——在MacOS 上使用 Python 脚本批量大幅度精简 GLB 模型(通过 Blender 处理)
python·macos·3d·图形渲染·blender
Blossom.11815 小时前
基于深度学习的医学图像分析:使用BERT实现医学文本分类
人工智能·深度学习·机器学习·3d·分类·cnn·bert