Difference between revisions of "Mesh:setVertexMap"

m (Prettified link)
(Added example)
Line 1: Line 1:
 
{{newin|[[0.9.0]]|090|type=function}}
 
{{newin|[[0.9.0]]|090|type=function}}
Sets the vertex map for a Geometry. The vertex map describes the order of the vertices when they are drawn.
+
Sets the vertex map for a Geometry. The vertex map describes the order in which the vertices are used when the Geometry is drawn.
  
The vertex map allows you to re-order or reuse vertices when drawing without changing the actual vertex parameters or duplicating vertices.
+
The vertex map allows you to re-order or reuse vertices when drawing without changing the actual vertex parameters or duplicating vertices. It is especially useful when combined with different [[GeometryDrawMode|Geometry Draw Modes]].
  
 
== Function ==
 
== Function ==
Line 13: Line 13:
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 +
== Function ==
 +
=== Synopsis ===
 +
<source lang="lua">
 +
Geometry:setVertexMap( vi1, vi2, vi3, ... )
 +
</source>
 +
=== Arguments ===
 +
{{param|number|vi1|The index of the first vertex to use when drawing. Must be in the range of [1, [[Geometry:getVertexCount|Geometry:getVertexCount()]]].}}
 +
{{param|number|vi2|The index of the second vertex to use when drawing.}}
 +
{{param|number|vi3|The index of the third vertex to use when drawing.}}
 +
=== Returns ===
 +
Nothing.
 +
 +
== Examples ==
 +
Use a vertex map to fix a visual glitch without copy/pasting vertices.
 +
<source lang="lua">
 +
function love.load()
 +
    image = love.graphics.newImage("pig.png")
 +
    local w,h = image:getWidth(), image:getHeight()
 +
 +
    -- We want to make a Geometry with 1 vertex in the middle of the image, and 4 at the corners.
 +
    local vertices = {
 +
        {w/2, h/2, 0.5,0.5, 255, 0,  0}, -- Center vertex has a red tint, the rest are white.
 +
        {0,  0,  0,  0,  255, 255, 255},
 +
        {w,  0,  1,  0,  255, 255, 255},
 +
        {w,  h,  1,  1,  255, 255, 255},
 +
        {0,  h    0,  1,  255, 255, 255},
 +
    }
 +
 +
    -- But there's a problem! The drawn geometry will have a big triangle missing on its left side.
 +
    -- This is because, in the default draw mode ("fan"), the vertices don't "loop": the top left vertex (#2) is unconnected to the bottom left one (#5).
 +
    geometry = love.graphics.newGeometry(vertices)
 +
 +
    -- We could copy/paste the second vertex onto the end of the table of vertices.
 +
    -- But instead we can just change the vertex map!
 +
    geometry:setVertexMap(1, 2, 3, 4, 5, 2)
 +
end
 +
 +
function love.draw()
 +
    love.graphics.draw(pig, geometry, 0, 0)
 +
end
 +
</source>
  
 
== See Also ==
 
== See Also ==

Revision as of 23:15, 16 August 2013

Available since LÖVE 0.9.0
This function is not supported in earlier versions.

Sets the vertex map for a Geometry. The vertex map describes the order in which the vertices are used when the Geometry is drawn.

The vertex map allows you to re-order or reuse vertices when drawing without changing the actual vertex parameters or duplicating vertices. It is especially useful when combined with different Geometry Draw Modes.

Function

Synopsis

Geometry:setVertexMap( vertex_map )

Arguments

table vertex_map
A table containing a list of vertex indices to use when drawing. Values must be in the range of [1, Geometry:getVertexCount()].

Returns

Nothing.

Function

Synopsis

Geometry:setVertexMap( vi1, vi2, vi3, ... )

Arguments

number vi1
The index of the first vertex to use when drawing. Must be in the range of [1, Geometry:getVertexCount()].
number vi2
The index of the second vertex to use when drawing.
number vi3
The index of the third vertex to use when drawing.

Returns

Nothing.

Examples

Use a vertex map to fix a visual glitch without copy/pasting vertices.

function love.load()
    image = love.graphics.newImage("pig.png")
    local w,h = image:getWidth(), image:getHeight()

    -- We want to make a Geometry with 1 vertex in the middle of the image, and 4 at the corners.
    local vertices = {
        {w/2, h/2, 0.5,0.5, 255, 0,   0}, -- Center vertex has a red tint, the rest are white.
        {0,   0,   0,  0,   255, 255, 255},
        {w,   0,   1,  0,   255, 255, 255},
        {w,   h,   1,  1,   255, 255, 255},
        {0,   h    0,  1,   255, 255, 255},
    }

    -- But there's a problem! The drawn geometry will have a big triangle missing on its left side.
    -- This is because, in the default draw mode ("fan"), the vertices don't "loop": the top left vertex (#2) is unconnected to the bottom left one (#5).
    geometry = love.graphics.newGeometry(vertices)

    -- We could copy/paste the second vertex onto the end of the table of vertices.
    -- But instead we can just change the vertex map!
    geometry:setVertexMap(1, 2, 3, 4, 5, 2)
end

function love.draw()
    love.graphics.draw(pig, geometry, 0, 0)
end

See Also

Other Languages