fixed basic rendering

This commit is contained in:
BENEDEK László 2024-10-12 02:22:54 +02:00
parent 6c9218f8f5
commit 800df6e6c4
11 changed files with 10090 additions and 181 deletions

View File

@ -0,0 +1,2 @@
# Blender 3.4.1 MTL File: 'None'
# www.blender.org

View File

@ -0,0 +1,47 @@
# Blender 3.4.1
# www.blender.org
mtllib cube.mtl
o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -0.0000 -1.0000 -0.0000
vn -0.0000 1.0000 -0.0000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 1.000000 1.000000
s 0
f 2/2/1 3/6/1 1/1/1
f 4/8/2 7/14/2 3/4/2
f 8/16/3 5/9/3 7/14/3
f 6/12/4 1/1/4 5/10/4
f 7/15/5 1/1/5 3/5/5
f 4/8/6 6/13/6 8/16/6
f 2/2/1 4/7/1 3/6/1
f 4/8/2 8/16/2 7/14/2
f 8/16/3 6/11/3 5/9/3
f 6/12/4 2/2/4 1/1/4
f 7/15/5 5/10/5 1/1/5
f 4/8/6 2/3/6 6/13/6

View File

@ -0,0 +1,2 @@
# Blender 3.4.1 MTL File: 'None'
# www.blender.org

View File

@ -0,0 +1,16 @@
# Blender 3.4.1
# www.blender.org
mtllib plane.mtl
o Plane
v -1.000000 -1.000000 -0.000000
v 1.000000 -1.000000 -0.000000
v -1.000000 1.000000 0.000000
v 1.000000 1.000000 0.000000
vn -0.0000 -0.0000 1.0000
vt 0.999900 0.000100
vt 0.999900 0.999900
vt 0.000100 0.000100
vt 0.000100 0.999900
s 0
f 2/2/1 3/3/1 1/1/1
f 2/2/1 4/4/1 3/3/1

9965
assets/geometries/teapot.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,8 @@
#version 460 core #version 460 core
layout (location = 0) in vec3 _pos; layout (location = 0) in vec3 _pos;
layout (location = 1) in vec3 _norm; layout (location = 1) in vec2 _tex;
layout (location = 2) in vec2 _tex; layout (location = 2) in vec3 _norm;
layout (location = 3) in uint _id;
uniform mat4 projection; uniform mat4 projection;
uniform mat4 camera; uniform mat4 camera;

5
go.mod
View File

@ -2,9 +2,12 @@ module git.tek.govt.hu/dowerx/opengl-deferred
go 1.23.0 go 1.23.0
require github.com/sanity-io/litter v1.5.5
require ( require (
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 // indirect 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/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a // indirect
github.com/go-gl/mathgl v1.1.0 // indirect github.com/go-gl/mathgl v1.1.0 // indirect
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f // indirect github.com/udhos/gwob v1.0.0 // indirect
golang.org/x/image v0.21.0 // indirect
) )

9
go.sum
View File

@ -1,9 +1,18 @@
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 h1:5BVwOaUSBTlVZowGO6VZGw2H/zl9nrd3eCZfYV+NfQA= 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/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 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/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 h1:0lzZ+rntPX3/oGrDzYGdowSLC2ky8Osirvf5uAwfIEA=
github.com/go-gl/mathgl v1.1.0/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ= github.com/go-gl/mathgl v1.1.0/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/udhos/gwob v1.0.0 h1:P9br9SLca7H5Hr/WuEcO9+ViUm+wlQnr4vxpPU6c6ww=
github.com/udhos/gwob v1.0.0/go.mod h1:xj4qGbkwL1sTPm1V17NfcIhkgG2rjfb8cUv9YIqE6DE=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f h1:FO4MZ3N56GnxbqxGKqh+YTzUWQ2sDwtFQEZgLOxh9Jc= golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f h1:FO4MZ3N56GnxbqxGKqh+YTzUWQ2sDwtFQEZgLOxh9Jc=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

35
main.go
View File

