mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Compare commits
4 Commits
57ab851534
...
8c6b1d5821
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c6b1d5821 | |||
| dfd1fe9c5e | |||
| 24613823a7 | |||
| 0386f441d6 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -16,3 +16,6 @@ vendor/
|
||||
.vscode/
|
||||
imgui.ini
|
||||
*~
|
||||
|
||||
# Custom
|
||||
*.pprof
|
||||
47
main.go
47
main.go
@ -2,7 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
"strconv"
|
||||
|
||||
imgui "github.com/AllenDang/cimgui-go"
|
||||
@ -188,6 +190,8 @@ const (
|
||||
|
||||
unscaledWindowWidth = 1280
|
||||
unscaledWindowHeight = 720
|
||||
|
||||
PROFILE_CPU = true
|
||||
)
|
||||
|
||||
var (
|
||||
@ -346,7 +350,36 @@ func main() {
|
||||
}
|
||||
window.EventCallbacks = append(window.EventCallbacks, game.handleWindowEvents)
|
||||
|
||||
if PROFILE_CPU {
|
||||
|
||||
pf, err := os.Create("cpu.pprof")
|
||||
if err == nil {
|
||||
defer pf.Close()
|
||||
pprof.StartCPUProfile(pf)
|
||||
} else {
|
||||
logging.ErrLog.Printf("Creating cpu.pprof file failed. CPU profiling will not run. Err=%v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
engine.Run(game, &window, game.ImGUIInfo)
|
||||
|
||||
if PROFILE_CPU {
|
||||
pprof.StopCPUProfile()
|
||||
|
||||
heapProfile, err := os.Create("heap.pprof")
|
||||
if err == nil {
|
||||
|
||||
err = pprof.WriteHeapProfile(heapProfile)
|
||||
if err != nil {
|
||||
logging.ErrLog.Printf("Writing heap profile to heap.pprof failed. Err=%v\n", err)
|
||||
}
|
||||
|
||||
heapProfile.Close()
|
||||
|
||||
} else {
|
||||
logging.ErrLog.Printf("Creating heap.pprof file failed. Err=%v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) handleWindowEvents(e sdl.Event) {
|
||||
@ -484,9 +517,11 @@ func (g *Game) Init() {
|
||||
screenQuadMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
|
||||
|
||||
unlitMat = materials.NewMaterial("Unlit mat", "./res/shaders/simple-unlit.glsl")
|
||||
unlitMat.Settings.Set(materials.MaterialSettings_HasModelMat)
|
||||
unlitMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
|
||||
|
||||
whiteMat = materials.NewMaterial("White mat", "./res/shaders/simple.glsl")
|
||||
whiteMat.Settings.Set(materials.MaterialSettings_HasModelMat | materials.MaterialSettings_HasNormalMat)
|
||||
whiteMat.Shininess = 64
|
||||
whiteMat.DiffuseTex = whiteTex.TexID
|
||||
whiteMat.SpecularTex = blackTex.TexID
|
||||
@ -506,6 +541,7 @@ func (g *Game) Init() {
|
||||
whiteMat.SetUnifInt32("spotLightShadowMaps", int32(materials.TextureSlot_ShadowMap_Array1))
|
||||
|
||||
containerMat = materials.NewMaterial("Container mat", "./res/shaders/simple.glsl")
|
||||
containerMat.Settings.Set(materials.MaterialSettings_HasModelMat | materials.MaterialSettings_HasNormalMat)
|
||||
containerMat.Shininess = 64
|
||||
containerMat.DiffuseTex = containerDiffuseTex.TexID
|
||||
containerMat.SpecularTex = containerSpecularTex.TexID
|
||||
@ -525,6 +561,7 @@ func (g *Game) Init() {
|
||||
containerMat.SetUnifInt32("spotLightShadowMaps", int32(materials.TextureSlot_ShadowMap_Array1))
|
||||
|
||||
palleteMat = materials.NewMaterial("Pallete mat", "./res/shaders/simple.glsl")
|
||||
palleteMat.Settings.Set(materials.MaterialSettings_HasModelMat | materials.MaterialSettings_HasNormalMat)
|
||||
palleteMat.Shininess = 64
|
||||
palleteMat.DiffuseTex = palleteTex.TexID
|
||||
palleteMat.SpecularTex = blackTex.TexID
|
||||
@ -543,12 +580,16 @@ func (g *Game) Init() {
|
||||
palleteMat.SetUnifInt32("spotLightShadowMaps", int32(materials.TextureSlot_ShadowMap_Array1))
|
||||
|
||||
debugDepthMat = materials.NewMaterial("Debug depth mat", "./res/shaders/debug-depth.glsl")
|
||||
debugDepthMat.Settings.Set(materials.MaterialSettings_HasModelMat)
|
||||
|
||||
depthMapMat = materials.NewMaterial("Depth Map mat", "./res/shaders/depth-map.glsl")
|
||||
depthMapMat.Settings.Set(materials.MaterialSettings_HasModelMat)
|
||||
|
||||
arrayDepthMapMat = materials.NewMaterial("Array Depth Map mat", "./res/shaders/array-depth-map.glsl")
|
||||
arrayDepthMapMat.Settings.Set(materials.MaterialSettings_HasModelMat)
|
||||
|
||||
omnidirDepthMapMat = materials.NewMaterial("Omnidirectional Depth Map mat", "./res/shaders/omnidirectional-depth-map.glsl")
|
||||
omnidirDepthMapMat.Settings.Set(materials.MaterialSettings_HasModelMat)
|
||||
|
||||
skyboxMat = materials.NewMaterial("Skybox mat", "./res/shaders/skybox.glsl")
|
||||
skyboxMat.CubemapTex = skyboxCmap.TexID
|
||||
@ -598,7 +639,7 @@ func (g *Game) initFbos() {
|
||||
assert.T(demoFbo.IsComplete(), "Demo fbo is not complete after init")
|
||||
|
||||
// Depth map fbo
|
||||
dirLightDepthMapFbo = buffers.NewFramebuffer(1024, 1024)
|
||||
dirLightDepthMapFbo = buffers.NewFramebuffer(2048, 2048)
|
||||
dirLightDepthMapFbo.SetNoColorBuffer()
|
||||
dirLightDepthMapFbo.NewDepthAttachment(
|
||||
buffers.FramebufferAttachmentType_Texture,
|
||||
@ -608,7 +649,7 @@ func (g *Game) initFbos() {
|
||||
assert.T(dirLightDepthMapFbo.IsComplete(), "Depth map fbo is not complete after init")
|
||||
|
||||
// Point light depth map fbo
|
||||
pointLightDepthMapFbo = buffers.NewFramebuffer(1024, 1024)
|
||||
pointLightDepthMapFbo = buffers.NewFramebuffer(512, 512)
|
||||
pointLightDepthMapFbo.SetNoColorBuffer()
|
||||
pointLightDepthMapFbo.NewDepthCubemapArrayAttachment(
|
||||
buffers.FramebufferAttachmentDataFormat_DepthF32,
|
||||
@ -618,7 +659,7 @@ func (g *Game) initFbos() {
|
||||
assert.T(pointLightDepthMapFbo.IsComplete(), "Point light depth map fbo is not complete after init")
|
||||
|
||||
// Spot light depth map fbo
|
||||
spotLightDepthMapFbo = buffers.NewFramebuffer(1024, 1024)
|
||||
spotLightDepthMapFbo = buffers.NewFramebuffer(512, 512)
|
||||
spotLightDepthMapFbo.SetNoColorBuffer()
|
||||
spotLightDepthMapFbo.NewDepthTextureArrayAttachment(
|
||||
buffers.FramebufferAttachmentDataFormat_DepthF32,
|
||||
|
||||
@ -21,9 +21,30 @@ const (
|
||||
TextureSlot_ShadowMap_Array1 TextureSlot = 13
|
||||
)
|
||||
|
||||
type MaterialSettings uint64
|
||||
|
||||
const (
|
||||
MaterialSettings_None MaterialSettings = iota
|
||||
MaterialSettings_HasModelMat MaterialSettings = 1 << (iota - 1)
|
||||
MaterialSettings_HasNormalMat
|
||||
)
|
||||
|
||||
func (ms *MaterialSettings) Set(flags MaterialSettings) {
|
||||
*ms |= flags
|
||||
}
|
||||
|
||||
func (ms *MaterialSettings) Remove(flags MaterialSettings) {
|
||||
*ms &= ^flags
|
||||
}
|
||||
|
||||
func (ms *MaterialSettings) Has(flags MaterialSettings) bool {
|
||||
return *ms&flags == flags
|
||||
}
|
||||
|
||||
type Material struct {
|
||||
Name string
|
||||
ShaderProg shaders.ShaderProgram
|
||||
Settings MaterialSettings
|
||||
|
||||
UnifLocs map[string]int32
|
||||
AttribLocs map[string]int32
|
||||
|
||||
@ -29,7 +29,14 @@ func (r *Rend3DGL) DrawMesh(mesh *meshes.Mesh, modelMat *gglm.TrMat, mat *materi
|
||||
r.BoundMat = mat
|
||||
}
|
||||
|
||||
if mat.Settings.Has(materials.MaterialSettings_HasModelMat) {
|
||||
mat.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||
}
|
||||
|
||||
if mat.Settings.Has(materials.MaterialSettings_HasNormalMat) {
|
||||
normalMat := modelMat.Clone().InvertAndTranspose().ToMat3()
|
||||
mat.SetUnifMat3("normalMat", &normalMat)
|
||||
}
|
||||
|
||||
for i := 0; i < len(mesh.SubMeshes); i++ {
|
||||
gl.DrawElementsBaseVertexWithOffset(gl.TRIANGLES, mesh.SubMeshes[i].IndexCount, gl.UNSIGNED_INT, uintptr(mesh.SubMeshes[i].BaseIndex), mesh.SubMeshes[i].BaseVertex)
|
||||
|
||||
@ -2,22 +2,18 @@
|
||||
#version 410
|
||||
|
||||
layout(location=0) in vec3 vertPosIn;
|
||||
layout(location=1) in vec3 vertNormalIn;
|
||||
layout(location=2) in vec2 vertUV0In;
|
||||
layout(location=3) in vec3 vertColorIn;
|
||||
|
||||
out vec3 vertNormal;
|
||||
out vec2 vertUV0;
|
||||
out vec3 vertColor;
|
||||
out vec3 fragPos;
|
||||
|
||||
//MVP = Model View Projection
|
||||
uniform mat4 modelMat;
|
||||
uniform mat4 projViewMat;
|
||||
|
||||
void main()
|
||||
{
|
||||
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
|
||||
vertUV0 = vertUV0In;
|
||||
vertColor = vertColorIn;
|
||||
|
||||
@ -31,7 +27,6 @@ void main()
|
||||
#version 410
|
||||
|
||||
in vec3 vertColor;
|
||||
in vec3 vertNormal;
|
||||
in vec2 vertUV0;
|
||||
in vec3 fragPos;
|
||||
|
||||
|
||||
@ -6,23 +6,15 @@ layout(location=1) in vec3 vertNormalIn;
|
||||
layout(location=2) in vec2 vertUV0In;
|
||||
layout(location=3) in vec3 vertColorIn;
|
||||
|
||||
out vec3 vertNormal;
|
||||
out vec2 vertUV0;
|
||||
out vec3 vertColor;
|
||||
out vec3 fragPos;
|
||||
|
||||
//MVP = Model View Projection
|
||||
uniform mat4 modelMat;
|
||||
uniform mat4 projViewMat;
|
||||
|
||||
void main()
|
||||
{
|
||||
// @TODO: Calculate this on the CPU and send it as a uniform
|
||||
//
|
||||
// This produces the normal matrix that multiplies with the model normal to produce the
|
||||
// world space normal. Based on 'One last thing' section from: https://learnopengl.com/Lighting/Basic-Lighting
|
||||
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
|
||||
|
||||
vertUV0 = vertUV0In;
|
||||
vertColor = vertColorIn;
|
||||
|
||||
@ -41,7 +33,6 @@ struct Material {
|
||||
uniform Material material;
|
||||
|
||||
in vec3 vertColor;
|
||||
in vec3 vertNormal;
|
||||
in vec2 vertUV0;
|
||||
in vec3 fragPos;
|
||||
|
||||
|
||||
@ -7,6 +7,7 @@ layout(location=2) in vec2 vertUV0In;
|
||||
layout(location=3) in vec3 vertColorIn;
|
||||
|
||||
uniform mat4 modelMat;
|
||||
uniform mat3 normalMat;
|
||||
uniform mat4 projViewMat;
|
||||
uniform mat4 dirLightProjViewMat;
|
||||
|
||||
@ -22,11 +23,7 @@ out vec4 fragPosSpotLight[NUM_SPOT_LIGHTS];
|
||||
|
||||
void main()
|
||||
{
|
||||
// @TODO: Calculate this on the CPU and send it as a uniform
|
||||
//
|
||||
// This produces the normal matrix that multiplies with the model normal to produce the
|
||||
// world space normal. Based on 'One last thing' section from: https://learnopengl.com/Lighting/Basic-Lighting
|
||||
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
|
||||
vertNormal = normalMat * vertNormalIn;
|
||||
|
||||
vertUV0 = vertUV0In;
|
||||
vertColor = vertColorIn;
|
||||
|
||||
Reference in New Issue
Block a user