Custom terrain by MeshVertextCoordinates

Everything related to 3D programming
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Hi,

I have a big problem: I can't calculate normales of vertices of my terrain and can't use addictive method of shadows.
Terrain map is the matrix 1000x1000 with height argument (like a *.bmp height-map).

Code: Select all

0.2 0.3 0.5
0.1 0.2 0.3
0.3 0.3 0.4
etc.
Please help. How can I calculate normales?
Thanks
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Custom terrain by MeshVertextCoordinates

Post by Bananenfreak »

You mean "Additive", or? Both additive shadowtypes don´t work correct...

Your using a mesh as custom Terrain, am I right? Then you can use NormalizeMesh(#Mesh [, SubMesh]).
Image
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Re: Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Bananenfreak wrote:You mean "Additive", or? Both additive shadowtypes don´t work correct...

Your using a mesh as custom Terrain, am I right? Then you can use NormalizeMesh(#Mesh [, SubMesh]).
Yes, additive.
Without normales I have artifacts of shadows on terrain - black and white spots. :(
I can NormalizeMesh() without MeshVertexNormal(x,y,z)? :shock: Really?
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Custom terrain by MeshVertextCoordinates

Post by Samuel »

Bananenfreak wrote:You mean "Additive", or? Both additive shadowtypes don´t work correct...
All three shadow types work fine for me. What issues are you having?


@Lexicon
You can use NormalizeMesh(#Mesh [, SubMesh]), but I believe you still need to add a normal to each vertex.
If I remember right it should work if you just use MeshVertexNormal(0, 0, 0) for each vertex and then use the normalize command.
It's been a while since I've used the built in normal calculations. So, I might be wrong.

If you don't want to try that. I can try to explain the process of calculating vertex normals to you.


EDIT:
Lexicon, your shadows are most likely messed up because of the incorrect normals.
I had that same issue when I first started creating mesh routines.
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Re: Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Samuel wrote:I can try to explain the process of calculating vertex normals to you.
YES please! I'll try, but please explain.
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Custom terrain by MeshVertextCoordinates

Post by Samuel »

I'm not the greatest teacher. So, maybe this article will be more useful to you. It covers everything you need for smooth vertex normals.
http://www.lighthouse3d.com/opengl/terr ... p3?normals
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Re: Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Samuel wrote:I'm not the greatest teacher. So, maybe this article will be more useful to you. It covers everything you need for smooth vertex normals.
http://www.lighthouse3d.com/opengl/terr ... p3?normals
Thanks Samuel! :D

MeshVertextNormal(0, 0, 0) and NormalizeMesh()

Please look at result.

http://stalkerfangroup.ru/Stalker_2/sho ... -42-12.jpg

http://stalkerfangroup.ru/Stalker_2/sho ... -42-15.jpg



And no shadows on the ground from objects.

http://stalkerfangroup.ru/Stalker_2/sho ... -42-54.jpg


Maybe you have any advice?
Thanks.
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Re: Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Samuel wrote:I'm not the greatest teacher. So, maybe this article will be more useful to you. It covers everything you need for smooth vertex normals.
http://www.lighthouse3d.com/opengl/terr ... p3?normals
It's a nightmare :|
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Custom terrain by MeshVertextCoordinates

Post by Samuel »

It looks like some of your normals are facing the wrong direction. Are you creating faces for both sides of your triangles?
If you face both sides then you need to create two normals at each vertex location. Then NormalizeMesh() should work.


If you want to calculate the normals yourself. We can take a look at the code Comtois showed me back when I first started.

Code: Select all

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Macro SubVector3(n, v, w)
  n\x = v\x - w\x
  n\y = v\y - w\y
  n\z = v\z - w\z
EndMacro

Procedure Normalize(*V.Vector3)
  Define.f magSq, oneOverMag
 
  magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z
  If magsq > 0
    oneOverMag = 1.0 / Sqr(magSq)
    *V\x * oneOverMag
    *V\y * oneOverMag
    *V\z * oneOverMag
  EndIf 
 
EndProcedure

Procedure NormalFace(*n.Vector3, *v1.Vector3, *v2.Vector3, *v3.Vector3)
  Protected.Vector3 v2v1, v3v1
  SubVector3(v2v1, *v2, *v1)
  SubVector3(v3v1, *v3, *v1)
 
  *n\x = v2v1\y * v3v1\z - v2v1\z * v3v1\y
  *n\y = v2v1\z * v3v1\x - v2v1\x * v3v1\z
  *n\z = v2v1\x * v3v1\y - v2v1\y * v3v1\x
EndProcedure               

Define.Vector3 Normal, v1, v2, v3

;Calculate normal using the three vertices of the face. The order you put them will affect the direction of the normal.
; Vertex 1
v1\x = 280
v1\y = 0
v1\z = 150
; Vertex 2
v2\x = -140
v2\y = 0
v2\z = -150
; Vertex 3
v3\x = -230
v3\y = 0
v3\z = -150

NormalFace(@normal, @v1, @v2, @v3)
Normalize(@normal)

;Normal for vertices
Debug normal\x
Debug normal\y
Debug normal\z
This code will calculate the normal for a triangular face. For flat shading all three vertices of the face would receive this normal.

For smooth shading there is another step that must be taken.
Let's say we have four triangles that all share the same vertex. First, we use the above code to calculate the normal for each of the four faces.
Then we will add those four normals together. That sum will then be normalized to receive the new vertex normal.
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Re: Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Thanks Samuel! I'll try and report. :wink:
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Re: Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Oops... How to apply a texture via this method?

Code: Select all


EnableWorldPhysics(#True)
EnableWorldCollisions(#True)
WorldShadows(#PB_Shadow_TextureAdditive, 100, RGB(CamShadowR, CamShadowG, CamShadowB), 2048)

BuildTerrainMesh = 10

CreateMesh(BuildTerrainMesh, #PB_Mesh_TriangleList, #PB_Mesh_Static)

TerLen.l = 100 ; Terrain side

For btz = 0 To TerLen - 1
 For btx = 0 To TerLen - 1

  bth = HeightMapSector.MapSector(btx, btz)\Height / 8
  MeshVertexPosition(btx, bth, btz)
  MeshVertexNormal(0, 0, 0)
  ; MeshVertexTextureCoordinate(TexX, TexZ)

 Next
Next	

For btz = 0 To TerLen - 2
 For btx = 0 To TerLen - 2

  btf = btz * TerLen + btx
  MeshFace(btf+0, btf+TerLen, btf+TerLen+1) : MeshFace(btf+TerLen+1, btf+1, btf+0)

 Next
Next	

FinishMesh(#True)
NormalizeMesh(BuildTerrainMesh)

CreateEntity(BuildTerrainMesh, MeshID(BuildTerrainMesh), MaterialID(GroundMat))
EntityRenderMode(BuildTerrainMesh, #PB_Entity_CastShadow)
EntityPhysicBody(BuildTerrainMesh, #PB_Entity_StaticBody)

material

Code: Select all

material ground/main_01
{
	technique
	{
		pass
		{
			ambient 1 1 1 1
			specular 0 0 0 0 1
			diffuse 1 1 1 1
			texture_unit
			{
				texture grnd_grass_14.jpg
			}
		}
	}
}
I will save all normalized terrain mesh and convert to data-file for read in real time. But texture... And no shadows from objects (loaded and primitives). :(

http://stalkerfangroup.ru/Stalker_2/sho ... -12-27.jpg
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Custom terrain by MeshVertextCoordinates

Post by Samuel »

Here's a little example using your code.
I didn't know your height values so I put in random numbers. Which causes the ground to look a little jagged.
I also setup the terrain to receive Texture Additive shadows.

With a little more work you could make a nice terrain generator.

Code: Select all

Enumeration
  #Camera
  #Texture
  #Material
  #Light
  #Terrain
  #SphereMesh
  #Sphere
EndEnumeration

Declare MyTerrain(Size)

If InitEngine3D(#PB_Engine3D_DebugLog)

  InitSprite()
  InitKeyboard()
  InitMouse()
  
ExamineDesktops()
DesktopW=DesktopWidth(0)
DesktopH=DesktopHeight(0)

Define.f MouseX,MouseY,KeyX,KeyY

If OpenWindow(0, 0, 0, DesktopW, DesktopH, "Basic Terrain Generator")
  If OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
    
    Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Textures",#PB_3DArchive_FileSystem)
    
    LoadTexture(#Texture,"grass.jpg")
    CreateMaterial(#Material,TextureID(#Texture))
    ;Need to scale the material for texture repeat
    ScaleMaterial(#Material, 0.1, 0.1)
    
    CreateSphere(#SphereMesh,5)
    CreateEntity(#Sphere,MeshID(#SphereMesh),MaterialID(#Material),0,20,0)
    
    MeshHandle=MyTerrain(100)
    Result = IsMesh(MeshHandle)
    If Result <> 0
      CreateEntity(#Terrain, MeshID(MeshHandle), MaterialID(#Material))
      EntityRenderMode(#Terrain, 0)
    EndIf
  
    CreateLight(#Light,RGB(0,0,0),50,50,0)
    SetLightColor(#Light,#PB_Light_DiffuseColor,RGB(255,255,255))
    
    AmbientColor(RGB(100,100,100))
    
    CreateCamera(#Camera, 0, 0, 100, 100)
    MoveCamera(#Camera, 50, 25, -50, #PB_Absolute)
    CameraLookAt(#Camera,0,0,0)
    CameraBackColor(#Camera,RGB(80,80,80))
    
    WorldShadows(#PB_Shadow_TextureAdditive, 100, RGB(200, 200, 200), 2048)

    
    Repeat
      Event=WaitWindowEvent()   
      
      If ExamineMouse()
        MouseX = -MouseDeltaX()/10
        MouseY = -MouseDeltaY()/10
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -0.2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 0.2
        Else
          KeyX = 0
        EndIf
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -0.2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 0.2
        Else
          KeyY = 0
        EndIf
      EndIf
      
      RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape)
  EndIf
EndIf  

Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
    
    
Procedure MyTerrain(Size)

  Define.f U,V,UVoffset

  TerLen = Size
  UVoffset = 1 / TerLen
  Handle=CreateMesh(#PB_Any, #PB_Mesh_TriangleList, #PB_Mesh_Static)
    For btz = 0 To TerLen - 1
      offset = 1
      For btx = 0 To TerLen - 1
        ;bty = HeightMapSector.MapSector(btx, btz)\Height / 8
        ;I don't know your old height values. So, I put in random numbers.
        bty = Random(1,0)
        MeshVertexPosition(btx-(TerLen*0.5), bty, btz-(TerLen*0.5))
        MeshVertexNormal(0, 0, 0)
        MeshVertexTextureCoordinate(U, V)
        V = V + UVoffset
      Next
      U = U + UVoffset
      V = 0
    Next   

    For btz = 0 To TerLen - 2
      For btx = 0 To TerLen - 2
        btf = btz * TerLen + btx
        MeshFace(btf+0, btf+TerLen, btf+TerLen+1)
        MeshFace(btf+TerLen+1, btf+1, btf+0)
      Next
    Next   
  FinishMesh(#True)
  NormalizeMesh(Handle)

  ProcedureReturn Handle

EndProcedure
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Re: Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Samuel wrote:Here's a little example using your code.
I didn't know your height values so I put in random numbers. Which causes the ground to look a little jagged.
I also setup the terrain to receive Texture Additive shadows.

With a little more work you could make a nice terrain generator.
Yes! Texture is ok! Thanks! :D
And one moment also. About shadows from the objects on the ground... What's wrong I do?

http://radium3dengine.com/img/shot_2014 ... -33-56.JPG
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Custom terrain by MeshVertextCoordinates

Post by Samuel »

Are you using #PB_Shadow_TextureAdditive for your shadow type?
If you are then you need to set the render mode to 0 on your terrain entity.
Try EntityRenderMode(#TerrainHandle, 0).
User avatar
Lexicon
User
User
Posts: 98
Joined: Mon Jul 22, 2013 6:16 am
Contact:

Re: Custom terrain by MeshVertextCoordinates

Post by Lexicon »

Samuel wrote:Are you using #PB_Shadow_TextureAdditive for your shadow type?
If you are then you need to set the render mode to 0 on your terrain entity.
Try EntityRenderMode(#TerrainHandle, 0).
I have been set, but no shadows again. :D

May be in my code something?

Code: Select all

CreateLight(SunLight ,RGB(0,  0, 0), 400, 400, 400, #PB_Light_Directional)
SetLightColor(SunLight, #PB_Light_DiffuseColor, RGB(250, 250, 250))
LightDirection(SunLight, 0.55, -0.3, -0.75)

WorldShadows(#PB_Shadow_TextureAdditive, 500, RGB(200, 200, 200), 2048)

EntityRenderMode(BuildTerrainMesh, 0)
If #PB_Light_Directional will remove then removes terrain shadow. But shadows form sphere on the ground are present in your example. I don't understand why. :(

Anyway, thanks a lot.
PureBasic 5.11 | WinXP | 2GB RAM | GForce GT240
Post Reply