@ -47,7 +47,7 @@ func main() {
} }
gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.DEPTH_TEST)
gl.Enable(gl.CULL_FACE) // gl.Enable(gl.CULL_FACE)
gl.DepthFunc(gl.LESS) gl.DepthFunc(gl.LESS)
gl.ClearColor(0, 0, 0, 0) gl.ClearColor(0, 0, 0, 0)
@ -71,42 +71,38 @@ func main() {
defer deferredShader.Delete() defer deferredShader.Delete()
// geometry // geometry
geometries, err := geometry.LoadOBJ("assets/geometries/suzanne.obj") cube, err := geometry.LoadOBJ("assets/geometries/suzanne.obj")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer func() { defer cube.Delete()
for _, geometry := range geometries {
geometry.Delete()
}
}()
// textures // textures
albedo, err := texture.Load("assets/textures/demo.png") albedo, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_albedo.tif")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer albedo.Delete() defer albedo.Delete()
normal, err := texture.Load("assets/textures/demo.png") normal, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_normal.tif")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer normal.Delete() defer normal.Delete()
specular, err := texture.Load("assets/textures/demo.png") specular, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer specular.Delete() defer specular.Delete()
roughness, err := texture.Load("assets/textures/demo.png") roughness, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer roughness.Delete() defer roughness.Delete()
metalic, err := texture.Load("assets/textures/demo.png") metalic, err := texture.Load("assets/textures/TCom_Pavement_PaintedConcrete3_512_roughness.tif")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -136,13 +132,22 @@ func main() {
deferredShader.Int("s_roughness", 3) deferredShader.Int("s_roughness", 3)
deferredShader.Int("s_metalic", 4) deferredShader.Int("s_metalic", 4)
angle := 0.0
previousTime := glfw.GetTime()
for !window.ShouldClose() { for !window.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// rotate
time := glfw.GetTime()
elapsed := time - previousTime
previousTime = time
angle += elapsed
model = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})
deferredShader.Mat4("model", model)
// render // render
for _, g := range geometries { cube.Draw()
g.Draw()
}
window.SwapBuffers() window.SwapBuffers()
glfw.PollEvents() glfw.PollEvents()

View File

@ -1,19 +1,14 @@
package geometry package geometry
import ( import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/go-gl/gl/v4.6-core/gl" "github.com/go-gl/gl/v4.6-core/gl"
"github.com/udhos/gwob"
) )
// attributes: // attributes:
// position 3 * float // position 3 * float
// texture 2 * float
// normal 3 * float // normal 3 * float
// texture coordinates 2 * float
type Geometry struct { type Geometry struct {
vao uint32 vao uint32
@ -34,58 +29,14 @@ func (g Geometry) Delete() {
gl.DeleteBuffers(1, &g.vbo) gl.DeleteBuffers(1, &g.vbo)
} }
type vec struct { func new(coords []float32, indicies []int) (Geometry, error) {
Values []float32
}
type point struct {
Vertex int
Normal int
Uv int
}
type face struct {
Points [3]point
}
func new(vertices []vec, normals []vec, uvs []vec, faces []face) (Geometry, error) {
fmt.Println(len(vertices), len(normals), len(uvs), len(faces))
i := 0
points := make(map[point]int)
indicies := []uint{}
for _, face := range faces {
for _, point := range face.Points {
if val, ok := points[point]; !ok {
points[point] = i
indicies = append(indicies, uint(i))
i++
} else {
indicies = append(indicies, uint(val))
}
}
}
rPoints := make(map[int]point)
for k, v := range points {
rPoints[v] = k
}
buffer := make([]float32, len(rPoints)*8)
for i := 0; i < len(rPoints); i++ {
v := rPoints[i]
buffer[i*8+0] = vertices[v.Vertex].Values[0]
buffer[i*8+1] = vertices[v.Vertex].Values[1]
buffer[i*8+2] = vertices[v.Vertex].Values[2]
buffer[i*8+3] = normals[v.Normal].Values[0]
buffer[i*8+4] = normals[v.Normal].Values[1]
buffer[i*8+5] = normals[v.Normal].Values[2]
buffer[i*8+6] = uvs[v.Uv].Values[0]
buffer[i*8+7] = uvs[v.Uv].Values[1]
}
var geometry Geometry var geometry Geometry
unsignedIndicies := make([]uint32, len(indicies))
for i, v := range indicies {
unsignedIndicies[i] = uint32(v)
}
// vao // vao
gl.GenVertexArrays(1, &geometry.vao) gl.GenVertexArrays(1, &geometry.vao)
gl.BindVertexArray(geometry.vao) gl.BindVertexArray(geometry.vao)
@ -93,127 +44,35 @@ func new(vertices []vec, normals []vec, uvs []vec, faces []face) (Geometry, erro
// vbo // vbo
gl.GenBuffers(1, &geometry.vbo) gl.GenBuffers(1, &geometry.vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, geometry.vbo) gl.BindBuffer(gl.ARRAY_BUFFER, geometry.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(buffer)*4, gl.Ptr(buffer), gl.STATIC_DRAW) gl.BufferData(gl.ARRAY_BUFFER, len(coords)*4, gl.Ptr(coords), gl.STATIC_DRAW)
// attributes // attributes
// position
gl.VertexAttribPointerWithOffset(0, 3, gl.FLOAT, false, 8*4, 0) gl.VertexAttribPointerWithOffset(0, 3, gl.FLOAT, false, 8*4, 0)
gl.EnableVertexAttribArray(0) gl.EnableVertexAttribArray(0)
gl.VertexAttribPointerWithOffset(1, 3, gl.FLOAT, false, 8*4, 3*4) // texture
gl.VertexAttribPointerWithOffset(1, 2, gl.FLOAT, false, 8*4, 3*4)
gl.EnableVertexAttribArray(1) gl.EnableVertexAttribArray(1)
gl.VertexAttribPointerWithOffset(2, 2, gl.FLOAT, false, 8*4, 6*4) // normal
gl.VertexAttribPointerWithOffset(2, 3, gl.FLOAT, false, 8*4, 5*4)
gl.EnableVertexAttribArray(2) gl.EnableVertexAttribArray(2)
// ebo // ebo
gl.GenBuffers(1, &geometry.ebo) gl.GenBuffers(1, &geometry.ebo)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, geometry.ebo) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, geometry.ebo)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(indicies)*4, gl.Ptr(indicies), gl.STATIC_DRAW) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(unsignedIndicies)*4, gl.Ptr(unsignedIndicies), gl.STATIC_DRAW)
geometry.size = int32(len(indicies)) geometry.size = int32(len(indicies))
return geometry, nil return geometry, nil
} }
func LoadOBJ(path string) (geometries []Geometry, err error) { func LoadOBJ(path string) (geometries Geometry, err error) {
file, err := os.Open(path) o, err := gwob.NewObjFromFile(path, &gwob.ObjParserOptions{})
if err != nil { if err != nil {
return nil, err
}
var vertices []vec
var normals []vec
var uvs []vec
var faces []face
scanner := bufio.NewScanner(file)
for scanner.Scan() {
switch scanner.Text()[0:2] {
case "# ":
continue
case "mt":
fmt.Println("material ignored")
continue
case "o ":
if len(vertices) != 0 {
geometry, err := new(vertices, normals, uvs, faces)
if err != nil {
return nil, err
}
geometries = append(geometries, geometry)
}
case "v ":
parts := strings.Split(scanner.Text(), " ")
vertex := vec{}
for i := 1; i < 4; i++ {
value, err := strconv.ParseFloat(parts[i], 32)
if err != nil {
return nil, err
}
vertex.Values = append(vertex.Values, float32(value))
}
vertices = append(vertices, vertex)
case "vn":
parts := strings.Split(scanner.Text(), " ")
normal := vec{}
for i := 1; i < 4; i++ {
value, err := strconv.ParseFloat(parts[i], 32)
if err != nil {
return nil, err
}
normal.Values = append(normal.Values, float32(value))
}
normals = append(normals, normal)
case "vt":
parts := strings.Split(scanner.Text(), " ")
uv := vec{}
for i := 1; i < 3; i++ {
value, err := strconv.ParseFloat(parts[i], 32)
if err != nil {
return nil, err
}
uv.Values = append(uv.Values, float32(value))
}
uvs = append(uvs, uv)
case "f ":
parts := strings.Split(scanner.Text(), " ")
face := face{}
for i, part := range parts[1:] {
bits := strings.Split(part, "/")
v, err := strconv.Atoi(bits[0])
if err != nil {
return nil, err
}
t, err := strconv.Atoi(bits[1])
if err != nil {
return nil, err
}
n, err := strconv.Atoi(bits[2])
if err != nil {
return nil, err
}
face.Points[i].Vertex = v - 1
face.Points[i].Normal = n - 1
face.Points[i].Uv = t - 1
}
faces = append(faces, face)
}
}
geometry, err := new(vertices, normals, uvs, faces)
if err != nil {
return nil, err
}
geometries = append(geometries, geometry)
return geometries, err return geometries, err
} }
return new(o.Coord, o.Indices)
}

View File

@ -7,6 +7,8 @@ import (
_ "image/png" _ "image/png"
"os" "os"
_ "golang.org/x/image/tiff"
"github.com/go-gl/gl/v4.6-core/gl" "github.com/go-gl/gl/v4.6-core/gl"
) )