Mesh generation problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Mesh generation problem

Post by Bigfoot71 »

Hello everyone, I'm coming back to you to find out if you have any suggestions, courses for example, to learn more about mesh generation.

I made a maze game (2d) with a level generator and I would have liked to make it a 3d one (with g3d), and for that it would therefore be necessary (I suppose) that I generate a mesh of this entire maze, so I try a lot of hack and tried to compare with existing functions in opengl but i can't even add multiple vertices to a mesh.

So I first tried like this:

Code: Select all

local mapVertices = {}

	local i, s = 0, 5
	for x, rows in pairs(map) do
		for y, value in pairs(rows) do

			i = i + s

			if value == 1 then
				mapVertices[i] = { --wall
					{-s + i, -s + i, 0},
					{-s + i, 0 + i, 0},
					{-s + i, -s + i, s},
					{-s + i, 0 + i, s},
					{0 + i, -s + i, 0},
					{0 + i, 0 + i, 0},
					{0 + i, -s + i, s},
					{0 + i, 0 + i, s}
				}
			elseif value == 2 then
				mapVertices[i] = { --ground
					{-s + i, -s + i, 0},
					{-s + i, 0 + i, 0},
					{-s + i, -s + i, s},
					{-s + i, 0 + i, s}
				}
			end

But I'm stuck on how to add each index one by one to a mesh, so I thought maybe I'm complicating my life and that all the positions could be in the same array and that this can be regulated at the parameter level ' vertices_format', so I separated into two arrays, one for the walls and one for the floor, all in one big array and it looks like this:

Code: Select all

	local wallsVertices, groundVertices = {}, {}

	local i, s, iwall, iground = 0, 5, 0, 0

	for x, rows in pairs(map) do
		for y, value in pairs(rows) do

			i = i + s

			if value == 1 then

				iwall = iwall + 8

				wallsVertices[iwall - 7] = {-s + i, -s + i, 0}
				wallsVertices[iwall - 6] = {-s + i, 0 + i, 0}
				wallsVertices[iwall - 5] = {-s + i, -s + i, s}
				wallsVertices[iwall - 4] = {-s + i, 0 + i, s}
				wallsVertices[iwall - 3] = {0 + i, -s + i, 0}
				wallsVertices[iwall - 2] = {0 + i, 0 + i, 0}
				wallsVertices[iwall - 1] = {0 + i, -s + i, s}
				wallsVertices[iwall]     = {0 + i, 0 + i, s}

			elseif value == 2 then

				iground = iground + 4

				wallsVertices[iground - 3] = {-s + i, -s + i, 0}
				wallsVertices[iground - 2] = {-s + i, 0 + i, 0}
				wallsVertices[iground - 1] = {-s + i, -s + i, s}
				wallsVertices[iground]     = {-s + i, 0 + i, s}

			end

		end
	end
But of course I got this kind of error:

Code: Select all

Number of vertex attribute components must be between 1 and 4 (got 576)
Then the real problem is that I haven't even really started OpenGL yet, I just did the first course a little while ago to display a triangle (lol) and so I'm starting myself in a project where I dry up on my own on how to do it, that's why I'm going back to you if you have any lessons that could help me because I'm completely going around in circles there...

Thanks in advance !
My avatar code for the curious :D V1, V2, V3.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Mesh generation problem

Post by ReFreezed »

mapVertices needs to be a flat array of vertices. (I assume mapVertices is the 'vertices' argument for love.graphics.newMesh().)

Code: Select all

-- Change
mapVertices[i] = { --wall
	{-s + i, -s + i, 0},
	{-s + i, 0 + i, 0},
	...
}
-- to
table.insert(mapVertices, {-s + i, -s + i, 0})
table.insert(mapVertices, {-s + i, 0 + i, 0})
...
The error for the second code snippet probably happened because you flipped the 'vertexformat' and 'vertices' arguments in newMesh().
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Mesh generation problem

Post by Bigfoot71 »

I was able to rewrite my code and have an error-free result during execution by doing like this:

Code: Select all

	for x, rows in pairs(map) do
		for y, value in pairs(rows) do

			i = i + s

			if value == 1 then

				table.insert(wallsVertices, {-s + i, -s + i, 0})
				table.insert(wallsVertices, {-s + i, 0 + i, 0})
				table.insert(wallsVertices, {-s + i, -s + i, s})
				table.insert(wallsVertices, {-s + i, 0 + i, s})
				table.insert(wallsVertices, {0 + i, -s + i, 0})
				table.insert(wallsVertices, {0 + i, 0 + i, 0})
				table.insert(wallsVertices, {0 + i, -s + i, s})
				table.insert(wallsVertices, {0 + i, 0 + i, s})

			elseif value == 2 then

				table.insert(groundVertices, {-s + i, -s + i, 0})
				table.insert(groundVertices, {-s + i, 0 + i, 0})
				table.insert(groundVertices, {-s + i, -s + i, s})
				table.insert(groundVertices, {-s + i, 0 + i, s})

			end

		end
	end

	self.wallModel = g3d.newModel(wallsVertices, "assets/textures/wall.jpg", {0,0,0}, nil, 1)
