About Monkey 2 › Forums › Monkey 2 Programming Help › Help creating my own meshes
This topic contains 9 replies, has 2 voices, and was last updated by 
 Ethernaut
 1 year, 8 months ago.
- 
		AuthorPosts
 - 
		
			
				
August 16, 2017 at 4:45 am #9847
I’m trying to create a 3D mesh from scratch, so I started with a simple “grid” object. For some reason, it doesn’t seem to apply the material correctly, and doesn’t receive shadows either. In this example code, if you comment out the line that uses the “CreateGrid” function and uncomment the line with “Mesh.CreateBox” you can see how create grid doesn’t work.
I looked at the mesh creation functions in the “meshprims.monkey2” file inside mojo3d, and can’t figure out why my function doesn’t work but functions like Mesh.CreateSphere and Mesh.CreateTorus do. Just like those, I’m creating a vertex list and an index (faces) list and using those to return a new mesh.

<edit> Argh! WordPress is refusing to let me post the code (“Unsafe operation” or something like that)… will keep trying.
August 16, 2017 at 5:12 am #9848Sorry, can’t post the code… getting a “403 Forbidden:A potentially unsafe operation has been detected in your request to this site.” error.
Has anyone else had this problem?
August 16, 2017 at 5:52 am #9849Have you tried attaching code or zip of code?
If that’s not working, can you email code to me? blitzmunter at gmail dot com
August 16, 2017 at 6:44 am #9855Attaching zip works…
http://monkeycoder.co.nz/wp-content/uploads/2017/08/3dgrid.monkey2-1.zipAugust 16, 2017 at 7:26 am #9856Ok, main problem seems to be that it lacked texture coordinates and normals.
I’m not 100% sure why this is affecting shadows so much though. There is a bit of hack in the shaders that moves points ‘off’ surfaces in the direction of the normal when comparing shadow depth, but it should still work with no normals. Will eventually investigate.
Alas there is no UpdateNormals() yet so you need to manually create them – easy in this case though as they are all the same, ie: 0,0,-1.
Also, for UpdateTangents to work a mesh must have both valid normals AND texture coordinates. Tangents are only used for bumpmapping though, so not strictly neccesary in this case.
Sorry I haven’t been able to spend more time on the 3d lately everyone. IAP is *nearly* finished so I should be back into it soon.
Anyway, here’s a version that works for me….
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142Function CreateGrid:Mesh( width:Double, height:Double, columns:Int = 1, rows:Int = 1, center:Vec2f = New Vec2f(0.5, 0.5 ) )If columns < 1 Then columns = 1If rows < 1 Then rows = 1Local i := -1Local verts := New Stack<Vertex3f>Local faces := New Stack<UInt>Local cellWidth := width / columnsLocal cellHeight := height / rowsFor Local y := 0 Until rowsFor Local x := 0 Until columns'Create each quadLocal startX := x*cellWidth - ( width*center.X )Local startY := y*cellHeight - ( height*center.Y )Local s0:=float(x)/float( columns ),s1:=float(x+1)/float( columns )Local t0:=float(y)/float( rows ),t1:=float(y+1)/float( rows )verts.Push( New Vertex3f( startX, startY, 0, s0,t0, 0,0,-1 ) )verts.Push( New Vertex3f( startX + cellWidth, startY, 0, s1,t0, 0,0,-1 ) )verts.Push( New Vertex3f( startX + cellWidth, startY + cellHeight, 0, s1,t1, 0,0,-1 ) )verts.Push( New Vertex3f( startX, startY + cellHeight, 0, s0,t1, 0,0,-1 ) )faces.Push( i+1 )faces.Push( i+2 )faces.Push( i+3 )faces.Push( i+1 )faces.Push( i+3 )faces.Push( i+4 )i += 4NextNextLocal data := New Mesh( verts.ToArray(), faces.ToArray() )data.FlipTriangles() 'initially triangles face away from camera, so this flips themdata.UpdateTangents()Return dataEndAugust 16, 2017 at 7:32 am #9857Actually, for now at least, all meshes that use PbrMaterial are gonna need texcoords, normals and tangents. Will add a simpler non-bumpy PBR style material in future.
August 16, 2017 at 8:34 am #9859Got it. For some reason I didn’t realize you can just add those extra channels in the Vertex class itself.
Actually, how to manipulate UVs would be my next question, so that covers it as well!Cheers!
August 16, 2017 at 8:38 am #9861Question: naming wise, wouldn’t it be more clear to name the texture arguments “u” and “v”, instead of “s” and “t”, since most CG artists refer to texture coordinates as UV coordinates, or just UVs?
August 16, 2017 at 4:15 pm #9869I use s,t,p,q for texture coordinates because that’s what opengl/glsl use.
If it’s a huge hassle I can change it I guess, or even add u ,v, overloads?
August 17, 2017 at 3:35 am #9878It’s good as is, makes more sense to be familiar to programmers who know OpenGL than to artists learning programming. Thanks!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.