mirror of
https://github.com/bloeys/nterm.git
synced 2025-12-29 14:38:19 +00:00
Move to latest nMage
This commit is contained in:
@ -173,16 +173,12 @@ func (gr *GlyphRend) updateFontAtlasTexture(fontFile string) error {
|
|||||||
}
|
}
|
||||||
defer os.Remove(pngFileName)
|
defer os.Remove(pngFileName)
|
||||||
|
|
||||||
atlasTex, err := assets.LoadPNGTexture(pngFileName)
|
atlasTex, err := assets.LoadTexturePNG(pngFileName, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gr.AtlasTex = &atlasTex
|
gr.AtlasTex = &atlasTex
|
||||||
|
|
||||||
//TODO: We want a function to load without caching. For now we clear manually
|
|
||||||
delete(assets.Textures, atlasTex.TexID)
|
|
||||||
delete(assets.TexturePaths, pngFileName)
|
|
||||||
|
|
||||||
gl.BindTexture(gl.TEXTURE_2D, atlasTex.TexID)
|
gl.BindTexture(gl.TEXTURE_2D, atlasTex.TexID)
|
||||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
||||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
||||||
@ -229,7 +225,7 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
|
|||||||
gr.GlyphMesh = &meshes.Mesh{
|
gr.GlyphMesh = &meshes.Mesh{
|
||||||
Name: "glypQuad",
|
Name: "glypQuad",
|
||||||
|
|
||||||
//VertPos, UV, Color; Instanced attributes are stored separately
|
//VertPos only. Instanced attributes are stored separately
|
||||||
Buf: buffers.NewBuffer(
|
Buf: buffers.NewBuffer(
|
||||||
buffers.Element{ElementType: buffers.DataTypeVec3},
|
buffers.Element{ElementType: buffers.DataTypeVec3},
|
||||||
),
|
),
|
||||||
@ -249,8 +245,7 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
|
|||||||
})
|
})
|
||||||
|
|
||||||
//Setup material
|
//Setup material
|
||||||
gr.GlyphMat = materials.NewMaterial("glyphMat", "./res/shaders/glyph")
|
gr.GlyphMat = materials.NewMaterial("glyphMat", "./res/shaders/glyph.glsl")
|
||||||
gr.GlyphMat.SetAttribute(gr.GlyphMesh.Buf)
|
|
||||||
gr.GlyphMat.DiffuseTex = gr.AtlasTex.TexID
|
gr.GlyphMat.DiffuseTex = gr.AtlasTex.TexID
|
||||||
|
|
||||||
//Create instanced buf and set its instanced attributes.
|
//Create instanced buf and set its instanced attributes.
|
||||||
@ -258,6 +253,12 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
|
|||||||
gr.InstancedBuf = buffers.Buffer{
|
gr.InstancedBuf = buffers.Buffer{
|
||||||
VAOID: gr.GlyphMesh.Buf.VAOID,
|
VAOID: gr.GlyphMesh.Buf.VAOID,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gl.GenBuffers(1, &gr.InstancedBuf.BufID)
|
||||||
|
if gr.InstancedBuf.BufID == 0 {
|
||||||
|
return nil, errors.New("failed to create OpenGL VBO buffer")
|
||||||
|
}
|
||||||
|
|
||||||
gr.InstancedBuf.SetLayout(
|
gr.InstancedBuf.SetLayout(
|
||||||
buffers.Element{ElementType: buffers.DataTypeVec2}, //UVST0
|
buffers.Element{ElementType: buffers.DataTypeVec2}, //UVST0
|
||||||
buffers.Element{ElementType: buffers.DataTypeVec2}, //UVST1
|
buffers.Element{ElementType: buffers.DataTypeVec2}, //UVST1
|
||||||
@ -269,11 +270,6 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
|
|||||||
buffers.Element{ElementType: buffers.DataTypeVec3}, //ModelScale
|
buffers.Element{ElementType: buffers.DataTypeVec3}, //ModelScale
|
||||||
)
|
)
|
||||||
|
|
||||||
gl.GenBuffers(1, &gr.InstancedBuf.BufID)
|
|
||||||
if gr.InstancedBuf.BufID == 0 {
|
|
||||||
return nil, errors.New("failed to create OpenGL VBO buffer")
|
|
||||||
}
|
|
||||||
|
|
||||||
gr.InstancedBuf.Bind()
|
gr.InstancedBuf.Bind()
|
||||||
gl.BindBuffer(gl.ARRAY_BUFFER, gr.InstancedBuf.BufID)
|
gl.BindBuffer(gl.ARRAY_BUFFER, gr.InstancedBuf.BufID)
|
||||||
layout := gr.InstancedBuf.GetLayout()
|
layout := gr.InstancedBuf.GetLayout()
|
||||||
@ -318,6 +314,9 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
|
|||||||
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
|
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
|
||||||
gr.InstancedBuf.UnBind()
|
gr.InstancedBuf.UnBind()
|
||||||
|
|
||||||
|
//Reset mesh layout because the instancedBuf setLayout over-wrote vertex attribute 0
|
||||||
|
gr.GlyphMesh.Buf.SetLayout(buffers.Element{ElementType: buffers.DataTypeVec3})
|
||||||
|
|
||||||
gr.SetScreenSize(screenWidth, screenHeight)
|
gr.SetScreenSize(screenWidth, screenHeight)
|
||||||
// fmt.Printf("lineHeight=%d, glyphInfo=%+v\n", gr.Atlas.LineHeight, gr.Atlas.Glyphs['A'])
|
// fmt.Printf("lineHeight=%d, glyphInfo=%+v\n", gr.Atlas.LineHeight, gr.Atlas.Glyphs['A'])
|
||||||
return gr, nil
|
return gr, nil
|
||||||
|
|||||||
4
go.mod
4
go.mod
@ -3,8 +3,8 @@ module github.com/bloeys/nterm
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bloeys/gglm v0.3.1
|
github.com/bloeys/gglm v0.41.10
|
||||||
github.com/bloeys/nmage v0.11.12
|
github.com/bloeys/nmage v0.12.12
|
||||||
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784
|
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
|
||||||
github.com/veandco/go-sdl2 v0.4.10
|
github.com/veandco/go-sdl2 v0.4.10
|
||||||
|
|||||||
8
go.sum
8
go.sum
@ -1,9 +1,9 @@
|
|||||||
github.com/bloeys/assimp-go v0.4.2 h1:ArVK74BCFcTO/rCGj2NgZG9xtbjnJdEn5npIeJx1Z04=
|
github.com/bloeys/assimp-go v0.4.2 h1:ArVK74BCFcTO/rCGj2NgZG9xtbjnJdEn5npIeJx1Z04=
|
||||||
github.com/bloeys/assimp-go v0.4.2/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0=
|
github.com/bloeys/assimp-go v0.4.2/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0=
|
||||||
github.com/bloeys/gglm v0.3.1 h1:Sy9upW7SBsBfDXrSmEhid3aQ+7J7itej+upwcxOnPMQ=
|
github.com/bloeys/gglm v0.41.10 h1:R9FMiI+VQVXAI+vDwCB7z9xqzy5VAR1657u8TQTDNKA=
|
||||||
github.com/bloeys/gglm v0.3.1/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
|
github.com/bloeys/gglm v0.41.10/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
|
||||||
github.com/bloeys/nmage v0.11.12 h1:b41h8uPxr2OITwt4Np/rHLlUWtMKwza2eqjNVuJ/ITk=
|
github.com/bloeys/nmage v0.12.12 h1:LeMR6NI+yMqxAZecdz7lOT1re4CWAVd1wDeJFK77+pI=
|
||||||
github.com/bloeys/nmage v0.11.12/go.mod h1:4h2tKtMvk9ab8r/+rem4QonPXEBTho6VWvpCMm0M6iM=
|
github.com/bloeys/nmage v0.12.12/go.mod h1:chDDenktiDvAG4BoFYNq1n8nACpbQ6/RFuCgCGmDxh4=
|
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784 h1:1Zi56D0LNfvkzM+BdoxKryvUEdyWO7LP8oRT+oSYJW0=
|
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784 h1:1Zi56D0LNfvkzM+BdoxKryvUEdyWO7LP8oRT+oSYJW0=
|
||||||
|
|||||||
40
main.go
40
main.go
@ -19,7 +19,6 @@ import (
|
|||||||
var _ engine.Game = &program{}
|
var _ engine.Game = &program{}
|
||||||
|
|
||||||
type program struct {
|
type program struct {
|
||||||
shouldRun bool
|
|
||||||
win *engine.Window
|
win *engine.Window
|
||||||
rend *rend3dgl.Rend3DGL
|
rend *rend3dgl.Rend3DGL
|
||||||
imguiInfo nmageimgui.ImguiInfo
|
imguiInfo nmageimgui.ImguiInfo
|
||||||
@ -34,13 +33,8 @@ type program struct {
|
|||||||
shouldDrawGrid bool
|
shouldDrawGrid bool
|
||||||
}
|
}
|
||||||
|
|
||||||
//nMage TODO:
|
//nTerm TODO:
|
||||||
// * Assert that engine is inited
|
// * Signed distance fields?
|
||||||
// * Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing)
|
|
||||||
// * Move SetAttribute away from material struct
|
|
||||||
// * Fix FPS counter
|
|
||||||
// * Allow texture loading without cache
|
|
||||||
// * Reduce/remove Game interface
|
|
||||||
|
|
||||||
const subPixelX = 64
|
const subPixelX = 64
|
||||||
const subPixelY = 64
|
const subPixelY = 64
|
||||||
@ -61,7 +55,6 @@ func main() {
|
|||||||
engine.SetVSync(true)
|
engine.SetVSync(true)
|
||||||
|
|
||||||
p := &program{
|
p := &program{
|
||||||
shouldRun: true,
|
|
||||||
win: win,
|
win: win,
|
||||||
rend: rend,
|
rend: rend,
|
||||||
imguiInfo: nmageimgui.NewImGUI(),
|
imguiInfo: nmageimgui.NewImGUI(),
|
||||||
@ -82,7 +75,7 @@ func main() {
|
|||||||
gl.ClearColor(0, 0, 0, 0)
|
gl.ClearColor(0, 0, 0, 0)
|
||||||
p.win.SDLWin.GLSwap()
|
p.win.SDLWin.GLSwap()
|
||||||
|
|
||||||
engine.Run(p)
|
engine.Run(p, p.win, p.imguiInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *program) Init() {
|
func (p *program) Init() {
|
||||||
@ -100,28 +93,21 @@ func (p *program) Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
glyphs.SaveImgToPNG(p.GlyphRend.Atlas.Img, "./debug-atlas.png")
|
glyphs.SaveImgToPNG(p.GlyphRend.Atlas.Img, "./debug-atlas.png")
|
||||||
}
|
|
||||||
|
|
||||||
func (p *program) Start() {
|
//Load resources
|
||||||
|
|
||||||
var err error
|
|
||||||
p.gridMesh, err = meshes.NewMesh("grid", "./res/models/quad.obj", 0)
|
p.gridMesh, err = meshes.NewMesh("grid", "./res/models/quad.obj", 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
p.gridMat = materials.NewMaterial("grid", "./res/shaders/grid")
|
p.gridMat = materials.NewMaterial("grid", "./res/shaders/grid.glsl")
|
||||||
p.gridMat.SetAttribute(p.gridMesh.Buf)
|
|
||||||
p.handleWindowResize()
|
p.handleWindowResize()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *program) FrameStart() {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *program) Update() {
|
func (p *program) Update() {
|
||||||
|
|
||||||
if input.IsQuitClicked() || input.KeyClicked(sdl.K_ESCAPE) {
|
if input.IsQuitClicked() || input.KeyClicked(sdl.K_ESCAPE) {
|
||||||
p.shouldRun = false
|
engine.Quit()
|
||||||
}
|
}
|
||||||
|
|
||||||
if input.KeyClicked(sdl.K_SPACE) {
|
if input.KeyClicked(sdl.K_SPACE) {
|
||||||
@ -209,19 +195,7 @@ func (p *program) drawGrid() {
|
|||||||
func (p *program) FrameEnd() {
|
func (p *program) FrameEnd() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *program) GetWindow() *engine.Window {
|
func (p *program) DeInit() {
|
||||||
return g.win
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *program) GetImGUI() nmageimgui.ImguiInfo {
|
|
||||||
return g.imguiInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *program) ShouldRun() bool {
|
|
||||||
return p.shouldRun
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *program) Deinit() {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,22 +0,0 @@
|
|||||||
#version 410
|
|
||||||
|
|
||||||
in vec4 vertColor;
|
|
||||||
in vec2 vertUV0;
|
|
||||||
in vec3 fragPos;
|
|
||||||
|
|
||||||
out vec4 fragColor;
|
|
||||||
|
|
||||||
uniform sampler2D diffTex;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
vec4 texColor = texture(diffTex, vertUV0);
|
|
||||||
// if (texColor.r == 0)
|
|
||||||
// {
|
|
||||||
// fragColor = vec4(0,1,0,0.25);
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
{
|
|
||||||
fragColor = vec4(vertColor.rgb, texColor.r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
//shader:vertex
|
||||||
#version 410
|
#version 410
|
||||||
|
|
||||||
layout(location=0) in vec3 vertPosIn;
|
layout(location=0) in vec3 vertPosIn;
|
||||||
@ -28,3 +29,27 @@ void main()
|
|||||||
|
|
||||||
gl_Position = projViewMat * modelMat * vec4(vertPosIn, 1.0);
|
gl_Position = projViewMat * modelMat * vec4(vertPosIn, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//shader:fragment
|
||||||
|
#version 410
|
||||||
|
|
||||||
|
in vec4 vertColor;
|
||||||
|
in vec2 vertUV0;
|
||||||
|
in vec3 fragPos;
|
||||||
|
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
|
uniform sampler2D diffTex;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 texColor = texture(diffTex, vertUV0);
|
||||||
|
// if (texColor.r == 0)
|
||||||
|
// {
|
||||||
|
// fragColor = vec4(0,1,0,0.25);
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
{
|
||||||
|
fragColor = vec4(vertColor.rgb, texColor.r);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,12 +0,0 @@
|
|||||||
#version 410
|
|
||||||
|
|
||||||
in vec4 vertColor;
|
|
||||||
in vec2 vertUV0;
|
|
||||||
in vec3 fragPos;
|
|
||||||
|
|
||||||
out vec4 fragColor;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
fragColor = vec4(1, 1, 1, 1);
|
|
||||||
}
|
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
//shader:vertex
|
||||||
#version 410
|
#version 410
|
||||||
|
|
||||||
layout(location=0) in vec3 vertPosIn;
|
layout(location=0) in vec3 vertPosIn;
|
||||||
@ -20,3 +21,17 @@ void main()
|
|||||||
|
|
||||||
gl_Position = projViewMat * modelMat * vec4(vertPosIn, 1.0);
|
gl_Position = projViewMat * modelMat * vec4(vertPosIn, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//shader:fragment
|
||||||
|
#version 410
|
||||||
|
|
||||||
|
in vec4 vertColor;
|
||||||
|
in vec2 vertUV0;
|
||||||
|
in vec3 fragPos;
|
||||||
|
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
fragColor = vec4(1, 1, 1, 1);
|
||||||
|
}
|
||||||
@ -1,13 +0,0 @@
|
|||||||
#version 410
|
|
||||||
|
|
||||||
uniform sampler2D Texture;
|
|
||||||
|
|
||||||
in vec2 Frag_UV;
|
|
||||||
in vec4 Frag_Color;
|
|
||||||
|
|
||||||
out vec4 Out_Color;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
Out_Color = vec4(Frag_Color.rgb, Frag_Color.a * texture(Texture, Frag_UV.st).r);
|
|
||||||
}
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
#version 410
|
|
||||||
|
|
||||||
uniform mat4 ProjMtx;
|
|
||||||
|
|
||||||
in vec3 Position;
|
|
||||||
in vec3 Normal;
|
|
||||||
in vec2 UV;
|
|
||||||
in vec4 Color;
|
|
||||||
|
|
||||||
out vec2 Frag_UV;
|
|
||||||
out vec4 Frag_Color;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
Frag_UV = UV;
|
|
||||||
Frag_Color = vec4(1,1,1,1);
|
|
||||||
Frag_Color = Color;
|
|
||||||
gl_Position = ProjMtx * vec4(Position.xy, 0, 1);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user