Move to latest nMage

This commit is contained in:
bloeys
2022-07-03 09:17:59 +04:00
parent 701ee97c66
commit 323cd1099f
10 changed files with 67 additions and 120 deletions

View File

@ -173,16 +173,12 @@ func (gr *GlyphRend) updateFontAtlasTexture(fontFile string) error {
}
defer os.Remove(pngFileName)
atlasTex, err := assets.LoadPNGTexture(pngFileName)
atlasTex, err := assets.LoadTexturePNG(pngFileName, nil)
if err != nil {
return err
}
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.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)
@ -229,7 +225,7 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
gr.GlyphMesh = &meshes.Mesh{
Name: "glypQuad",
//VertPos, UV, Color; Instanced attributes are stored separately
//VertPos only. Instanced attributes are stored separately
Buf: buffers.NewBuffer(
buffers.Element{ElementType: buffers.DataTypeVec3},
),
@ -249,8 +245,7 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
})
//Setup material
gr.GlyphMat = materials.NewMaterial("glyphMat", "./res/shaders/glyph")
gr.GlyphMat.SetAttribute(gr.GlyphMesh.Buf)
gr.GlyphMat = materials.NewMaterial("glyphMat", "./res/shaders/glyph.glsl")
gr.GlyphMat.DiffuseTex = gr.AtlasTex.TexID
//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{
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(
buffers.Element{ElementType: buffers.DataTypeVec2}, //UVST0
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
)
gl.GenBuffers(1, &gr.InstancedBuf.BufID)
if gr.InstancedBuf.BufID == 0 {
return nil, errors.New("failed to create OpenGL VBO buffer")
}
gr.InstancedBuf.Bind()
gl.BindBuffer(gl.ARRAY_BUFFER, gr.InstancedBuf.BufID)
layout := gr.InstancedBuf.GetLayout()
@ -318,6 +314,9 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
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)
// fmt.Printf("lineHeight=%d, glyphInfo=%+v\n", gr.Atlas.LineHeight, gr.Atlas.Glyphs['A'])
return gr, nil

4
go.mod
View File

@ -3,8 +3,8 @@ module github.com/bloeys/nterm
go 1.18
require (
github.com/bloeys/gglm v0.3.1
github.com/bloeys/nmage v0.11.12
github.com/bloeys/gglm v0.41.10
github.com/bloeys/nmage v0.12.12
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/veandco/go-sdl2 v0.4.10

8
go.sum
View File

@ -1,9 +1,9 @@
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/gglm v0.3.1 h1:Sy9upW7SBsBfDXrSmEhid3aQ+7J7itej+upwcxOnPMQ=
github.com/bloeys/gglm v0.3.1/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
github.com/bloeys/nmage v0.11.12 h1:b41h8uPxr2OITwt4Np/rHLlUWtMKwza2eqjNVuJ/ITk=
github.com/bloeys/nmage v0.11.12/go.mod h1:4h2tKtMvk9ab8r/+rem4QonPXEBTho6VWvpCMm0M6iM=
github.com/bloeys/gglm v0.41.10 h1:R9FMiI+VQVXAI+vDwCB7z9xqzy5VAR1657u8TQTDNKA=
github.com/bloeys/gglm v0.41.10/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
github.com/bloeys/nmage v0.12.12 h1:LeMR6NI+yMqxAZecdz7lOT1re4CWAVd1wDeJFK77+pI=
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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784 h1:1Zi56D0LNfvkzM+BdoxKryvUEdyWO7LP8oRT+oSYJW0=

40
main.go
View File

@ -19,7 +19,6 @@ import (
var _ engine.Game = &program{}
type program struct {
shouldRun bool
win *engine.Window
rend *rend3dgl.Rend3DGL
imguiInfo nmageimgui.ImguiInfo
@ -34,13 +33,8 @@ type program struct {
shouldDrawGrid bool
}
//nMage TODO:
// * Assert that engine is inited
// * 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
//nTerm TODO:
// * Signed distance fields?
const subPixelX = 64
const subPixelY = 64
@ -61,7 +55,6 @@ func main() {
engine.SetVSync(true)
p := &program{
shouldRun: true,
win: win,
rend: rend,
imguiInfo: nmageimgui.NewImGUI(),
@ -82,7 +75,7 @@ func main() {
gl.ClearColor(0, 0, 0, 0)
p.win.SDLWin.GLSwap()
engine.Run(p)
engine.Run(p, p.win, p.imguiInfo)
}
func (p *program) Init() {
@ -100,28 +93,21 @@ func (p *program) Init() {
}
glyphs.SaveImgToPNG(p.GlyphRend.Atlas.Img, "./debug-atlas.png")
}
func (p *program) Start() {
var err error
//Load resources
p.gridMesh, err = meshes.NewMesh("grid", "./res/models/quad.obj", 0)
if err != nil {
panic(err.Error())
}
p.gridMat = materials.NewMaterial("grid", "./res/shaders/grid")
p.gridMat.SetAttribute(p.gridMesh.Buf)
p.gridMat = materials.NewMaterial("grid", "./res/shaders/grid.glsl")
p.handleWindowResize()
}
func (p *program) FrameStart() {
}
func (p *program) Update() {
if input.IsQuitClicked() || input.KeyClicked(sdl.K_ESCAPE) {
p.shouldRun = false
engine.Quit()
}
if input.KeyClicked(sdl.K_SPACE) {
@ -209,19 +195,7 @@ func (p *program) drawGrid() {
func (p *program) FrameEnd() {
}
func (g *program) GetWindow() *engine.Window {
return g.win
}
func (g *program) GetImGUI() nmageimgui.ImguiInfo {
return g.imguiInfo
}
func (p *program) ShouldRun() bool {
return p.shouldRun
}
func (p *program) Deinit() {
func (p *program) DeInit() {
}

View File

@ -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);
}
}

View File

@ -1,3 +1,4 @@
//shader:vertex
#version 410
layout(location=0) in vec3 vertPosIn;
@ -27,4 +28,28 @@ void main()
fragPos = vec3(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);
}
}

View File

@ -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);
}

View File

@ -1,3 +1,4 @@
//shader:vertex
#version 410
layout(location=0) in vec3 vertPosIn;
@ -19,4 +20,18 @@ void main()
fragPos = vec3(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);
}

View File

@ -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);
}

View File

@ -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);
}