CreateMesh not displaying correctly

Blitz3D Forums/Blitz3D Programming/CreateMesh not displaying correctly

Axel Wheeler(Posted 2010) [#1]
Specificially, the lighting is like a fullbright effect, but duller.

I am using:

createmesh()
addsurface()
addvertex()
addtriangle()
updatenormals()

The problem is the same regardless of the size, complexity, or shape of the mesh.

I have tried various entityfx settings to no avail.

It does take an entitycolor but not shaded.

Any help would be much appreciated. Thanks.


Warner(Posted 2010) [#2]
It sounds like a normal issue. So then, "UpdateNormals mesh" should be able to solve it. Have you tried creating a light? Or turning it?


Dreamora(Posted 2010) [#3]
also, ensure that you don't create more than 65535 vertices and triangles, thats the max per mesh


PowerPC603(Posted 2010) [#4]
Sounds like there is no light source anywhere in the scene, except the ambientlight.

This makes the mesh look dull, as the ambientlight lights the mesh equally from all around.

Try adding a light somewhere near the mesh (within the range of the light).


Ross C(Posted 2010) [#5]
Can you post the code that is creating the problem. It does sound like an ambient lighting problemn, if your using update normals.


Axel Wheeler(Posted 2010) [#6]
Thanks for your replies.

A sphere in the scene is lit correctly.
UpdateNormals has been applied to the mesh.
The scene is lit by a light that has been rotated to the upper right.
Setting ambient light to zero makes the mesh disappear (but the sphere remains)

However, I have been able to "solve" the problem by not adding triangles to both sides of the mesh (it's a flat surface that is shaped, kind of like a flag). Does simply having triangles on the other side of a mesh (i.e. 0-2-1 in addition to 0-1-2) cancel out all lighting on the front side (except ambient light)? I knew there could be weird effects around the edges, but across the whole surface?

I can create the other side as another surface, I suppose. Or as a new set of vertices. Thoughts, experiences?

Thanks again folks.


Floyd(Posted 2010) [#7]
If you have a two-sided triangle, which is really two identical triangles back-to-back, then UpdateNormals gets confused. At each vertex it generates two normals, then uses their average for that vertex. The normals face opposite directions, so the average is zero. Hence no lighting.

UpdateNormals has no idea what a mesh is supposed to represent or what normals you really want. It just crawls around the mesh examining each vertex and trying to guess reasonable values for normals. In the two-sided triangle case it guesses badly.

This shouldn't be a problem. Since you are building the mesh you know the correct normals for the two sides. Just set the normals manually.


Floyd(Posted 2010) [#8]
I just remembered answering a similar question long ago. Although I still recommend setting the normals manually here is a "quick fix" to get two-sided triangles with UpdateNormals.

Graphics3D 640,480,0,2

cam = CreateCamera()
TurnEntity cam,90,0,0
MoveEntity cam,0,0,-10
CameraZoom cam, 5


lit = CreateLight()
TurnEntity lit,90,0,0

square = CreateMesh()
ripsurf = CreateSurface(square)

v1 = AddVertex(ripsurf,-1,0,-1)
v2 = AddVertex(ripsurf,-1,0,1)
v3 = AddVertex(ripsurf,1,0,-1)
v4 = AddVertex(ripsurf,1,0,1)

AddTriangle(ripsurf,v1,v2,v3)
AddTriangle(ripsurf,v2,v4,v3)

; We have a one-sided square with no normals defined. Now apply quick and dirty fix.

UpdateNormals square

temp = CopyMesh(square)
FlipMesh temp
AddMesh temp, square
FreeEntity temp

EntityColor square,255,0,0

EntityShininess square,1

While Not KeyDown(1)
	TurnEntity square,1,0,0
	RenderWorld
	Flip
Wend



Axel Wheeler(Posted 2010) [#9]
Floyd,

As I suspected. Thanks for the code. It doesn't actually solve the problem because addmesh is putting the new vertices into the same surface, which is still confusing updatenormals.

Your suggestion to set the normals manually would (I would think) work only for one side, since the same vertex normal info is used for both sides.

However, when I addmesh the copy to a second (i.e. new) surface it works like magic.

Actually addmesh would only add to the first surface, so I'm doing something fishy: I'm creating two surfaces at the beginning and doing all the creation to the second one (leaving the first one empty). Then when I addmesh the flipped copy, it goes into the empty surface and all works well. However I would expect it to be more robust to just copy the vertices and triangles.

Anyway problem solved. Thanks all!


Stevie G(Posted 2010) [#10]
@ Axel, Floyd's code work fine. I suspect you are using updatenormals after you have flipped and added the copied mesh. If you look at the code, he updates the normals on the first mesh only. The flipmesh reverses the normals. You don't need 2 surfaces.


jfk EO-11110(Posted 2010) [#11]
If you however want to addmesh, but prevent a fusion of identic surfaces/materials (which is what DX does for optimation purposes) then you may eg. BrushColor the to be added mesh slightly diffrent, eg: 254,255,255 vs 255,255,255.