Jump to content

Need help texturing triangle strips.


Einlander
 Share

Recommended Posts

I'm trying to make a terrain generator, I have the mesh plane generated, but I'm at a total loss at how to generate the uv's for all the triangles.

 

 

This is the code that I currently have. How would I go about programmaticly adding uv's?

 

function Script:MakeTerrain(TerrainSize)

 

local terrain = nil

terrain = Model:Create()

terrain:SetColor(0.5,0.5,0.5)

local surface = terrain:AddSurface()

 

-- add vertexes

System:Print("Generating Vertices")

for TerrainWidth=0 , TerrainSize do

for TerrainDepth=0 , TerrainSize do

surface:AddVertex(TerrainWidth*self.TerrainSquareSize,0,TerrainDepth*self.TerrainSquareSize, 0,1,0)

end

end

-- add triangles

 

 

System:Print("Generating faces")

trianglestrip = 0

System:Print(tostring(surface:CountVertices()))

 

for triangle = 0 , (TerrainSize*TerrainSize)+(TerrainSize-2) do --(surface:CountVertices())/2 do

point1 =trianglestrip

point2 =trianglestrip+(TerrainSize+1)

point3 =trianglestrip+(TerrainSize)-(TerrainSize-1)

point4 = point3+(TerrainSize+1)

System:Print("Bottom:"..tostring(point1+1).."|"..tostring(point2+1).."|"..tostring(point3+1).."["..tostring(point4+1).."]")

System:Print("Top:"..tostring(point2+1).."|"..tostring(point3+1).."|"..tostring(point4+1))

surface:AddTriangle(point1,point3,point2)

surface:AddTriangle(point2,point3,point4)

trianglestrip =point3

end

 

 

-- This is where you would add the uv's

System:Print("Adding UV's")

 

surface:Update(true)

System:Print("")

return terrain

end

Link to comment
Share on other sites

I'd recommend using the default terrain generator. It is a lot easier to setup, but I'm guessing you may want holes and such.

This is the add vertex function from the docs:

 

AddVertex(const Vec3& position, const Vec3& normal=Vec3(0), const Vec2& texcoords0=Vec2(0), const Vec2& texcoords1=Vec2(0), const Vec4& color=Vec4(1))

 

You want something like:

 

surface:AddVertex(Vec3(TerrainWidth*self.TerrainSquareSize,0,TerrainDepth*self.TerrainSquareSize), Vec3(0,1,0), Vec2(TerrainWidth / TerrainSize, TerrainDepth / TerrainSize))

 

The uvs are just a texture mapping for that vertex between 0 and 1 on the x and y, but I'm guessing you know that... maybe smile.png

So if you did the xcoord/terrainSize that would get all points between 0 and 1. and the same for the ycoord

  • Upvote 1
Link to comment
Share on other sites

That worked beautifully. I was trying to apply the texture coordinates for every single triangle individually. I also added a scaling feature.

 

The new function for all thoe intrested.

 


function Script:MakeTerrain(TerrainSize,texscale)

--texscale must be => 1

if ( (texscale == nil) == true or (texscale <= 0) == true) then texscale = TerrainSize end

local terrain = nil

terrain = Model:Create()

terrain:SetColor(0.5,0.5,0.5)

local surface = terrain:AddSurface()

-- add vertexes and uv's

System:Print("Generating Vertices")

for TerrainWidth=0 , TerrainSize do

for TerrainDepth=0 , TerrainSize do

surface:AddVertex(Vec3(TerrainWidth*self.TerrainSquareSize,0,TerrainDepth*self.TerrainSquareSize), Vec3(0,1,0),Vec2((TerrainWidth / TerrainSize)*texscale, ((TerrainDepth / TerrainSize)*texscale)*-1))

end

end

-- add triangles

System:Print("Generating faces")

trianglestrip = 0

System:Print(tostring(surface:CountVertices()))

for triangle = 0 , (TerrainSize*TerrainSize)+(TerrainSize-2) do --(surface:CountVertices())/2 do

point1 =trianglestrip

point2 =trianglestrip+(TerrainSize+1)

point3 =trianglestrip+(TerrainSize)-(TerrainSize-1)

point4 = point3+(TerrainSize+1)

--System:Print("Bottom:"..tostring(point1+1).."|"..tostring(point2+1).."|"..tostring(point3+1).."["..tostring(point4+1).."]")

--System:Print("Top:"..tostring(point2+1).."|"..tostring(point3+1).."|"..tostring(point4+1))

surface:AddTriangle(point1,point3,point2)

surface:AddTriangle(point2,point3,point4)

trianglestrip =point3

end

surface:Update(true)

System:Print("")

return terrain

end

 

This isn't quite ready for prime time. I need to swap the direction of the triangles every other row as described here: http://dan.lecocq.us/wordpress/2009/12/25/triangle-strip-for-grids-a-construction/ and that should be easy enough.

 

A last question, Is it possible to change the material for a single polygon as if it were a multi-material mesh?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...