This commit is contained in:
BENEDEK László 2024-10-11 18:38:21 +02:00
commit 6046cea17c
7 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,27 @@
#version 460 core
layout (location = 0) in vec3 _pos;
layout (location = 1) in vec3 _norm;
layout (location = 2) in vec2 _tex;
layout (location = 3) in uint _id;
layout (std140) uniform matrices
{
mat4 projection;
mat4 camera;
}
uniform mat4 model;
out vec3 pos;
out vec3 norm;
out vec2 tex;
out vec2 id;
void main() {
gl_Position = projection * camera * model * vec4(_pos, 1.0);
pos = vec3(model * vec4(_pos, 1.0));
norm = _norm;
tex = _tex;
id = _id;
}

View File

@ -0,0 +1,32 @@
#version 460
struct material_t {
sampler2D albedo;
sampler2D normal;
sampler2D specular;
sampler2D rougness;
sampler2D metalic;
};
layout (std140) buffer material_buffer {
material_t materials[];
};
in vec3 pos;
in vec3 norm;
in vec2 tex;
in uint id;
out vec3 position;
out vec3 albedo;
out vec3 normal;
out vec3 material;
void main() {
position = pos;
albedo = texture(materials[id].albedo, tex).rgb;
normal = texture(materials[id].normal, tex).rgb;
material.x = texture(materials[id].specular, tex).x;
material.y = texture(materials[id].rougness, tex).y;
material.z = texture(materials[id].metalic, tex).z;
}

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module git.tek.govt.hu/dowerx/opengl-deferred
go 1.23.0
require (
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a // indirect
github.com/go-gl/mathgl v1.1.0 // indirect
)

8
go.sum Normal file
View File

@ -0,0 +1,8 @@
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 h1:5BVwOaUSBTlVZowGO6VZGw2H/zl9nrd3eCZfYV+NfQA=
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a h1:vxnBhFDDT+xzxf1jTJKMKZw3H0swfWk9RpWbBbDK5+0=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/mathgl v1.1.0 h1:0lzZ+rntPX3/oGrDzYGdowSLC2ky8Osirvf5uAwfIEA=
github.com/go-gl/mathgl v1.1.0/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

62
main.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"fmt"
"os"
"git.tek.govt.hu/dowerx/opengl-deferred/types/shader"
"github.com/go-gl/gl/v4.6-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
const (
WIDTH = 640
HEIGHT = 480
TITLE = "opengl-deferred"
)
func main() {
// init glfw
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 6)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
window, err := glfw.CreateWindow(WIDTH, HEIGHT, TITLE, nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
// init glow
if err := gl.Init(); err != nil {
panic(err)
}
fmt.Println("OpenGL version:", gl.GoStr(gl.GetString(gl.VERSION)))
// load assets
// shader
vertexSource, err := os.ReadFile("assets/shader/deferred/deferred.vs")
if err != nil {
panic(err)
}
fragmentSource, err := os.ReadFile("assets/shader/deferred/deferred.fs")
if err != nil {
panic(err)
}
deferredShader, err := shader.New(string(vertexSource), string(fragmentSource))
if err != nil {
panic(err)
}
defer deferredShader.Delete()
deferredShader.Use()
}

BIN
opengl-deferred Executable file

Binary file not shown.

79
types/shader/shader.go Normal file
View File

@ -0,0 +1,79 @@
package shader
import (
"errors"
"strings"
"github.com/go-gl/gl/v4.6-core/gl"
)
type Shader struct {
program uint32
uniforms map[string]uint32
}
func (s *Shader) Use() {
gl.UseProgram(s.program)
}
func (s *Shader) Delete() {
gl.DeleteProgram(s.program)
}
func compileShader(source string, shaderType uint32) (uint32, error) {
shader := gl.CreateShader(shaderType)
csource, free := gl.Strs(source)
gl.ShaderSource(shader, 1, csource, nil)
free()
gl.CompileShader(shader)
var status int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
return 0, errors.New(log)
}
return shader, nil
}
func New(vertexSource string, fragmantSource string) (shader Shader, _ error) {
shader.uniforms = make(map[string]uint32)
vertexShader, err := compileShader(vertexSource, gl.VERTEX_SHADER)
if err != nil {
return shader, err
}
fragmantShader, err := compileShader(fragmantSource, gl.FRAGMENT_SHADER)
if err != nil {
return shader, err
}
shader.program = gl.CreateProgram()
gl.AttachShader(shader.program, vertexShader)
gl.AttachShader(shader.program, fragmantShader)
gl.LinkProgram(shader.program)
defer gl.DeleteShader(vertexShader)
defer gl.DeleteShader(fragmantShader)
var status int32
gl.GetProgramiv(shader.program, gl.LINK_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetProgramiv(shader.program, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetProgramInfoLog(shader.program, logLength, nil, gl.Str(log))
return shader, errors.New(log)
}
return shader, nil
}