collada_scene: replaces all uses of love.graphics.Mesh with vertex pulling
This commit is contained in:
parent
bef01af5e8
commit
025d2707ee
@ -6,8 +6,6 @@ local scalar = _math.scalar
|
||||
|
||||
local collada_types = require 'collada_types'
|
||||
|
||||
local geometries_meshes = {}
|
||||
|
||||
local pixel_data = love.filesystem.newFileData("pixel.glsl")
|
||||
local vertex_data = love.filesystem.newFileData("vertex.glsl")
|
||||
local shader = love.graphics.newShader(pixel_data, vertex_data)
|
||||
@ -17,18 +15,29 @@ local node_instances = {}
|
||||
|
||||
local collada_scene
|
||||
|
||||
local index_buffer
|
||||
|
||||
collada_scene = {
|
||||
load_geometries = function(vertex_buffer, index_buffer, geometries)
|
||||
for _, geometry in ipairs(geometries) do
|
||||
local offset = geometry.mesh.vertex_buffer_offset / (4 * 3 * 3)
|
||||
local attribute_list = pnt_attribute_list(vertex_buffer, offset)
|
||||
load_buffers = function()
|
||||
----------------------------------------------------------------------
|
||||
-- index buffer
|
||||
----------------------------------------------------------------------
|
||||
|
||||
local draw_mode = "triangles"
|
||||
local mesh = love.graphics.newMesh(attribute_list, draw_mode)
|
||||
mesh:setIndexBuffer(index_buffer)
|
||||
local index_data = love.filesystem.newFileData("scene/test/test.idx")
|
||||
index_buffer = love.graphics.newBuffer("uint32", index_data, { index = true, usage = "static" })
|
||||
|
||||
geometries_meshes[geometry] = mesh
|
||||
end
|
||||
----------------------------------------------------------------------
|
||||
-- vertex shader storage buffer
|
||||
----------------------------------------------------------------------
|
||||
|
||||
local format = {
|
||||
{ name = 'Position', format = 'floatvec4' },
|
||||
{ name = 'Normal', format = 'floatvec4' },
|
||||
{ name = 'Texture', format = 'floatvec4' },
|
||||
}
|
||||
local vertex_data = love.filesystem.newFileData("scene/test/test.vtx")
|
||||
local shaderstorage_buffer = love.graphics.newBuffer(format, vertex_data, { shaderstorage = true, usage = "static" })
|
||||
shader:send("VertexLayout", shaderstorage_buffer)
|
||||
end,
|
||||
|
||||
load_images = function(base_path, images)
|
||||
@ -132,16 +141,16 @@ collada_scene = {
|
||||
draw_geometry = function(geometry, instance_materials)
|
||||
local base_index_buffer_offset = geometry.mesh.index_buffer_offset / 4
|
||||
|
||||
local mesh = geometries_meshes[geometry]
|
||||
|
||||
for _, instance_material in ipairs(instance_materials) do
|
||||
collada_scene.set_instance_material(instance_material)
|
||||
local triangles = geometry.mesh.triangles[instance_material.element_index + 1]
|
||||
|
||||
local index_offset = base_index_buffer_offset + triangles.index_offset
|
||||
local index_count = triangles.count * 3
|
||||
mesh:setDrawRange(1 + index_offset, index_count)
|
||||
love.graphics.draw(mesh, 0, 0, 0, 0, 0)
|
||||
|
||||
local vertex_offset = geometry.mesh.vertex_buffer_offset / (4 * 4 * 3)
|
||||
shader:send("VertexOffset", vertex_offset)
|
||||
love.graphics.drawFromShader(index_buffer, index_count, 1, 1 + index_offset)
|
||||
end
|
||||
end,
|
||||
|
||||
@ -179,7 +188,7 @@ collada_scene = {
|
||||
|
||||
return {
|
||||
draw_nodes = collada_scene.draw_nodes,
|
||||
load_geometries = collada_scene.load_geometries,
|
||||
load_buffers = collada_scene.load_buffers,
|
||||
load_node_world_transforms = collada_scene.load_node_world_transforms,
|
||||
load_images = collada_scene.load_images,
|
||||
}
|
||||
|
||||
43
main.lua
43
main.lua
@ -11,50 +11,15 @@ local scene_test = require 'scene.test.test'
|
||||
local collada_scene = require 'collada_scene'
|
||||
|
||||
local vertexformat = {
|
||||
{ name = 'VertexPosition', format = 'floatvec3', location = 0 },
|
||||
{ name = 'VertexNormal', format = 'floatvec3', location = 1 },
|
||||
{ name = 'VertexTexture', format = 'floatvec3', location = 2 },
|
||||
{ name = 'VertexPosition', format = 'floatvec4', location = 0 },
|
||||
{ name = 'VertexNormal', format = 'floatvec4', location = 1 },
|
||||
{ name = 'VertexTexture', format = 'floatvec4', location = 2 },
|
||||
}
|
||||
|
||||
function pnt_attribute_list(vertex_buffer, offset)
|
||||
return {
|
||||
{
|
||||
buffer = vertex_buffer,
|
||||
location = 0,
|
||||
name = "VertexPosition",
|
||||
nameinbuffer = nil,
|
||||
step = "pervertex",
|
||||
startindex = 1 + offset,
|
||||
},
|
||||
{
|
||||
buffer = vertex_buffer,
|
||||
location = 1,
|
||||
name = "VertexNormal",
|
||||
nameinbuffer = nil,
|
||||
step = "pervertex",
|
||||
startindex = 1 + offset,
|
||||
},
|
||||
{
|
||||
buffer = vertex_buffer,
|
||||
location = 2,
|
||||
name = "VertexTexture",
|
||||
nameinbuffer = nil,
|
||||
step = "pervertex",
|
||||
startindex = 1 + offset,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
function love.load(args)
|
||||
love.window.setMode(1024, 1024, {depth=true})
|
||||
|
||||
local vertex_data = love.filesystem.newFileData("scene/test/test.vtx")
|
||||
local index_data = love.filesystem.newFileData("scene/test/test.idx")
|
||||
|
||||
local vertex_buffer = love.graphics.newBuffer(vertexformat, vertex_data, { vertex = true, usage = "static" })
|
||||
local index_buffer = love.graphics.newBuffer("uint32", index_data, { index = true, usage = "static" })
|
||||
|
||||
collada_scene.load_geometries(vertex_buffer, index_buffer, scene_test.descriptor.geometries)
|
||||
collada_scene.load_buffers()
|
||||
collada_scene.load_node_world_transforms(scene_test.descriptor.nodes)
|
||||
collada_scene.load_images("scene/test", scene_test.descriptor.images)
|
||||
end
|
||||
|
||||
@ -196,7 +196,7 @@ local geometry_geom_plane = {
|
||||
triangles_count = 1,
|
||||
|
||||
vertex_buffer_offset = 0,
|
||||
vertex_buffer_size = 367236,
|
||||
vertex_buffer_size = 489648,
|
||||
|
||||
index_buffer_offset = 0,
|
||||
index_buffer_size = 240000,
|
||||
@ -214,8 +214,8 @@ local geometry_geom_torus_knot23 = {
|
||||
triangles = triangles_geom_torus_knot23,
|
||||
triangles_count = 1,
|
||||
|
||||
vertex_buffer_offset = 367236,
|
||||
vertex_buffer_size = 94068,
|
||||
vertex_buffer_offset = 489648,
|
||||
vertex_buffer_size = 125424,
|
||||
|
||||
index_buffer_offset = 240000,
|
||||
index_buffer_size = 57600,
|
||||
@ -233,8 +233,8 @@ local geometry_geom_torusknot25 = {
|
||||
triangles = triangles_geom_torusknot25,
|
||||
triangles_count = 1,
|
||||
|
||||
vertex_buffer_offset = 461304,
|
||||
vertex_buffer_size = 86832,
|
||||
vertex_buffer_offset = 615072,
|
||||
vertex_buffer_size = 115776,
|
||||
|
||||
index_buffer_offset = 297600,
|
||||
index_buffer_size = 28800,
|
||||
|
||||
Binary file not shown.
29
vertex.glsl
29
vertex.glsl
@ -1,8 +1,17 @@
|
||||
#pragma language glsl3
|
||||
#pragma language glsl4
|
||||
|
||||
layout (location = 0) in vec4 VertexPosition;
|
||||
layout (location = 1) in vec4 VertexNormal;
|
||||
layout (location = 2) in vec4 VertexTexture;
|
||||
struct vertex_t {
|
||||
vec4 Position;
|
||||
vec4 Normal;
|
||||
vec4 Texture;
|
||||
};
|
||||
|
||||
layout (std430) readonly buffer VertexLayout
|
||||
{
|
||||
vertex_t VertexBuffer[];
|
||||
};
|
||||
|
||||
uniform int VertexOffset;
|
||||
|
||||
uniform mat4 world_transform;
|
||||
uniform mat4 transform;
|
||||
@ -10,15 +19,15 @@ uniform mat4 transform;
|
||||
varying vec4 PixelNormal;
|
||||
varying vec4 PixelTexture;
|
||||
varying vec4 PixelWorldPosition;
|
||||
varying float PixelId;
|
||||
|
||||
void vertexmain()
|
||||
{
|
||||
PixelNormal = world_transform * vec4(VertexNormal.xyz, 0);
|
||||
PixelTexture = VertexTexture;
|
||||
PixelId = float(gl_VertexID) / (4800 * 3);
|
||||
vertex_t Vertex = VertexBuffer[VertexOffset + gl_VertexID];
|
||||
|
||||
PixelWorldPosition = world_transform * vec4(VertexPosition.xyz, 1);
|
||||
PixelNormal = world_transform * vec4(Vertex.Normal.xyz, 0);
|
||||
PixelTexture = Vertex.Texture;
|
||||
|
||||
love_Position = transform * vec4(VertexPosition.xyz, 1);
|
||||
PixelWorldPosition = world_transform * vec4(Vertex.Position.xyz, 1);
|
||||
|
||||
love_Position = transform * vec4(Vertex.Position.xyz, 1);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user