shadmar Posted April 28, 2014 Share Posted April 28, 2014 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? Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
AggrorJorn Posted April 28, 2014 Share Posted April 28, 2014 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) { } 1 Quote Link to comment Share on other sites More sharing options...
SlipperyBrick Posted April 28, 2014 Share Posted April 28, 2014 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; } } } Quote Link to comment Share on other sites More sharing options...
shadmar Posted April 28, 2014 Author Share Posted April 28, 2014 Thanks, still some issues to resolve but I got to see motion Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
Josh Posted April 28, 2014 Share Posted April 28, 2014 You could also use tessellation to do this. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
shadmar Posted April 29, 2014 Author Share Posted April 29, 2014 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? Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
Einlander Posted April 29, 2014 Share Posted April 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.