Jump to content

4096 vertices needs triangles for FFT water


shadmar
 Share

Recommended Posts

void cOcean::evaluateWavesFFT(float t, Leadwerks::Model* m) {
if (m->CountSurfaces()>0 && m->GetSurface(0)->CountVertices() == 0) {
 int index;
 Surface* surface = m->GetSurface(0);
 //Set up water patch for vertices
 for (int m_prime = 0; m_prime < N; m_prime++) {
  for (int n_prime = 0; n_prime < N; n_prime++) {
   index = m_prime * Nplus1 + n_prime;
   surface->AddVertex(vertices[index].ox, vertices[index].oy, vertices[index].oz);
  }
 }
 for (int m_prime = 0; m_prime < N; m_prime++) {
  for (int n_prime = 0; n_prime < N; n_prime++) {
   //surface->AddTriangle(...);  //HOW???
  }
 }
}
}

 

Any tip?

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

This is what I have. Don't know if this is what you are looking for though.

terrain.cpp

 

#include "ATerrain.h"

using namespace Leadwerks;

ATerrain::ATerrain()
{
   width = 100;
   tileWidth = 1;
   defaultColor = Vec4(0.4,0.6,1,1);

   terrainModel = Model::Create();
   terrainSurface = terrainModel->AddSurface();

   CreateVertices();
   CreateTriangles();
   terrainSurface->Update();
   terrainSurface->UpdateAABB();
   terrainSurface->FlipNormals();
}

int ATerrain::VectorToIndex(Vec3 vector)
{
   return (vector.z*width)+vector.x; 
}

void ATerrain::CreateVertices()
{
   int tw = tileWidth;
   for (int i = 0; i < width; i++)
   {
       for (int j = 0; j < width; j++)
       {
           Vec3 indexVector = Vec3(j*tw, 0*tw, i*tw);
           terrainSurface->AddVertex(indexVector);
           terrainSurface->SetVertexColor(VectorToIndex(indexVector), defaultColor);
           terrainSurface->SetVertexNormal(VectorToIndex(indexVector), Vec3(0,1,0));
       }
   }
}

void ATerrain::CreateTriangles()
{    
   for (int i = 0; i < width-1; i++)
   {
       for (int j = 0; j < width-1; j++)
       {
           int oriIndex = VectorToIndex(Vec3(i,0,j));
           terrainSurface->AddTriangle(oriIndex+width, oriIndex, oriIndex+1);
           terrainSurface->AddTriangle(oriIndex+width, oriIndex+1, oriIndex+width+1);
       }
   }
}


ATerrain::~ATerrain(void)
{
}

  • Upvote 1
Link to comment
Share on other sites

indices_count = 0;

for (int m_prime = 0; m_prime < N; m_prime++) {

for (int n_prime = 0; n_prime < N; n_prime++) {

index = m_prime * Nplus1 + n_prime;

if (geometry) {

indices[indices_count++] = index; // lines

indices[indices_count++] = index + 1;

indices[indices_count++] = index;

indices[indices_count++] = index + Nplus1;

indices[indices_count++] = index;

indices[indices_count++] = index + Nplus1 + 1;

if (n_prime == N - 1) {

indices[indices_count++] = index + 1;

indices[indices_count++] = index + Nplus1 + 1;

}

if (m_prime == N - 1) {

indices[indices_count++] = index + Nplus1;

indices[indices_count++] = index + Nplus1 + 1;

}

} else {

indices[indices_count++] = index; // two triangles

indices[indices_count++] = index + Nplus1;

indices[indices_count++] = index + Nplus1 + 1;

indices[indices_count++] = index;

indices[indices_count++] = index + Nplus1 + 1;

indices[indices_count++] = index + 1;

}

}

}

Link to comment
Share on other sites

This is painfully slow. From 800 fps to 98 just to do the fft calculation (pure math, no water vertices drawn)

How is this even usable outside fancy demos? Might work better offloading fft calculation in a second thread.

Why havent anyone done all calculations on gpu?

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

I wrote this exact thing last week in lua, I needed to generate some mesh terrains that I could modify in real time. To make the faces/triangles, I did some linear calculations I use to use on my graphics calculator to turn a 1*X matrix into a simulated 3d matrix

 


Script.null = 0 -- choice "Terrain Generator" "By Einlander"

 

-- The size of the terrain in squares, so an extra vertex will be added in both directions, so a 10x10 square will have 11x11 vertices

Script.TerrainSize = 10 -- int "Terrain Size"

-- 100 is the sweet spot

 

 

-- 1 is the size the leadwerks edior measurment

Script.TerrainSquareSize = 10 -- int "Terrain Square Size"

 

 

function Script:Start()

 

-- measure performance

local timer= Time:Millisecs()

local timer2= Time:Millisecs()

-- terrain size squared

 

System:Print("Starting ")

 

timer2 = Time:Millisecs()

System:Print("Generating terrain ")

self.terrain = self:MakeTerrain(self.TerrainSize)

System:Print("Done "..(Time:Millisecs()-timer2)/1000)

 

timer2 = Time:Millisecs()

System:Print("Modify terrain ")

self.terrain = self:ModifyTerrainVertex(self.terrain, 2,2,15)

self.terrain = self:ModifyTerrainVertex(self.terrain, 2,3,15)

System:Print("Done "..(Time:Millisecs()-timer2)/1000)

 

 

timer2 = Time:Millisecs()

System:Print("Generating Collision Mesh ") -- this is where all the time goes

local shape = Shape:PolyMesh(self.terrain:GetSurface(0)) -- has to be a poly mesh, unless it's perfectly flat.

System:Print("Done "..(Time:Millisecs()-timer2)/1000)

 

 

self.terrain:SetShape(shape)

self.terrain:SetCollisionType(Collision.Scene)

self.terrain:SetPosition(0,0,0)

System:Print("Loading Complete "..(Time:Millisecs()-timer)/1000)

end

 

 

 

function Script:UpdateWorld()

 

end

 

 

function Script:MakeTerrain(TerrainSize) -- capable of creating non square terrain with slight modification

 

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

local currentrow = 0

System:Print("Generating faces")

for trinaglestrip=0 , ((TerrainSize)^2)+(TerrainSize-1) do

surface:AddTriangle(trinaglestrip,trinaglestrip+1,trinaglestrip+TerrainSize+1)

surface:AddTriangle(trinaglestrip,trinaglestrip+TerrainSize+1,trinaglestrip+TerrainSize)

end

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

System:Print("")

return terrain

end

 

function Script:ModifyTerrainVertex(terrain, X,Y,Elevation) -- Raises individual vertices remeber there is one extra vertex in both directions.

local tempterrain = terrain

local tempsurface=tempterrain:GetSurface(0)

--!!!GET THE VERTEX POSITION FIRST, THEN MODIFY THE VALUES!!!

--(X*(self.TerrainSize+1))+(Y)) the linear index

tempvertexposition = tempsurface:GetVertexPosition((X*(self.TerrainSize+1))+(Y))

tempsurface:SetVertexPosition((X*(self.TerrainSize+1))+(Y),tempvertexposition[0],Elevation,tempvertexposition[2])

return tempterrain

end

 

Attach it to a pivot and run. It generates a collision polymesh, that's where the slowdown is. Otherwise, if you don't need to generate a collision mesh, it goes so much faster. It even has a method to manipulate the vertex elevation.

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...