Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How does the new Mesh API work? — Gideros Forum

How does the new Mesh API work?

MellsMells Guru
edited October 2012 in General questions
Hi,

thank you for providing new features.
The following displays nothing on screen, but I don't see what could be missing?
        -- Create a new mesh object
	local mesh = Mesh.new()
 
	-- set 3 vertices with one function call
	mesh:setVertices(1, 100, 100, 2, 150, 100, 3, 100, 150)
 
	-- set the first 3 indices as 10, 11 and 12.
	mesh:setIndices(1, 10, 2, 11, 3, 12)
 
	-- set the first 3 colors as (0xff0000, 0.5), (0x00ff00, 0.7) and (0x0000ff, 1.0).
	mesh:setColors(0xff0000, 0.5, 0x00ff00, 0.7, 0x0000ff, 1.0)
 
	-- Position mesh
	mesh:setPosition(300, 300)
 
	-- Add to stage
	stage:addChild(mesh)
I insist on the need to provide updated docs + test files with each new release.
Trying to guess can, sometimes, be frustrating.

Thank you

Dislikes: mehmetuysal

twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
+1 -1 (+0 / -1 )Share on Facebook
«1

Comments

  • atilimatilim Maintainer
    edited October 2012 Accepted Answer
    Here, you need to set indices as (1, 2, 3):
    mesh:setIndices(1, 1, 2, 2, 3, 3)
    -- or
    mesh:setIndexBuffer(1, 2, 3)
    And if you want to draw a rectangle (consists of 2 triangles), you need to specify 4 vertices as v1(0, 0), v2(100, 0), v3(100, 150), v4(0, 150) and indices as (1, 2, 3, 1, 3, 4) (first 3 index is first triangle and second 3 index is second triangle):
    mesh:setVertexBuffer(0, 0,   100, 0,   100, 150,   0, 150)
    mesh:setIndexBuffer(1, 2, 3,     1, 3, 4)
    --                  ^^^^^^^      ^^^^^^^
    --                     |            |
    --                 first tri    second tri

    Now I need to go out but when I come back, I'll explain what is vertex and what is index. And after that everything will become much more clear.
  • atilimatilim Maintainer
    And I realized that reference manual has some mistakes in the examples. Here
    mesh:setColors(0xff0000, 0.5, 0x00ff00, 0.7, 0x0000ff, 1.0)
    -- should be
    mesh:setColors(1, 0xff0000, 0.5, 2, 0x00ff00, 0.7, 3, 0x0000ff, 1.0)
    -- or
    mesh:setColorBuffer(0xff0000, 0.5, 0x00ff00, 0.7, 0x0000ff, 1.0)
    While uploading the new reference manual to the server, I'll fix these.
  • MellsMells Guru
    edited October 2012
    Ok here is a basic working example :
            -- Create a new mesh object
    	local mesh = Mesh.new()
     
    	-- set 3 vertices with one function call
    	mesh:setVertices(1, 100, 100, 2, 700, 100, 3, 700, 600, 4, 100, 600)
     
     
    	-- set the first 3 indices
    	mesh:setIndexBuffer(1, 2, 3, 1, 3, 4)
     
    	-- set the first 3 colors as (0xff0000, 0.5), (0x00ff00, 0.7) and (0x0000ff, 1.0).
    	mesh:setColorBuffer(0xff0000, 1, 0x00ff00, 1, 0x0000ff, 1.0, 0xFF00FF, .4)
     
    	-- Set mesh Position
    	mesh:setPosition(300, 300)
     
    	-- Add to stage
    	stage:addChild(mesh)
     
     
    	Timer.delayedCall(2000, function()		
    		mesh:setVertex(5, 700, 1000)
    		mesh:setIndexBuffer(1, 2, 3, 1, 3, 4, 4, 3, 5)	
    		mesh:setColor(5, 0xF2F2F2) 
    	end)

    @atilim
    Thank you for the answer, I'm waiting for your test files & explanations of how all the mesh API works (vertex, index).

    Is that possible to :
    1. get the list of all vertices
    2. get the size of the vertex buffer
    That would make it easier to add new vertices, unless I'm missing something.



    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Is there any way to specify GL_TRIANGLE_FAN or GL_TRIANGLE_STRIP for mech rendering?
  • atilimatilim Maintainer
    edited October 2012
    @Mells Yours is a perfect example. I can add these 2 functions.

    @dasowl No. First, I've implemented the function Mesh:setPrimitiveType but then I removed it because Adobe stage3d doesn't support triangle fans and strips (and lines and points). I don't know which platforms we will support in the future but I just want to be on the safe side.
  • MellsMells Guru
    edited October 2012
    @atilim
    Would it make the API too messy?
    Mesh:addVertex(x, y)
    --Allows to set a Vertex without knowing the number of vertices in the mesh (index is set to nbVertices+1)
    Also is that possible to get a vertex?
    Mesh:getVertex(i)
    return vertexX, vertexY
    --Allows to get a vertex's x and y values
    For example that would make possible to check proximity between a touch on screen and the closest vertex, for example.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • atilimatilim Maintainer
    edited October 2012
    About Meshes
    -----------

    Meshes in Gideros are simply a set of triangles and (at least) both vertex and index buffers should be set to specify a mesh.

    Assume that you want to draw a mesh shown in the attached image. It has 8 vertices and consists of 6 triangles. Each set of three indices identifies a triangle and therefore 6 * 3 indices should be specified:
    1, 2, 6, 1, 6, 5, 2, 3, 7, 2, 7, 6, 3, 4, 8, 3, 8, 7
    ^^^^^^^  ^^^^^^^  ^^^^^^^  ^^^^^^^  ^^^^^^^  ^^^^^^^
      1st      2nd      3rd      4th      5th      6th
    Optionally, you can set specify 8 colors or 8 texture coordinates to enable vertex coloring and texture mapping.
    mesh.png
    232 x 106 - 4K
  • atilimatilim Maintainer
    @Mells Yes I can add these. Mesh API already has many methods to modify vertices, indices, etc as fast as possible.
  • @atilim Great, thank you for listening to user feedback.
    Where in the roadmap do those last suggestions fit?

    I believe those requests would be enough to let us explore this new feature.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • atilimatilim Maintainer
    Accepted Answer
    > Where in the roadmap do those last suggestions fit?
    Most probably with the next release.

    Likes: Mells

    +1 -1 (+1 / -0 )Share on Facebook
  • MellsMells Guru
    edited October 2012
    @atilim

    Mesh scaling with a tween shows a strange behavior, as if the anchorPoint was moving during the scaling.
    I don't know how to make a better report, this sample seems pretty simple.

    Here is the { Test file } if needed.

    Code :
    	local w, h = application:getContentWidth(), application:getContentHeight()
     
    	-- ------------------------------------------
    	-- Create Mesh
    	-- ------------------------------------------
     
    	-- Create a new mesh object
    	local mesh = Mesh.new()
     
    	-- set Vertices
    	mesh:setVertices(
                                    1, w*.25, h*.25,
    				2, w*.75, h*.25,
    				3, w*.75, h*.75,
    				4, w*.25, h*.75					
    			         )
     
    	-- set Indices
    	mesh:setIndexBuffer(	
                                    1, 2, 3,
    				1, 3, 4
    				)
     
    	-- set Colors
    	mesh:setColorBuffer(
    				0x000000, 1, 0x000000, 1,
    				0x00FFFF, 1, 0x00FFFF, 1						
    				)
     
    	-- Add to stage
    	stage:addChild(mesh)
     
     
    	-- Animate
    	-- Not the expected behavior
    	GTween.new(mesh, 4, {scaleY = 1.5}, {repeatCount = 0, reflect = true, setLoop = true})
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • and when i add mesh on stage, it changes color, alpha of all others sprite? is this normal? :-?
  • atilimatilim Maintainer
    @Mells,

    You need to change setVertices as:
    	mesh:setVertices(
                  1, -w*.25, -h*.25,
                  2,  w*.25, -h*.25,
                  3,  w*.25,  h*.25,
                  4, -w*.25,  h*.25)
    And position the mesh:
    mesh:setPosition(w/2, h/2)
  • atilimatilim Maintainer
    @hnim oh.. definitely not normal. Can you post a little example? Because I've already tried different combinations but couldn't catch the bug.
  • @atilim: i use this code:
    local mesh = Mesh.new() 
    mesh:setVertices(1, 10, 10, 2, 100, 10, 3, 100, 100, 4, 10, 100) 
    mesh:setIndexBuffer(1, 2, 3, 1, 3, 4) 
    mesh:setColorBuffer(0xff0000, 1, 0x00ff00, 1, 0x0000ff, 1.0, 0xFF00FF, .4) 
    stage:addChild(mesh)
    --
    local tx = Texture.new("mario.png", true)
    local bitmap = Bitmap.new(tx)
    stage:addChild(bitmap)
    and here is result: before and after adding mesh
    mesh-color.png
    641 x 261 - 68K
  • atilimatilim Maintainer
    @hnim This is definitely a bug but interestingly on my side it worked on windows, mac, ios and android. For a last request, can you post your mario.png also?
    scr.png
    226 x 304 - 59K
    scr.png 59.1K
  • MellsMells Guru
    edited October 2012
    @atilim
    I see, the anchor point is at (0, 0) so it's better to construct the mesh around that anchor before positioning if there is a plan for rotation/scaling etc... later.

    In a 3d software, when you add polygons to an object the center of the object is automatically modified (based on its bounding box).
    Is something like this achievable so we could add triangles to the mesh based on user input and still have the possibility to rotate and scale that new object from its center?

    @hnim please provide a test file and I'll see how it works on my side.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • here is my test project. im working on windows 7 64bit.
    zip
    zip
    MeshTest.zip
    29K
  • ar2rsawseenar2rsawseen Maintainer
    edited October 2012
    Works fine for me
    On Windows 7 32bit Gideros player
    meshtest.png
    336 x 539 - 47K
  • works fine on android, so problem is my player :-?
  • atilimatilim Maintainer
    edited October 2012
    I start to think that this is an OpenGL driver bug. I can put a workaround to overcome this bug. Please post more results.
  • evsevs Member
    Works ok here on OSX 10.7.5 Gideros Player
    Screen Shot 2012-10-09 at 08.25.02.png
    508 x 532 - 56K
  • atilimatilim Maintainer
    edited October 2012
    @hnim, can you put a line bitmap:setColorTransform(1, 1, 1, 0.99) and test again?
  • hnimhnim Member
    edited October 2012
    @hnim, can you put a line bitmap:setColorTransform(1, 1, 1, 0.99) and test again?
    ok, now it works fine. So my pc has trouble?
  • atilimatilim Maintainer
    edited October 2012
    ops.. please ignore my post about OpenGL driver bug: http://lwjgl.org/forum/index.php?topic=2424.0;wap2 Now I'll fix it.
  • atilimatilim Maintainer
    edited October 2012
    @hnim No, your pc is perfectly fine.

    As a side note, I always think that glColor4f and GL_COLOR_ARRAY doesn't effect each other. But according to this http://lwjgl.org/forum/index.php?topic=2424.0;wap2 GL_COLOR_ARRAY effects the last value of glColor4f. It seems for some OpenGL drivers, GL_COLOR_ARRAY effects glColor4f and for some drivers it doesn't.

    This is the bug. Thank you for reporting.
  • atilimatilim Maintainer
    Now, I've fixed it. Thank you again.
  • MellsMells Guru
    edited October 2012
    @atilim

    I rewrite my comment here so it's not lost :
    I see, the anchor point is at (0, 0) so it's better to construct the mesh around that anchor before positioning if there is a plan for rotation/scaling etc... later.

    In a 3d software, when you add polygons to an object the center of the object is automatically modified (based on its bounding box).

    { ? } Is something like this achievable so we could add triangles to the mesh based on user input and still have the possibility to rotate and scale that new object from its center?

    @hnim your file works fine on my Macbook Air.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • atilimatilim Maintainer
    @Mells Unfortunately, it can really complicate the API. I can suggest adding the mesh to a parent sprite and tweening the parent:
    	local w, h = application:getContentWidth(), application:getContentHeight()
     
    	-- ------------------------------------------
    	-- Create Mesh
    	-- ------------------------------------------
     
    	-- Create a new mesh object
    	local mesh = Mesh.new()
     
    	-- set Vertices
    	mesh:setVertices(
                                    1, w*.25, h*.25,
    				2, w*.75, h*.25,
    				3, w*.75, h*.75,
    				4, w*.25, h*.75					
    			         )
     
    	-- set Indices
    	mesh:setIndexBuffer(	
                                    1, 2, 3,
    				1, 3, 4
    				)
     
    	-- set Colors
    	mesh:setColorBuffer(
    				0x000000, 1, 0x000000, 1,
    				0x00FFFF, 1, 0x00FFFF, 1						
    				)
     
    	-- get bounds
    	local x, y, w, h = mesh:getBounds(mesh)
     
    	-- create mesh-parent-stage hierarchy
    	local parent = Sprite.new()
    	parent:addChild(mesh)
    	stage:addChild(parent)
     
    	-- calculate the center
    	local cx = x + w / 2
    	local cy = y + h / 2
     
    	-- set the center
    	parent:setPosition(cx, cy)
    	mesh:setPosition(-cx, -cy)
     
    	-- tween parent instead of mesh
    	GTween.new(parent, 4, {scaleY = 1.5}, {repeatCount = 0, reflect = true, setLoop = true})
Sign In or Register to comment.