I hadn't understood that g3d itself take care of making the meshes if we pass the vertices to it. Problem it shows me lots of triangles anyhow, so I thought I must be doing it wrong, so I redid everything from scratch in a little script like this:

Code: Select all

g3d = require("lib/g3d")

cubeverts = {
                {-1, -1, -1},
                {1, -1, -1},
                {1, 1, -1},
                {-1, 1, -1},
                {-1, -1, 1},
                {1, -1, 1},
                {1, 1, 1},
                {-1, 1, 1}
            }

cube = g3d.newModel(cubeverts, "assets/textures/wall.jpg", {0,0,0}, nil, 1)

function love.update(dt)
    g3d.camera.firstPersonMovement(dt)
end

function love.mousemoved(x,y,dx,dy)
    g3d.camera.firstPersonLook(dx,dy)
end

function love.draw()
    cube:draw()
end
And it gives me this thing:
Image

I tried in vain to modify the vertex format present in g3d, I never get the expected result...

Code: Select all

model.vertexFormat = {
    {"VertexPosition", "float", 3},
    {"VertexTexCoord", "float", 2},
    {"VertexNormal", "float", 3},
    {"VertexColor", "byte", 4},
}
My avatar code for the curious :D V1, V2, V3.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Mesh generation problem

Post by ReFreezed »

g3d doesn't know you want a cube - it just expects a list of vertices to make separate triangles of. I'm guessing the first 2x3 vertices out of the 8 form the two triangles you see, and the last two vertices are ignored as they are not enough for another triangle. You would need 2x3 vertices (two triangles) per face of the cube.

Edit: You could also mess with Mesh:setVertexMap to reuse vertices for multiple triangles. It seems you can access the mesh through model.mesh .
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Mesh generation problem

Post by Bigfoot71 »

Thank you very much, I understood it and that's how I try to do finally, thank you again for your help ^^

Edit: If anyone ever tries to do something similar, here's how I got a cube ! The rest I'll let you guess :D

Code: Select all

local g3d = require("g3d")

local vertices, model = {

    --Front

    {-0.5, 0.5, 0.5},
    {-0.5, -0.5, 0.5},
    {0.5, 0.5, 0.5},

    {0.5, 0.5, 0.5},
    {-0.5, -0.5, 0.5},
    {0.5, -0.5, 0.5},

    --Right

    {0.5, 0.5, 0.5},
    {0.5, -0.5, 0.5},
    {0.5, 0.5, -0.5},

    {0.5, 0.5, -0.5},
    {0.5, -0.5, 0.5},
    {0.5, -0.5, -0.5},
    
    --Back

    {0.5, 0.5, -0.5},
    {0.5, -0.5, -0.5},
    {-0.5, 0.5, -0.5},

    {-0.5, 0.5, -0.5},
    {0.5, -0.5, -0.5},
    {-0.5, -0.5, -0.5},
    
    --Left

    {-0.5, 0.5, -0.5},
    {-0.5, -0.5, -0.5},
    {-0.5, 0.5, 0.5},

    {-0.5, 0.5, 0.5},
    {-0.5, -0.5, -0.5},
    {-0.5, -0.5, 0.5},
    
    --Top
    
    {-0.5, 0.5, -0.5},
    {-0.5, 0.5, 0.5},
    {0.5, 0.5, -0.5},

    {0.5, 0.5, -0.5},
    {-0.5, 0.5, 0.5},
    {0.5, 0.5, 0.5},
    
    --Bottom
    
    {-0.5, -0.5, 0.5},
    {-0.5, -0.5, -0.5},
    {0.5, -0.5, 0.5},

    {0.5, -0.5, 0.5},
    {-0.5, -0.5, -0.5},
    {0.5, -0.5, -0.5}

}

function love.load()

    model = g3d.newModel(vertices, "wall.jpg", {0,0,0}, nil, 1)

end

function love.update(dt)

    g3d.camera.firstPersonMovement(dt)

end

function love.mousemoved(x,y, dx,dy)

    g3d.camera.firstPersonLook(dx,dy)

end

function love.draw()

    model:draw()

end
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 209 guests